datapolicy.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. /* Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file datapolicy/class/datapolicy.class.php
  19. * \ingroup datapolicy
  20. * \brief Class to manage feature of Data Policy module.
  21. */
  22. include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  23. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  24. include_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  25. include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
  26. /**
  27. * Class DataPolicy
  28. */
  29. class DataPolicy
  30. {
  31. /**
  32. * Constructor
  33. *
  34. * @param DoliDB $db Database handler
  35. */
  36. public function __construct($db)
  37. {
  38. $this->db = $db;
  39. }
  40. /**
  41. * getAllContactNotInformed
  42. *
  43. * @return number
  44. */
  45. public function getAllContactNotInformed()
  46. {
  47. global $langs, $conf, $db, $user;
  48. $langs->load("companies");
  49. $sql = "SELECT c.rowid";
  50. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  51. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
  52. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as spe ON spe.fk_object = c.rowid";
  53. $sql .= " WHERE (c.statut=1 AND c.no_email=0 AND (spe.datapolicy_consentement=0 OR spe.datapolicy_consentement IS NULL) AND (spe.datapolicy_opposition_traitement=0 OR spe.datapolicy_opposition_traitement IS NULL) AND (spe.datapolicy_opposition_prospection=0 OR spe.datapolicy_opposition_prospection IS NULL))";
  54. $sql .= " AND spe.datapolicy_send IS NULL";
  55. $sql .= " AND c.entity=".$conf->entity;
  56. $resql = $this->db->query($sql);
  57. if ($resql) {
  58. $num = $this->db->num_rows($resql);
  59. $i = 0;
  60. while ($i < $num) {
  61. $obj = $this->db->fetch_object($resql);
  62. $contact = new Contact($db);
  63. $contact->fetch($obj->rowid);
  64. DataPolicy::sendMailDataPolicyContact($contact);
  65. $i++;
  66. }
  67. } else {
  68. $this->error = $this->db->error();
  69. return -1;
  70. }
  71. }
  72. /**
  73. * getAllCompaniesNotInformed
  74. *
  75. * @return number
  76. */
  77. public function getAllCompaniesNotInformed()
  78. {
  79. global $langs, $conf, $db, $user;
  80. $langs->load("companies");
  81. $sql = "SELECT s.rowid";
  82. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  83. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as se ON se.fk_object = s.rowid";
  84. $sql .= " WHERE s.statut=0 AND (se.datapolicy_consentement=0 OR se.datapolicy_consentement IS NULL) AND (se.datapolicy_opposition_traitement=0 OR se.datapolicy_opposition_traitement IS NULL) AND (se.datapolicy_opposition_prospection=0 OR se.datapolicy_opposition_prospection IS NULL)";
  85. $sql .= " AND se.datapolicy_send IS NULL";
  86. $sql .= " AND s.entity=".$conf->entity;
  87. $resql = $this->db->query($sql);
  88. if ($resql) {
  89. $num = $this->db->num_rows($resql);
  90. $i = 0;
  91. while ($i < $num) {
  92. $obj = $this->db->fetch_object($resql);
  93. $societe = new Societe($db);
  94. $societe->fetch($obj->rowid);
  95. DataPolicy::sendMailDataPolicyCompany($societe);
  96. $i++;
  97. }
  98. } else {
  99. $this->error = $this->db->error();
  100. return -1;
  101. }
  102. }
  103. /**
  104. * getAllAdherentsNotInformed
  105. *
  106. * @return number
  107. */
  108. public function getAllAdherentsNotInformed()
  109. {
  110. global $langs, $conf, $db, $user;
  111. $langs->load("adherent");
  112. $sql = "SELECT a.rowid";
  113. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
  114. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields as ae ON ae.fk_object = a.rowid";
  115. $sql .= " WHERE a.statut=0 AND (ae.datapolicy_consentement=0 OR ae.datapolicy_consentement IS NULL) AND (ae.datapolicy_opposition_traitement=0 OR ae.datapolicy_opposition_traitement IS NULL) AND (ae.datapolicy_opposition_prospection=0 OR ae.datapolicy_opposition_prospection IS NULL)";
  116. $sql .= " AND ae.datapolicy_send IS NULL";
  117. $sql .= " AND a.entity=".$conf->entity;
  118. $resql = $this->db->query($sql);
  119. if ($resql) {
  120. $num = $this->db->num_rows($resql);
  121. $i = 0;
  122. while ($i < $num) {
  123. $obj = $this->db->fetch_object($resql);
  124. $adherent = new Adherent($db);
  125. $adherent->fetch($obj->rowid);
  126. DataPolicy::sendMailDataPolicyAdherent($adherent);
  127. $i++;
  128. }
  129. } else {
  130. $this->error = $this->db->error();
  131. return -1;
  132. }
  133. }
  134. /**
  135. * sendMailDataPolicyContact
  136. *
  137. * @param mixed $contact Contact
  138. * @return void
  139. */
  140. public static function sendMailDataPolicyContact($contact)
  141. {
  142. global $langs, $conf, $db, $user;
  143. $error = 0;
  144. $from = $user->getFullName($langs).' <'.$user->email.'>';
  145. $sendto = $contact->email;
  146. $code = dol_hash($contact->email, 'md5');
  147. if (!empty($contact->default_lang)) {
  148. $l = $contact->default_lang;
  149. } else {
  150. $l = $langs->defaultlang;
  151. }
  152. // TODO Use a dolibarr email template
  153. $s = "DATAPOLICYSUBJECT_".$l;
  154. $ma = "DATAPOLICYCONTENT_".$l;
  155. $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
  156. $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
  157. $subject = $conf->global->$s;
  158. $message = $conf->global->$ma;
  159. $linka = $conf->global->$la;
  160. $linkr = $conf->global->$lr;
  161. $sendtocc = $sendtobcc = '';
  162. $filepath = $mimetype = $filename = array();
  163. $deliveryreceipt = 0;
  164. $substitutionarray = array(
  165. '__LINKACCEPT__' => '<a href="'.dol_buildpath('/public/datapolicy/index.php?action=1&c='.$contact->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linka.'</a>',
  166. '__LINKREFUSED__' => '<a href="'.dol_buildpath('/public/datapolicy/index.php?action=2&c='.$contact->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linkr.'</a>',
  167. '__FIRSTNAME__' => $contact->firstname,
  168. '__NAME__' => $contact->lastname,
  169. '__CIVILITY__' => $contact->civility,
  170. );
  171. $subject = make_substitutions($subject, $substitutionarray);
  172. $message = make_substitutions($message, $substitutionarray);
  173. $actiontypecode = 'AC_EMAIL';
  174. $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
  175. if ($message) {
  176. if ($sendtocc) {
  177. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
  178. }
  179. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
  180. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
  181. $actionmsg = dol_concatdesc($actionmsg, $message);
  182. }
  183. // Send mail
  184. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  185. $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
  186. if ($mailfile->error) {
  187. $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
  188. } else {
  189. $result4 = $mailfile->sendfile();
  190. if (!$error) {
  191. $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
  192. $contact->array_options['options_datapolicy_send'] = date('Y-m-d', time());
  193. $contact->update($contact->id);
  194. } else {
  195. dol_print_error($db);
  196. }
  197. }
  198. setEventMessage($resultmasssend);
  199. }
  200. /**
  201. * sendMailDataPolicyCompany
  202. *
  203. * @param Societe $societe Object societe
  204. * @return void
  205. */
  206. public static function sendMailDataPolicyCompany($societe)
  207. {
  208. global $langs, $conf, $db, $user;
  209. $error = 0;
  210. $from = $user->getFullName($langs).' <'.$user->email.'>';
  211. $sendto = $societe->email;
  212. $code = dol_hash($societe->email, 'md5');
  213. if (!empty($societe->default_lang)) {
  214. $l = $societe->default_lang;
  215. } else {
  216. $l = $langs->defaultlang;
  217. }
  218. // TODO Use a dolibarr email template
  219. $s = "DATAPOLICYSUBJECT_".$l;
  220. $ma = "DATAPOLICYCONTENT_".$l;
  221. $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
  222. $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
  223. $subject = $conf->global->$s;
  224. $message = $conf->global->$ma;
  225. $linka = $conf->global->$la;
  226. $linkr = $conf->global->$lr;
  227. $sendtocc = $sendtobcc = '';
  228. $filepath = $mimetype = $filename = array();
  229. $deliveryreceipt = 0;
  230. $substitutionarray = array(
  231. '__LINKACCEPT__' => '<a href="'.dol_buildpath('/public/datapolicy/index.php?action=1&s='.$societe->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linka.'</a>',
  232. '__LINKREFUSED__' => '<a href="'.dol_buildpath('/public/datapolicy/index.php?action=2&s='.$societe->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linkr.'</a>',
  233. );
  234. $subject = make_substitutions($subject, $substitutionarray);
  235. $message = make_substitutions($message, $substitutionarray);
  236. $actiontypecode = 'AC_EMAIL';
  237. $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
  238. if ($message) {
  239. if ($sendtocc) {
  240. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
  241. }
  242. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
  243. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
  244. $actionmsg = dol_concatdesc($actionmsg, $message);
  245. }
  246. // Send mail
  247. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  248. $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
  249. if ($mailfile->error) {
  250. $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
  251. } else {
  252. $result4 = $mailfile->sendfile();
  253. if (!$error) {
  254. $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
  255. $societe->array_options['options_datapolicy_send'] = date('Y-m-d', time());
  256. $societe->update($societe->id);
  257. } else {
  258. dol_print_error($db);
  259. }
  260. }
  261. setEventMessage($resultmasssend);
  262. }
  263. /**
  264. * sendMailDataPolicyAdherent
  265. *
  266. * @param Adherent $adherent Member
  267. * @return void
  268. */
  269. public static function sendMailDataPolicyAdherent($adherent)
  270. {
  271. global $langs, $conf, $db, $user;
  272. $error = 0;
  273. $from = $user->getFullName($langs).' <'.$user->email.'>';
  274. $sendto = $adherent->email;
  275. $code = dol_hash($adherent->email, 'md5');
  276. if (!empty($adherent->default_lang)) {
  277. $l = $adherent->default_lang;
  278. } else {
  279. $l = $langs->defaultlang;
  280. }
  281. // TODO Use a dolibarr email template
  282. $s = 'TXTLINKDATAPOLICYSUBJECT_'.$l;
  283. $ma = 'TXTLINKDATAPOLICYMESSAGE_'.$l;
  284. $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
  285. $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
  286. $subject = $conf->global->$s;
  287. $message = $conf->global->$ma;
  288. $linka = $conf->global->$la;
  289. $linkr = $conf->global->$lr;
  290. $sendtocc = $sendtobcc = '';
  291. $filepath = $mimetype = $filename = array();
  292. $deliveryreceipt = 0;
  293. $substitutionarray = array(
  294. '__LINKACCEPT__' => '<a href="'.dol_buildpath('/public/datapolicy/index.php?action=1&a='.$adherent->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linka.'</a>',
  295. '__LINKREFUSED__' => '<a href="'.dol_buildpath('/public/datapolicy/index.php?action=2&a='.$adherent->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linkr.'</a>',
  296. );
  297. $subject = make_substitutions($subject, $substitutionarray);
  298. $message = make_substitutions($message, $substitutionarray);
  299. $actiontypecode = 'AC_EMAIL';
  300. $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
  301. if ($message) {
  302. if ($sendtocc) {
  303. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
  304. }
  305. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
  306. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
  307. $actionmsg = dol_concatdesc($actionmsg, $message);
  308. }
  309. // Send mail
  310. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  311. $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
  312. if ($mailfile->error) {
  313. $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
  314. } else {
  315. $result4 = $mailfile->sendfile();
  316. if (!$error) {
  317. $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
  318. $adherent->array_options['options_datapolicy_send'] = date('Y-m-d', time());
  319. $adherent->update($user);
  320. } else {
  321. dol_print_error($db);
  322. }
  323. }
  324. setEventMessage($resultmasssend);
  325. }
  326. }