From 304aab957b53f5975aa3c95d309b2fbab5eacb3a Mon Sep 17 00:00:00 2001 From: Florian HENRY Date: Fri, 1 Apr 2016 10:52:19 +0200 Subject: [PATCH 01/11] Start implement advance arget emalaing --- htdocs/comm/mailing/advtargetemailing.php | 1126 +++++++++++++++++ .../mailing/class/advtargetemailing.class.php | 780 ++++++++++++ .../html.formadvtargetemailing.class.php | 495 ++++++++ htdocs/core/lib/emailing.lib.php | 9 +- .../mailings/advthirdparties.modules.php | 303 +++++ htdocs/langs/en_US/mails.lang | 1 + 6 files changed, 2712 insertions(+), 2 deletions(-) create mode 100644 htdocs/comm/mailing/advtargetemailing.php create mode 100644 htdocs/comm/mailing/class/advtargetemailing.class.php create mode 100644 htdocs/comm/mailing/class/html.formadvtargetemailing.class.php create mode 100755 htdocs/core/modules/mailings/advthirdparties.modules.php diff --git a/htdocs/comm/mailing/advtargetemailing.php b/htdocs/comm/mailing/advtargetemailing.php new file mode 100644 index 00000000000..ffb6dd68a48 --- /dev/null +++ b/htdocs/comm/mailing/advtargetemailing.php @@ -0,0 +1,1126 @@ + +* +* +* 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 . +*/ +$res = @include ("../../main.inc.php"); // For root directory +if (! $res) + $res = @include ("../../../main.inc.php"); // For "custom" directory +if (! $res) + die("Include of main fails"); + +require_once DOL_DOCUMENT_ROOT . '/comm/mailing/class/mailing.class.php'; +require_once DOL_DOCUMENT_ROOT . '/core/lib/emailing.lib.php'; +dol_include_once('/advtargetemailing/class/advtargetemailing.class.php'); +dol_include_once('/advtargetemailing/class/html.formadvtargetemailing.class.php'); +dol_include_once('/advtargetemailing/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'; + +// Translations +$langs->load("mails"); +$langs->load("advtargetemailing@advtargetemailing"); +$langs->load("companies"); +if (! empty($conf->categorie->enabled)) { + $langs->load("categories"); +} + +// Security check +if (! $user->rights->mailing->lire || $user->societe_id > 0) + accessforbidden(); + +$sortfield = GETPOST("sortfield", 'alpha'); +$sortorder = GETPOST("sortorder", 'alpha'); +$page = GETPOST("page", 'int'); +if ($page == - 1) { + $page = 0; +} +$offset = $conf->liste_limit * $page; +$pageprev = $page - 1; +$pagenext = $page + 1; +if (! $sortorder) + $sortorder = "ASC"; +if (! $sortfield) + $sortfield = "email"; + +$id = GETPOST('id', 'int'); +$rowid = GETPOST('rowid', 'int'); +$action = GETPOST("action"); +$search_nom = GETPOST("search_nom"); +$search_prenom = GETPOST("search_prenom"); +$search_email = GETPOST("search_email"); +$template_id = GETPOST('template_id', 'int'); + +// Do we click on purge search criteria ? +if (GETPOST("button_removefilter_x")) { + $search_nom = ''; + $search_prenom = ''; + $search_email = ''; +} + +$array_query = array (); + +$object = new Mailing($db); +$advTarget = new AdvanceTargetingMailing($db); +if (empty($template_id)) { + $advTarget->fk_mailing = $id; + $result = $advTarget->fetch_by_mailing(); +} else { + $result = $advTarget->fetch($template_id); +} + +if ($result < 0) { + setEventMessage($advTarget->error, 'errors'); +} else { + if (! empty($advTarget->id)) { + $array_query = json_decode($advTarget->filtervalue, true); + } +} + +/* + * Action + */ + +if ($action == 'loadfilter') { + if (! empty($template_id)) { + $result = $advTarget->fetch($template_id); + if ($result < 0) { + setEventMessage($advTarget->error, 'errors'); + } else { + if (! empty($advTarget->id)) { + $array_query = json_decode($advTarget->filtervalue, true); + } + } + } +} + +if ($action == 'add') { + + $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) { + setEventMessage($advTarget->error, 'errors'); + } + /*} else { + $advTarget->thirdparty_lines = array (); + }*/ + + if ($user_contact_query && ($array_query['type_of_target'] == 1 || $array_query['type_of_target'] == 2)) { + $result = $advTarget->query_contact($array_query); + if ($result < 0) { + setEventMessage($advTarget->error, '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 (); + } + + if ((count($advTarget->thirdparty_lines) > 0) || (count($advTarget->contact_lines) > 0)) { + // Add targets into database + $obj = new mailing_advthirdparties($db); + $result = $obj->add_to_target($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; + } + header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $id . $query_temlate_id); + exit(); + } + if ($result == 0) { + setEventMessage($langs->trans("WarningNoEMailsAdded"), 'warnings'); + } + if ($result < 0) { + setEventMessage($obj->error, 'errors'); + } +} + +if ($action == 'clear') { + // Chargement de la classe + $classname = "MailingTargets"; + $obj = new $classname($db); + $obj->clear_target($id); + + header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $id); + exit(); +} + +if ($action == 'savefilter' || $action == 'createfilter') { + + $template_name = GETPOST('template_name'); + $error = 0; + + if ($action == 'createfilter' && empty($template_name)) { + setEventMessage($langs->trans('ErrorFieldRequired', $langs->trans('AdvTgtOrCreateNewFilter')), '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') { + $advTarget->name = $template_name; + $result = $advTarget->create($user); + if ($result < 0) { + setEventMessage($advTarget->error, 'errors'); + } + } elseif ($action == 'savefilter') { + $result = $advTarget->update($user); + if ($result < 0) { + setEventMessage($advTarget->error, 'errors'); + } + } + $template_id = $advTarget->id; + } +} + +if ($action == 'deletefilter') { + $result = $advTarget->delete($user); + if ($result < 0) { + setEventMessage($advTarget->error, 'errors'); + } + header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $id); + exit(); +} + +if ($action == 'delete') { + // Ici, rowid indique le destinataire et id le mailing + $sql = "DELETE FROM " . MAIN_DB_PREFIX . "mailing_cibles WHERE rowid=" . $rowid; + $resql = $db->query($sql); + if ($resql) { + if (! empty($id)) { + $classname = "MailingTargets"; + $obj = new $classname($db); + $obj->update_nb($id); + + header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $id); + exit(); + } else { + header("Location: liste.php"); + exit(); + } + } else { + dol_print_error($db); + } +} + +if ($_POST["button_removefilter"]) { + $search_nom = ''; + $search_prenom = ''; + $search_email = ''; +} + +/* + * View + */ +$extrajs = array ( + '/advtargetemailing/includes/multiselect/js/ui.multiselect.js' +); +$extracss = array ( + '/advtargetemailing/includes/multiselect/css/ui.multiselect.css', + '/advtargetemailing/css/advtargetemailing.css' +); + +llxHeader('', $langs->trans("AdvTgtTabsTarget"), '', '', '', '', $extrajs, $extracss); + +print ''; + +$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); + + dol_fiche_head($head, 'tabAdvTgtTabsTarget', $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->titre . '
' . $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 (! empty($conf->global->MAILING_LIMIT_SENDBYWEB) && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail) { + $text = $langs->trans('LimitSendingEmailing', $conf->global->MAILING_LIMIT_SENDBYWEB); + print $form->textwithpicto($nbemail, $text, 1, 'warning'); + } else { + print $nbemail; + } + print '
'; + + print ""; + + // Show email selectors + if ($object->statut == 0 && $user->rights->mailing->creer) { + print_fiche_titre($langs->trans("AdvTgtTitle")); + + print '
' . "\n"; + print '
' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + + print '' . "\n"; + + print '' . "\n"; + + // Customer name + print '' . "\n"; + + // Code Client + print '' . "\n"; + + // Address Client + print '' . "\n"; + + // Zip Client + print '' . "\n"; + + // City Client + print '' . "\n"; + + // Customer Country + print '' . "\n"; + + // State Customer + print '' . "\n"; + + // Mother Company + print '' . "\n"; + + // Prospect/Customer + $selected = $array_query['cust_typecust']; + print '' . "\n"; + + // Prospection status + print '' . "\n"; + + // Prospection comm status + print '' . "\n"; + + // Customer Type + print '' . "\n"; + + // Staff number + print '' . "\n"; + + // Sales manager + print '' . "\n"; + + // Customer Default Langauge + if (! empty($conf->global->MAIN_MULTILANGS)) { + + print '' . "\n"; + } + + if (! empty($conf->categorie->enabled) && $user->rights->categorie->lire) { + // Customer Categories + print '' . "\n"; + } + + // Standard Extrafield feature + if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) { + // fetch optionals attributes and labels + dol_include_once('/core/class/extrafields.class.php'); + $extrafields = new ExtraFields($db); + $extralabels = $extrafields->fetch_name_optionals_label('societe'); + foreach ( $extralabels as $key => $val ) { + if ($key != 'ts_nameextra' && $key != 'ts_payeur') { + print '' . "\n"; + } + } + } else { + $std_soc = new Societe($db); + $action_search = 'query'; + // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array + include_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php'; + $hookmanager = new HookManager($db); + $hookmanager->initHooks(array ( + 'thirdpartycard' + )); + if (! empty($advTarget->id)) { + $parameters = array ( + 'array_query' => $advTarget->filtervalue + ); + } + // Module extrafield feature + $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $std_soc, $action_search); + } + + // State Contact + print '' . "\n"; + + // Civility + print ''; + + // contact name + print '' . "\n"; + print '' . "\n"; + + // Contact Country + print '' . "\n"; + + // Never send mass mailing + print '' . "\n"; + + // Contact Date Create + print '' . "\n"; + + // Contact update Create + print '' . "\n"; + + if (! empty($conf->categorie->enabled) && $user->rights->categorie->lire) { + // Customer Categories + print '' . "\n"; + } + + // Standard Extrafield feature + if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) { + // fetch optionals attributes and labels + dol_include_once('/core/class/extrafields.class.php'); + $extrafields = new ExtraFields($db); + $extralabels = $extrafields->fetch_name_optionals_label('socpeople'); + foreach ( $extralabels as $key => $val ) { + + print '' . "\n"; + } + } + + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '
' . "\n"; + + print '' . "\n"; + + print '
' . $langs->trans('AdvTgtNameTemplate') . ''; + if (! empty($template_id)) { + $default_template = $template_id; + } else { + $default_template = $advTarget->id; + } + print $formadvtargetemaling->select_advtargetemailing_template('template_id', $default_template); + print ''; + print ''; + print ''; + print $langs->trans('AdvTgtOrCreateNewFilter'); + print ''; + print ''; + print '' . "\n"; + print '
' . $langs->trans('AdvTgtTypeOfIncude') . ''; + print $form->selectarray('type_of_target', $advTarget->select_target_type, $array_query['type_of_target']); + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtTypeOfIncudeHelp"), 1, 'help'); + print '
' . $langs->trans('ThirdPartyName'); + if (! empty($array_query['cust_name'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchTextHelp"), 1, 'help'); + print '
' . $langs->trans('CustomerCode'); + if (! empty($array_query['cust_code'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchTextHelp"), 1, 'help'); + print '
' . $langs->trans('Address'); + if (! empty($array_query['cust_adress'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchTextHelp"), 1, 'help'); + print '
' . $langs->trans('Zip'); + if (! empty($array_query['cust_zip'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchTextHelp"), 1, 'help'); + print '
' . $langs->trans('Town'); + if (! empty($array_query['cust_city'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchTextHelp"), 1, 'help'); + print '
' . $langs->trans("Country"); + if (count($array_query['cust_country']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $formadvtargetemaling->multiselect_country('cust_country', $array_query['cust_country']); + print '' . "\n"; + print '
' . $langs->trans('Status') . ' ' . $langs->trans('ThirdParty'); + if (count($array_query['cust_status']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print ''; + print $formadvtargetemaling->advMultiselectarray('cust_status', array ( + '0' => $langs->trans('ActivityCeased'), + '1' => $langs->trans('InActivity') + ), $array_query['cust_status']); + print '' . "\n"; + print '
' . $langs->trans("Maison mère"); + if (! empty($array_query['cust_mothercompany'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print ''; + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchTextHelp"), 1, 'help'); + print '
' . $langs->trans('ProspectCustomer') . ' ' . $langs->trans('ThirdParty'); + if (count($array_query['cust_typecust']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print ''; + $options_array = array ( + 2 => $langs->trans('Prospect'), + 3 => $langs->trans('ProspectCustomer'), + 1 => $langs->trans('Customer'), + 0 => $langs->trans('NorProspectNorCustomer') + ); + print $formadvtargetemaling->advMultiselectarray('cust_typecust', $options_array, $array_query['cust_typecust']); + print '' . "\n"; + print '
' . $langs->trans('ProspectLevel'); + if (count($array_query['cust_prospect_status']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print ''; + print $formadvtargetemaling->multiselect_prospection_status($array_query['cust_prospect_status'], 'cust_prospect_status', 1); + print '' . "\n"; + print '
' . $langs->trans('StatusProsp'); + if (count($array_query['cust_comm_status']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print ''; + print $formadvtargetemaling->advMultiselectarray('cust_comm_status', $advTarget->type_statuscommprospect, $array_query['cust_comm_status']); + print '' . "\n"; + print '
' . $langs->trans("ThirdPartyType"); + if (count($array_query['cust_typeent']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $formadvtargetemaling->advMultiselectarray('cust_typeent', $formcompany->typent_array(0, " AND id <> 0"), $array_query['cust_typeent']); + print '' . "\n"; + print '
' . $langs->trans("Staff"); + if (count($array_query['cust_effectif_id']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print ''; + print $formadvtargetemaling->advMultiselectarray("cust_effectif_id", $formcompany->effectif_array(0, " AND id <> 0"), $array_query['cust_effectif_id']); + print '' . "\n"; + print '
' . $langs->trans("SalesRepresentatives"); + if (count($array_query['cust_saleman']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $formadvtargetemaling->multiselectselect_salesrepresentatives('cust_saleman', $array_query['cust_saleman'], $user); + print '' . "\n"; + print '
' . $langs->trans("DefaultLang"); + if (count($array_query['cust_language']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $formadvtargetemaling->multiselectselect_language('cust_language', $array_query['cust_language']); + print '' . "\n"; + print '
' . $langs->trans("CustomersCategoryShort"); + if (count($array_query['cust_categ']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $formadvtargetemaling->multiselect_customercategories('cust_categ', $array_query['cust_categ']); + print '' . "\n"; + print '
' . $extrafields->attribute_label[$key]; + if (! empty($array_query['options_' . $key]) || (is_array($array_query['options_' . $key]) && count($array_query['options_' . $key]) > 0)) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print ''; + if (($extrafields->attribute_type[$key] == 'varchar') || ($extrafields->attribute_type[$key] == 'text')) { + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchTextHelp"), 1, 'help'); + } elseif (($extrafields->attribute_type[$key] == 'int') || ($extrafields->attribute_type[$key] == 'double')) { + print $langs->trans("AdvTgtMinVal") . ''; + print $langs->trans("AdvTgtMaxVal") . ''; + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchIntHelp"), 1, 'help'); + } elseif (($extrafields->attribute_type[$key] == 'date') || ($extrafields->attribute_type[$key] == 'datetime')) { + + print ''; + print '
' . $langs->trans("AdvTgtStartDt") . ''; + print $form->select_date('', 'options_' . $key . '_st_dt'); + print '' . $langs->trans("AdvTgtEndDt") . ''; + print $form->select_date('', 'options_' . $key . '_end_dt'); + print '
'; + + print '
' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchDtHelp"), 1, 'help'); + } elseif (($extrafields->attribute_type[$key] == 'boolean')) { + print $form->selectarray('options_' . $key, array ( + '' => '', + '1' => $langs->trans('Yes'), + '0' => $langs->trans('No') + ), $array_query['options_' . $key]); + print '' . "\n"; + } elseif (($extrafields->attribute_type[$key] == 'select')) { + print $formadvtargetemaling->advMultiselectarray('options_' . $key, $extrafields->attribute_param[$key]['options'], $array_query['options_' . $key]); + print '' . "\n"; + } elseif (($extrafields->attribute_type[$key] == 'sellist')) { + print $formadvtargetemaling->advMultiselectarray_selllist('options_' . $key, $extrafields->attribute_param[$key]['options'], $array_query['options_' . $key]); + print '' . "\n"; + } else { + + print ''; + print '
'; + if (is_array($array_query['options_' . $key])) { + print $extrafields->showInputField($key, implode(',', $array_query['options_' . $key])); + } else { + print $extrafields->showInputField($key, $array_query['options_' . $key]); + } + print '
'; + + print '
' . "\n"; + } + print '
' . $langs->trans('Status') . ' ' . $langs->trans('Contact'); + if (count($array_query['contact_status']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print ''; + print $formadvtargetemaling->advMultiselectarray('contact_status', array ( + '0' => $langs->trans('ActivityCeased'), + '1' => $langs->trans('InActivity') + ), $array_query['contact_status']); + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtContactHelp"), 1, 'help'); + print '
' . $langs->trans("UserTitle"); + if (count($array_query['contact_civility']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print ''; + print $formadvtargetemaling->multiselect_civility('contact_civility', $array_query['contact_civility']); + print '
' . $langs->trans('Contact') . ' ' . $langs->trans('Lastname'); + if (! empty($array_query['contact_lastname'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchTextHelp"), 1, 'help'); + print '
' . $langs->trans('Contact') . ' ' . $langs->trans('Firstname'); + if (! empty($array_query['contact_firstname'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchTextHelp"), 1, 'help'); + print '
' . $langs->trans('Contact') . ' ' . $langs->trans("Country"); + if (count($array_query['contact_country']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $formadvtargetemaling->multiselect_country('contact_country', $array_query['contact_country']); + print '' . "\n"; + print '
' . $langs->trans('Contact') . ' ' . $langs->trans("No_Email"); + if (! empty($array_query['contact_no_email'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $form->selectarray('contact_no_email', array ( + '' => '', + '1' => $langs->trans('Yes'), + '0' => $langs->trans('No') + ), $array_query['contact_no_email']); + print '' . "\n"; + print '
' . $langs->trans('Contact') . ' ' . $langs->trans("DateCreation"); + if (! empty($array_query['contact_create_st_dt'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print ''; + print '
' . $langs->trans("AdvTgtStartDt") . ''; + print $form->select_date($array_query['contact_create_st_dt'], 'contact_create_st_dt', 0, 0, 1, 'find_customer', 1, 1); + print '' . $langs->trans("AdvTgtEndDt") . ''; + print $form->select_date($array_query['contact_create_end_dt'], 'contact_create_end_dt', 0, 0, 1, 'find_customer', 1, 1); + print '
'; + print '
' . "\n"; + print '
' . $langs->trans('Contact') . ' ' . $langs->trans("DateLastModification"); + if (! empty($array_query['contact_update_st_dt'])) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print ''; + print '
' . $langs->trans("AdvTgtStartDt") . ''; + print $form->select_date($array_query['contact_update_st_dt'], 'contact_update_st_dt', 0, 0, 1, 'find_customer', 1, 1); + print '' . $langs->trans("AdvTgtEndDt") . ''; + print $form->select_date($array_query['contact_update_end_dt'], 'contact_update_end_dt', 0, 0, 1, 'find_customer', 1, 1); + print '
'; + print '
' . "\n"; + print '
' . $langs->trans("ContactCategoriesShort"); + if (count($array_query['contact_categ']) > 0) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print '' . "\n"; + print $formadvtargetemaling->multiselect_contactcategories('contact_categ', $array_query['contact_categ']); + print '' . "\n"; + print '
' . $extrafields->attribute_label[$key]; + if ($array_query['options_' . $key . '_cnct'] != '' || (is_array($array_query['options_' . $key . '_cnct']) && count($array_query['options_' . $key . '_cnct']) > 0)) { + print img_picto($langs->trans('AdvTgtUse'), 'ok.png@advtargetemailing'); + } + print ''; + if (($extrafields->attribute_type[$key] == 'varchar') || ($extrafields->attribute_type[$key] == 'text')) { + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchTextHelp"), 1, 'help'); + } elseif (($extrafields->attribute_type[$key] == 'int') || ($extrafields->attribute_type[$key] == 'double')) { + print $langs->trans("AdvTgtMinVal") . ''; + print $langs->trans("AdvTgtMaxVal") . ''; + print '' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchIntHelp"), 1, 'help'); + } elseif (($extrafields->attribute_type[$key] == 'date') || ($extrafields->attribute_type[$key] == 'datetime')) { + + print ''; + print '
' . $langs->trans("AdvTgtStartDt") . ''; + print $form->select_date('', 'options_' . $key . '_st_dt' . '_cnct'); + print '' . $langs->trans("AdvTgtEndDt") . ''; + print $form->select_date('', 'options_' . $key . '_end_dt' . '_cnct'); + print '
'; + + print '
' . "\n"; + print $form->textwithpicto('', $langs->trans("AdvTgtSearchDtHelp"), 1, 'help'); + } elseif (($extrafields->attribute_type[$key] == 'boolean')) { + print $form->selectarray('options_' . $key . '_cnct', array ( + '' => '', + '1' => $langs->trans('Yes'), + '0' => $langs->trans('No') + ), $array_query['options_' . $key . '_cnct']); + print '' . "\n"; + } elseif (($extrafields->attribute_type[$key] == 'select')) { + print $formadvtargetemaling->advMultiselectarray('options_' . $key . '_cnct', $extrafields->attribute_param[$key]['options'], $array_query['options_' . $key . '_cnct']); + print '' . "\n"; + } elseif (($extrafields->attribute_type[$key] == 'sellist')) { + print $formadvtargetemaling->advMultiselectarray_selllist('options_' . $key . '_cnct', $extrafields->attribute_param[$key]['options'], $array_query['options_' . $key . '_cnct']); + print '' . "\n"; + } else { + + print ''; + print '
'; + if (is_array($array_query['options_' . $key . '_cnct'])) { + print $extrafields->showInputField($key, implode(',', $array_query['options_' . $key . '_cnct']), '', '_cnct'); + } else { + print $extrafields->showInputField($key, $array_query['options_' . $key . '_cnct'], '', '_cnct'); + } + print '
'; + + print '
' . "\n"; + } + print '
' . "\n"; + + print '' . "\n"; + + print '
' . "\n"; + print '
' . "\n"; + print '
' . "\n"; + + print '
'; + print ''; + print_titre($langs->trans("ToClearAllRecipientsClickHere")); + print ''; + print ''; + print ''; + print ''; + print '
'; + print '
'; + print '
'; + } + if (empty($conf->mailchimp->enabled) || (! empty($conf->mailchimp->enabled) && $object->statut != 3)) { + // List of selected targets + print "\n\n"; + print '
'; + print ''; + print ''; + print ''; + print ''; + + $sql = "SELECT mc.rowid, mc.lastname, mc.firstname, mc.email, mc.other, mc.statut, mc.date_envoi, mc.source_url, mc.source_id, mc.source_type"; + $sql .= " FROM " . MAIN_DB_PREFIX . "mailing_cibles as mc"; + $sql .= " WHERE mc.fk_mailing=" . $object->id; + if ($search_nom) + $sql .= " AND mc.lastname LIKE '%" . $db->escape($search_nom) . "%'"; + if ($search_prenom) + $sql .= " AND mc.firstname LIKE '%" . $db->escape($search_prenom) . "%'"; + if ($search_email) + $sql .= " AND mc.email LIKE '%" . $db->escape($search_email) . "%'"; + $sql .= $db->order($sortfield, $sortorder); + $sql .= $db->plimit($conf->liste_limit + 1, $offset); + + dol_syslog('advtargetemailing.php:: sql=' . $sql); + $resql = $db->query($sql); + if ($resql) { + $num = $db->num_rows($resql); + + $parm = "&id=" . $object->id; + if ($search_nom) + $parm .= "&search_nom=" . urlencode($search_nom); + if ($search_prenom) + $parm .= "&search_prenom=" . urlencode($search_prenom); + if ($search_email) + $parm .= "&search_email=" . urlencode($search_email); + + print_barre_liste($langs->trans("MailSelectedRecipients"), $page, $_SERVER["PHP_SELF"], $parm, $sortfield, $sortorder, "", $num, $object->nbemail, ''); + + if ($page) + $parm .= "&page=" . $page; + print ''; + print ''; + print_liste_field_titre($langs->trans("EMail"), $_SERVER["PHP_SELF"], "mc.email", $parm, "", "", $sortfield, $sortorder); + print_liste_field_titre($langs->trans("Lastname"), $_SERVER["PHP_SELF"], "mc.lastname", $parm, "", "", $sortfield, $sortorder); + print_liste_field_titre($langs->trans("Firstname"), $_SERVER["PHP_SELF"], "mc.firstname", $parm, "", "", $sortfield, $sortorder); + print_liste_field_titre($langs->trans("OtherInformations"), $_SERVER["PHP_SELF"], "", $parm, "", "", $sortfield, $sortorder); + print_liste_field_titre($langs->trans("Source"), $_SERVER["PHP_SELF"], "", $parm, "", 'align="center"', $sortfield, $sortorder); + + // Date sendinf + if ($object->statut < 2) { + print ''; + } else { + print_liste_field_titre($langs->trans("DateSending"), $_SERVER["PHP_SELF"], "mc.date_envoi", $parm, '', 'align="center"', $sortfield, $sortorder); + } + + // Statut + print_liste_field_titre($langs->trans("Status"), $_SERVER["PHP_SELF"], "mc.statut", $parm, '', 'align="right"', $sortfield, $sortorder); + + print ''; + + // Ligne des champs de filtres + print ''; + // EMail + print ''; + // Name + print ''; + // Firstname + print ''; + // Other + print ''; + // SendDate + print ''; + // Source + print ''; + print ''; + + $var = true; + $i = 0; + + if ($num) { + while ( $i < min($num, $conf->liste_limit) ) { + $obj = $db->fetch_object($resql); + $var = ! $var; + + print ""; + print ''; + print ''; + print ''; + print ''; + print ''; + + // Statut pour l'email destinataire (Attentioon != statut du mailing) + if ($obj->statut == 0) { + print ''; + print ''; + } else { + print ''; + print ''; + } + print ''; + + $i ++; + } + } else { + print ''; + } + print "
 
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ' '; + print ''; + print ' '; + print ''; + print ''; + print '  '; + print ''; + print '
' . $obj->email . '' . $obj->lastname . '' . $obj->firstname . '' . $obj->other . ''; + if (empty($obj->source_id) || empty($obj->source_type)) { + print $obj->source_url; // For backward compatibility + } else { + + if ($obj->source_type == 'thirdparty') { + include_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php'; + $m = new Societe($db); + $m->fetch($obj->source_id); + print $m->getNomUrl(1); + } elseif ($obj->source_type == 'contact') { + include_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php'; + $m = new Contact($db); + $m->fetch($obj->source_id); + print $m->getNomUrl(1); + } + } + print ' ' . $langs->trans("MailingStatusNotSent"); + if ($user->rights->mailing->creer) { + print '' . img_delete($langs->trans("RemoveRecipient")); + } + print '' . $obj->date_envoi . ''; + if ($obj->statut == - 1) + print $langs->trans("MailingStatusError") . ' ' . img_error(); + if ($obj->statut == 1) + print $langs->trans("MailingStatusSent") . ' ' . img_picto($langs->trans("MailingStatusSent"), 'statut4'); + if ($obj->statut == 2) + print $langs->trans("MailingStatusRead") . ' ' . img_picto($langs->trans("MailingStatusRead"), 'statut6'); + if ($obj->statut == 3) + print $langs->trans("MailingStatusNotContact") . ' ' . img_picto($langs->trans("MailingStatusNotContact"), 'statut8'); + print '
' . $langs->trans("NoTargetYet") . '

"; + + $db->free($resql); + } else { + setEventMessage($db->lasterror(), 'errors'); + } + + print '
'; + } +} + +llxFooter(); +$db->close(); \ No newline at end of file diff --git a/htdocs/comm/mailing/class/advtargetemailing.class.php b/htdocs/comm/mailing/class/advtargetemailing.class.php new file mode 100644 index 00000000000..1b9e92179aa --- /dev/null +++ b/htdocs/comm/mailing/class/advtargetemailing.class.php @@ -0,0 +1,780 @@ + +* +* 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 class/advtargetemailing.class.php + * \ingroup advtargetemailing + * \brief This file is an example CRUD class file (Create/Read/Update/Delete) + + */ + + +/** + * Put your class' description here + */ +class AdvanceTargetingMailing extends CommonObject +{ + + var $db; //!< To store db handler + var $error; //!< To return error code (or message) + var $errors = array(); //!< To return several error codes (or messages) + var $element='advtargetemailing'; //!< Id that identify managed objects + var $table_element='advtargetemailing'; //!< Name of table without prefix where object is stored + + var $id; + + var $name; + var $entity; + var $fk_mailing; + var $filtervalue; + var $fk_user_author; + var $datec=''; + var $fk_user_mod; + var $tms=''; + + var $select_target_type = array(); + var $type_statuscommprospect=array(); + var $thirdparty_lines; + var $contact_lines; + + + /** + * Constructor + * + * @param DoliDb $db Database handler + */ + function __construct($db) + { + global $langs; + $langs->load('customers'); + + $this->db = $db; + + $this->select_target_type = array('2'=>$langs->trans('Contacts'),'1'=>$langs->trans('Contacts').'+'.$langs->trans('ThirdParty'), + '3'=>$langs->trans('ThirdParty'), + ); + $this->type_statuscommprospect=array( + -1=>$langs->trans("StatusProspect-1"), + 0=>$langs->trans("StatusProspect0"), + 1=>$langs->trans("StatusProspect1"), + 2=>$langs->trans("StatusProspect2"), + 3=>$langs->trans("StatusProspect3")); + + + return 1; + } + + /** + * Create object into database + * + * @param User $user User that creates + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, Id of created object if OK + */ + function create($user, $notrigger=0) + { + global $conf, $langs; + $error=0; + + // Clean parameters + if (isset($this->fk_mailing)) $this->fk_mailing=trim($this->fk_mailing); + if (isset($this->name)) $this->name=trim($this->name); + if (isset($this->filtervalue)) $this->filtervalue=trim($this->filtervalue); + if (isset($this->fk_user_author)) $this->fk_user_author=trim($this->fk_user_author); + if (isset($this->fk_user_mod)) $this->fk_user_mod=trim($this->fk_user_mod); + + + + // Check parameters + // Put here code to add control on parameters values + + // Insert request + $sql = "INSERT INTO ".MAIN_DB_PREFIX."advtargetemailing("; + + $sql.= "name,"; + $sql.= "entity,"; + $sql.= "fk_mailing,"; + $sql.= "filtervalue,"; + $sql.= "fk_user_author,"; + $sql.= "datec,"; + $sql.= "fk_user_mod"; + + + $sql.= ") VALUES ("; + + $sql.= " ".(! isset($this->name)?'NULL':"'".$this->db->escape($this->name)."'").","; + $sql.= " ".$conf->entity.","; + $sql.= " ".(! isset($this->fk_mailing)?'NULL':"'".$this->fk_mailing."'").","; + $sql.= " ".(! isset($this->filtervalue)?'NULL':"'".$this->db->escape($this->filtervalue)."'").","; + $sql.= " ".$user->id.","; + $sql.= " ".$this->db->idate(dol_now()).","; + $sql.= " ".$user->id; + + + $sql.= ")"; + + $this->db->begin(); + + dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG); + $resql=$this->db->query($sql); + if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); } + + if (! $error) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."advtargetemailing"); + + if (! $notrigger) + { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; + //$interface=new Interfaces($this->db); + //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf); + //if ($result < 0) { $error++; $this->errors=$interface->errors; } + //// End call triggers + } + } + + // Commit or rollback + if ($error) + { + foreach($this->errors as $errmsg) + { + dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; + } + else + { + $this->db->commit(); + return $this->id; + } + } + + /** + * Load object in memory from the database + * + * @param int $id Id object + * @return int <0 if KO, >0 if OK + */ + function fetch($id) + { + global $langs; + $sql = "SELECT"; + $sql.= " t.rowid,"; + + $sql.= " t.name,"; + $sql.= " t.entity,"; + $sql.= " t.fk_mailing,"; + $sql.= " t.filtervalue,"; + $sql.= " t.fk_user_author,"; + $sql.= " t.datec,"; + $sql.= " t.fk_user_mod,"; + $sql.= " t.tms"; + + $sql.= " FROM ".MAIN_DB_PREFIX."advtargetemailing as t"; + $sql.= " WHERE t.rowid = ".$id; + + dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG); + $resql=$this->db->query($sql); + if ($resql) + { + if ($this->db->num_rows($resql)) + { + $obj = $this->db->fetch_object($resql); + + $this->id = $obj->rowid; + + $this->name = $obj->name; + $this->entity = $obj->entity; + $this->fk_mailing = $obj->fk_mailing; + $this->filtervalue = $obj->filtervalue; + $this->fk_user_author = $obj->fk_user_author; + $this->datec = $this->db->jdate($obj->datec); + $this->fk_user_mod = $obj->fk_user_mod; + $this->tms = $this->db->jdate($obj->tms); + + } + $this->db->free($resql); + + return 1; + } + else + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR); + return -1; + } + } + + /** + * Load object in memory from the database + * + * @param int $id Id object + * @return int <0 if KO, >0 if OK + */ + function fetch_by_mailing($id=0) + { + global $langs; + $sql = "SELECT"; + $sql.= " t.rowid,"; + + $sql.= " t.name,"; + $sql.= " t.entity,"; + $sql.= " t.fk_mailing,"; + $sql.= " t.filtervalue,"; + $sql.= " t.fk_user_author,"; + $sql.= " t.datec,"; + $sql.= " t.fk_user_mod,"; + $sql.= " t.tms"; + + $sql.= " FROM ".MAIN_DB_PREFIX."advtargetemailing as t"; + if (!empty($id)) { + $sql.= " WHERE t.fk_mailing = ".$id; + }else { + $sql.= " WHERE t.fk_mailing = ".$this->fk_mailing; + } + + dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG); + $resql=$this->db->query($sql); + if ($resql) + { + if ($this->db->num_rows($resql)) + { + $obj = $this->db->fetch_object($resql); + + $this->id = $obj->rowid; + + $this->name = $obj->name; + $this->entity = $obj->entity; + $this->fk_mailing = $obj->fk_mailing; + $this->filtervalue = $obj->filtervalue; + $this->fk_user_author = $obj->fk_user_author; + $this->datec = $this->db->jdate($obj->datec); + $this->fk_user_mod = $obj->fk_user_mod; + $this->tms = $this->db->jdate($obj->tms); + + } + $this->db->free($resql); + + return 1; + } + else + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR); + return -1; + } + } + + /** + * Update object into database + * + * @param User $user User that modifies + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, >0 if OK + */ + function update($user=0, $notrigger=0) + { + global $conf, $langs; + $error=0; + + // Clean parameters + if (isset($this->fk_mailing)) $this->fk_mailing=trim($this->fk_mailing); + if (isset($this->name)) $this->name=trim($this->name); + if (isset($this->filtervalue)) $this->filtervalue=trim($this->filtervalue); + if (isset($this->fk_user_author)) $this->fk_user_author=trim($this->fk_user_author); + if (isset($this->fk_user_mod)) $this->fk_user_mod=trim($this->fk_user_mod); + + + + // Check parameters + // Put here code to add a control on parameters values + + // Update request + $sql = "UPDATE ".MAIN_DB_PREFIX."advtargetemailing SET"; + + $sql.= " name=".(isset($this->name)?"'".$this->db->escape($this->name)."'":"''").","; + $sql.= " entity=".$conf->entity.","; + $sql.= " fk_mailing=".(isset($this->fk_mailing)?$this->fk_mailing:"null").","; + $sql.= " filtervalue=".(isset($this->filtervalue)?"'".$this->db->escape($this->filtervalue)."'":"null").","; + $sql.= " fk_user_mod=".$user->id; + + $sql.= " WHERE rowid=".$this->id; + + $this->db->begin(); + + dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG); + $resql = $this->db->query($sql); + if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); } + + if (! $error) + { + if (! $notrigger) + { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; + //$interface=new Interfaces($this->db); + //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf); + //if ($result < 0) { $error++; $this->errors=$interface->errors; } + //// End call triggers + } + } + + // Commit or rollback + if ($error) + { + foreach($this->errors as $errmsg) + { + dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; + } + else + { + $this->db->commit(); + return 1; + } + } + + /** + * Delete object in database + * + * @param User $user User that deletes + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, >0 if OK + */ + function delete($user, $notrigger=0) + { + global $conf, $langs; + $error=0; + + $this->db->begin(); + + if (! $error) + { + if (! $notrigger) + { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; + //$interface=new Interfaces($this->db); + //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf); + //if ($result < 0) { $error++; $this->errors=$interface->errors; } + //// End call triggers + } + } + + if (! $error) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."advtargetemailing"; + $sql.= " WHERE rowid=".$this->id; + + dol_syslog(get_class($this)."::delete sql=".$sql); + $resql = $this->db->query($sql); + if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); } + } + + // Commit or rollback + if ($error) + { + foreach($this->errors as $errmsg) + { + dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; + } + else + { + $this->db->commit(); + return 1; + } + } + + + /** + * Save query in database to retreive it + * + * @param User $user User that deletes + * @param array $arrayquery All element to Query + * @return int <0 if KO, >0 if OK + */ + function savequery($user,$arrayquery) + { + global $langs,$conf; + + if (!empty($arrayquery)) { + $result=$this->fetch_by_mailing($this->fk_mailing); + $this->filtervalue=json_encode($arrayquery); + if ($result<0) { + return -1; + } + if (!empty($this->id)) { + $this->update($user); + }else { + $this->create($user); + } + } + } + + + + /** + * Load object in memory from database + * + * @param array $arrayquery All element to Query + * @return int <0 if KO, >0 if OK + */ + function query_thirdparty($arrayquery) + { + global $langs,$conf; + + $sql = "SELECT"; + $sql.= " t.rowid"; + $sql.= " FROM " . MAIN_DB_PREFIX . "societe as t"; + $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe_extrafields as te ON te.fk_object=t.rowid "; + + $sqlwhere=array(); + + $sqlwhere[]= 't.entity IN ('.getEntity('societe',1).')'; + + if (count($arrayquery)>0) { + + if (array_key_exists('cust_saleman', $arrayquery)) { + $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as saleman ON saleman.fk_soc=t.rowid "; + } + if (array_key_exists('cust_categ', $arrayquery)) { + $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "categorie_societe as custcateg ON custcateg.fk_societe=t.rowid "; + } + + if (!empty($arrayquery['cust_name'])) { + + $sqlwhere[]= $this->transformToSQL('t.nom',$arrayquery['cust_name']); + } + if (!empty($arrayquery['cust_code'])) { + $sqlwhere[]= $this->transformToSQL('t.code_client',$arrayquery['cust_code']); + } + if (!empty($arrayquery['cust_adress'])) { + $sqlwhere[]= $this->transformToSQL('t.address',$arrayquery['cust_adress']); + } + if (!empty($arrayquery['cust_zip'])) { + $sqlwhere[]= $this->transformToSQL('t.zip',$arrayquery['cust_zip']); + } + if (!empty($arrayquery['cust_city'])) { + $sqlwhere[]= $this->transformToSQL('t.town',$arrayquery['cust_city']); + } + if (!empty($arrayquery['cust_mothercompany'])) { + $str=$this->transformToSQL('nom',$arrayquery['cust_mothercompany']); + $sqlwhere[]= " (t.parent IN (SELECT rowid FROM " . MAIN_DB_PREFIX . "societe WHERE ('.$str.')))"; + } + if (!empty($arrayquery['cust_status']) && count($arrayquery['cust_status'])>0) { + $sqlwhere[]= " (t.status IN (".implode(',',$arrayquery['cust_status'])."))"; + } + if (!empty($arrayquery['cust_typecust']) && count($arrayquery['cust_typecust'])>0) { + $sqlwhere[]= " (t.client IN (".implode(',',$arrayquery['cust_typecust'])."))"; + } + if (!empty($arrayquery['cust_comm_status']) && count($arrayquery['cust_comm_status']>0)) { + $sqlwhere[]= " (t.fk_stcomm IN (".implode(',',$arrayquery['cust_comm_status'])."))"; + } + if (!empty($arrayquery['cust_prospect_status']) && count($arrayquery['cust_prospect_status'])>0) { + $sqlwhere[]= " (t.fk_prospectlevel IN ('".implode("','",$arrayquery['cust_prospect_status'])."'))"; + } + if (!empty($arrayquery['cust_typeent']) && count($arrayquery['cust_typeent'])>0) { + $sqlwhere[]= " (t.fk_typent IN (".implode(',',$arrayquery['cust_typeent'])."))"; + } + if (!empty($arrayquery['cust_saleman']) && count($arrayquery['cust_saleman'])>0) { + $sqlwhere[]= " (saleman.fk_user IN (".implode(',',$arrayquery['cust_saleman'])."))"; + } + if (!empty($arrayquery['cust_country']) && count($arrayquery['cust_country'])>0) { + $sqlwhere[]= " (t.fk_pays IN (".implode(',',$arrayquery['cust_country'])."))"; + } + if (!empty($arrayquery['cust_effectif_id']) && count($arrayquery['cust_effectif_id'])>0) { + $sqlwhere[]= " (t.fk_effectif IN (".implode(',',$arrayquery['cust_effectif_id'])."))"; + } + if (!empty($arrayquery['cust_categ']) && count($arrayquery['cust_categ'])>0) { + $sqlwhere[]= " (custcateg.fk_categorie IN (".implode(',',$arrayquery['cust_categ'])."))"; + } + if (!empty($arrayquery['cust_language']) && count($arrayquery['cust_language'])>0) { + $sqlwhere[]= " (t.default_lang IN ('".implode("','",$arrayquery['cust_language'])."'))"; + } + + //Standard Extrafield feature + if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) { + // fetch optionals attributes and labels + dol_include_once('/core/class/extrafields.class.php'); + $extrafields = new ExtraFields($this->db); + $extralabels=$extrafields->fetch_name_optionals_label('societe'); + + foreach($extralabels as $key=>$val) { + + if (($extrafields->attribute_type[$key] == 'varchar') || + ($extrafields->attribute_type[$key] == 'text')) { + if (!empty($arrayquery['options_'.$key])) { + $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key]."')"; + } + } elseif (($extrafields->attribute_type[$key] == 'int') || + ($extrafields->attribute_type[$key] == 'double')) { + if (!empty($arrayquery['options_'.$key.'_max'])) { + $sqlwhere[]= " (te.".$key." >= ".$arrayquery['options_'.$key.'_max']." AND te.".$key." <= ".$arrayquery['options_'.$key.'_min'].")"; + } + } else if (($extrafields->attribute_type[$key] == 'date') || + ($extrafields->attribute_type[$key] == 'datetime')) { + if (!empty($arrayquery['options_'.$key.'_end_dt'])){ + $sqlwhere[]= " (te.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt'])."' AND te.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt'])."')"; + } + }else if ($extrafields->attribute_type[$key] == 'boolean') { + if ($arrayquery['options_'.$key]!=''){ + $sqlwhere[]= " (te.".$key." = ".$arrayquery['options_'.$key].")"; + } + }else{ + if (is_array($arrayquery['options_'.$key])) { + $sqlwhere[]= " (te.".$key." IN ('".implode("','",$arrayquery['options_'.$key])."'))"; + } elseif (!empty($arrayquery['options_'.$key])) { + $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key]."')"; + } + } + + } + + + } + + if (count($sqlwhere)>0) $sql.= " WHERE ".implode(" AND ",$sqlwhere); + + } + + + dol_syslog(get_class($this) . "::query_thirdparty sql=" . $sql, LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $this->thirdparty_lines = array(); + $num = $this->db->num_rows($resql); + $i = 0; + + if ($num) + { + while( $i < $num) + { + $obj = $this->db->fetch_object($resql); + + $this->thirdparty_lines[$i] = $obj->rowid; + + $i++; + } + } + $this->db->free($resql); + + return $num; + } else { + $this->error = "Error " . $this->db->lasterror(); + dol_syslog(get_class($this) . "::query_thirdparty " . $this->error, LOG_ERR); + return -1; + } + } + + /** + * Load object in memory from database + * + * @param array $arrayquery All element to Query + * @return int <0 if KO, >0 if OK + */ + function query_contact($arrayquery) + { + global $langs,$conf; + + $sql = "SELECT"; + $sql.= " t.rowid"; + $sql.= " FROM " . MAIN_DB_PREFIX . "socpeople as t"; + $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "socpeople_extrafields as te ON te.fk_object=t.rowid "; + + $sqlwhere=array(); + + $sqlwhere[]= 't.entity IN ('.getEntity('socpeople',1).')'; + + if (count($arrayquery)>0) { + + if (array_key_exists('contact_categ', $arrayquery)) { + $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "categorie_contact as contactcateg ON contactcateg.fk_socpeople=t.rowid "; + } + + if (!empty($arrayquery['contact_lastname'])) { + $sqlwhere[]=$this->transformToSQL('t.lastname',$arrayquery['contact_lastname']); + } + if (!empty($arrayquery['contact_firstname'])) { + $sqlwhere[]=$this->transformToSQL('t.firstname',$arrayquery['contact_firstname']); + } + if (!empty($arrayquery['contact_country']) && count($arrayquery['contact_country'])) { + $sqlwhere[]= " (t.fk_pays IN (".implode(',',$arrayquery['contact_country'])."))"; + } + if (!empty($arrayquery['contact_status']) && count($arrayquery['contact_status'])>0) { + $sqlwhere[]= " (t.statut IN (".implode(',',$arrayquery['contact_status'])."))"; + } + if (!empty($arrayquery['contact_civility']) && count($arrayquery['contact_civility'])>0) { + $sqlwhere[]= " (t.civility IN ('".implode("','",$arrayquery['contact_civility'])."'))"; + } + if ($arrayquery['contact_no_email']!='') { + $sqlwhere[]= " (t.no_email='".$arrayquery['contact_no_email']."')"; + } + if ($arrayquery['contact_update_st_dt']!='') { + $sqlwhere[]= " (t.tms >= '".$this->db->idate($arrayquery['contact_update_st_dt'])."' AND t.tms <= '".$this->db->idate($arrayquery['contact_update_end_dt'])."')"; + } + if ($arrayquery['contact_create_st_dt']!='') { + $sqlwhere[]= " (t.datec >= '".$this->db->idate($arrayquery['contact_create_st_dt'])."' AND t.datec <= '".$this->db->idate($arrayquery['contact_create_end_dt'])."')"; + } + if (!empty($arrayquery['contact_categ']) && count($arrayquery['contact_categ'])>0) { + $sqlwhere[]= " (contactcateg.fk_categorie IN (".implode(",",$arrayquery['contact_categ'])."))"; + } + + //Standard Extrafield feature + if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) { + // fetch optionals attributes and labels + dol_include_once('/core/class/extrafields.class.php'); + $extrafields = new ExtraFields($this->db); + $extralabels=$extrafields->fetch_name_optionals_label('socpeople'); + + foreach($extralabels as $key=>$val) { + + if (($extrafields->attribute_type[$key] == 'varchar') || + ($extrafields->attribute_type[$key] == 'text')) { + if (!empty($arrayquery['options_'.$key.'_cnct'])) { + $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key.'_cnct']."')"; + } + } elseif (($extrafields->attribute_type[$key] == 'int') || + ($extrafields->attribute_type[$key] == 'double')) { + if (!empty($arrayquery['options_'.$key.'_max'.'_cnct'])) { + $sqlwhere[]= " (te.".$key." >= ".$arrayquery['options_'.$key.'_max'.'_cnct']." AND te.".$key." <= ".$arrayquery['options_'.$key.'_min'.'_cnct'].")"; + } + } else if (($extrafields->attribute_type[$key] == 'date') || + ($extrafields->attribute_type[$key] == 'datetime')) { + if (!empty($arrayquery['options_'.$key.'_end_dt'.'_cnct'])){ + $sqlwhere[]= " (te.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt'.'_cnct'])."' AND te.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt'.'_cnct'])."')"; + } + }else if ($extrafields->attribute_type[$key] == 'boolean') { + if ($arrayquery['options_'.$key.'_cnct']!=''){ + if ($arrayquery['options_'.$key.'_cnct']==0) { + $sqlwhere[]= " (te.".$key." = ".$arrayquery['options_'.$key.'_cnct']." OR ((te.".$key." IS NULL) AND (te.fk_object IS NOT NULL)))"; + }else { + $sqlwhere[]= " (te.".$key." = ".$arrayquery['options_'.$key.'_cnct'].")"; + } + } + }else{ + if (is_array($arrayquery['options_'.$key.'_cnct'])) { + $sqlwhere[]= " (te.".$key." IN ('".implode("','",$arrayquery['options_'.$key.'_cnct'])."'))"; + } elseif (!empty($arrayquery['options_'.$key.'_cnct'])) { + $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key.'_cnct']."')"; + } + } + + } + + + } + + if (count($sqlwhere)>0) $sql.= " WHERE ".implode(" AND ",$sqlwhere); + + } + + + dol_syslog(get_class($this) . "::query_contact sql=" . $sql, LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $this->contact_lines = array(); + $num = $this->db->num_rows($resql); + $i = 0; + + if ($num) + { + while( $i < $num) + { + $obj = $this->db->fetch_object($resql); + + $this->contact_lines[$i] = $obj->rowid; + + $i++; + } + } + $this->db->free($resql); + + return $num; + } else { + $this->error = "Error " . $this->db->lasterror(); + dol_syslog(get_class($this) . "::query_contact " . $this->error, LOG_ERR); + return -1; + } + } + + + /** + * Parse criteria to return a SQL qury formated + * + * @param string $column_to_test column to test + * @param string $criteria Use %% as magic caracters. For exemple to find all item like jean, joe, jim, you can input j%%, you can also use ; as separator for value, + * and use ! for except this value. + * For exemple jean;joe;jim%%;!jimo;!jima%> will target all jean, joe, start with jim but not jimo and not everythnig taht start by jima + * @return int <0 if KO, >0 if OK + */ + public function transformToSQL($column_to_test,$criteria) { + $return_sql_criteria = '('; + + //This is a multiple value test + if (preg_match('/;/',$criteria)) { + $return_sql_not_like=array(); + $return_sql_like=array(); + + $criteria_array=explode(';',$criteria); + foreach($criteria_array as $inter_criteria) { + if (preg_match('/!/',$inter_criteria)) { + $return_sql_not_like[]= '('.$column_to_test.' NOT LIKE \''.str_replace('!', '', $inter_criteria).'\')'; + } else { + $return_sql_like[]= '('.$column_to_test.' LIKE \''.$inter_criteria.'\')'; + } + } + + if (count($return_sql_like)>0) { + $return_sql_criteria .= '(' . implode (' OR ', $return_sql_like) .')'; + } + if (count($return_sql_not_like)>0) { + $return_sql_criteria .= ' AND (' . implode (' AND ', $return_sql_not_like).')'; + } + + }else { + $return_sql_criteria .= $column_to_test . ' LIKE \''.$this->db->escape($criteria).'\''; + } + + $return_sql_criteria .= ')'; + + return $return_sql_criteria; + } + + +} + +?> diff --git a/htdocs/comm/mailing/class/html.formadvtargetemailing.class.php b/htdocs/comm/mailing/class/html.formadvtargetemailing.class.php new file mode 100644 index 00000000000..a63ea7b2a42 --- /dev/null +++ b/htdocs/comm/mailing/class/html.formadvtargetemailing.class.php @@ -0,0 +1,495 @@ + +* +* +* 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 advtargetemailing/class/html.formadvtragetemaling.class.php + * \brief Fichier de la classe des fonctions predefinie de composants html advtargetemaling + */ + +/** + * Class to manage building of HTML components + */ +class FormAdvTargetEmailing extends Form { + var $db; + var $error; + + /** + * Constructor + * + * @param DoliDB $db handler + */ + function __construct($db) { + global $langs; + + $this->db = $db; + + return 1; + } + + /** + * Affiche un champs select contenant une liste + * + * @param int $selected_array à preselectionner + * @param string $htmlname select field + * @param int $showempty empty field + * @return string select field + */ + function multiselect_prospection_status($selected_array, $htmlname = 'cust_prospect_status') { + global $conf, $langs; + $options_array = array (); + + $sql = "SELECT code, label"; + $sql .= " FROM " . MAIN_DB_PREFIX . "c_prospectlevel"; + $sql .= " WHERE active > 0"; + $sql .= " ORDER BY sortorder"; + dol_syslog ( get_class ( $this ) . '::multiselect_prospection_status sql=' . $sql, LOG_DEBUG ); + $resql = $this->db->query ( $sql ); + if ($resql) { + $num = $this->db->num_rows ( $resql ); + $i = 0; + while ( $i < $num ) { + $obj = $this->db->fetch_object ( $resql ); + + $level = $langs->trans ( $obj->code ); + if ($level == $obj->code) + $level = $langs->trans ( $obj->label ); + $options_array [$obj->code] = $level; + + $i ++; + } + } else { + dol_print_error ( $this->db ); + } + return $this->advMultiselectarray ( $htmlname, $options_array, $selected_array ); + } + + /** + * Return combo list of activated countries, into language of user + * + * @param string $htmlname of html select object + * @param string $selected or Code or Label of preselected country + * @return string HTML string with select + */ + function multiselect_country($htmlname = 'country_id', $selected_array=array()) { + global $conf, $langs; + + $langs->load ( "dict" ); + + $out = ''; + $countryArray = array (); + $label = array (); + + $options_array = array (); + + $sql = "SELECT rowid, code as code_iso, label"; + $sql .= " FROM " . MAIN_DB_PREFIX . "c_country"; + $sql .= " WHERE active = 1 AND code<>''"; + $sql .= " ORDER BY code ASC"; + + dol_syslog ( get_class ( $this ) . "::select_country sql=" . $sql ); + $resql = $this->db->query ( $sql ); + if ($resql) { + + $num = $this->db->num_rows ( $resql ); + $i = 0; + if ($num) { + $foundselected = false; + + while ( $i < $num ) { + $obj = $this->db->fetch_object ( $resql ); + $countryArray [$i] ['rowid'] = $obj->rowid; + $countryArray [$i] ['code_iso'] = $obj->code_iso; + $countryArray [$i] ['label'] = ($obj->code_iso && $langs->transnoentitiesnoconv ( "Country" . $obj->code_iso ) != "Country" . $obj->code_iso ? $langs->transnoentitiesnoconv ( "Country" . $obj->code_iso ) : ($obj->label != '-' ? $obj->label : '')); + $label [$i] = $countryArray [$i] ['label']; + $i ++; + } + + array_multisort ( $label, SORT_ASC, $countryArray ); + + foreach ( $countryArray as $row ) { + $label = dol_trunc ( $row ['label'], $maxlength, 'middle' ); + if ($row ['code_iso']) + $label .= ' (' . $row ['code_iso'] . ')'; + + $options_array [$row ['rowid']] = $label; + } + } + } else { + dol_print_error ( $this->db ); + } + + return $this->advMultiselectarray ( $htmlname, $options_array, $selected_array ); + } + + /** + * Return select list for categories (to use in form search selectors) + * + * @param string $selected value + * @param string $htmlname of combo list (example: 'search_sale') + * @param User $user user + * @return string combo list code + */ + function multiselectselect_salesrepresentatives($htmlname, $selected_array, $user) { + + global $conf; + + $options_array = array (); + + + $sql_usr .= "SELECT DISTINCT u2.rowid, u2.lastname as name, u2.firstname, u2.login"; + $sql_usr .= " FROM " . MAIN_DB_PREFIX . "user as u2, " . MAIN_DB_PREFIX . "societe_commerciaux as sc"; + $sql_usr .= " WHERE u2.entity IN (0," . $conf->entity . ")"; + $sql_usr .= " AND u2.rowid = sc.fk_user "; + + if (! empty ( $conf->global->USER_HIDE_INACTIVE_IN_COMBOBOX )) + $sql_usr .= " AND u2.statut<>0 "; + $sql_usr .= " ORDER BY name ASC"; + // print $sql_usr;exit; + + $resql_usr = $this->db->query ( $sql_usr ); + if ($resql_usr) { + while ( $obj_usr = $this->db->fetch_object ( $resql_usr ) ) { + + $label = $obj_usr->firstname . " " . $obj_usr->name . " (" . $obj_usr->login . ')'; + + $options_array [$obj_usr->rowid] = $label; + + } + $this->db->free ( $resql_usr ); + } else { + dol_print_error ( $this->db ); + } + + return $this->advMultiselectarray ( $htmlname, $options_array, $selected_array ); + } + + /** + * Return select list for categories (to use in form search selectors) + * + * @param string $selected value + * @param string $htmlname of combo list (example: 'search_sale') + * @param User $user user + * @return string combo list code + */ + function multiselectselect_language($htmlname, $selected_array) { + + global $conf,$langs; + + $options_array = array (); + + $langs_available=$langs->get_available_languages(DOL_DOCUMENT_ROOT,12); + + foreach ($langs_available as $key => $value) + { + $label = $value; + $options_array [$key] = $label; + } + asort($options_array); + return $this->advMultiselectarray ( $htmlname, $options_array, $selected_array ); + } + + /** + * Return multiselect list of entities for extrafeild type sellist + * + * @param string $htmlname select + * @param array $options_array to manage + * @param array $selected_array to manage + * @return string combo list code + */ + function advMultiselectarray_selllist($htmlname, $sqlqueryparam = array(), $selected_array = array()) { + + if (is_array ( $sqlqueryparam )) { + $options_array=array(); + + $param_list = array_keys ( $sqlqueryparam ); + $InfoFieldList = explode ( ":", $param_list [0] ); + + // 0 1 : tableName + // 1 2 : label field name Nom du champ contenant le libelle + // 2 3 : key fields name (if differ of rowid) + // 3 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value + + $keyList = 'rowid'; + + if (count ( $InfoFieldList ) >= 3) { + if (strpos ( $InfoFieldList [3], 'extra.' ) !== false) { + $keyList = 'main.' . $InfoFieldList [2] . ' as rowid'; + } else { + $keyList = $InfoFieldList [2] . ' as rowid'; + } + } + + $sql = 'SELECT ' . $keyList . ', ' . $InfoFieldList [1]; + $sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList [0]; + if (! empty ( $InfoFieldList [3] )) { + + // We have to join on extrafield table + if (strpos ( $InfoFieldList [3], 'extra' ) !== false) { + $sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList [0] . '_extrafields as extra'; + $sql .= ' WHERE extra.fk_object=main.' . $InfoFieldList [2] . ' AND ' . $InfoFieldList [3]; + } else { + $sql .= ' WHERE ' . $InfoFieldList [3]; + } + } + if (! empty ( $InfoFieldList [1] ) && $key == 'ts_payeur') { + $sql .= " ORDER BY nom"; + } + // $sql.= ' WHERE entity = '.$conf->entity; + + dol_syslog ( get_class ( $this ) . '::advMultiselectarray_selllist sql=' . $sql ); + $resql = $this->db->query ( $sql ); + if ($resql) { + + $num = $this->db->num_rows ( $resql ); + $i = 0; + if ($num) { + while ( $i < $num ) { + $obj = $this->db->fetch_object ( $resql ); + $labeltoshow = dol_trunc ( $obj->$InfoFieldList [1], 90 ); + $options_array[$obj->rowid]=$labeltoshow; + $i ++; + } + } + $this->db->free ( $resql ); + } + } + + return $this->advMultiselectarray ( $htmlname, $options_array, $selected_array ); + } + + /** + * Return combo list with people title + * + * @param string $selected Title preselected + * @param string $htmlname Name of HTML select combo field + * @return void + */ + function multiselect_civility($htmlname='civilite_id',$selected_array = array()) + { + global $conf,$langs,$user; + $langs->load("dict"); + + $options_array=array(); + + + $sql = "SELECT rowid, code, label as civilite, active FROM ".MAIN_DB_PREFIX."c_civility"; + $sql.= " WHERE active = 1"; + + dol_syslog("Form::select_civility sql=".$sql); + $resql=$this->db->query($sql); + if ($resql) + { + + $num = $this->db->num_rows($resql); + $i = 0; + if ($num) + { + while ($i < $num) + { + $obj = $this->db->fetch_object($resql); + // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut + $label= ($langs->trans("Civility".$obj->code)!="Civility".$obj->code ? $langs->trans("Civility".$obj->code) : ($obj->civilite!='-'?$obj->civilite:'')); + + + $options_array[$obj->code]=$label; + + $i++; + } + } + + } + else + { + dol_print_error($this->db); + } + + return $this->advMultiselectarray ( $htmlname, $options_array, $selected_array ); + } + + /** + * Return multiselect list of entities. + * + * @param string $htmlname select + * @param array $options_array to manage + * @param array $selected_array to manage + * @param int $showempty show empty + * @return void + */ + function advMultiselectarray($htmlname, $options_array = array(), $selected_array = array(), $showempty = 0) { + global $conf, $langs; + + $return = ''; + + $return .= ''; + + return $return; + } + + /** + * Return combo list with customer categories + * + * @param string $htmlname Name of categorie + * @param array $selected_array value selected + * @return void + */ + function multiselect_customercategories($htmlname='cust_cat',$selected_array = array()) + { + return $this->multiselect_categories($htmlname,$selected_array,2); + } + + /** + * Return combo list with customer contact + * + * @param string $htmlname Name of categorie + * @param array $selected_array value selected + * @return void + */ + function multiselect_contactcategories($htmlname='contact_cat',$selected_array = array()) + { + return $this->multiselect_categories($htmlname,$selected_array,4); + } + + /** + * Return combo list of categories + * + * @param string $htmlname Name of categorie + * @param array $selected_array value selected + * @return void + */ + public function multiselect_categories($htmlname='',$selected_array = array(), $type=0) + { + global $conf,$langs,$user; + $langs->load("dict"); + + $options_array=array(); + + + $sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."categorie"; + $sql.= " WHERE type=".$type; + + dol_syslog(get_class($this)."::multiselectselect_customercategories sql=".$sql); + $resql=$this->db->query($sql); + if ($resql) + { + + $num = $this->db->num_rows($resql); + $i = 0; + if ($num) + { + while ($i < $num) + { + $obj = $this->db->fetch_object($resql); + + $options_array[$obj->rowid]=$obj->label; + + $i++; + } + } + + } + else + { + dol_print_error($this->db); + } + + return $this->advMultiselectarray ( $htmlname, $options_array, $selected_array ); + } + + + public function select_advtargetemailing_template($htmlname='template_id',$selected=0,$showempty=0) { + global $conf, $user, $langs; + + $out = ''; + + $sql = "SELECT c.rowid, c.name, c.fk_mailing"; + $sql .= " FROM " . MAIN_DB_PREFIX . "advtargetemailing as c"; + $sql .= " ORDER BY c.name"; + + dol_syslog ( get_class ( $this ) . "::select_advtargetemailing_template sql=" . $sql, LOG_DEBUG ); + $resql = $this->db->query ( $sql ); + if ($resql) { + + + $out .= ''; + } else { + dol_print_error ( $this->db ); + } + $this->db->free ( $resql ); + return $out; + } +} \ No newline at end of file diff --git a/htdocs/core/lib/emailing.lib.php b/htdocs/core/lib/emailing.lib.php index 7ca2aba9db2..10523bba82b 100644 --- a/htdocs/core/lib/emailing.lib.php +++ b/htdocs/core/lib/emailing.lib.php @@ -38,7 +38,7 @@ function emailing_prepare_head(Mailing $object) $head[$h][1] = $langs->trans("MailCard"); $head[$h][2] = 'card'; $h++; - + if (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! $user->rights->mailing->mailing_advance->recipient) { return $head; } @@ -48,11 +48,16 @@ function emailing_prepare_head(Mailing $object) $head[$h][2] = 'targets'; $h++; + $head[$h][0] = DOL_URL_ROOT."/comm/mailing/advtargetemailing.php?id=".$object->id; + $head[$h][1] = $langs->trans("MailAdvTargetRecipients"); + $head[$h][2] = 'targets'; + $h++; + $head[$h][0] = DOL_URL_ROOT."/comm/mailing/info.php?id=".$object->id; $head[$h][1] = $langs->trans("Info"); $head[$h][2] = 'info'; $h++; - + complete_head_from_modules($conf,$langs,$object,$head,$h,'emailing'); complete_head_from_modules($conf,$langs,$object,$head,$h,'emailing','remove'); diff --git a/htdocs/core/modules/mailings/advthirdparties.modules.php b/htdocs/core/modules/mailings/advthirdparties.modules.php new file mode 100755 index 00000000000..7ef711a435e --- /dev/null +++ b/htdocs/core/modules/mailings/advthirdparties.modules.php @@ -0,0 +1,303 @@ + + * Copyright (C) 2005-2009 Regis Houssin +* +* This file is an example to follow to add your own email selector inside +* the Dolibarr email tool. +* Follow instructions given in README file to know what to change to build +* your own emailing list selector. +* Code that need to be changed in this file are marked by "CHANGE THIS" tag. +*/ + +/** + * \file advtargetingemaling/modules/mailings/advthirdparties.modules.php + * \ingroup advtargetingemaling + * \brief Example file to provide a list of recipients for mailing module + */ + +include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php'; +include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; +include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php'; + + +/** + * Class to manage a list of personalised recipients for mailing feature + */ +class mailing_advthirdparties extends MailingTargets +{ + var $name='ThirdPartyAdvancedTargeting'; + var $desc="Third parties"; + var $require_admin=0; + + var $require_module=array("none"); // This module should not be displayed as Selector in mailling + var $picto='company'; + var $db; + + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + function __construct($db) + { + global $conf; + + $this->db=$db; + } + + + /** + * This is the main function that returns the array of emails + * + * @param int $mailing_id Id of mailing. No need to use it. + * @param array $socid Array of id soc to add + * @param int $type_of_target define in advtargetemailing.class.php + * @param array $contactid Array of contact id to add + * @return int <0 if error, number of emails added if ok + */ + function add_to_target($mailing_id,$socid,$type_of_target, $contactid) + { + global $conf, $langs; + + dol_syslog(get_class($this)."::add_to_target socid=".var_export($socid,true).' contactid='.var_export($contactid,true)); + + $cibles = array(); + + if (($type_of_target==1) || ($type_of_target==3)) { + // Select the third parties from category + if (count($socid)>0) + { + $sql= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact"; + $sql.= " FROM ".MAIN_DB_PREFIX."societe as s LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object=s.rowid"; + $sql.= " WHERE s.entity IN (".getEntity('societe', 1).")"; + $sql.= " AND s.rowid IN (".implode(',',$socid).")"; + $sql.= " ORDER BY email"; + } + + + dol_syslog(get_class($this)."::add_to_target societe sql=".$sql, LOG_DEBUG); + // Stock recipients emails into targets table + $result=$this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + $i = 0; + + dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found", LOG_DEBUG); + + $old = ''; + while ($i < $num) + { + $obj = $this->db->fetch_object($result); + + if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) { + if (!array_key_exists($obj->email, $cibles)) { + $cibles[$obj->email] = array( + 'email' => $obj->email, + 'fk_contact' => $obj->fk_contact, + 'name' => $obj->name, + 'firstname' => $obj->firstname, + 'other' => '', + 'source_url' => $this->url($obj->id,'thirdparty'), + 'source_id' => $obj->id, + 'source_type' => 'thirdparty' + ); + } + } + + $i++; + } + } + else + { + dol_syslog($this->db->error()); + $this->error=$this->db->error(); + return -1; + } + } + + if (($type_of_target==1) || ($type_of_target==2)) { + // Select the third parties from category + if (count($socid)>0 || count($contactid)>0) + { + $sql= "SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname"; + $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as socp"; + $sql.= " WHERE socp.entity IN (".getEntity('societe', 1).")"; + if (count($contactid)>0) { + $sql.= " AND socp.rowid IN (".implode(',',$contactid).")"; + } + if (count($socid)>0) { + $sql.= " AND socp.fk_soc IN (".implode(',',$socid).")"; + } + $sql.= " ORDER BY email"; + } + + + dol_syslog(get_class($this)."::add_to_target contact sql=".$sql); + // Stock recipients emails into targets table + $result=$this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + $i = 0; + + dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found"); + + $old = ''; + while ($i < $num) + { + $obj = $this->db->fetch_object($result); + + if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) { + if (!array_key_exists($obj->email, $cibles)) { + $cibles[$obj->email] = array( + 'email' => $obj->email, + 'fk_contact' =>$obj->id, + 'lastname' => $obj->lastname, + 'firstname' => $obj->firstname, + 'other' => '', + 'source_url' => $this->url($obj->id,'contact'), + 'source_id' => $obj->id, + 'source_type' => 'contact' + ); + } + } + + $i++; + } + } + else + { + dol_syslog($this->db->error()); + $this->error=$this->db->error(); + return -1; + } + } + + + dol_syslog(get_class($this)."::add_to_target mailing cibles=".var_export($cibles,true), LOG_DEBUG); + return parent::add_to_target($mailing_id, $cibles); + } + + + /** + * On the main mailing area, there is a box with statistics. + * If you want to add a line in this report you must provide an + * array of SQL request that returns two field: + * One called "label", One called "nb". + * + * @return array Array with SQL requests + */ + function getSqlArrayForStats() + { + // CHANGE THIS: Optionnal + + //var $statssql=array(); + //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL"; + return array(); + } + + + /** + * Return here number of distinct emails returned by your selector. + * For example if this selector is used to extract 500 different + * emails from a text file, this function must return 500. + * + * @return int Nb of recipients + */ + function getNbOfRecipients() + { + global $conf; + + $sql = "SELECT count(distinct(s.email)) as nb"; + $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; + $sql.= " WHERE s.email != ''"; + $sql.= " AND s.entity IN (".getEntity('societe', 1).")"; + + // La requete doit retourner un champ "nb" pour etre comprise + // par parent::getNbOfRecipients + return parent::getNbOfRecipients($sql); + } + + /** + * This is to add a form filter to provide variant of selector + * If used, the HTML select must be called "filter" + * + * @return string A html select zone + */ + function formFilter() + { + global $conf, $langs; + + $langs->load("companies"); + + $s=''; + $s.=''; + return $s; + + } + + + /** + * Can include an URL link on each record provided by selector shown on target page. + * + * @param int $id ID + * @return string Url link + */ + function url($id,$type) + { + if ($type=='thirdparty') { + $companystatic=new Societe($this->db); + $companystatic->fetch($id); + return $companystatic->getNomUrl(0); + } elseif ($type=='contact') { + $contactstatic=new Contact($this->db); + $contactstatic->fetch($id); + return $contactstatic->getNomUrl(0); + } + } + +} + +?> diff --git a/htdocs/langs/en_US/mails.lang b/htdocs/langs/en_US/mails.lang index a5f620c7a7a..144af2e5563 100644 --- a/htdocs/langs/en_US/mails.lang +++ b/htdocs/langs/en_US/mails.lang @@ -147,3 +147,4 @@ MailSendSetupIs2=You must first go, with an admin account, into menu %sHome - Se MailSendSetupIs3=If you have any questions on how to setup your SMTP server, you can ask to %s. YouCanAlsoUseSupervisorKeyword=You can also add the keyword __SUPERVISOREMAIL__ to have email being sent to the supervisor of user (works only if an email is defined for this supervisor) NbOfTargetedContacts=Current number of targeted contact emails +MailAdvTargetRecipients=Advance recipients targeting From 081c7824d2aceae106ce56524b4fb4e7af78dd2a Mon Sep 17 00:00:00 2001 From: Florian HENRY Date: Fri, 1 Apr 2016 13:59:09 +0200 Subject: [PATCH 02/11] FIX : range is MySQL reserved word, cannot be used as column names --- htdocs/admin/dict.php | 8 ++++---- htdocs/core/class/html.formaccounting.class.php | 8 ++++---- htdocs/install/mysql/migration/3.9.0-4.0.0.sql | 2 +- htdocs/install/mysql/tables/llx_c_accounting_category.sql | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/htdocs/admin/dict.php b/htdocs/admin/dict.php index d88df512b8c..f839ff3e49d 100644 --- a/htdocs/admin/dict.php +++ b/htdocs/admin/dict.php @@ -189,7 +189,7 @@ $tabsql[28]= "SELECT h.rowid as rowid, h.code, h.label, h.affect, h.delay, h.new $tabsql[29]= "SELECT rowid as rowid, code, label, percent, position, active FROM ".MAIN_DB_PREFIX."c_lead_status"; $tabsql[30]= "SELECT rowid, code, name, paper_size, orientation, metric, leftmargin, topmargin, nx, ny, spacex, spacey, width, height, font_size, custom_x, custom_y, active FROM ".MAIN_DB_PREFIX."c_format_cards"; $tabsql[31]= "SELECT s.rowid as rowid, pcg_version, s.fk_pays as country_id, c.code as country_code, c.label as country, s.label, s.active FROM ".MAIN_DB_PREFIX."accounting_system as s, ".MAIN_DB_PREFIX."c_country as c WHERE s.fk_pays=c.rowid and c.active=1"; -$tabsql[32]= "SELECT a.rowid as rowid, a.code as code, a.label, a.range, a.position, a.fk_country as country_id, c.code as country_code, c.label as country, a.active FROM ".MAIN_DB_PREFIX."c_accounting_category as a, ".MAIN_DB_PREFIX."c_country as c WHERE a.fk_country=c.rowid and c.active=1"; +$tabsql[32]= "SELECT a.rowid as rowid, a.code as code, a.label, a.ranges, a.position, a.fk_country as country_id, c.code as country_code, c.label as country, a.active FROM ".MAIN_DB_PREFIX."c_accounting_category as a, ".MAIN_DB_PREFIX."c_country as c WHERE a.fk_country=c.rowid and c.active=1"; $tabsql[33]= "SELECT rowid, pos, code, label, active FROM ".MAIN_DB_PREFIX."c_hrm_department"; $tabsql[34]= "SELECT rowid, pos, code, label, c_level, active FROM ".MAIN_DB_PREFIX."c_hrm_function"; @@ -644,7 +644,7 @@ if (GETPOST('actionadd') || GETPOST('actionmodify')) $ok=0; setEventMessages($langs->transnoentities("ErrorFieldMustBeANumeric",$langs->transnoentities("Code")), null, 'errors'); } - + // Clean some parameters if (isset($_POST["localtax1"]) && empty($_POST["localtax1"])) $_POST["localtax1"]='0'; // If empty, we force to 0 if (isset($_POST["localtax2"]) && empty($_POST["localtax2"])) $_POST["localtax2"]='0'; // If empty, we force to 0 @@ -1064,7 +1064,7 @@ if ($id) } print ''; print ""; - + if ($tabname[$id] == MAIN_DB_PREFIX.'c_email_templates') { print '* '.$langs->trans("AvailableVariables").": "; @@ -1407,7 +1407,7 @@ if ($id) if (in_array($obj->code, array('AC_OTH','AC_OTH_AUTO')) || in_array($obj->type, array('systemauto'))) { $canbedisabled=0; $canbedisabled = 0; } $canbemodified=$iserasable; if ($obj->code == 'RECEP') $canbemodified=1; - + $url = $_SERVER["PHP_SELF"].'?'.($page?'page='.$page.'&':'').'sortfield='.$sortfield.'&sortorder='.$sortorder.'&rowid='.(! empty($obj->rowid)?$obj->rowid:(! empty($obj->code)?$obj->code:'')).'&code='.(! empty($obj->code)?urlencode($obj->code):'').'&id='.$id.'&'; // Favorite diff --git a/htdocs/core/class/html.formaccounting.class.php b/htdocs/core/class/html.formaccounting.class.php index e8664e3166d..37edb6a7239 100644 --- a/htdocs/core/class/html.formaccounting.class.php +++ b/htdocs/core/class/html.formaccounting.class.php @@ -64,7 +64,7 @@ class FormAccounting if (! empty($mysoc->country_id)) { - $sql = "SELECT c.rowid, c.label as type, c.range"; + $sql = "SELECT c.rowid, c.label as type, c.ranges"; $sql.= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c"; $sql.= " WHERE c.active = 1"; $sql.= " AND c.fk_country = ".$mysoc->country_id; @@ -72,14 +72,14 @@ class FormAccounting } else { - $sql = "SELECT c.rowid, c.label as type, c.range"; + $sql = "SELECT c.rowid, c.label as type, c.ranges"; $sql.= " FROM ".MAIN_DB_PREFIX."c_accounting_category as c, ".MAIN_DB_PREFIX."c_country as co"; $sql.= " WHERE c.active = 1 AND c.fk_country = co.rowid"; $sql.= " AND co.code = '".$mysoc->country_code."'"; $sql.= " ORDER BY c.label ASC"; } - dol_syslog("Form::select_accounting_category", LOG_DEBUG); + dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG); $resql=$db->query($sql); if ($resql) { @@ -96,7 +96,7 @@ class FormAccounting print '