dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
26include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
27include_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
28include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
29
30
35{
39 public $db;
40
44 public $error;
45
46
52 public function __construct($db)
53 {
54 $this->db = $db;
55 }
56
62 public function getAllContactNotInformed()
63 {
64 global $langs, $conf, $db, $user;
65
66 $langs->load("companies");
67
68 $sql = "SELECT c.rowid";
69 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
70 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
71 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as spe ON spe.fk_object = c.rowid";
72 $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))";
73 $sql .= " AND spe.datapolicy_send IS NULL";
74 $sql .= " AND c.entity=".$conf->entity;
75 $resql = $this->db->query($sql);
76 if ($resql) {
77 $num = $this->db->num_rows($resql);
78 $i = 0;
79 while ($i < $num) {
80 $obj = $this->db->fetch_object($resql);
81 $contact = new Contact($db);
82 $contact->fetch($obj->rowid);
83
85 $i++;
86 }
87 } else {
88 $this->error = $this->db->error();
89 return -1;
90 }
91
92 return 1;
93 }
94
101 {
102 global $langs, $conf, $db, $user;
103
104 $langs->load("companies");
105
106 $sql = "SELECT s.rowid";
107 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
108 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as se ON se.fk_object = s.rowid";
109 $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)";
110 $sql .= " AND se.datapolicy_send IS NULL";
111 $sql .= " AND s.entity=".$conf->entity;
112 $resql = $this->db->query($sql);
113 if ($resql) {
114 $num = $this->db->num_rows($resql);
115 $i = 0;
116 while ($i < $num) {
117 $obj = $this->db->fetch_object($resql);
118 $societe = new Societe($db);
119 $societe->fetch($obj->rowid);
120
122 $i++;
123 }
124 } else {
125 $this->error = $this->db->error();
126 return -1;
127 }
128
129 return 1;
130 }
131
138 {
139 global $langs, $conf, $db, $user;
140
141 $langs->load("adherent");
142
143 $sql = "SELECT a.rowid";
144 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
145 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields as ae ON ae.fk_object = a.rowid";
146 $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)";
147 $sql .= " AND ae.datapolicy_send IS NULL";
148 $sql .= " AND a.entity=".$conf->entity;
149 $resql = $this->db->query($sql);
150 if ($resql) {
151 $num = $this->db->num_rows($resql);
152 $i = 0;
153 while ($i < $num) {
154 $obj = $this->db->fetch_object($resql);
155 $adherent = new Adherent($db);
156 $adherent->fetch($obj->rowid);
157
159 $i++;
160 }
161 } else {
162 $this->error = $this->db->error();
163 return -1;
164 }
165
166 return 1;
167 }
168
175 public static function sendMailDataPolicyContact($contact)
176 {
177 global $langs, $conf, $db, $user;
178
179 $error = 0;
180
181 $from = $user->getFullName($langs).' <'.$user->email.'>';
182
183 $sendto = $contact->email;
184 $code = dol_hash($contact->email, 'md5');
185 if (!empty($contact->default_lang)) {
186 $l = $contact->default_lang;
187 } else {
188 $l = $langs->defaultlang;
189 }
190 // TODO Use a dolibarr email template
191 $s = "DATAPOLICYSUBJECT_".$l;
192 $ma = "DATAPOLICYCONTENT_".$l;
193 $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
194 $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
195
196 $subject = getDolGlobalString($s);
197 $message = getDolGlobalString($ma);
198 $linka = getDolGlobalString($la);
199 $linkr = getDolGlobalString($lr);
200 $sendtocc = $sendtobcc = '';
201 $filepath = $mimetype = $filename = array();
202 $deliveryreceipt = 0;
203
204 $substitutionarray = array(
205 '__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>',
206 '__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>',
207 '__FIRSTNAME__' => $contact->firstname,
208 '__NAME__' => $contact->lastname,
209 '__CIVILITY__' => $contact->civility,
210 );
211 $subject = make_substitutions($subject, $substitutionarray);
212 $message = make_substitutions($message, $substitutionarray);
213
214 $actiontypecode = 'AC_EMAIL';
215 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
216 if ($message) {
217 if ($sendtocc) {
218 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
219 }
220 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
221 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
222 $actionmsg = dol_concatdesc($actionmsg, $message);
223 }
224
225
226 // Send mail
227 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
228 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
229
230 $resultmasssend = '';
231 if ($mailfile->error) {
232 $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
233 } else {
234 $resultmail = $mailfile->sendfile();
235
236 if ($resultmail) {
237 $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
238 $contact->array_options['options_datapolicy_send'] = date('Y-m-d', time());
239 $contact->update($contact->id);
240 } else {
241 dol_print_error($db);
242 }
243 }
244 setEventMessage($resultmasssend);
245 }
246
253 public static function sendMailDataPolicyCompany($societe)
254 {
255 global $langs, $conf, $db, $user;
256
257 $error = 0;
258
259 $from = $user->getFullName($langs).' <'.$user->email.'>';
260
261 $sendto = $societe->email;
262
263 $code = dol_hash($societe->email, 'md5');
264 if (!empty($societe->default_lang)) {
265 $l = $societe->default_lang;
266 } else {
267 $l = $langs->defaultlang;
268 }
269 // TODO Use a dolibarr email template
270 $s = "DATAPOLICYSUBJECT_".$l;
271 $ma = "DATAPOLICYCONTENT_".$l;
272 $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
273 $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
274
275 $subject = getDolGlobalString($s);
276 $message = getDolGlobalString($ma);
277 $linka = getDolGlobalString($la);
278 $linkr = getDolGlobalString($lr);
279 $sendtocc = $sendtobcc = '';
280 $filepath = $mimetype = $filename = array();
281 $deliveryreceipt = 0;
282
283 $substitutionarray = array(
284 '__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>',
285 '__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>',
286 );
287 $subject = make_substitutions($subject, $substitutionarray);
288 $message = make_substitutions($message, $substitutionarray);
289
290 $actiontypecode = 'AC_EMAIL';
291 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
292 if ($message) {
293 if ($sendtocc) {
294 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
295 }
296 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
297 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
298 $actionmsg = dol_concatdesc($actionmsg, $message);
299 }
300
301 // Send mail
302 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
303 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
304
305 $resultmasssend = '';
306 if ($mailfile->error) {
307 $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
308 } else {
309 $resultmail = $mailfile->sendfile();
310
311 if ($resultmail) {
312 $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
313 $societe->array_options['options_datapolicy_send'] = date('Y-m-d', time());
314 $societe->update($societe->id, $user);
315 } else {
316 dol_print_error($db);
317 }
318 }
319 setEventMessage($resultmasssend);
320 }
321
328 public static function sendMailDataPolicyAdherent($adherent)
329 {
330 global $langs, $conf, $db, $user;
331
332 $error = 0;
333
334 $from = $user->getFullName($langs).' <'.$user->email.'>';
335
336 $sendto = $adherent->email;
337
338 $code = dol_hash($adherent->email, 'md5');
339 if (!empty($adherent->default_lang)) {
340 $l = $adherent->default_lang;
341 } else {
342 $l = $langs->defaultlang;
343 }
344 // TODO Use a dolibarr email template
345 $s = 'TXTLINKDATAPOLICYSUBJECT_'.$l;
346 $ma = 'TXTLINKDATAPOLICYMESSAGE_'.$l;
347 $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
348 $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
349
350 $subject = getDolGlobalString($s);
351 $message = getDolGlobalString($ma);
352 $linka = getDolGlobalString($la);
353 $linkr = getDolGlobalString($lr);
354 $sendtocc = $sendtobcc = '';
355 $filepath = $mimetype = $filename = array();
356 $deliveryreceipt = 0;
357
358 $substitutionarray = array(
359 '__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>',
360 '__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>',
361 );
362 $subject = make_substitutions($subject, $substitutionarray);
363 $message = make_substitutions($message, $substitutionarray);
364
365 $actiontypecode = 'AC_EMAIL';
366 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
367 if ($message) {
368 if ($sendtocc) {
369 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
370 }
371 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
372 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
373 $actionmsg = dol_concatdesc($actionmsg, $message);
374 }
375
376
377 // Send mail
378 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
379 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
380
381 $resultmasssend = '';
382 if ($mailfile->error) {
383 $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
384 } else {
385 $resultmail = $mailfile->sendfile();
386
387 if ($resultmail) {
388 $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
389 $adherent->array_options['options_datapolicy_send'] = date('Y-m-d', time());
390 $adherent->update($user);
391 } else {
392 dol_print_error($db);
393 }
394 }
395 setEventMessage($resultmasssend);
396 }
397}
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, $attop=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 a Dolibarr global constant string value.
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.