dolibarr  16.0.5
datapolicy.class.php
Go to the documentation of this file.
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 
23 include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
24 include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
25 include_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
26 
27 
32 {
38  public function __construct($db)
39  {
40  $this->db = $db;
41  }
42 
48  public function getAllContactNotInformed()
49  {
50  global $langs, $conf, $db, $user;
51 
52  $langs->load("companies");
53 
54  $sql = "SELECT c.rowid";
55  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
56  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
57  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as spe ON spe.fk_object = c.rowid";
58  $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))";
59  $sql .= " AND spe.datapolicy_send IS NULL";
60  $sql .= " AND c.entity=".$conf->entity;
61  $resql = $this->db->query($sql);
62  if ($resql) {
63  $num = $this->db->num_rows($resql);
64  $i = 0;
65  while ($i < $num) {
66  $obj = $this->db->fetch_object($resql);
67  $contact = new Contact($db);
68  $contact->fetch($obj->rowid);
69 
71  $i++;
72  }
73  } else {
74  $this->error = $this->db->error();
75  return -1;
76  }
77  }
78 
84  public function getAllCompaniesNotInformed()
85  {
86  global $langs, $conf, $db, $user;
87 
88  $langs->load("companies");
89 
90  $sql = "SELECT s.rowid";
91  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
92  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as se ON se.fk_object = s.rowid";
93  $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)";
94  $sql .= " AND se.datapolicy_send IS NULL";
95  $sql .= " AND s.entity=".$conf->entity;
96  $resql = $this->db->query($sql);
97  if ($resql) {
98  $num = $this->db->num_rows($resql);
99  $i = 0;
100  while ($i < $num) {
101  $obj = $this->db->fetch_object($resql);
102  $societe = new Societe($db);
103  $societe->fetch($obj->rowid);
104 
106  $i++;
107  }
108  } else {
109  $this->error = $this->db->error();
110  return -1;
111  }
112  }
113 
119  public function getAllAdherentsNotInformed()
120  {
121  global $langs, $conf, $db, $user;
122 
123  $langs->load("adherent");
124 
125  $sql = "SELECT a.rowid";
126  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
127  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields as ae ON ae.fk_object = a.rowid";
128  $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)";
129  $sql .= " AND ae.datapolicy_send IS NULL";
130  $sql .= " AND a.entity=".$conf->entity;
131  $resql = $this->db->query($sql);
132  if ($resql) {
133  $num = $this->db->num_rows($resql);
134  $i = 0;
135  while ($i < $num) {
136  $obj = $this->db->fetch_object($resql);
137  $adherent = new Adherent($db);
138  $adherent->fetch($obj->rowid);
139 
141  $i++;
142  }
143  } else {
144  $this->error = $this->db->error();
145  return -1;
146  }
147  }
148 
155  public static function sendMailDataPolicyContact($contact)
156  {
157  global $langs, $conf, $db, $user;
158 
159  $error = 0;
160 
161  $from = $user->getFullName($langs).' <'.$user->email.'>';
162 
163  $sendto = $contact->email;
164  $code = md5($contact->email);
165  if (!empty($contact->default_lang)) {
166  $l = $contact->default_lang;
167  } else {
168  $l = $langs->defaultlang;
169  }
170  // TODO Use a dolibarr email template
171  $s = "DATAPOLICIESSUBJECT_".$l;
172  $ma = "DATAPOLICIESCONTENT_".$l;
173  $la = 'TXTLINKDATAPOLICIESACCEPT_'.$l;
174  $lr = 'TXTLINKDATAPOLICIESREFUSE_'.$l;
175 
176  $subject = $conf->global->$s;
177  $message = $conf->global->$ma;
178  $linka = $conf->global->$la;
179  $linkr = $conf->global->$lr;
180  $sendtocc = $sendtobcc = '';
181  $filepath = $mimetype = $filename = array();
182  $deliveryreceipt = 0;
183 
184  $substitutionarray = array(
185  '__LINKACCEPT__' => '<a href="'.dol_buildpath('/datapolicy/public/index.php?action=1&c='.$contact->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linka.'</a>',
186  '__LINKREFUSED__' => '<a href="'.dol_buildpath('/datapolicy/public/index.php?action=2&c='.$contact->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linkr.'</a>',
187  '__FIRSTNAME__' => $contact->firstname,
188  '__NAME__' => $contact->lastname,
189  '__CIVILITY__' => $contact->civility,
190  );
191  $subject = make_substitutions($subject, $substitutionarray);
192  $message = make_substitutions($message, $substitutionarray);
193 
194  $actiontypecode = 'AC_EMAIL';
195  $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
196  if ($message) {
197  if ($sendtocc) {
198  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
199  }
200  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
201  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
202  $actionmsg = dol_concatdesc($actionmsg, $message);
203  }
204 
205 
206  // Send mail
207  require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
208  $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
209 
210  if ($mailfile->error) {
211  $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
212  } else {
213  $result4 = $mailfile->sendfile();
214  if (!$error) {
215  $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
216  $contact->array_options['options_datapolicy_send'] = date('Y-m-d', time());
217  $contact->update($contact->id);
218  } else {
219  dol_print_error($db);
220  }
221  }
222  setEventMessage($resultmasssend);
223  }
224 
231  public static function sendMailDataPolicyCompany($societe)
232  {
233  global $langs, $conf, $db, $user;
234 
235  $error = 0;
236 
237  $from = $user->getFullName($langs).' <'.$user->email.'>';
238 
239  $sendto = $societe->email;
240 
241  $code = md5($societe->email);
242  if (!empty($societe->default_lang)) {
243  $l = $societe->default_lang;
244  } else {
245  $l = $langs->defaultlang;
246  }
247  // TODO Use a dolibarr email template
248  $s = "DATAPOLICIESSUBJECT_".$l;
249  $ma = "DATAPOLICIESCONTENT_".$l;
250  $la = 'TXTLINKDATAPOLICIESACCEPT_'.$l;
251  $lr = 'TXTLINKDATAPOLICIESREFUSE_'.$l;
252 
253  $subject = $conf->global->$s;
254  $message = $conf->global->$ma;
255  $linka = $conf->global->$la;
256  $linkr = $conf->global->$lr;
257  $sendtocc = $sendtobcc = '';
258  $filepath = $mimetype = $filename = array();
259  $deliveryreceipt = 0;
260 
261  $substitutionarray = array(
262  '__LINKACCEPT__' => '<a href="'.dol_buildpath('/datapolicy/public/index.php?action=1&s='.$societe->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linka.'</a>',
263  '__LINKREFUSED__' => '<a href="'.dol_buildpath('/datapolicy/public/index.php?action=2&s='.$societe->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linkr.'</a>',
264  );
265  $subject = make_substitutions($subject, $substitutionarray);
266  $message = make_substitutions($message, $substitutionarray);
267 
268  $actiontypecode = 'AC_EMAIL';
269  $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
270  if ($message) {
271  if ($sendtocc) {
272  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
273  }
274  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
275  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
276  $actionmsg = dol_concatdesc($actionmsg, $message);
277  }
278 
279  // Send mail
280  require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
281  $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
282  if ($mailfile->error) {
283  $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
284  } else {
285  $result4 = $mailfile->sendfile();
286 
287  if (!$error) {
288  $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
289  $societe->array_options['options_datapolicy_send'] = date('Y-m-d', time());
290  $societe->update($societe->id);
291  } else {
292  dol_print_error($db);
293  }
294  }
295  setEventMessage($resultmasssend);
296  }
297 
304  public static function sendMailDataPolicyAdherent($adherent)
305  {
306  global $langs, $conf, $db, $user;
307 
308  $error = 0;
309 
310  $from = $user->getFullName($langs).' <'.$user->email.'>';
311 
312  $sendto = $adherent->email;
313 
314  $code = md5($adherent->email);
315  if (!empty($adherent->default_lang)) {
316  $l = $adherent->default_lang;
317  } else {
318  $l = $langs->defaultlang;
319  }
320  // TODO Use a dolibarr email template
321  $s = 'TXTLINKDATAPOLICIESSUBJECT_'.$l;
322  $ma = 'TXTLINKDATAPOLICIESMESSAGE_'.$l;
323  $la = 'TXTLINKDATAPOLICIESACCEPT_'.$l;
324  $lr = 'TXTLINKDATAPOLICIESREFUSE_'.$l;
325 
326  $subject = $conf->global->$s;
327  $message = $conf->global->$ma;
328  $linka = $conf->global->$la;
329  $linkr = $conf->global->$lr;
330  $sendtocc = $sendtobcc = '';
331  $filepath = $mimetype = $filename = array();
332  $deliveryreceipt = 0;
333 
334  $substitutionarray = array(
335  '__LINKACCEPT__' => '<a href="'.dol_buildpath('/datapolicy/public/index.php?action=1&a='.$adherent->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linka.'</a>',
336  '__LINKREFUSED__' => '<a href="'.dol_buildpath('/datapolicy/public/index.php?action=2&a='.$adherent->id.'&l='.$l.'&key='.$code, 3).'" target="_blank" rel="noopener noreferrer">'.$linkr.'</a>',
337  );
338  $subject = make_substitutions($subject, $substitutionarray);
339  $message = make_substitutions($message, $substitutionarray);
340 
341  $actiontypecode = 'AC_EMAIL';
342  $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
343  if ($message) {
344  if ($sendtocc) {
345  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
346  }
347  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
348  $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
349  $actionmsg = dol_concatdesc($actionmsg, $message);
350  }
351 
352 
353  // Send mail
354  require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
355  $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
356  if ($mailfile->error) {
357  $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
358  } else {
359  $result4 = $mailfile->sendfile();
360 
361  if (!$error) {
362  $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
363  $adherent->array_options['options_datapolicy_send'] = date('Y-m-d', time());
364  $adherent->update($user);
365  } else {
366  dol_print_error($db);
367  }
368  }
369  setEventMessage($resultmasssend);
370  }
371 }
make_substitutions
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
Definition: functions.lib.php:7839
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
db
$conf db
API class for accounts.
Definition: inc.php:41
DataPolicy\__construct
__construct($db)
Constructor.
Definition: datapolicy.class.php:38
DataPolicy\sendMailDataPolicyAdherent
static sendMailDataPolicyAdherent($adherent)
sendMailDataPolicyAdherent
Definition: datapolicy.class.php:304
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
DataPolicy\getAllCompaniesNotInformed
getAllCompaniesNotInformed()
getAllCompaniesNotInformed
Definition: datapolicy.class.php:84
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
CMailFile
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Definition: CMailFile.class.php:38
DataPolicy\sendMailDataPolicyContact
static sendMailDataPolicyContact($contact)
sendMailDataPolicyContact
Definition: datapolicy.class.php:155
dol_concatdesc
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...
Definition: functions.lib.php:7248
setEventMessage
setEventMessage($mesgs, $style='mesgs')
Set event message in dol_events session object.
Definition: functions.lib.php:8108
Contact
Class to manage contact/addresses.
Definition: contact.class.php:40
Adherent
Class to manage members of a foundation.
Definition: adherent.class.php:46
DataPolicy\getAllAdherentsNotInformed
getAllAdherentsNotInformed()
getAllAdherentsNotInformed
Definition: datapolicy.class.php:119
DataPolicy\getAllContactNotInformed
getAllContactNotInformed()
getAllContactNotInformed
Definition: datapolicy.class.php:48
DataPolicy\sendMailDataPolicyCompany
static sendMailDataPolicyCompany($societe)
sendMailDataPolicyCompany
Definition: datapolicy.class.php:231
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
DataPolicy
Class DataPolicy.
Definition: datapolicy.class.php:31