2011-03-24 00:59:21 +01:00
< ? php
/* Copyright ( C ) 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2005 - 2009 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2009 Regis Houssin < regis . houssin @ inodbox . com >
2025-01-05 18:12:36 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2024-11-13 15:27:26 +01:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2011-03-24 00:59:21 +01:00
*
* 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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2011-03-24 00:59:21 +01:00
* ( 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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2011-03-24 00:59:21 +01:00
*/
/**
2011-10-24 14:11:49 +02:00
* \file htdocs / core / modules / mailings / contacts1 . modules . php
2011-03-24 00:59:21 +01:00
* \ingroup mailing
* \brief File of class to offer a selector of emailing targets with Rule 'Poire' .
*/
2011-10-24 14:11:49 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/modules/mailings/modules_mailings.php' ;
2011-03-24 00:59:21 +01:00
/**
2017-08-25 21:12:22 +02:00
* Class to offer a selector of emailing targets from contacts
2011-03-24 00:59:21 +01:00
*/
class mailing_contacts1 extends MailingTargets
{
2020-04-10 10:59:32 +02:00
public $name = 'ContactCompanies' ; // Identifiant du module mailing
2017-01-29 14:53:05 +01:00
// This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
2020-04-10 10:59:32 +02:00
public $desc = 'Contacts of thirdparties (prospects, customers, suppliers...)' ;
public $require_module = array ( " societe " ); // Module mailing actif si modules require_module actifs
public $require_admin = 0 ; // Module mailing actif pour user admin ou non
2019-10-30 09:58:19 +01:00
2024-11-13 15:27:26 +01:00
/**
* @ var string condition to enable module
*/
2022-11-29 01:23:46 +01:00
public $enabled = 'isModEnabled("societe")' ;
2022-05-28 20:42:29 +02:00
2019-10-30 09:48:34 +01:00
/**
* @ var string String with name of icon for myobject . Must be the part after the 'object_' into object_myobject . png
*/
2020-04-10 10:59:32 +02:00
public $picto = 'contact' ;
2017-08-25 21:12:22 +02:00
2011-03-24 00:59:21 +01:00
2020-10-31 14:32:18 +01:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
public function __construct ( $db )
{
$this -> db = $db ;
}
2011-03-24 00:59:21 +01:00
2020-10-31 14:32:18 +01:00
/**
2011-11-13 11:45:25 +01:00
* 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 " .
*
2015-04-07 02:45:30 +02:00
* @ return string [] Array with SQL requests
2011-11-13 11:45:25 +01:00
*/
2020-10-31 14:32:18 +01:00
public function getSqlArrayForStats ()
{
2024-01-11 14:37:32 +01:00
global $langs ;
2011-03-24 00:59:21 +01:00
$langs -> load ( " commercial " );
2020-04-10 10:59:32 +02:00
$statssql = array ();
2020-09-19 22:41:05 +02:00
$statssql [ 0 ] = " SELECT ' " . $this -> db -> escape ( $langs -> trans ( " NbOfCompaniesContacts " )) . " ' as label, " ;
2020-04-10 10:59:32 +02:00
$statssql [ 0 ] .= " count(distinct(c.email)) as nb " ;
$statssql [ 0 ] .= " FROM " . MAIN_DB_PREFIX . " socpeople as c " ;
2022-03-18 12:05:59 +01:00
$statssql [ 0 ] .= " WHERE c.entity IN ( " . getEntity ( 'contact' ) . " ) " ;
2021-10-23 22:27:29 +02:00
$statssql [ 0 ] .= " AND c.email <> '' " ; // Note that null != '' is false
2020-04-10 10:59:32 +02:00
$statssql [ 0 ] .= " AND c.statut = 1 " ;
2011-03-24 00:59:21 +01:00
return $statssql ;
2020-10-31 14:32:18 +01:00
}
2011-03-24 00:59:21 +01:00
/**
2012-01-10 10:23:57 +01:00
* 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.
*
2022-05-29 09:58:36 +02:00
* @ param string $sql Requete sql de comptage
* @ return int | string Nb of recipient , or < 0 if error , or '' if NA
2011-03-24 00:59:21 +01:00
*/
2020-10-31 14:32:18 +01:00
public function getNbOfRecipients ( $sql = '' )
{
2023-11-30 17:45:41 +01:00
global $conf ;
2020-04-10 10:59:32 +02:00
$sql = " SELECT count(distinct(c.email)) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as c " ;
2020-10-31 14:32:18 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON s.rowid = c.fk_soc " ;
2022-03-18 12:05:59 +01:00
$sql .= " WHERE c.entity IN ( " . getEntity ( 'contact' ) . " ) " ;
2021-10-23 22:27:29 +02:00
$sql .= " AND c.email <> '' " ; // Note that null != '' is false
2023-04-08 17:16:22 +02:00
if ( empty ( $this -> evenunsubscribe )) {
$sql .= " AND NOT EXISTS (SELECT rowid FROM " . MAIN_DB_PREFIX . " mailing_unsubscribe as mu WHERE mu.email = c.email and mu.entity = " . (( int ) $conf -> entity ) . " ) " ;
}
2020-01-07 16:13:48 +01:00
// exclude unsubscribed users
2020-04-10 10:59:32 +02:00
$sql .= " AND c.statut = 1 " ;
2011-03-24 00:59:21 +01:00
2017-08-25 21:12:22 +02:00
// The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
2011-03-24 00:59:21 +01:00
return parent :: getNbOfRecipients ( $sql );
2020-10-31 14:32:18 +01:00
}
2011-03-24 00:59:21 +01:00
/**
2012-01-10 10:23:57 +01:00
* Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings
*
* @ return string Retourne zone select
2011-03-24 00:59:21 +01:00
*/
2020-10-31 14:32:18 +01:00
public function formFilter ()
{
2023-01-04 17:04:54 +01:00
global $conf , $langs ;
2018-09-12 15:09:12 +02:00
// Load translation files required by the page
2020-10-31 14:32:18 +01:00
$langs -> loadLangs ( array ( " commercial " , " companies " , " suppliers " , " categories " ));
2011-03-24 00:59:21 +01:00
2020-04-10 10:59:32 +02:00
$s = '' ;
2017-08-25 21:12:22 +02:00
// Add filter on job position
$sql = " SELECT sp.poste, count(distinct(sp.email)) AS nb " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as sp " ;
2022-03-18 12:05:59 +01:00
$sql .= " WHERE sp.entity IN ( " . getEntity ( 'contact' ) . " ) " ;
2021-10-23 22:27:29 +02:00
$sql .= " AND sp.email <> '' " ; // Note that null != '' is false
2021-10-23 22:22:45 +02:00
$sql .= " AND sp.statut = 1 " ;
2021-10-23 22:27:29 +02:00
$sql .= " AND (sp.poste IS NOT NULL AND sp.poste <> '') " ;
2020-04-10 10:59:32 +02:00
$sql .= " GROUP BY sp.poste " ;
$sql .= " ORDER BY sp.poste " ;
2017-08-25 21:12:22 +02:00
$resql = $this -> db -> query ( $sql );
2023-02-10 23:11:47 +01:00
$s .= '<select id="filter_jobposition_contact" name="filter_jobposition" class="flat maxwidth200" placeholder="' . dol_escape_htmltag ( $langs -> trans ( " PostOrFunction " )) . '">' ;
2022-01-31 12:02:48 +01:00
$s .= '<option value="-1">' . $langs -> trans ( " PostOrFunction " ) . '</option>' ;
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-08-25 21:12:22 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-07-31 17:20:48 +02:00
if ( $num > 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 {
$s .= '<option disabled="disabled" value="">' . $langs -> trans ( " None " ) . '</option>' ;
2017-08-25 21:12:22 +02:00
}
2021-02-23 22:03:23 +01:00
} else {
dol_print_error ( $this -> db );
2017-08-25 21:12:22 +02:00
}
2020-04-10 10:59:32 +02:00
$s .= '</select>' ;
2022-01-31 12:02:48 +01:00
$s .= ajax_combobox ( " filter_jobposition_contact " );
2020-04-10 10:59:32 +02:00
$s .= ' ' ;
2017-08-25 21:12:22 +02:00
// Filter on contact category
$sql = " SELECT c.label, count(distinct(sp.email)) AS nb " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " ;
$sql .= " " . MAIN_DB_PREFIX . " socpeople as sp, " ;
$sql .= " " . MAIN_DB_PREFIX . " categorie as c, " ;
$sql .= " " . MAIN_DB_PREFIX . " categorie_contact as cs " ;
2022-03-18 12:05:59 +01:00
$sql .= " WHERE sp.entity IN ( " . getEntity ( 'contact' ) . " ) " ;
2021-10-23 22:27:29 +02:00
$sql .= " AND sp.email <> '' " ; // Note that null != '' is false
2021-10-23 22:22:45 +02:00
$sql .= " AND sp.statut = 1 " ;
2020-04-10 10:59:32 +02:00
$sql .= " AND cs.fk_categorie = c.rowid " ;
$sql .= " AND cs.fk_socpeople = sp.rowid " ;
$sql .= " GROUP BY c.label " ;
$sql .= " ORDER BY c.label " ;
2017-08-25 21:12:22 +02:00
$resql = $this -> db -> query ( $sql );
2023-02-10 23:11:47 +01:00
$s .= '<select id="filter_category_contact" name="filter_category" class="flat maxwidth200">' ;
2022-01-31 12:02:48 +01:00
$s .= '<option value="-1">' . $langs -> trans ( " ContactCategoriesShort " ) . '</option>' ;
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-08-25 21:12:22 +02:00
$num = $this -> db -> num_rows ( $resql );
2021-02-23 22:03:23 +01:00
if ( $num ) {
2017-08-25 21:12:22 +02:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2017-08-25 21:12:22 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2020-04-10 10:59:32 +02:00
$s .= '<option value="' . $obj -> label . '">' . $obj -> label . ' (' . $obj -> nb . ')</option>' ;
2017-08-25 21:12:22 +02:00
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$s .= '<option value="-1" disabled="disabled">' . $langs -> trans ( " NoContactWithCategoryFound " ) . '</option>' ;
2017-08-25 21:12:22 +02:00
}
2021-02-23 22:03:23 +01:00
} else {
dol_print_error ( $this -> db );
2017-08-25 21:12:22 +02:00
}
2020-04-10 10:59:32 +02:00
$s .= '</select>' ;
2022-01-31 12:02:48 +01:00
$s .= ajax_combobox ( " filter_category_contact " );
2020-04-10 10:59:32 +02:00
$s .= '<br>' ;
2017-08-25 21:12:22 +02:00
2011-09-20 12:30:56 +02:00
// Add prospect of a particular level
2023-02-10 23:11:47 +01:00
$s .= '<select id="filter_contact" name="filter" class="flat maxwidth200">' ;
2011-03-24 00:59:21 +01:00
$sql = " SELECT code, label " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_prospectlevel " ;
$sql .= " WHERE active > 0 " ;
$sql .= " ORDER BY label " ;
2011-03-24 00:59:21 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2011-03-24 00:59:21 +01:00
$num = $this -> db -> num_rows ( $resql );
2021-02-23 22:03:23 +01:00
if ( $num ) {
2022-01-31 12:02:48 +01:00
$s .= '<option value="-1">' . $langs -> trans ( " NatureOfThirdParty " ) . '</option>' ;
2021-02-23 22:03:23 +01:00
} else {
2022-01-31 12:02:48 +01:00
$s .= '<option value="-1">' . $langs -> trans ( " ContactsAllShort " ) . '</option>' ;
2021-02-23 22:03:23 +01:00
}
2020-04-10 10:59:32 +02:00
$s .= '<option value="prospects">' . $langs -> trans ( " ThirdPartyProspects " ) . '</option>' ;
2011-03-24 00:59:21 +01:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2011-03-24 00:59:21 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2020-04-10 10:59:32 +02:00
$level = $langs -> trans ( $obj -> code );
2021-02-23 22:03:23 +01:00
if ( $level == $obj -> code ) {
$level = $langs -> trans ( $obj -> label );
}
2023-07-30 15:20:16 +02:00
$labeltoshow = $langs -> trans ( " ThirdPartyProspects " ) . ' <span class="opacitymedium">(' . $langs -> trans ( " ProspectLevelShort " ) . '=' . $level . ')</span>' ;
$s .= '<option value="prospectslevel' . $obj -> code . '" data-html="' . dol_escape_htmltag ( $labeltoshow ) . '">' . $labeltoshow . '</option>' ;
2011-03-24 00:59:21 +01:00
$i ++ ;
}
2021-02-23 22:03:23 +01:00
} else {
dol_print_error ( $this -> db );
2011-03-24 00:59:21 +01:00
}
2020-04-10 10:59:32 +02:00
$s .= '<option value="customers">' . $langs -> trans ( " ThirdPartyCustomers " ) . '</option>' ;
2011-03-24 00:59:21 +01:00
//$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
2020-04-10 10:59:32 +02:00
$s .= '<option value="suppliers">' . $langs -> trans ( " ThirdPartySuppliers " ) . '</option>' ;
$s .= '</select>' ;
2022-01-31 12:02:48 +01:00
$s .= ajax_combobox ( " filter_contact " );
2017-08-25 21:12:22 +02:00
2020-04-10 10:59:32 +02:00
$s .= ' ' ;
2017-08-25 21:12:22 +02:00
// Filter on thirdparty category
$sql = " SELECT c.label, count(distinct(sp.email)) AS nb " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " ;
$sql .= " " . MAIN_DB_PREFIX . " socpeople as sp, " ;
$sql .= " " . MAIN_DB_PREFIX . " categorie as c, " ;
$sql .= " " . MAIN_DB_PREFIX . " categorie_societe as cs " ;
2022-03-18 12:05:59 +01:00
$sql .= " WHERE sp.entity IN ( " . getEntity ( 'contact' ) . " ) " ;
2021-10-23 22:27:29 +02:00
$sql .= " AND sp.email <> '' " ; // Note that null != '' is false
2021-10-23 22:22:45 +02:00
$sql .= " AND sp.statut = 1 " ;
2020-04-10 10:59:32 +02:00
$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 " ;
2017-08-25 21:12:22 +02:00
$resql = $this -> db -> query ( $sql );
2022-01-31 12:02:48 +01:00
$s .= '<select id="filter_category_customer_contact" name="filter_category_customer" class="flat maxwidth200">' ;
$s .= '<option value="-1">' . $langs -> trans ( " CustomersProspectsCategoriesShort " ) . '</option>' ;
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-08-25 21:12:22 +02:00
$num = $this -> db -> num_rows ( $resql );
2021-02-23 22:03:23 +01:00
if ( $num ) {
2017-08-25 21:12:22 +02:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2017-08-25 21:12:22 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2020-04-10 10:59:32 +02:00
$s .= '<option value="' . $obj -> label . '">' . $obj -> label . ' (' . $obj -> nb . ')</option>' ;
2017-08-25 21:12:22 +02:00
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$s .= '<option value="-1" disabled="disabled">' . $langs -> trans ( " NoContactLinkedToThirdpartieWithCategoryFound " ) . '</option>' ;
2017-08-25 21:12:22 +02:00
}
2021-02-23 22:03:23 +01:00
} else {
dol_print_error ( $this -> db );
2017-08-25 21:12:22 +02:00
}
2020-04-10 10:59:32 +02:00
$s .= '</select>' ;
2022-01-31 12:02:48 +01:00
$s .= ajax_combobox ( " filter_category_customer_contact " );
2017-08-25 21:12:22 +02:00
2020-04-10 10:59:32 +02:00
$s .= ' ' ;
2017-08-25 21:12:22 +02:00
// Filter on thirdparty category
$sql = " SELECT c.label, count(distinct(sp.email)) AS nb " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " ;
$sql .= " " . MAIN_DB_PREFIX . " socpeople as sp, " ;
$sql .= " " . MAIN_DB_PREFIX . " categorie as c, " ;
$sql .= " " . MAIN_DB_PREFIX . " categorie_fournisseur as cs " ;
2022-03-18 12:05:59 +01:00
$sql .= " WHERE sp.entity IN ( " . getEntity ( 'contact' ) . " ) " ;
2021-10-23 22:27:29 +02:00
$sql .= " AND sp.email <> '' " ; // Note that null != '' is false
2021-10-23 22:22:45 +02:00
$sql .= " AND sp.statut = 1 " ;
2020-04-10 10:59:32 +02:00
$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 " ;
2017-08-25 21:12:22 +02:00
$resql = $this -> db -> query ( $sql );
2022-01-31 12:02:48 +01:00
$s .= '<select id="filter_category_supplier_contact" name="filter_category_supplier" class="flat maxwidth200">' ;
$s .= '<option value="-1">' . $langs -> trans ( " SuppliersCategoriesShort " ) . '</option>' ;
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2017-08-25 21:12:22 +02:00
$num = $this -> db -> num_rows ( $resql );
2021-02-23 22:03:23 +01:00
if ( $num ) {
2017-08-25 21:12:22 +02:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2017-08-25 21:12:22 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2020-04-10 10:59:32 +02:00
$s .= '<option value="' . $obj -> label . '">' . $obj -> label . ' (' . $obj -> nb . ')</option>' ;
2017-08-25 21:12:22 +02:00
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$s .= '<option value="-1" disabled="disabled">' . $langs -> trans ( " NoContactLinkedToThirdpartieWithCategoryFound " ) . '</option>' ;
2017-08-25 21:12:22 +02:00
}
2021-02-23 22:03:23 +01:00
} else {
dol_print_error ( $this -> db );
2017-08-25 21:12:22 +02:00
}
2020-04-10 10:59:32 +02:00
$s .= '</select>' ;
2017-08-25 21:12:22 +02:00
2022-01-31 12:02:48 +01:00
$s .= ajax_combobox ( " filter_category_supplier_contact " );
2022-04-22 14:49:22 +02:00
2022-04-29 17:56:56 +02:00
// Choose language
2023-01-04 17:04:54 +01:00
if ( getDolGlobalInt ( 'MAIN_MULTILANGS' )) {
require_once DOL_DOCUMENT_ROOT . '/core/class/html.formadmin.class.php' ;
$formadmin = new FormAdmin ( $this -> db );
2023-04-24 01:32:47 +02:00
$s .= img_picto ( $langs -> trans ( " DefaultLang " ), 'language' , 'class="pictofixedwidth"' );
2025-01-05 18:12:36 +01:00
$s .= $formadmin -> select_language ( GETPOST ( 'filter_lang' , 'aZ09' ), 'filter_lang' , 0 , array (), $langs -> trans ( " DefaultLang " ), 0 , 0 , '' , 0 , 0 , 0 , array (), 1 );
2023-01-04 17:04:54 +01:00
}
2022-04-22 14:49:22 +02:00
2011-03-24 00:59:21 +01:00
return $s ;
2020-10-31 14:32:18 +01:00
}
2011-03-24 00:59:21 +01:00
/**
2024-08-07 01:20:43 +02:00
* Provide the URL to the car of the source information of the recipient for the mailing
2012-01-10 10:23:57 +01:00
*
2020-10-31 14:32:18 +01:00
* @ param int $id ID
2024-08-07 01:20:43 +02:00
* @ return string URL link
2011-03-24 00:59:21 +01:00
*/
2020-10-31 14:32:18 +01:00
public function url ( $id )
{
return '<a href="' . DOL_URL_ROOT . '/contact/card.php?id=' . $id . '">' . img_object ( '' , " contact " ) . '</a>' ;
}
2011-03-24 00:59:21 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2011-03-24 00:59:21 +01:00
/**
2023-04-08 17:16:22 +02:00
* Add some recipients into target table
2012-01-10 10:23:57 +01:00
*
2018-09-06 21:27:18 +02:00
* @ param int $mailing_id Id of emailing
2023-12-06 15:46:39 +01:00
* @ return int Return integer < 0 si erreur , nb ajout si ok
2011-03-24 00:59:21 +01:00
*/
2020-10-31 14:32:18 +01:00
public function add_to_target ( $mailing_id )
{
// phpcs:enable
2011-03-24 00:59:21 +01:00
global $conf , $langs ;
2019-01-27 11:55:16 +01:00
$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' );
2022-04-22 14:49:22 +02:00
$filter_lang = GETPOST ( 'filter_lang' , 'alpha' );
2017-08-25 21:12:22 +02:00
2011-03-24 00:59:21 +01:00
$cibles = array ();
2011-09-20 12:30:56 +02:00
// List prospects levels
2020-04-10 10:59:32 +02:00
$prospectlevel = array ();
2011-03-24 00:59:21 +01:00
$sql = " SELECT code, label " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_prospectlevel " ;
$sql .= " WHERE active > 0 " ;
$sql .= " ORDER BY label " ;
2011-03-24 00:59:21 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2011-03-24 00:59:21 +01:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2011-03-24 00:59:21 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2020-04-10 10:59:32 +02:00
$prospectlevel [ $obj -> code ] = $obj -> label ;
2011-03-24 00:59:21 +01:00
$i ++ ;
}
2021-02-23 22:03:23 +01:00
} else {
dol_print_error ( $this -> db );
2011-03-24 00:59:21 +01:00
}
2017-08-25 21:12:22 +02:00
// 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, " ;
2020-04-10 10:59:32 +02:00
$sql .= " s.nom as companyname " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as sp " ;
2020-10-31 14:32:18 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON s.rowid = sp.fk_soc " ;
2022-01-31 12:02:48 +01:00
if ( $filter_category != 'all' && $filter_category != '-1' ) {
2021-02-23 22:03:23 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " categorie as c " ;
$sql .= " , " . MAIN_DB_PREFIX . " categorie_contact as cs " ;
}
2022-01-31 12:02:48 +01:00
if ( $filter_category_customer != 'all' && $filter_category_customer != '-1' ) {
2021-02-23 22:03:23 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " categorie as c2 " ;
$sql .= " , " . MAIN_DB_PREFIX . " categorie_societe as c2s " ;
}
2022-01-31 12:02:48 +01:00
if ( $filter_category_supplier != 'all' && $filter_category_supplier != '-1' ) {
2021-02-23 22:03:23 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " categorie as c3 " ;
$sql .= " , " . MAIN_DB_PREFIX . " categorie_fournisseur as c3s " ;
}
2022-03-18 12:05:59 +01:00
$sql .= " WHERE sp.entity IN ( " . getEntity ( 'contact' ) . " ) " ;
2020-04-10 10:59:32 +02:00
$sql .= " AND sp.email <> '' " ;
2022-11-29 01:23:46 +01:00
2023-04-08 17:16:22 +02:00
if ( empty ( $this -> evenunsubscribe )) {
$sql .= " AND NOT EXISTS (SELECT rowid FROM " . MAIN_DB_PREFIX . " mailing_unsubscribe as mu WHERE mu.email = sp.email and mu.entity = " . (( int ) $conf -> entity ) . " ) " ;
}
2024-01-13 19:48:20 +01:00
// Exclude unsubscribed email addresses
2020-04-10 10:59:32 +02:00
$sql .= " AND sp.statut = 1 " ;
2021-08-23 18:56:46 +02:00
$sql .= " AND sp.email NOT IN (SELECT email FROM " . MAIN_DB_PREFIX . " mailing_cibles WHERE fk_mailing= " . (( int ) $mailing_id ) . " ) " ;
2022-04-29 19:31:05 +02:00
2017-08-25 21:12:22 +02:00
// Filter on category
2022-01-31 12:02:48 +01:00
if ( $filter_category != 'all' && $filter_category != '-1' ) {
2021-02-23 22:03:23 +01:00
$sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid " ;
$sql .= " AND c.label = ' " . $this -> db -> escape ( $filter_category ) . " ' " ;
}
2022-01-31 12:02:48 +01:00
if ( $filter_category_customer != 'all' && $filter_category_customer != '-1' ) {
2021-02-23 22:03:23 +01:00
$sql .= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc " ;
$sql .= " AND c2.label = ' " . $this -> db -> escape ( $filter_category_customer ) . " ' " ;
}
2022-01-31 12:02:48 +01:00
if ( $filter_category_supplier != 'all' && $filter_category_supplier != '-1' ) {
2021-02-23 22:03:23 +01:00
$sql .= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc " ;
$sql .= " AND c3.label = ' " . $this -> db -> escape ( $filter_category_supplier ) . " ' " ;
}
2022-04-29 19:31:05 +02:00
2022-04-29 17:57:55 +02:00
// Filter on language
2023-07-30 15:20:16 +02:00
if ( ! empty ( $filter_lang ) && $filter_lang != '-1' ) {
2022-11-29 01:23:46 +01:00
$sql .= " AND sp.default_lang LIKE ' " . $this -> db -> escape ( $filter_lang ) . " %' " ;
2022-04-29 17:57:55 +02:00
}
2022-04-29 19:31:05 +02:00
2022-04-29 17:57:55 +02:00
// Filter on nature
2017-08-25 21:12:22 +02:00
$key = $filter ;
2022-01-31 12:02:48 +01:00
//print "xx".$key;
2021-02-23 22:03:23 +01:00
if ( $key == 'prospects' ) {
2022-12-26 17:07:54 +01:00
$sql .= " AND s.client = 2 " ;
2021-02-23 22:03:23 +01:00
}
foreach ( $prospectlevel as $codelevel => $valuelevel ) {
if ( $key == 'prospectslevel' . $codelevel ) {
2022-01-31 12:02:48 +01:00
$sql .= " AND s.fk_prospectlevel = ' " . $this -> db -> escape ( $codelevel ) . " ' " ;
2021-02-23 22:03:23 +01:00
}
2011-03-24 00:59:21 +01:00
}
2021-02-23 22:03:23 +01:00
if ( $key == 'customers' ) {
2022-12-26 17:07:54 +01:00
$sql .= " AND s.client = 1 " ;
2021-02-23 22:03:23 +01:00
}
if ( $key == 'suppliers' ) {
2022-12-26 17:07:54 +01:00
$sql .= " AND s.fournisseur = 1 " ;
2011-03-24 00:59:21 +01:00
}
2022-01-31 12:02:48 +01:00
2017-08-25 21:12:22 +02:00
// Filter on job position
$key = $filter_jobposition ;
2022-01-31 12:02:48 +01:00
if ( ! empty ( $key ) && $key != 'all' && $key != '-1' ) {
$sql .= " AND sp.poste = ' " . $this -> db -> escape ( $key ) . " ' " ;
2021-02-23 22:03:23 +01:00
}
2022-01-31 12:02:48 +01:00
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY sp.email " ;
2023-07-30 15:20:16 +02:00
//print "wwwwwwx".$sql;
// Store recipients in target tables
2020-04-10 10:59:32 +02:00
$result = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $result ) {
2011-03-24 00:59:21 +01:00
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
$j = 0 ;
dol_syslog ( get_class ( $this ) . " ::add_to_target mailing " . $num . " targets found " );
$old = '' ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2011-03-24 00:59:21 +01:00
$obj = $this -> db -> fetch_object ( $result );
2023-12-27 12:12:20 +01:00
if ( $old != $obj -> email ) {
2011-03-24 00:59:21 +01:00
$cibles [ $j ] = array (
2021-02-23 22:03:23 +01:00
'email' => $obj -> email ,
2024-11-08 14:43:12 +01:00
'fk_contact' => ( int ) $obj -> fk_contact ,
2021-02-23 22:03:23 +01:00
'lastname' => $obj -> lastname ,
'firstname' => $obj -> firstname ,
'other' =>
( $langs -> transnoentities ( " ThirdParty " ) . '=' . $obj -> companyname ) . ';' .
( $langs -> transnoentities ( " UserTitle " ) . '=' . ( $obj -> civility_id ? $langs -> transnoentities ( " Civility " . $obj -> civility_id ) : '' )) . ';' .
2023-02-11 12:28:23 +01:00
( $langs -> transnoentities ( " PostOrFunction " ) . '=' . $obj -> jobposition ),
2021-02-23 22:03:23 +01:00
'source_url' => $this -> url ( $obj -> id ),
2024-11-08 14:43:12 +01:00
'source_id' => ( int ) $obj -> id ,
2021-02-23 22:03:23 +01:00
'source_type' => 'contact'
2011-03-24 00:59:21 +01:00
);
$old = $obj -> email ;
$j ++ ;
}
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2011-03-24 00:59:21 +01:00
dol_syslog ( $this -> db -> error ());
2020-04-10 10:59:32 +02:00
$this -> error = $this -> db -> error ();
2011-03-24 00:59:21 +01:00
return - 1 ;
}
2023-12-04 12:07:31 +01:00
return parent :: addTargetsToDatabase ( $mailing_id , $cibles );
2020-10-31 14:32:18 +01:00
}
2011-03-24 00:59:21 +01:00
}