2016-04-01 10:52:19 +02:00
< ? php
/* Copyright ( C ) 2005 - 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2009 Regis Houssin < regis . houssin @ inodbox . com >
2016-04-01 10:52:19 +02:00
*
* 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 .
*/
/**
2019-12-15 10:02:20 +01:00
* \file htdocs / core / modules / mailings / advthirdparties . modules . php
* \ingroup mailing
2016-04-01 10:52:19 +02:00
* \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
{
2020-10-31 14:32:18 +01:00
public $name = 'ThirdPartyAdvancedTargeting' ;
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-10-31 14:32:18 +01:00
public $desc = " Third parties " ;
public $require_admin = 0 ;
2016-04-01 10:52:19 +02:00
2020-10-31 14:32:18 +01:00
public $require_module = array ( " none " ); // This module should not be displayed as Selector in mailling
2019-10-30 09:58:19 +01:00
2020-10-31 14:32:18 +01:00
/**
* @ var string String with name of icon for myobject . Must be the part after the 'object_' into object_myobject . png
*/
public $picto = 'company' ;
2018-09-05 20:36:44 +02:00
2018-08-22 11:06:34 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var DoliDB Database handler .
*/
public $db ;
2016-04-01 10:52:19 +02:00
2022-05-28 20:42:29 +02:00
public $enabled = '$conf->societe->enabled' ;
2016-04-01 10:52:19 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2019-02-28 22:07:48 +01:00
public function __construct ( $db )
2016-04-01 10:52:19 +02:00
{
2020-04-10 10:59:32 +02:00
$this -> db = $db ;
2016-04-01 10:52:19 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2016-04-01 10:52:19 +02:00
/**
* 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
2016-05-08 11:33:46 +02:00
* @ param int $type_of_target Defined in advtargetemailing . class . php
2016-04-01 10:52:19 +02:00
* @ param array $contactid Array of contact id to add
* @ return int < 0 if error , number of emails added if ok
*/
2019-02-28 22:07:48 +01:00
public function add_to_target_spec ( $mailing_id , $socid , $type_of_target , $contactid )
2016-04-01 10:52:19 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2016-04-01 10:52:19 +02:00
global $conf , $langs ;
2019-09-09 14:18:25 +02:00
dol_syslog ( get_class ( $this ) . " ::add_to_target_spec socid= " . var_export ( $socid , true ) . ' contactid=' . var_export ( $contactid , true ));
2016-04-01 17:09:03 +02:00
2016-04-01 10:52:19 +02:00
$cibles = array ();
2020-04-10 10:59:32 +02:00
if (( $type_of_target == 1 ) || ( $type_of_target == 3 )) {
2016-04-01 10:52:19 +02:00
// Select the third parties from category
2021-02-23 22:03:23 +01:00
if ( count ( $socid ) > 0 ) {
2020-04-10 10:59:32 +02:00
$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' ) . " ) " ;
2021-03-22 12:47:23 +01:00
$sql .= " AND s.rowid IN ( " . $this -> db -> sanitize ( implode ( ',' , $socid )) . " ) " ;
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY email " ;
2016-04-01 17:09:03 +02:00
2020-10-31 14:32:18 +01:00
// Stock recipients emails into targets table
$result = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $result ) {
2020-10-31 14:32:18 +01:00
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
dol_syslog ( get_class ( $this ) . " ::add_to_target_spec mailing " . $num . " targets found " , LOG_DEBUG );
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2020-10-31 14:32:18 +01:00
$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 ;
}
2016-04-01 10:52:19 +02:00
}
}
2016-04-01 17:09:03 +02:00
2020-04-10 10:59:32 +02:00
if (( $type_of_target == 1 ) || ( $type_of_target == 2 ) || ( $type_of_target == 4 )) {
2016-04-01 10:52:19 +02:00
// Select the third parties from category
2021-02-23 22:03:23 +01:00
if ( count ( $socid ) > 0 || count ( $contactid ) > 0 ) {
2020-04-10 10:59:32 +02:00
$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 " ;
2022-03-18 12:05:59 +01:00
$sql .= " WHERE socp.entity IN ( " . getEntity ( 'contact' ) . " ) " ;
2020-04-10 10:59:32 +02:00
if ( count ( $contactid ) > 0 ) {
2021-03-22 11:30:18 +01:00
$sql .= " AND socp.rowid IN ( " . $this -> db -> sanitize ( implode ( ',' , $contactid )) . " ) " ;
2016-04-01 17:09:03 +02:00
}
2020-04-10 10:59:32 +02:00
if ( count ( $socid ) > 0 ) {
2021-03-22 11:30:18 +01:00
$sql .= " AND socp.fk_soc IN ( " . $this -> db -> sanitize ( implode ( ',' , $socid )) . " ) " ;
2016-04-01 10:52:19 +02:00
}
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY email " ;
2016-06-30 17:10:06 +02:00
2020-10-31 14:32:18 +01:00
// Stock recipients emails into targets table
$result = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $result ) {
2020-10-31 14:32:18 +01:00
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
dol_syslog ( get_class ( $this ) . " ::add_to_target_spec mailing " . $num . " targets found " );
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2020-10-31 14:32:18 +01:00
$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 ;
}
2016-04-01 10:52:19 +02:00
}
}
2016-04-01 17:09:03 +02:00
2019-09-09 14:18:25 +02:00
dol_syslog ( get_class ( $this ) . " ::add_to_target_spec mailing cibles= " . var_export ( $cibles , true ), LOG_DEBUG );
return parent :: addTargetsToDatabase ( $mailing_id , $cibles );
2016-04-01 10:52:19 +02: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 " .
*
* @ return array Array with SQL requests
*/
2019-02-28 22:07:48 +01:00
public function getSqlArrayForStats ()
2016-04-01 10:52:19 +02:00
{
// 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.
*
2022-05-29 09:58:36 +02:00
* @ param string $sql Not use here
* @ return int | string Nb of recipient , or < 0 if error , or '' if NA
2016-04-01 10:52:19 +02:00
*/
2019-02-28 22:07:48 +01:00
public function getNbOfRecipients ( $sql = '' )
2016-04-01 10:52:19 +02:00
{
global $conf ;
$sql = " SELECT count(distinct(s.email)) as nb " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe as s " ;
$sql .= " WHERE s.email != '' " ;
$sql .= " AND s.entity IN ( " . getEntity ( 'societe' ) . " ) " ;
2016-04-01 10:52:19 +02:00
2022-05-29 09:58:36 +02:00
// La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
2016-04-01 10:52:19 +02:00
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
*/
2019-02-28 22:07:48 +01:00
public function formFilter ()
2016-04-01 10:52:19 +02:00
{
global $conf , $langs ;
$langs -> load ( " companies " );
2020-04-10 10:59:32 +02:00
$s = '' ;
$s .= '<select name="filter" class="flat">' ;
2016-04-01 10:52:19 +02:00
// Show categories
$sql = " SELECT rowid, label, type, visible " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " categorie " ;
$sql .= " WHERE type in (1,2) " ; // We keep only categories for suppliers and customers/prospects
2016-04-01 10:52:19 +02:00
// $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
2020-04-10 10:59:32 +02:00
$sql .= " AND entity = " . $conf -> entity ;
$sql .= " ORDER BY label " ;
2016-04-01 10:52:19 +02:00
//print $sql;
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2016-04-01 10:52:19 +02:00
$num = $this -> db -> num_rows ( $resql );
2021-02-23 22:03:23 +01:00
if ( empty ( $conf -> categorie -> enabled )) {
$num = 0 ; // Force empty list if category module is not enabled
}
2016-04-01 10:52:19 +02:00
2021-02-23 22:03:23 +01:00
if ( $num ) {
$s .= '<option value="0"> </option>' ;
} else {
$s .= '<option value="0">' . $langs -> trans ( " ContactsAllShort " ) . '</option>' ;
}
2016-04-01 10:52:19 +02:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2016-04-01 10:52:19 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2020-04-10 10:59:32 +02:00
$type = '' ;
2021-02-23 22:03:23 +01:00
if ( $obj -> type == 1 ) {
$type = $langs -> trans ( " Supplier " );
}
if ( $obj -> type == 2 ) {
$type = $langs -> trans ( " Customer " );
}
2020-04-10 10:59:32 +02:00
$s .= '<option value="' . $obj -> rowid . '">' . dol_trunc ( $obj -> label , 38 , 'middle' );
2021-02-23 22:03:23 +01:00
if ( $type ) {
$s .= ' (' . $type . ')' ;
}
2020-04-10 10:59:32 +02:00
$s .= '</option>' ;
2016-04-01 10:52:19 +02:00
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2016-04-01 10:52:19 +02:00
dol_print_error ( $this -> db );
}
2020-04-10 10:59:32 +02:00
$s .= '</select>' ;
2016-04-01 10:52:19 +02:00
return $s ;
}
/**
* Can include an URL link on each record provided by selector shown on target page .
*
* @ param int $id ID
2016-04-01 17:09:03 +02:00
* @ param string $type type
2016-04-01 10:52:19 +02:00
* @ return string Url link
*/
2019-02-28 22:07:48 +01:00
public function url ( $id , $type )
2016-04-01 10:52:19 +02:00
{
2020-04-10 10:59:32 +02:00
if ( $type == 'thirdparty' ) {
$companystatic = new Societe ( $this -> db );
2016-04-01 10:52:19 +02:00
$companystatic -> fetch ( $id );
2018-03-12 19:02:30 +01:00
return $companystatic -> getNomUrl ( 0 , '' , 0 , 1 );
2020-04-10 10:59:32 +02:00
} elseif ( $type == 'contact' ) {
$contactstatic = new Contact ( $this -> db );
2016-04-01 10:52:19 +02:00
$contactstatic -> fetch ( $id );
2018-05-21 10:58:16 +02:00
return $contactstatic -> getNomUrl ( 0 , '' , 0 , '' , - 1 , 1 );
2016-04-01 10:52:19 +02:00
}
}
}