dolibarr 21.0.0-beta
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
26include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
27include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
28include_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
29include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
30
31
36{
40 public $db;
41
45 public $error;
46
47
53 public function __construct($db)
54 {
55 $this->db = $db;
56 }
57
63 public function getAllContactNotInformed()
64 {
65 global $langs, $conf, $db;
66
67 $langs->load("companies");
68
69 $sql = "SELECT c.rowid";
70 $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
71 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
72 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as spe ON spe.fk_object = c.rowid";
73 $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))";
74 $sql .= " AND spe.datapolicy_send IS NULL";
75 $sql .= " AND c.entity = ".((int) $conf->entity);
76 $resql = $this->db->query($sql);
77 if ($resql) {
78 $num = $this->db->num_rows($resql);
79 $i = 0;
80 while ($i < $num) {
81 $obj = $this->db->fetch_object($resql);
82 $contact = new Contact($db);
83 $contact->fetch($obj->rowid);
84
86 $i++;
87 }
88 } else {
89 $this->error = $this->db->error();
90 return -1;
91 }
92
93 return 1;
94 }
95
102 {
103 global $langs, $conf, $db;
104
105 $langs->load("companies");
106
107 $sql = "SELECT s.rowid";
108 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
109 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as se ON se.fk_object = s.rowid";
110 $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)";
111 $sql .= " AND se.datapolicy_send IS NULL";
112 $sql .= " AND s.entity = ".((int) $conf->entity);
113 $resql = $this->db->query($sql);
114 if ($resql) {
115 $num = $this->db->num_rows($resql);
116 $i = 0;
117 while ($i < $num) {
118 $obj = $this->db->fetch_object($resql);
119 $societe = new Societe($db);
120 $societe->fetch($obj->rowid);
121
123 $i++;
124 }
125 } else {
126 $this->error = $this->db->error();
127 return -1;
128 }
129
130 return 1;
131 }
132
139 {
140 global $langs, $conf, $db;
141
142 $langs->load("adherent");
143
144 $sql = "SELECT a.rowid";
145 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
146 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields as ae ON ae.fk_object = a.rowid";
147 $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)";
148 $sql .= " AND ae.datapolicy_send IS NULL";
149 $sql .= " AND a.entity = ".((int) $conf->entity);
150 $resql = $this->db->query($sql);
151 if ($resql) {
152 $num = $this->db->num_rows($resql);
153 $i = 0;
154 while ($i < $num) {
155 $obj = $this->db->fetch_object($resql);
156 $adherent = new Adherent($db);
157 $adherent->fetch($obj->rowid);
158
160 $i++;
161 }
162 } else {
163 $this->error = $this->db->error();
164 return -1;
165 }
166
167 return 1;
168 }
169
176 public static function sendMailDataPolicyContact($contact)
177 {
178 global $langs, $db, $user;
179
180 $error = 0;
181
182 $from = $user->getFullName($langs).' <'.$user->email.'>';
183
184 $sendto = $contact->email;
185 $code = dol_hash($contact->email, 'md5');
186 if (!empty($contact->default_lang)) {
187 $l = $contact->default_lang;
188 } else {
189 $l = $langs->defaultlang;
190 }
191 // TODO Use a dolibarr email template
192 $s = "DATAPOLICYSUBJECT_".$l;
193 $ma = "DATAPOLICYCONTENT_".$l;
194 $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
195 $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
196
197 $subject = getDolGlobalString($s);
198 $message = getDolGlobalString($ma);
199 $linka = getDolGlobalString($la);
200 $linkr = getDolGlobalString($lr);
201 $sendtocc = $sendtobcc = '';
202 $filepath = $mimetype = $filename = array();
203 $deliveryreceipt = 0;
204
205 $substitutionarray = array(
206 '__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>',
207 '__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>',
208 '__FIRSTNAME__' => $contact->firstname,
209 '__NAME__' => $contact->lastname,
210 '__CIVILITY__' => $contact->civility,
211 );
212 $subject = make_substitutions($subject, $substitutionarray);
213 $message = make_substitutions($message, $substitutionarray);
214
215 $actiontypecode = 'AC_EMAIL';
216 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
217 if ($message) {
218 if ($sendtocc) {
219 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
220 }
221 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
222 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
223 $actionmsg = dol_concatdesc($actionmsg, $message);
224 }
225
226
227 // Send mail
228 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
229 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
230
231 $resultmasssend = '';
232 if ($mailfile->error) {
233 $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
234 } else {
235 $resultmail = $mailfile->sendfile();
236
237 if ($resultmail) {
238 $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
239 $contact->array_options['options_datapolicy_send'] = date('Y-m-d', time());
240 $contact->update($contact->id);
241 } else {
242 dol_print_error($db);
243 }
244 }
245 setEventMessage($resultmasssend);
246 }
247
254 public static function sendMailDataPolicyCompany($societe)
255 {
256 global $langs, $db, $user;
257
258 $error = 0;
259
260 $from = $user->getFullName($langs).' <'.$user->email.'>';
261
262 $sendto = $societe->email;
263
264 $code = dol_hash($societe->email, 'md5');
265 if (!empty($societe->default_lang)) {
266 $l = $societe->default_lang;
267 } else {
268 $l = $langs->defaultlang;
269 }
270 // TODO Use a dolibarr email template
271 $s = "DATAPOLICYSUBJECT_".$l;
272 $ma = "DATAPOLICYCONTENT_".$l;
273 $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
274 $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
275
276 $subject = getDolGlobalString($s);
277 $message = getDolGlobalString($ma);
278 $linka = getDolGlobalString($la);
279 $linkr = getDolGlobalString($lr);
280 $sendtocc = $sendtobcc = '';
281 $filepath = $mimetype = $filename = array();
282 $deliveryreceipt = 0;
283
284 $substitutionarray = array(
285 '__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>',
286 '__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>',
287 );
288 $subject = make_substitutions($subject, $substitutionarray);
289 $message = make_substitutions($message, $substitutionarray);
290
291 $actiontypecode = 'AC_EMAIL';
292 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
293 if ($message) {
294 if ($sendtocc) {
295 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
296 }
297 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
298 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
299 $actionmsg = dol_concatdesc($actionmsg, $message);
300 }
301
302 // Send mail
303 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
304 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
305
306 $resultmasssend = '';
307 if ($mailfile->error) {
308 $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
309 } else {
310 $resultmail = $mailfile->sendfile();
311
312 if ($resultmail) {
313 $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
314 $societe->array_options['options_datapolicy_send'] = date('Y-m-d', time());
315 $societe->update($societe->id, $user);
316 } else {
317 dol_print_error($db);
318 }
319 }
320 setEventMessage($resultmasssend);
321 }
322
329 public static function sendMailDataPolicyAdherent($adherent)
330 {
331 global $langs, $db, $user;
332
333 $error = 0;
334
335 $from = $user->getFullName($langs).' <'.$user->email.'>';
336
337 $sendto = $adherent->email;
338
339 $code = dol_hash($adherent->email, 'md5');
340 if (!empty($adherent->default_lang)) {
341 $l = $adherent->default_lang;
342 } else {
343 $l = $langs->defaultlang;
344 }
345 // TODO Use a dolibarr email template
346 $s = 'TXTLINKDATAPOLICYSUBJECT_'.$l;
347 $ma = 'TXTLINKDATAPOLICYMESSAGE_'.$l;
348 $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
349 $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
350
351 $subject = getDolGlobalString($s);
352 $message = getDolGlobalString($ma);
353 $linka = getDolGlobalString($la);
354 $linkr = getDolGlobalString($lr);
355 $sendtocc = $sendtobcc = '';
356 $filepath = $mimetype = $filename = array();
357 $deliveryreceipt = 0;
358
359 $substitutionarray = array(
360 '__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>',
361 '__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>',
362 );
363 $subject = make_substitutions($subject, $substitutionarray);
364 $message = make_substitutions($message, $substitutionarray);
365
366 $actiontypecode = 'AC_EMAIL';
367 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
368 if ($message) {
369 if ($sendtocc) {
370 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
371 }
372 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
373 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
374 $actionmsg = dol_concatdesc($actionmsg, $message);
375 }
376
377
378 // Send mail
379 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
380 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
381
382 $resultmasssend = '';
383 if ($mailfile->error) {
384 $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
385 } else {
386 $resultmail = $mailfile->sendfile();
387
388 if ($resultmail) {
389 $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
390 $adherent->array_options['options_datapolicy_send'] = date('Y-m-d', time());
391 $adherent->update($user);
392 } else {
393 dol_print_error($db);
394 }
395 }
396 setEventMessage($resultmasssend);
397 }
398}
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.