From a47a16f281262c8b6315c3471628be42d9119863 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 30 Nov 2011 11:55:33 +0100 Subject: [PATCH] Fix: Select target on supplier category --- .../modules/mailings/thirdparties.modules.php | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/htdocs/core/modules/mailings/thirdparties.modules.php b/htdocs/core/modules/mailings/thirdparties.modules.php index b3d62e8fdbe..131ee661a18 100755 --- a/htdocs/core/modules/mailings/thirdparties.modules.php +++ b/htdocs/core/modules/mailings/thirdparties.modules.php @@ -40,7 +40,7 @@ class mailing_thirdparties extends MailingTargets /** * Constructor * - * @param DoliDB $DB Database handler + * @param DoliDB $db Database handler */ function mailing_thirdparties($db) { @@ -49,10 +49,11 @@ class mailing_thirdparties extends MailingTargets /** - * \brief This is the main function that returns the array of emails - * \param mailing_id Id of mailing. No need to use it. - * \param filterarray If you used the formFilter function. Empty otherwise. - * \return int <0 if error, number of emails added if ok + * 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 $filterarray If you used the formFilter function. Empty otherwise. + * @return int <0 if error, number of emails added if ok */ function add_to_target($mailing_id,$filtersarray=array()) { @@ -60,23 +61,35 @@ class mailing_thirdparties extends MailingTargets $cibles = array(); - // CHANGE THIS // Select the third parties from category - $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname,"; - if ($_POST['filter']) $sql.= " c.label"; - else $sql.=" null as label"; - $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; - if ($_POST['filter']) + if (empty($_POST['filter'])) { - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_societe as cs ON cs.fk_societe = s.rowid"; - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie as c ON c.rowid = cs.fk_categorie"; + $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label"; + $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; + $sql.= " WHERE s.email != ''"; + $sql.= " AND s.entity = ".$conf->entity; } - $sql.= " WHERE s.email != ''"; - $sql.= " AND s.entity = ".$conf->entity; - if ($_POST['filter']) $sql.= " AND c.rowid='".$_POST['filter']."'"; - $sql.= " ORDER BY s.email"; + else + { + $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label"; + $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c"; + $sql.= " WHERE s.email != ''"; + $sql.= " AND s.entity = ".$conf->entity; + $sql.= " AND cs.fk_societe = s.rowid"; + $sql.= " AND c.rowid = cs.fk_categorie"; + $sql.= " AND c.rowid='".$this->db->escape($_POST['filter'])."'"; + $sql.= " UNION "; + $sql.= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label"; + $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c"; + $sql.= " WHERE s.email != ''"; + $sql.= " AND s.entity = ".$conf->entity; + $sql.= " AND cs.fk_societe = s.rowid"; + $sql.= " AND c.rowid = cs.fk_categorie"; + $sql.= " AND c.rowid='".$this->db->escape($_POST['filter'])."'"; + } + $sql.= " ORDER BY email"; - // Stocke destinataires dans cibles + // Stock recipients emails into targets table $result=$this->db->query($sql); if ($result) {