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 >
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
2011-08-01 01:24:38 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2011-03-24 00:59:21 +01:00
* or see http :// www . gnu . org /
*/
/**
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
{
var $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
var $desc = 'Contacts of thirdparties (prospects, customers, suppliers...)' ;
2011-03-24 00:59:21 +01:00
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' ;
2017-08-25 21:12:22 +02:00
2018-08-22 11:06:34 +02:00
/**
* @ var DoliDB Database handler .
*/
public $db ;
2011-03-24 00:59:21 +01:00
2011-11-13 11:45:25 +01:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2012-03-14 11:49:11 +01:00
function __construct ( $db )
2011-03-24 00:59:21 +01:00
{
2011-11-13 11:45:25 +01:00
$this -> db = $db ;
2011-03-24 00:59:21 +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
*/
2011-03-24 00:59:21 +01:00
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 " ;
2013-04-10 16:29:53 +02:00
$statssql [ 0 ] .= " FROM " . MAIN_DB_PREFIX . " socpeople as c " ;
2018-04-27 10:42:10 +02:00
$statssql [ 0 ] .= " WHERE c.entity IN ( " . getEntity ( 'socpeople' ) . " ) " ;
2011-03-24 00:59:21 +01:00
$statssql [ 0 ] .= " AND c.email != '' " ; // Note that null != '' is false
2012-08-23 00:00:01 +02:00
$statssql [ 0 ] .= " AND c.no_email = 0 " ;
2015-09-30 14:06:45 +02:00
$statssql [ 0 ] .= " AND c.statut = 1 " ;
2011-03-24 00:59:21 +01:00
return $statssql ;
}
/**
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.
*
2015-04-06 12:22:52 +02:00
* @ param string $sql Requete sql de comptage
2012-01-10 10:23:57 +01:00
* @ return int
2011-03-24 00:59:21 +01:00
*/
2013-04-05 09:23:31 +02:00
function getNbOfRecipients ( $sql = '' )
2011-03-24 00:59:21 +01:00
{
global $conf ;
$sql = " SELECT count(distinct(c.email)) as nb " ;
2013-04-10 16:29:53 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as c " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON s.rowid = c.fk_soc " ;
2018-04-27 10:42:10 +02:00
$sql .= " WHERE c.entity IN ( " . getEntity ( 'socpeople' ) . " ) " ;
2013-04-10 16:29:53 +02:00
$sql .= " AND c.email != '' " ; // Note that null != '' is false
$sql .= " AND c.no_email = 0 " ;
2015-09-30 14:06:45 +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 );
}
/**
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
*/
function formFilter ()
{
global $langs ;
2018-09-12 15:09:12 +02:00
// Load translation files required by the page
$langs -> loadLangs ( array ( " commercial " , " companies " , " suppliers " , " categories " ));
2011-03-24 00:59:21 +01: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 " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as sp " ;
2018-04-27 10:42:10 +02:00
$sql .= " WHERE sp.entity IN ( " . getEntity ( 'socpeople' ) . " ) " ;
2017-08-25 21:12:22 +02:00
/* $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 != ''";
2018-04-27 10:42:10 +02:00
//$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
2017-08-25 21:12:22 +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 " ;
$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
{
2017-08-25 21:27:19 +02:00
$s .= '<option value="-1" disabled="disabled">' . $langs -> trans ( " NoContactWithCategoryFound " ) . '</option>' ;
2017-08-25 21:12:22 +02:00
}
}
else dol_print_error ( $this -> db );
$s .= '</select>' ;
$s .= '<br>' ;
2011-09-20 12:30:56 +02:00
// Add prospect of a particular level
2017-08-25 21:12:22 +02:00
$s .= $langs -> trans ( " NatureOfThirdParty " ) . ': ' ;
$s .= '<select name="filter" class="flat">' ;
2011-03-24 00:59:21 +01:00
$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>' ;
2017-08-25 21:12:22 +02:00
$s .= '<option value="prospects">' . $langs -> trans ( " ThirdPartyProspects " ) . '</option>' ;
2011-03-24 00:59:21 +01:00
$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 ++ ;
}
}
2017-08-25 21:12:22 +02:00
else dol_print_error ( $this -> db );
2011-03-24 00:59:21 +01:00
$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>' ;
2017-08-25 21:12:22 +02:00
$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 != ''";
2018-04-27 10:42:10 +02:00
//$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
2017-08-25 21:12:22 +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 " ;
$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 != ''";
2018-04-27 10:42:10 +02:00
//$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
2017-08-25 21:12:22 +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 " ;
$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>' ;
2011-03-24 00:59:21 +01:00
return $s ;
}
/**
2012-01-10 10:23:57 +01:00
* Renvoie url lien vers fiche de la source du destinataire du mailing
*
* @ param int $id ID
* @ return string Url lien
2011-03-24 00:59:21 +01:00
*/
function url ( $id )
{
2014-09-18 21:18:25 +02:00
return '<a href="' . DOL_URL_ROOT . '/contact/card.php?id=' . $id . '">' . img_object ( '' , " contact " ) . '</a>' ;
2011-03-24 00:59:21 +01:00
}
2018-09-06 21:27:18 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2011-03-24 00:59:21 +01:00
/**
2012-01-10 10:23:57 +01:00
* Ajoute destinataires dans table des cibles
*
2018-09-06 21:27:18 +02:00
* @ param int $mailing_id Id of emailing
2012-01-10 10:23:57 +01:00
* @ return int < 0 si erreur , nb ajout si ok
2011-03-24 00:59:21 +01:00
*/
2018-12-11 10:15:13 +01:00
function add_to_target ( $mailing_id )
2011-03-24 00:59:21 +01:00
{
2018-09-06 21:27:18 +02:00
// phpcs:enable
2011-03-24 00:59:21 +01:00
global $conf , $langs ;
2017-08-25 21:12:22 +02: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' );
2011-03-24 00:59:21 +01:00
$cibles = array ();
2011-09-20 12:30:56 +02:00
// List prospects levels
2011-03-24 00:59:21 +01:00
$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 );
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, " ;
2011-03-24 00:59:21 +01:00
$sql .= " s.nom as companyname " ;
2017-08-25 21:12:22 +02:00
$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 " ;
2018-04-27 10:42:10 +02:00
$sql .= " WHERE sp.entity IN ( " . getEntity ( 'socpeople' ) . " ) " ;
2017-08-25 21:12:22 +02:00
$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 ;
2011-03-24 00:59:21 +01:00
{
//print "xx".$key;
2017-08-25 21:12:22 +02:00
if ( $key == 'prospects' ) $sql .= " AND s.client=2 " ;
2011-03-24 00:59:21 +01:00
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 " ;
}
2017-08-25 21:12:22 +02:00
// 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;
2011-03-24 00:59:21 +01:00
// 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 ,
2013-03-09 20:34:38 +01:00
'lastname' => $obj -> lastname ,
2011-03-24 00:59:21 +01:00
'firstname' => $obj -> firstname ,
'other' =>
( $langs -> transnoentities ( " ThirdParty " ) . '=' . $obj -> companyname ) . ';' .
2017-08-25 21:12:22 +02:00
( $langs -> transnoentities ( " UserTitle " ) . '=' . ( $obj -> civility_id ? $langs -> transnoentities ( " Civility " . $obj -> civility_id ) : '' )) . ';' .
( $langs -> transnoentities ( " JobPosition " ) . '=' . $obj -> jobposition ),
'source_url' => $this -> url ( $obj -> id ),
2011-03-24 00:59:21 +01:00
'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 );
}
}