123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- <?php
- /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
- * Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * or see http://www.gnu.org/
- */
- /**
- * \file htdocs/core/modules/mailings/contacts1.modules.php
- * \ingroup mailing
- * \brief File of class to offer a selector of emailing targets with Rule 'Poire'.
- */
- include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
- /**
- * Class to offer a selector of emailing targets from contacts
- */
- class mailing_contacts1 extends MailingTargets
- {
- var $name='ContactCompanies'; // Identifiant du module mailing
- // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
- var $desc='Contacts of thirdparties (prospects, customers, suppliers...)';
- var $require_module=array("societe"); // Module mailing actif si modules require_module actifs
- var $require_admin=0; // Module mailing actif pour user admin ou non
- var $picto='contact';
- /**
- * @var DoliDB Database handler.
- */
- public $db;
- /**
- * Constructor
- *
- * @param DoliDB $db Database handler
- */
- function __construct($db)
- {
- $this->db=$db;
- }
- /**
- * 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 string[] Array with SQL requests
- */
- function getSqlArrayForStats()
- {
- global $conf, $langs;
- $langs->load("commercial");
- $statssql=array();
- $statssql[0] = "SELECT '".$langs->trans("NbOfCompaniesContacts")."' as label,";
- $statssql[0].= " count(distinct(c.email)) as nb";
- $statssql[0].= " FROM ".MAIN_DB_PREFIX."socpeople as c";
- $statssql[0].= " WHERE c.entity IN (".getEntity('socpeople').")";
- $statssql[0].= " AND c.email != ''"; // Note that null != '' is false
- $statssql[0].= " AND c.no_email = 0";
- $statssql[0].= " AND c.statut = 1";
- return $statssql;
- }
- /**
- * 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.
- *
- * @param string $sql Requete sql de comptage
- * @return int
- */
- function getNbOfRecipients($sql='')
- {
- global $conf;
- $sql = "SELECT count(distinct(c.email)) as nb";
- $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c";
- $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc";
- $sql.= " WHERE c.entity IN (".getEntity('socpeople').")";
- $sql.= " AND c.email != ''"; // Note that null != '' is false
- $sql.= " AND c.no_email = 0";
- $sql.= " AND c.statut = 1";
- // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
- return parent::getNbOfRecipients($sql);
- }
- /**
- * Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings
- *
- * @return string Retourne zone select
- */
- function formFilter()
- {
- global $langs;
- // Load translation files required by the page
- $langs->loadLangs(array("commercial","companies","suppliers","categories"));
- $s='';
- // Add filter on job position
- $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
- $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
- $sql.= " WHERE sp.entity IN (".getEntity('socpeople').")";
- /*$sql.= " AND sp.email != ''"; // Note that null != '' is false
- $sql.= " AND sp.no_email = 0";
- $sql.= " AND sp.statut = 1";*/
- $sql.= " AND (sp.poste IS NOT NULL AND sp.poste != '')";
- $sql.= " GROUP BY sp.poste";
- $sql.= " ORDER BY sp.poste";
- $resql = $this->db->query($sql);
- $s.=$langs->trans("PostOrFunction").': ';
- $s.='<select name="filter_jobposition" class="flat">';
- $s.='<option value="all"> </option>';
- if ($resql)
- {
- $num = $this->db->num_rows($resql);
- $i = 0;
- while ($i < $num)
- {
- $obj = $this->db->fetch_object($resql);
- $s.='<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
- $i++;
- }
- }
- else dol_print_error($this->db);
- $s.='</select>';
- $s.=' ';
- // Filter on contact category
- $s .= $langs->trans("ContactCategoriesShort").': ';
- $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
- $sql.= " FROM ";
- $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
- $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
- $sql.= " ".MAIN_DB_PREFIX."categorie_contact as cs";
- $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
- //$sql.= " AND sp.no_email = 0";
- //$sql.= " AND sp.email != ''";
- //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
- $sql.= " AND cs.fk_categorie = c.rowid";
- $sql.= " AND cs.fk_socpeople = sp.rowid";
- $sql.= " GROUP BY c.label";
- $sql.= " ORDER BY c.label";
- $resql = $this->db->query($sql);
- $s.='<select name="filter_category" class="flat">';
- $s.='<option value="all"> </option>';
- if ($resql)
- {
- $num = $this->db->num_rows($resql);
- if ($num)
- {
- $i = 0;
- while ($i < $num)
- {
- $obj = $this->db->fetch_object($resql);
- $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
- $i++;
- }
- }
- else
- {
- $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
- }
- }
- else dol_print_error($this->db);
- $s.='</select>';
- $s.='<br>';
- // Add prospect of a particular level
- $s.=$langs->trans("NatureOfThirdParty").': ';
- $s.='<select name="filter" class="flat">';
- $sql = "SELECT code, label";
- $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
- $sql.= " WHERE active > 0";
- $sql.= " ORDER BY label";
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $num = $this->db->num_rows($resql);
- if ($num) $s.='<option value="all"> </option>';
- else $s.='<option value="all">'.$langs->trans("ContactsAllShort").'</option>';
- $s.='<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
- $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);
- $s.='<option value="prospectslevel'.$obj->code.'">'.$langs->trans("ThirdPartyProspects").' ('.$langs->trans("ProspectLevelShort").'='.$level.')</option>';
- $i++;
- }
- }
- else dol_print_error($this->db);
- $s.='<option value="customers">'.$langs->trans("ThirdPartyCustomers").'</option>';
- //$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
- $s.='<option value="suppliers">'.$langs->trans("ThirdPartySuppliers").'</option>';
- $s.='</select>';
- $s.= ' ';
- // Filter on thirdparty category
- $s .= $langs->trans("CustomersProspectsCategoriesShort").': ';
- $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
- $sql.= " FROM ";
- $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
- $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
- $sql.= " ".MAIN_DB_PREFIX."categorie_societe as cs";
- $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
- //$sql.= " AND sp.no_email = 0";
- //$sql.= " AND sp.email != ''";
- //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
- $sql.= " AND cs.fk_categorie = c.rowid";
- $sql.= " AND cs.fk_soc = sp.fk_soc";
- $sql.= " GROUP BY c.label";
- $sql.= " ORDER BY c.label";
- $resql = $this->db->query($sql);
- $s.='<select name="filter_category_customer" class="flat">';
- $s.='<option value="all"> </option>';
- if ($resql)
- {
- $num = $this->db->num_rows($resql);
- if ($num)
- {
- $i = 0;
- while ($i < $num)
- {
- $obj = $this->db->fetch_object($resql);
- $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
- $i++;
- }
- }
- else
- {
- $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
- }
- }
- else dol_print_error($this->db);
- $s.='</select>';
- $s.= ' ';
- // Filter on thirdparty category
- $s .= $langs->trans("SuppliersCategoriesShort").': ';
- $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
- $sql.= " FROM ";
- $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
- $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
- $sql.= " ".MAIN_DB_PREFIX."categorie_fournisseur as cs";
- $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
- //$sql.= " AND sp.no_email = 0";
- //$sql.= " AND sp.email != ''";
- //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
- $sql.= " AND cs.fk_categorie = c.rowid";
- $sql.= " AND cs.fk_soc = sp.fk_soc";
- $sql.= " GROUP BY c.label";
- $sql.= " ORDER BY c.label";
- $resql = $this->db->query($sql);
- $s.='<select name="filter_category_supplier" class="flat">';
- $s.='<option value="all"> </option>';
- if ($resql)
- {
- $num = $this->db->num_rows($resql);
- if ($num)
- {
- $i = 0;
- while ($i < $num)
- {
- $obj = $this->db->fetch_object($resql);
- $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
- $i++;
- }
- }
- else
- {
- $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
- }
- }
- else dol_print_error($this->db);
- $s.='</select>';
- return $s;
- }
- /**
- * Renvoie url lien vers fiche de la source du destinataire du mailing
- *
- * @param int $id ID
- * @return string Url lien
- */
- function url($id)
- {
- return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('',"contact").'</a>';
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
- /**
- * Ajoute destinataires dans table des cibles
- *
- * @param int $mailing_id Id of emailing
- * @param array $filtersarray Optional filter data (deprecated)
- * @return int <0 si erreur, nb ajout si ok
- */
- function add_to_target($mailing_id,$filtersarray=array())
- {
- // phpcs:enable
- global $conf, $langs;
- $filter = GETPOST('filter','alpha');
- $filter_jobposition = GETPOST('filter_jobposition','alpha');
- $filter_category = GETPOST('filter_category','alpha');
- $filter_category_customer = GETPOST('filter_category_customer','alpha');
- $filter_category_supplier = GETPOST('filter_category_supplier','alpha');
- $cibles = array();
- // List prospects levels
- $prospectlevel=array();
- $sql = "SELECT code, label";
- $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
- $sql.= " WHERE active > 0";
- $sql.= " ORDER BY label";
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $num = $this->db->num_rows($resql);
- $i = 0;
- while ($i < $num)
- {
- $obj = $this->db->fetch_object($resql);
- $prospectlevel[$obj->code]=$obj->label;
- $i++;
- }
- }
- else dol_print_error($this->db);
- // Request must return: id, email, fk_contact, lastname, firstname, other
- $sql = "SELECT sp.rowid as id, sp.email as email, sp.rowid as fk_contact, sp.lastname, sp.firstname, sp.civility as civility_id, sp.poste as jobposition,";
- $sql.= " s.nom as companyname";
- $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
- $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
- if ($filter_category <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c";
- if ($filter_category <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
- if ($filter_category_customer <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c2";
- if ($filter_category_customer <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
- if ($filter_category_supplier <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c3";
- if ($filter_category_supplier <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
- $sql.= " WHERE sp.entity IN (".getEntity('socpeople').")";
- $sql.= " AND sp.email <> ''";
- $sql.= " AND sp.no_email = 0";
- $sql.= " AND sp.statut = 1";
- $sql.= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
- // Filter on category
- if ($filter_category <> 'all') $sql.= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
- if ($filter_category <> 'all') $sql.= " AND c.label = '".$this->db->escape($filter_category)."'";
- if ($filter_category_customer <> 'all') $sql.= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
- if ($filter_category_customer <> 'all') $sql.= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
- if ($filter_category_supplier <> 'all') $sql.= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
- if ($filter_category_supplier <> 'all') $sql.= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
- // Filter on nature
- $key = $filter;
- {
- //print "xx".$key;
- if ($key == 'prospects') $sql.= " AND s.client=2";
- foreach($prospectlevel as $codelevel=>$valuelevel) if ($key == 'prospectslevel'.$codelevel) $sql.= " AND s.fk_prospectlevel='".$codelevel."'";
- if ($key == 'customers') $sql.= " AND s.client=1";
- if ($key == 'suppliers') $sql.= " AND s.fournisseur=1";
- }
- // Filter on job position
- $key = $filter_jobposition;
- if (! empty($key) && $key != 'all') $sql.= " AND sp.poste ='".$this->db->escape($key)."'";
- $sql.= " ORDER BY sp.email";
- //print "wwwwwwx".$sql;
- // Stocke destinataires dans cibles
- $result=$this->db->query($sql);
- if ($result)
- {
- $num = $this->db->num_rows($result);
- $i = 0;
- $j = 0;
- dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
- $old = '';
- while ($i < $num)
- {
- $obj = $this->db->fetch_object($result);
- if ($old <> $obj->email)
- {
- $cibles[$j] = array(
- 'email' => $obj->email,
- 'fk_contact' => $obj->fk_contact,
- 'lastname' => $obj->lastname,
- 'firstname' => $obj->firstname,
- 'other' =>
- ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
- ($langs->transnoentities("UserTitle").'='.($obj->civility_id?$langs->transnoentities("Civility".$obj->civility_id):'')).';'.
- ($langs->transnoentities("JobPosition").'='.$obj->jobposition),
- 'source_url' => $this->url($obj->id),
- 'source_id' => $obj->id,
- 'source_type' => 'contact'
- );
- $old = $obj->email;
- $j++;
- }
- $i++;
- }
- }
- else
- {
- dol_syslog($this->db->error());
- $this->error=$this->db->error();
- return -1;
- }
- return parent::add_to_target($mailing_id, $cibles);
- }
- }
|