dolibarr 20.0.0
datapolicy.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
25include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
26include_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
27include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
28
29
34{
38 public $db;
39
40 public $error;
41
42
48 public function __construct($db)
49 {
50 $this->db = $db;
51 }
52
58 public function getAllContactNotInformed()
59 {
60 global $langs, $conf, $db, $user;
61
62 $langs->load("companies");
63
64 $sql = "SELECT c.rowid";
65 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
66 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
67 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as spe ON spe.fk_object = c.rowid";
68 $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))";
69 $sql .= " AND spe.datapolicy_send IS NULL";
70 $sql .= " AND c.entity=".$conf->entity;
71 $resql = $this->db->query($sql);
72 if ($resql) {
73 $num = $this->db->num_rows($resql);
74 $i = 0;
75 while ($i < $num) {
76 $obj = $this->db->fetch_object($resql);
77 $contact = new Contact($db);
78 $contact->fetch($obj->rowid);
79
81 $i++;
82 }
83 } else {
84 $this->error = $this->db->error();
85 return -1;
86 }
87
88 return 1;
89 }
90
97 {
98 global $langs, $conf, $db, $user;
99
100 $langs->load("companies");
101
102 $sql = "SELECT s.rowid";
103 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
104 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as se ON se.fk_object = s.rowid";
105 $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)";
106 $sql .= " AND se.datapolicy_send IS NULL";
107 $sql .= " AND s.entity=".$conf->entity;
108 $resql = $this->db->query($sql);
109 if ($resql) {
110 $num = $this->db->num_rows($resql);
111 $i = 0;
112 while ($i < $num) {
113 $obj = $this->db->fetch_object($resql);
114 $societe = new Societe($db);
115 $societe->fetch($obj->rowid);
116
118 $i++;
119 }
120 } else {
121 $this->error = $this->db->error();
122 return -1;
123 }
124
125 return 1;
126 }
127
134 {
135 global $langs, $conf, $db, $user;
136
137 $langs->load("adherent");
138
139 $sql = "SELECT a.rowid";
140 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
141 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields as ae ON ae.fk_object = a.rowid";
142 $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)";
143 $sql .= " AND ae.datapolicy_send IS NULL";
144 $sql .= " AND a.entity=".$conf->entity;
145 $resql = $this->db->query($sql);
146 if ($resql) {
147 $num = $this->db->num_rows($resql);
148 $i = 0;
149 while ($i < $num) {
150 $obj = $this->db->fetch_object($resql);
151 $adherent = new Adherent($db);
152 $adherent->fetch($obj->rowid);
153
155 $i++;
156 }
157 } else {
158 $this->error = $this->db->error();
159 return -1;
160 }
161
162 return 1;
163 }
164
171 public static function sendMailDataPolicyContact($contact)
172 {
173 global $langs, $conf, $db, $user;
174
175 $error = 0;
176
177 $from = $user->getFullName($langs).' <'.$user->email.'>';
178
179 $sendto = $contact->email;
180 $code = dol_hash($contact->email, 'md5');
181 if (!empty($contact->default_lang)) {
182 $l = $contact->default_lang;
183 } else {
184 $l = $langs->defaultlang;
185 }
186 // TODO Use a dolibarr email template
187 $s = "DATAPOLICYSUBJECT_".$l;
188 $ma = "DATAPOLICYCONTENT_".$l;
189 $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
190 $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
191
192 $subject = getDolGlobalString($s);
193 $message = getDolGlobalString($ma);
194 $linka = getDolGlobalString($la);
195 $linkr = getDolGlobalString($lr);
196 $sendtocc = $sendtobcc = '';
197 $filepath = $mimetype = $filename = array();
198 $deliveryreceipt = 0;
199
200 $substitutionarray = array(
201 '__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>',
202 '__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>',
203 '__FIRSTNAME__' => $contact->firstname,
204 '__NAME__' => $contact->lastname,
205 '__CIVILITY__' => $contact->civility,
206 );
207 $subject = make_substitutions($subject, $substitutionarray);
208 $message = make_substitutions($message, $substitutionarray);
209
210 $actiontypecode = 'AC_EMAIL';
211 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
212 if ($message) {
213 if ($sendtocc) {
214 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
215 }
216 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
217 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
218 $actionmsg = dol_concatdesc($actionmsg, $message);
219 }
220
221
222 // Send mail
223 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
224 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
225
226 $resultmasssend = '';
227 if ($mailfile->error) {
228 $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
229 } else {
230 $resultmail = $mailfile->sendfile();
231
232 if ($resultmail) {
233 $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
234 $contact->array_options['options_datapolicy_send'] = date('Y-m-d', time());
235 $contact->update($contact->id);
236 } else {
237 dol_print_error($db);
238 }
239 }
240 setEventMessage($resultmasssend);
241 }
242
249 public static function sendMailDataPolicyCompany($societe)
250 {
251 global $langs, $conf, $db, $user;
252
253 $error = 0;
254
255 $from = $user->getFullName($langs).' <'.$user->email.'>';
256
257 $sendto = $societe->email;
258
259 $code = dol_hash($societe->email, 'md5');
260 if (!empty($societe->default_lang)) {
261 $l = $societe->default_lang;
262 } else {
263 $l = $langs->defaultlang;
264 }
265 // TODO Use a dolibarr email template
266 $s = "DATAPOLICYSUBJECT_".$l;
267 $ma = "DATAPOLICYCONTENT_".$l;
268 $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
269 $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
270
271 $subject = getDolGlobalString($s);
272 $message = getDolGlobalString($ma);
273 $linka = getDolGlobalString($la);
274 $linkr = getDolGlobalString($lr);
275 $sendtocc = $sendtobcc = '';
276 $filepath = $mimetype = $filename = array();
277 $deliveryreceipt = 0;
278
279 $substitutionarray = array(
280 '__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>',
281 '__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>',
282 );
283 $subject = make_substitutions($subject, $substitutionarray);
284 $message = make_substitutions($message, $substitutionarray);
285
286 $actiontypecode = 'AC_EMAIL';
287 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
288 if ($message) {
289 if ($sendtocc) {
290 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
291 }
292 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
293 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
294 $actionmsg = dol_concatdesc($actionmsg, $message);
295 }
296
297 // Send mail
298 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
299 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
300
301 $resultmasssend = '';
302 if ($mailfile->error) {
303 $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
304 } else {
305 $resultmail = $mailfile->sendfile();
306
307 if ($resultmail) {
308 $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
309 $societe->array_options['options_datapolicy_send'] = date('Y-m-d', time());
310 $societe->update($societe->id, $user);
311 } else {
312 dol_print_error($db);
313 }
314 }
315 setEventMessage($resultmasssend);
316 }
317
324 public static function sendMailDataPolicyAdherent($adherent)
325 {
326 global $langs, $conf, $db, $user;
327
328 $error = 0;
329
330 $from = $user->getFullName($langs).' <'.$user->email.'>';
331
332 $sendto = $adherent->email;
333
334 $code = dol_hash($adherent->email, 'md5');
335 if (!empty($adherent->default_lang)) {
336 $l = $adherent->default_lang;
337 } else {
338 $l = $langs->defaultlang;
339 }
340 // TODO Use a dolibarr email template
341 $s = 'TXTLINKDATAPOLICYSUBJECT_'.$l;
342 $ma = 'TXTLINKDATAPOLICYMESSAGE_'.$l;
343 $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
344 $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
345
346 $subject = getDolGlobalString($s);
347 $message = getDolGlobalString($ma);
348 $linka = getDolGlobalString($la);
349 $linkr = getDolGlobalString($lr);
350 $sendtocc = $sendtobcc = '';
351 $filepath = $mimetype = $filename = array();
352 $deliveryreceipt = 0;
353
354 $substitutionarray = array(
355 '__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>',
356 '__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>',
357 );
358 $subject = make_substitutions($subject, $substitutionarray);
359 $message = make_substitutions($message, $substitutionarray);
360
361 $actiontypecode = 'AC_EMAIL';
362 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
363 if ($message) {
364 if ($sendtocc) {
365 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
366 }
367 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
368 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
369 $actionmsg = dol_concatdesc($actionmsg, $message);
370 }
371
372
373 // Send mail
374 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
375 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
376
377 $resultmasssend = '';
378 if ($mailfile->error) {
379 $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
380 } else {
381 $resultmail = $mailfile->sendfile();
382
383 if ($resultmail) {
384 $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
385 $adherent->array_options['options_datapolicy_send'] = date('Y-m-d', time());
386 $adherent->update($user);
387 } else {
388 dol_print_error($db);
389 }
390 }
391 setEventMessage($resultmasssend);
392 }
393}
Class to manage members of a foundation.
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class to manage contact/addresses.
Class DataPolicy.
__construct($db)
Constructor.
static sendMailDataPolicyAdherent($adherent)
sendMailDataPolicyAdherent
getAllContactNotInformed()
getAllContactNotInformed
static sendMailDataPolicyContact($contact)
sendMailDataPolicyContact
getAllCompaniesNotInformed()
getAllCompaniesNotInformed
getAllAdherentsNotInformed()
getAllAdherentsNotInformed
static sendMailDataPolicyCompany($societe)
sendMailDataPolicyCompany
Class to manage third parties objects (customers, suppliers, prospects...)
setEventMessage($mesgs, $style='mesgs', $noduplicate=0)
Set event message in dol_events session object.
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.