* Copyright (C) 2005-2021 Laurent Destailleur * Copyright (C) 2005-2010 Regis Houssin * Copyright (C) 2014 Florian Henry * * 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/cibles.php * \ingroup mailing * \brief Page to define emailing targets */ // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php'; require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmailing.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/emailing.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; // Load translation files required by the page $langs->load("mails"); // Load variable for pagination $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit; $sortfield = GETPOST('sortfield', 'aZ09comma'); $sortorder = GETPOST('sortorder', 'aZ09comma'); $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int'); 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 (!$sortfield) { $sortfield = "mc.statut,email"; } if (!$sortorder) { $sortorder = "DESC,ASC"; } $id = GETPOST('id', 'int'); $rowid = GETPOST('rowid', 'int'); $action = GETPOST('action', 'aZ09'); $search_lastname = GETPOST("search_lastname", 'alphanohtml'); $search_firstname = GETPOST("search_firstname", 'alphanohtml'); $search_email = GETPOST("search_email", 'alphanohtml'); $search_other = GETPOST("search_other", 'alphanohtml'); $search_dest_status = GETPOST('search_dest_status', 'int'); // Search modules dirs $modulesdir = dolGetModulesDirs('/mailings'); $object = new Mailing($db); $result = $object->fetch($id); // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context $hookmanager->initHooks(array('ciblescard', 'globalcard')); // Security check if (!$user->rights->mailing->lire || (empty($conf->global->EXTERNAL_USERS_ARE_AUTHORIZED) && $user->socid > 0)) { accessforbidden(); } //$result = restrictedArea($user, 'mailing'); /* * Actions */ if ($action == 'add') { $module = GETPOST("module", 'alpha'); $result = -1; foreach ($modulesdir as $dir) { // Load modules attributes in arrays (name, numero, orders) from dir directory //print $dir."\n
"; dol_syslog("Scan directory ".$dir." for modules"); // Loading Class $file = $dir."/".$module.".modules.php"; $classname = "mailing_".$module; if (file_exists($file)) { require_once $file; // Add targets into database $obj = new $classname($db); dol_syslog("Call add_to_target on class ".$classname); $result = $obj->add_to_target($id); } } if ($result > 0) { setEventMessages($langs->trans("XTargetsAdded", $result), null, 'mesgs'); $action = ''; } if ($result == 0) { setEventMessages($langs->trans("WarningNoEMailsAdded"), null, 'warnings'); } if ($result < 0) { setEventMessages($langs->trans("Error").($obj->error ? ' '.$obj->error : ''), null, 'errors'); } } if (GETPOST('clearlist', 'int')) { // Loading Class $obj = new MailingTargets($db); $obj->clear_target($id); /* Avoid this to allow reposition header("Location: ".$_SERVER['PHP_SELF']."?id=".$id); exit; */ } if (GETPOST('exportcsv', 'int')) { $completefilename = 'targets_emailing'.$object->id.'_'.dol_print_date(dol_now(), 'dayhourlog').'.csv'; header('Content-Type: text/csv'); header('Content-Disposition: attachment;filename='.$completefilename); // List of selected targets $sql = "SELECT mc.rowid, mc.lastname, mc.firstname, mc.email, mc.other, mc.statut as status, mc.date_envoi, mc.tms,"; $sql .= " mc.source_id, mc.source_type, mc.error_text"; $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc"; $sql .= " WHERE mc.fk_mailing = ".((int) $object->id); $sql .= $db->order($sortfield, $sortorder); $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); $sep = ','; while ($obj = $db->fetch_object($resql)) { print $obj->rowid.$sep; print '"'.$obj->lastname.'"'.$sep; print '"'.$obj->firstname.'"'.$sep; print $obj->email.$sep; print $obj->other.$sep; print $obj->tms.$sep; print $obj->source_type.$sep; print $obj->source_id.$sep; print $obj->date_envoi.$sep; print $obj->status.$sep; print '"'.$obj->error_text.'"'.$sep; print "\n"; } exit; } else { dol_print_error($db); } exit; } if ($action == 'delete') { // 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)) { $obj = new MailingTargets($db); $obj->update_nb($id); setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs'); } else { header("Location: list.php"); exit; } } else { dol_print_error($db); } } // Purge search criteria if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers $search_lastname = ''; $search_firstname = ''; $search_email = ''; $search_other = ''; $search_dest_status = ''; } /* * View */ llxHeader('', $langs->trans("Mailing"), 'EN:Module_EMailing|FR:Module_Mailing|ES:Módulo_Mailing'); $form = new Form($db); $formmailing = new FormMailing($db); if ($object->fetch($id) >= 0) { $head = emailing_prepare_head($object); print dol_get_fiche_head($head, 'targets', $langs->trans("Mailing"), -1, 'email'); $linkback = ''.$langs->trans("BackToList").''; $morehtmlright = ''; $nbtry = $nbok = 0; if ($object->statut == 2 || $object->statut == 3) { $nbtry = $object->countNbOfTargets('alreadysent'); $nbko = $object->countNbOfTargets('alreadysentko'); $nbok = ($nbtry - $nbko); $morehtmlright .= ' ('.$nbtry.'/'.$object->nbemail; if ($nbko) { $morehtmlright .= ' - '.$nbko.' '.$langs->trans("Error"); } $morehtmlright .= ')   '; } dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', '', '', 0, '', $morehtmlright); print '
'; print '
'; print ''; print ''; print ''; // Errors to print ''; // Nb of distinct emails print ''; print '
'.$langs->trans("MailTitle").''.$object->title.'
'.$langs->trans("MailFrom").''; $emailarray = CMailFile::getArrayAddress($object->email_from); foreach ($emailarray as $email => $name) { if ($name && $name != $email) { print dol_escape_htmltag($name).' <'.$email; print '>'; if (!isValidEmail($email)) { $langs->load("errors"); print img_warning($langs->trans("ErrorBadEMail", $email)); } } else { print dol_print_email($object->email_from, 0, 0, 0, 0, 1); } } //print dol_print_email($object->email_from, 0, 0, 0, 0, 1); //var_dump($object->email_from); print '
'.$langs->trans("MailErrorsTo").''; $emailarray = CMailFile::getArrayAddress($object->email_errorsto); foreach ($emailarray as $email => $name) { if ($name != $email) { print dol_escape_htmltag($name).' <'.$email; print '>'; if (!isValidEmail($email)) { $langs->load("errors"); print img_warning($langs->trans("ErrorBadEMail", $email)); } } else { print dol_print_email($object->email_errorsto, 0, 0, 0, 0, 1); } } print '
'; print $langs->trans("TotalNbOfDistinctRecipients"); print ''; $nbemail = ($object->nbemail ? $object->nbemail : 0); if (is_numeric($nbemail)) { $text = ''; if ((!empty($conf->global->MAILING_LIMIT_SENDBYWEB) && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail) && ($object->statut == 1 || ($object->statut == 2 && $nbtry < $nbemail))) { if ($conf->global->MAILING_LIMIT_SENDBYWEB > 0) { $text .= $langs->trans('LimitSendingEmailing', $conf->global->MAILING_LIMIT_SENDBYWEB); } else { $text .= $langs->trans('SendingFromWebInterfaceIsNotAllowed'); } } if (empty($nbemail)) { $nbemail .= ' '.img_warning('').' '.$langs->trans("NoTargetYet").''; } if ($text) { print $form->textwithpicto($nbemail, $text, 1, 'warning'); } else { print $nbemail; } } print '
'; print "
"; print dol_get_fiche_end(); print '
'; $allowaddtarget = ($object->statut == 0); // Show email selectors if ($allowaddtarget && $user->rights->mailing->creer) { print load_fiche_titre($langs->trans("ToAddRecipientsChooseHere"), ($user->admin ?info_admin($langs->trans("YouCanAddYourOwnPredefindedListHere"), 1) : ''), 'generic'); print '
'; print '
'; print '
'; print '
'; print '
'.$langs->trans("RecipientSelectionModules").'
'; print '
'.$langs->trans("NbOfUniqueEMails").'
'; print '
'.$langs->trans("Filter").'
'; print '
 
'; print '
'; // End tr clearstatcache(); foreach ($modulesdir as $dir) { $modulenames = array(); // Load modules attributes in arrays (name, numero, orders) from dir directory //print $dir."\n
"; dol_syslog("Scan directory ".$dir." for modules"); $handle = @opendir($dir); if (is_resource($handle)) { while (($file = readdir($handle)) !== false) { if (substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS') { $reg = array(); if (preg_match("/(.*)\.modules\.php$/i", $file, $reg)) { if ($reg[1] == 'example') { continue; } $modulenames[] = $reg[1]; } } } closedir($handle); } // Sort $modulenames sort($modulenames); $var = true; // Loop on each submodule foreach ($modulenames as $modulename) { // Loading Class $file = $dir.$modulename.".modules.php"; $classname = "mailing_".$modulename; require_once $file; $obj = new $classname($db); // Check if qualified $qualified = (is_null($obj->enabled) ? 1 : dol_eval($obj->enabled, 1)); // Check dependencies foreach ($obj->require_module as $key) { if (empty($conf->$key->enabled) || (empty($user->admin) && $obj->require_admin)) { $qualified = 0; //print "Les prerequis d'activation du module mailing ne sont pas respectes. Il ne sera pas actif"; break; } } // If module is qualified if ($qualified) { $var = !$var; if ($allowaddtarget) { print '
'; print ''; print ''; } else { print '
'; } print '
'; if (empty($obj->picto)) { $obj->picto = 'generic'; } print img_object($langs->trans("EmailingTargetSelector").': '.get_class($obj), $obj->picto, 'class="valignmiddle width25 size15x"'); print '
'; print '
'; // style="height: 4em" print $obj->getDesc(); print '
'; try { $nbofrecipient = $obj->getNbOfRecipients(''); } catch (Exception $e) { dol_syslog($e->getMessage(), LOG_ERR); } print '
'; if ($nbofrecipient === '' || $nbofrecipient >= 0) { print $nbofrecipient; } else { print $langs->trans("Error").' '.img_error($obj->error); } print '
'; print '
'; if ($allowaddtarget) { try { $filter = $obj->formFilter(); } catch (Exception $e) { dol_syslog($e->getMessage(), LOG_ERR); } if ($filter) { print $filter; } else { print $langs->trans("None"); } } print '
'; print '
'; if ($allowaddtarget) { print ''; } else { print ''; //print $langs->trans("MailNoChangePossible"); print " "; } print '
'; if ($allowaddtarget) { print ''; } else { print '
'; } } } } // End foreach dir $parameters = array(); $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook print $hookmanager->resPrint; print '
'; // End table print '
'; print '

'; } // List of selected targets $sql = "SELECT mc.rowid, mc.lastname, mc.firstname, mc.email, mc.other, mc.statut, mc.date_envoi, mc.tms,"; $sql .= " mc.source_url, mc.source_id, mc.source_type, mc.error_text"; $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc"; $sql .= " WHERE mc.fk_mailing=".((int) $object->id); $asearchcriteriahasbeenset = 0; if ($search_lastname) { $sql .= natural_search("mc.lastname", $search_lastname); $asearchcriteriahasbeenset++; } if ($search_firstname) { $sql .= natural_search("mc.firstname", $search_firstname); $asearchcriteriahasbeenset++; } if ($search_email) { $sql .= natural_search("mc.email", $search_email); $asearchcriteriahasbeenset++; } if ($search_other) { $sql .= natural_search("mc.other", $search_other); $asearchcriteriahasbeenset++; } if ($search_dest_status != '' && $search_dest_status >= -1) { $sql .= " AND mc.statut = ".((int) $search_dest_status); $asearchcriteriahasbeenset++; } $sql .= $db->order($sortfield, $sortorder); // Count total nb of records $nbtotalofrecords = ''; if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) { $result = $db->query($sql); $nbtotalofrecords = $db->num_rows($result); if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0 $page = 0; $offset = 0; } // Fix/update nbemail on emailing record if it differs (may happen if user edit lines from database directly) if (empty($asearchcriteriahasbeenset)) { if ($nbtotalofrecords != $object->nbemail) { dol_syslog("We found a difference in nb of record in target table and the property ->nbemail, we fix ->nbemail"); //print "nbemail=".$object->nbemail." nbtotalofrecords=".$nbtotalofrecords; $resultrefresh = $object->refreshNbOfTargets(); if ($resultrefresh < 0) { dol_print_error($db, $object->error, $object->errors); } } } } //$nbtotalofrecords=$object->nbemail; // nbemail is a denormalized field storing nb of targets $sql .= $db->plimit($limit + 1, $offset); $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); $param = "&id=".$object->id; //if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.urlencode($contextpage); if ($limit > 0 && $limit != $conf->liste_limit) { $param .= '&limit='.urlencode($limit); } if ($search_lastname) { $param .= "&search_lastname=".urlencode($search_lastname); } if ($search_firstname) { $param .= "&search_firstname=".urlencode($search_firstname); } if ($search_email) { $param .= "&search_email=".urlencode($search_email); } if ($search_other) { $param .= "&search_other=".urlencode($search_other); } print '
'; print ''; print ''; print ''; print ''; print ''; $morehtmlcenter = ''; if ($allowaddtarget) { $morehtmlcenter = ''.$langs->trans("ToClearAllRecipientsClickHere").' id.'" class="button reposition smallpaddingimp">'.$langs->trans("TargetsReset").''; } $morehtmlcenter .= '   id.'">'.img_picto('', 'download', 'class="pictofixedwidth"').$langs->trans("Download").''; $massactionbutton = ''; print_barre_liste($langs->trans("MailSelectedRecipients"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $morehtmlcenter, $num, $nbtotalofrecords, 'generic', 0, '', '', $limit, 0, 0, 1); print '
'; print "\n\n"; print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '
'; print ''; // Ligne des champs de filtres print ''; // EMail print ''; // Name print ''; // Firstname print ''; // Other print ''; // Source print ''; // Date last update print ''; // Date sending print ''; //Statut print ''; // Action column print ''; print ''; if ($page) { $param .= "&page=".urlencode($page); } print ''; print_liste_field_titre("EMail", $_SERVER["PHP_SELF"], "mc.email", $param, "", "", $sortfield, $sortorder); print_liste_field_titre("Lastname", $_SERVER["PHP_SELF"], "mc.lastname", $param, "", "", $sortfield, $sortorder); print_liste_field_titre("Firstname", $_SERVER["PHP_SELF"], "mc.firstname", $param, "", "", $sortfield, $sortorder); print_liste_field_titre("OtherInformations", $_SERVER["PHP_SELF"], "", $param, "", "", $sortfield, $sortorder); print_liste_field_titre("Source", $_SERVER["PHP_SELF"], "", $param, "", 'align="center"', $sortfield, $sortorder); // Date last update print_liste_field_titre("DateLastModification", $_SERVER["PHP_SELF"], "mc.tms", $param, "", 'align="center"', $sortfield, $sortorder); // Date sending print_liste_field_titre("DateSending", $_SERVER["PHP_SELF"], "mc.date_envoi", $param, '', 'align="center"', $sortfield, $sortorder); print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "mc.statut", $param, '', 'class="right"', $sortfield, $sortorder); print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'maxwidthsearch '); print ''; $i = 0; if ($num) { include_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php'; include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php'; include_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorboothattendee.class.php'; $objectstaticmember = new Adherent($db); $objectstaticuser = new User($db); $objectstaticcompany = new Societe($db); $objectstaticcontact = new Contact($db); $objectstaticeventorganization = new ConferenceOrBoothAttendee($db); while ($i < min($num, $limit)) { $obj = $db->fetch_object($resql); print ''; print ''; print ''; print ''; print ''; print ''; // Date last update print ''; // Status of recipient sending email (Warning != status of emailing) if ($obj->statut == 0) { // Date sent print ''; print ''; } else { // Date sent print ''; print ''; } // Search Icon print ''; print ''; $i++; } } else { if ($object->statut < 2) { print ''; } } print "
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ' '; print ''; print ' '; print ''; print ' '; print ''; print $formmailing->selectDestinariesStatus($search_dest_status, 'search_dest_status', 1); print ''; $searchpicto = $form->showFilterAndCheckAddButtons($massactionbutton ? 1 : 0, 'checkforselect', 1); print $searchpicto; print '
'.img_picto('$obj->email', 'email', 'class="paddingright"').dol_escape_htmltag($obj->email).''.dol_escape_htmltag($obj->lastname).''.dol_escape_htmltag($obj->firstname).''.dol_escape_htmltag($obj->other).''; if (empty($obj->source_id) || empty($obj->source_type)) { print empty($obj->source_url) ? '' : $obj->source_url; // For backward compatibility } else { if ($obj->source_type == 'member') { $objectstaticmember->fetch($obj->source_id); print $objectstaticmember->getNomUrl(1); } elseif ($obj->source_type == 'user') { $objectstaticuser->fetch($obj->source_id); print $objectstaticuser->getNomUrl(1); } elseif ($obj->source_type == 'thirdparty') { $objectstaticcompany->fetch($obj->source_id); print $objectstaticcompany->getNomUrl(1); } elseif ($obj->source_type == 'contact') { $objectstaticcontact->fetch($obj->source_id); print $objectstaticcontact->getNomUrl(1); } elseif ($obj->source_type == 'eventorganizationattendee') { $objectstaticeventorganization->fetch($obj->source_id); print $objectstaticeventorganization->getNomUrl(1); } else { print $obj->source_url; } } print ''; print dol_print_date($obj->tms, 'dayhour'); print ''; print $object::libStatutDest($obj->statut, 2, ''); print ''.$obj->date_envoi.''; print $object::libStatutDest($obj->statut, 2, $obj->error_text); print ''; print ''; if ($obj->statut == 0) { // Not sent yet if (!empty($user->rights->mailing->creer) && $allowaddtarget) { print ''.img_delete($langs->trans("RemoveRecipient")).''; } } /*if ($obj->statut == -1) // Sent with error { print ''.$langs->trans("Retry").''; }*/ print '
'; print $langs->trans("NoTargetYet"); print '

"; print '
'; print '
'; $db->free($resql); } else { dol_print_error($db); } print "\n\n"; } // End of page llxFooter(); $db->close();