* Copyright (C) 2016 Laurent Destailleur * Copyright (C) 2024 MDW * * 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 . */ /** * \file htdocs/comm/mailing/advtargetemailing.php * \ingroup mailing * \brief Page to define emailing targets. Visible when option EMAILING_USE_ADVANCED_SELECTOR is on (need MAIN_FEATURES_LEVEL to 1 to see this option) * @TODO This page needs a lot of works to be stable and understandable. */ if (!defined('NOSTYLECHECK')) { define('NOSTYLECHECK', '1'); } // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/emailing.lib.php'; require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/advtargetemailing.class.php'; require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/html.formadvtargetemailing.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/advthirdparties.modules.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; // Load translation files required by the page $langs->loadLangs(array('mails', 'companies')); if (isModEnabled('category')) { $langs->load("categories"); } // Load variable for pagination $limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit; $sortfield = GETPOST('sortfield', 'aZ09comma'); $sortorder = GETPOST('sortorder', 'aZ09comma'); $page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page"); if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1 $offset = $limit * $page; $pageprev = $page - 1; $pagenext = $page + 1; if (!$sortorder) { $sortorder = "ASC"; } if (!$sortfield) { $sortfield = "email"; } $id = GETPOSTINT('id'); $rowid = GETPOSTINT('rowid'); $action = GETPOST('action', 'aZ09'); $search_nom = GETPOST("search_nom"); $search_prenom = GETPOST("search_prenom"); $search_email = GETPOST("search_email"); $template_id = GETPOSTINT('template_id'); // Do we click on purge search criteria ? if (GETPOST('button_removefilter_x', 'alpha')) { $search_nom = ''; $search_prenom = ''; $search_email = ''; } $array_query = array(); $object = new Mailing($db); $advTarget = new AdvanceTargetingMailing($db); if (empty($template_id)) { $advTarget->fk_element = $id; $advTarget->type_element = 'mailing'; $result = $advTarget->fetch_by_mailing(); } else { $result = $advTarget->fetch($template_id); } if ($result < 0) { setEventMessages($advTarget->error, $advTarget->errors, 'errors'); } else { if (!empty($advTarget->id)) { $array_query = json_decode($advTarget->filtervalue, true); } } // Security check if (!$user->hasRight('mailing', 'lire') || (!getDolGlobalString('EXTERNAL_USERS_ARE_AUTHORIZED') && $user->socid > 0)) { accessforbidden(); } //$result = restrictedArea($user, 'mailing'); $permissiontoread = $user->hasRight('mailing', 'lire'); $permissiontoadd = $user->hasRight('mailing', 'creer'); /* * Actions */ if ($action == 'loadfilter' && $permissiontoread) { if (!empty($template_id)) { $result = $advTarget->fetch($template_id); if ($result < 0) { setEventMessages($advTarget->error, $advTarget->errors, 'errors'); } else { if (!empty($advTarget->id)) { $array_query = json_decode($advTarget->filtervalue, true); } } } } if ($action == 'add' && $permissiontoadd) { $user_contact_query = false; $array_query = array(); // Get extra fields foreach ($_POST as $key => $value) { // print '$key='.$key.' $value='.$value.'
'; if (preg_match("/^options_.*(?query_thirdparty($array_query); if ($result < 0) { setEventMessages($advTarget->error, $advTarget->errors, 'errors'); } /*} else { $advTarget->thirdparty_lines = array (); }*/ if ($user_contact_query && ($array_query['type_of_target'] == 1 || $array_query['type_of_target'] == 2 || $array_query['type_of_target'] == 4)) { $result = $advTarget->query_contact($array_query, 1); if ($result < 0) { setEventMessages($advTarget->error, $advTarget->errors, 'errors'); } // If use contact but no result use artefact to so not use socid into add_to_target if (count($advTarget->contact_lines) == 0) { $advTarget->contact_lines = array( 0 ); } } else { $advTarget->contact_lines = array(); } $mailingadvthirdparties = null; if ((count($advTarget->thirdparty_lines) > 0) || (count($advTarget->contact_lines) > 0)) { // Add targets into database $mailingadvthirdparties = new mailing_advthirdparties($db); $result = $mailingadvthirdparties->add_to_target_spec($id, $advTarget->thirdparty_lines, $array_query['type_of_target'], $advTarget->contact_lines); } else { $result = 0; } if ($result > 0) { $query_temlate_id = ''; if (!empty($template_id)) { $query_temlate_id = '&template_id='.$template_id; } setEventMessages($langs->trans("XTargetsAdded", $result), null, 'mesgs'); header("Location: ".$_SERVER['PHP_SELF']."?id=".$id.$query_temlate_id); exit(); } if ($result == 0) { setEventMessages($langs->trans("WarningNoEMailsAdded"), null, 'warnings'); } if ($result < 0 && is_object($mailingadvthirdparties)) { setEventMessages($mailingadvthirdparties->error, $mailingadvthirdparties->errors, 'errors'); } } if ($action == 'clear' && $permissiontoadd) { $mailingtargets = new MailingTargets($db); $mailingtargets->clear_target($id); header("Location: ".$_SERVER['PHP_SELF']."?id=".$id); exit(); } if (($action == 'savefilter' || $action == 'createfilter') && $permissiontoadd) { $template_name = GETPOST('template_name'); $error = 0; if ($action == 'createfilter' && empty($template_name) && $permissiontoadd) { setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('AdvTgtOrCreateNewFilter')), null, 'errors'); $error++; } if (empty($error)) { $array_query = array(); // Get extra fields foreach ($_POST as $key => $value) { if (preg_match("/^options_.*(?filtervalue = json_encode($array_query); if ($action == 'createfilter') { // Test on permission already done $advTarget->name = $template_name; $result = $advTarget->create($user); if ($result < 0) { setEventMessages($advTarget->error, $advTarget->errors, 'errors'); } } elseif ($action == 'savefilter') { // Test on permission already done $result = $advTarget->update($user); if ($result < 0) { setEventMessages($advTarget->error, $advTarget->errors, 'errors'); } } $template_id = $advTarget->id; } } if ($action == 'deletefilter' && $permissiontoadd) { $result = $advTarget->delete($user); if ($result < 0) { setEventMessages($advTarget->error, $advTarget->errors, 'errors'); } header("Location: ".$_SERVER['PHP_SELF']."?id=".$id); exit(); } if ($action == 'delete' && $permissiontoadd) { // Ici, rowid indique le destinataire et id le mailing $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE rowid = ".((int) $rowid); $resql = $db->query($sql); if ($resql) { if (!empty($id)) { $mailingtargets = new MailingTargets($db); $mailingtargets->update_nb($id); header("Location: ".$_SERVER['PHP_SELF']."?id=".$id); exit(); } else { header("Location: liste.php"); exit(); } } else { dol_print_error($db); } } if (GETPOST("button_removefilter")) { $search_nom = ''; $search_prenom = ''; $search_email = ''; } /* * View */ llxHeader('', $langs->trans("MailAdvTargetRecipients")); $form = new Form($db); $formadvtargetemaling = new FormAdvTargetEmailing($db); $formcompany = new FormCompany($db); $formother = new FormOther($db); if ($object->fetch($id) >= 0) { $head = emailing_prepare_head($object); print dol_get_fiche_head($head, 'advtargets', $langs->trans("Mailing"), 0, 'email'); print ''; $linkback = ''.$langs->trans("BackToList").''; print ''; print ''; print ''; print ''; // Errors to print ''; // Status print ''; // Nb of distinct emails print ''; print '
'.$langs->trans("Ref").''; print $form->showrefnav($object, 'id', $linkback); print '
'.$langs->trans("MailTitle").''.$object->title.'
'.$langs->trans("MailFrom").''.dol_print_email($object->email_from, 0, 0, 0, 0, 1).'
'.$langs->trans("MailErrorsTo").''.dol_print_email($object->email_errorsto, 0, 0, 0, 0, 1); print '
'.$langs->trans("Status").''.$object->getLibStatut(4).'
'; print $langs->trans("TotalNbOfDistinctRecipients"); print ''; $nbemail = ($object->nbemail ? $object->nbemail : '0'); if (getDolGlobalString('MAILING_LIMIT_SENDBYWEB') && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail) { $text = $langs->trans('LimitSendingEmailing', getDolGlobalString('MAILING_LIMIT_SENDBYWEB')); // @phan-suppress-next-line PhanPluginSuspiciousParamPosition print $form->textwithpicto($nbemail, $text, 1, 'warning'); } else { print $nbemail; } print '
'; print ""; // Show email selectors if ($object->status == 0 && $user->hasRight('mailing', 'creer')) { include DOL_DOCUMENT_ROOT.'/core/tpl/advtarget.tpl.php'; } } // End of page llxFooter(); $db->close();