| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <?php
- /* Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- /**
- * \file datapolicy/class/datapolicy.class.php
- * \ingroup datapolicy
- * \brief Class to manage feature of Data Policy module.
- */
- include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
- include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
- include_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
- include_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
- /**
- * Class DataPolicy
- */
- class DataPolicy
- {
- /**
- * Constructor
- *
- * @param DoliDB $db Database handler
- */
- public function __construct($db)
- {
- $this->db = $db;
- }
- /**
- * getAllContactNotInformed
- *
- * @return number
- */
- public function getAllContactNotInformed()
- {
- global $langs, $conf, $db, $user;
- $langs->load("companies");
- $sql = "SELECT c.rowid";
- $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as spe ON spe.fk_object = c.rowid";
- $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))";
- $sql .= " AND spe.datapolicy_send IS NULL";
- $sql .= " AND c.entity=".$conf->entity;
- $resql = $this->db->query($sql);
- if ($resql) {
- $num = $this->db->num_rows($resql);
- $i = 0;
- while ($i < $num) {
- $obj = $this->db->fetch_object($resql);
- $contact = new Contact($db);
- $contact->fetch($obj->rowid);
- DataPolicy::sendMailDataPolicyContact($contact);
- $i++;
- }
- } else {
- $this->error = $this->db->error();
- return -1;
- }
- }
- /**
- * getAllCompaniesNotInformed
- *
- * @return number
- */
- public function getAllCompaniesNotInformed()
- {
- global $langs, $conf, $db, $user;
- $langs->load("companies");
- $sql = "SELECT s.rowid";
- $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields as se ON se.fk_object = s.rowid";
- $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)";
- $sql .= " AND se.datapolicy_send IS NULL";
- $sql .= " AND s.entity=".$conf->entity;
- $resql = $this->db->query($sql);
- if ($resql) {
- $num = $this->db->num_rows($resql);
- $i = 0;
- while ($i < $num) {
- $obj = $this->db->fetch_object($resql);
- $societe = new Societe($db);
- $societe->fetch($obj->rowid);
- DataPolicy::sendMailDataPolicyCompany($societe);
- $i++;
- }
- } else {
- $this->error = $this->db->error();
- return -1;
- }
- }
- /**
- * getAllAdherentsNotInformed
- *
- * @return number
- */
- public function getAllAdherentsNotInformed()
- {
- global $langs, $conf, $db, $user;
- $langs->load("adherent");
- $sql = "SELECT a.rowid";
- $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
- $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields as ae ON ae.fk_object = a.rowid";
- $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)";
- $sql .= " AND ae.datapolicy_send IS NULL";
- $sql .= " AND a.entity=".$conf->entity;
- $resql = $this->db->query($sql);
- if ($resql) {
- $num = $this->db->num_rows($resql);
- $i = 0;
- while ($i < $num) {
- $obj = $this->db->fetch_object($resql);
- $adherent = new Adherent($db);
- $adherent->fetch($obj->rowid);
- DataPolicy::sendMailDataPolicyAdherent($adherent);
- $i++;
- }
- } else {
- $this->error = $this->db->error();
- return -1;
- }
- }
- /**
- * sendMailDataPolicyContact
- *
- * @param mixed $contact Contact
- * @return void
- */
- public static function sendMailDataPolicyContact($contact)
- {
- global $langs, $conf, $db, $user;
- $error = 0;
- $from = $user->getFullName($langs).' <'.$user->email.'>';
- $sendto = $contact->email;
- $code = dol_hash($contact->email, 'md5');
- if (!empty($contact->default_lang)) {
- $l = $contact->default_lang;
- } else {
- $l = $langs->defaultlang;
- }
- // TODO Use a dolibarr email template
- $s = "DATAPOLICYSUBJECT_".$l;
- $ma = "DATAPOLICYCONTENT_".$l;
- $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
- $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
- $subject = $conf->global->$s;
- $message = $conf->global->$ma;
- $linka = $conf->global->$la;
- $linkr = $conf->global->$lr;
- $sendtocc = $sendtobcc = '';
- $filepath = $mimetype = $filename = array();
- $deliveryreceipt = 0;
- $substitutionarray = array(
- '__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>',
- '__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>',
- '__FIRSTNAME__' => $contact->firstname,
- '__NAME__' => $contact->lastname,
- '__CIVILITY__' => $contact->civility,
- );
- $subject = make_substitutions($subject, $substitutionarray);
- $message = make_substitutions($message, $substitutionarray);
- $actiontypecode = 'AC_EMAIL';
- $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
- if ($message) {
- if ($sendtocc) {
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
- }
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
- $actionmsg = dol_concatdesc($actionmsg, $message);
- }
- // Send mail
- require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
- $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
- if ($mailfile->error) {
- $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
- } else {
- $result4 = $mailfile->sendfile();
- if (!$error) {
- $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
- $contact->array_options['options_datapolicy_send'] = date('Y-m-d', time());
- $contact->update($contact->id);
- } else {
- dol_print_error($db);
- }
- }
- setEventMessage($resultmasssend);
- }
- /**
- * sendMailDataPolicyCompany
- *
- * @param Societe $societe Object societe
- * @return void
- */
- public static function sendMailDataPolicyCompany($societe)
- {
- global $langs, $conf, $db, $user;
- $error = 0;
- $from = $user->getFullName($langs).' <'.$user->email.'>';
- $sendto = $societe->email;
- $code = dol_hash($societe->email, 'md5');
- if (!empty($societe->default_lang)) {
- $l = $societe->default_lang;
- } else {
- $l = $langs->defaultlang;
- }
- // TODO Use a dolibarr email template
- $s = "DATAPOLICYSUBJECT_".$l;
- $ma = "DATAPOLICYCONTENT_".$l;
- $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
- $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
- $subject = $conf->global->$s;
- $message = $conf->global->$ma;
- $linka = $conf->global->$la;
- $linkr = $conf->global->$lr;
- $sendtocc = $sendtobcc = '';
- $filepath = $mimetype = $filename = array();
- $deliveryreceipt = 0;
- $substitutionarray = array(
- '__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>',
- '__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>',
- );
- $subject = make_substitutions($subject, $substitutionarray);
- $message = make_substitutions($message, $substitutionarray);
- $actiontypecode = 'AC_EMAIL';
- $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
- if ($message) {
- if ($sendtocc) {
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
- }
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
- $actionmsg = dol_concatdesc($actionmsg, $message);
- }
- // Send mail
- require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
- $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
- if ($mailfile->error) {
- $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
- } else {
- $result4 = $mailfile->sendfile();
- if (!$error) {
- $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
- $societe->array_options['options_datapolicy_send'] = date('Y-m-d', time());
- $societe->update($societe->id);
- } else {
- dol_print_error($db);
- }
- }
- setEventMessage($resultmasssend);
- }
- /**
- * sendMailDataPolicyAdherent
- *
- * @param Adherent $adherent Member
- * @return void
- */
- public static function sendMailDataPolicyAdherent($adherent)
- {
- global $langs, $conf, $db, $user;
- $error = 0;
- $from = $user->getFullName($langs).' <'.$user->email.'>';
- $sendto = $adherent->email;
- $code = dol_hash($adherent->email, 'md5');
- if (!empty($adherent->default_lang)) {
- $l = $adherent->default_lang;
- } else {
- $l = $langs->defaultlang;
- }
- // TODO Use a dolibarr email template
- $s = 'TXTLINKDATAPOLICYSUBJECT_'.$l;
- $ma = 'TXTLINKDATAPOLICYMESSAGE_'.$l;
- $la = 'TXTLINKDATAPOLICYACCEPT_'.$l;
- $lr = 'TXTLINKDATAPOLICYREFUSE_'.$l;
- $subject = $conf->global->$s;
- $message = $conf->global->$ma;
- $linka = $conf->global->$la;
- $linkr = $conf->global->$lr;
- $sendtocc = $sendtobcc = '';
- $filepath = $mimetype = $filename = array();
- $deliveryreceipt = 0;
- $substitutionarray = array(
- '__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>',
- '__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>',
- );
- $subject = make_substitutions($subject, $substitutionarray);
- $message = make_substitutions($message, $substitutionarray);
- $actiontypecode = 'AC_EMAIL';
- $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
- if ($message) {
- if ($sendtocc) {
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
- }
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subject);
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
- $actionmsg = dol_concatdesc($actionmsg, $message);
- }
- // Send mail
- require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
- $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1);
- if ($mailfile->error) {
- $resultmasssend .= '<div class="error">'.$mailfile->error.'</div>';
- } else {
- $result4 = $mailfile->sendfile();
- if (!$error) {
- $resultmasssend .= $langs->trans("MailSent").': '.$sendto."<br>";
- $adherent->array_options['options_datapolicy_send'] = date('Y-m-d', time());
- $adherent->update($user);
- } else {
- dol_print_error($db);
- }
- }
- setEventMessage($resultmasssend);
- }
- }
|