2008-10-16 01:36:30 +02:00
< ? php
2018-06-19 11:46:03 +02:00
/* Copyright ( C ) 2018 - 2018 Andre Schild < a . schild @ aarboard . ch >
* 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 >
2008-10-16 01:36:30 +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 .
*/
/**
2011-10-24 14:11:49 +02:00
* \file htdocs / core / modules / mailings / thirdparties . modules . php
2009-07-29 20:41:39 +02:00
* \ingroup mailing
* \brief Example file to provide a list of recipients for mailing module
2009-05-22 17:45:31 +02:00
*/
2008-10-16 01:36:30 +02:00
2011-10-24 14:11:49 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/modules/mailings/modules_mailings.php' ;
2008-10-16 01:36:30 +02:00
/**
2012-08-27 20:21:53 +02:00
* Class to manage a list of personalised recipients for mailing feature
2009-01-25 23:04:55 +01:00
*/
2011-03-23 17:24:23 +01:00
class mailing_thirdparties extends MailingTargets
2008-10-16 01:36:30 +02:00
{
2020-04-10 10:59:32 +02:00
public $name = 'ThirdPartiesByCategories' ;
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 = " Third parties (by categories) " ;
public $require_admin = 0 ;
2009-05-22 17:45:31 +02:00
2020-04-10 10:59:32 +02:00
public $require_module = array ( " societe " ); // This module allows to select by categories must be also enabled if category module is not activated
2019-10-30 09:58:19 +01:00
2022-05-28 20:42:29 +02:00
public $enabled = '$conf->societe->enabled' ;
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 = 'company' ;
2018-09-04 22:52:55 +02:00
2018-08-22 11:06:34 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var DoliDB Database handler .
*/
public $db ;
2009-05-22 17:45:31 +02:00
2011-11-13 11:45:25 +01:00
/**
* Constructor
*
2011-11-30 11:55:33 +01:00
* @ param DoliDB $db Database handler
2011-11-13 11:45:25 +01:00
*/
2020-10-31 14:32:18 +01:00
public function __construct ( $db )
2009-05-22 17:45:31 +02:00
{
2018-06-19 11:46:03 +02:00
global $conf , $langs ;
2020-10-31 14:32:18 +01:00
$langs -> load ( " companies " );
2012-08-27 20:21:53 +02:00
2020-04-10 10:59:32 +02:00
$this -> db = $db ;
2009-05-22 17:45:31 +02:00
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2009-05-22 17:45:31 +02:00
/**
2011-11-30 11:55:33 +01:00
* This is the main function that returns the array of emails
*
* @ param int $mailing_id Id of mailing . No need to use it .
* @ return int < 0 if error , number of emails added if ok
2009-05-22 17:45:31 +02:00
*/
2020-10-31 14:32:18 +01:00
public function add_to_target ( $mailing_id )
2009-05-22 17:45:31 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2009-05-22 17:45:31 +02:00
global $conf , $langs ;
$cibles = array ();
2020-10-31 14:32:18 +01:00
$addDescription = " " ;
2010-07-16 09:18:52 +02:00
// Select the third parties from category
2022-02-22 23:44:56 +01:00
if ( ! GETPOST ( 'filter' )) {
2020-10-31 14:32:18 +01:00
$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 IN ( " . getEntity ( 'societe' ) . " ) " ;
2021-06-09 15:36:47 +02:00
$sql .= " AND s.email NOT IN (SELECT email FROM " . MAIN_DB_PREFIX . " mailing_cibles WHERE fk_mailing= " . (( int ) $mailing_id ) . " ) " ;
2022-04-29 17:56:56 +02:00
if ( GETPOST ( 'default_lang' , 'alpha' )) {
2022-04-29 19:31:05 +02:00
$sql .= " AND s.default_lang LIKE ' " . $this -> db -> escape ( GETPOST ( 'default_lang' , 'alpha' )) . " %' " ;
2022-04-22 15:05:38 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2020-10-31 14:32:18 +01:00
$addFilter = " " ;
2020-11-30 14:47:07 +01:00
if ( GETPOSTISSET ( " filter_client " ) && GETPOST ( " filter_client " ) <> '-1' ) {
$addFilter .= " AND s.client= " . (( int ) GETPOST ( " filter_client " , 'int' ));
2020-10-31 14:32:18 +01:00
$addDescription = $langs -> trans ( 'ProspectCustomer' ) . " = " ;
2021-03-24 19:53:31 +01:00
if ( GETPOST ( " filter_client " ) == 0 ) {
2020-10-31 14:32:18 +01:00
$addDescription .= $langs -> trans ( 'NorProspectNorCustomer' );
2021-03-24 19:53:31 +01:00
} elseif ( GETPOST ( " filter_client " ) == 1 ) {
2020-10-31 14:32:18 +01:00
$addDescription .= $langs -> trans ( 'Customer' );
2021-03-24 19:53:31 +01:00
} elseif ( GETPOST ( " filter_client " ) == 2 ) {
2020-10-31 14:32:18 +01:00
$addDescription .= $langs -> trans ( 'Prospect' );
2021-03-24 19:53:31 +01:00
} elseif ( GETPOST ( " filter_client " ) == 3 ) {
2020-10-31 14:32:18 +01:00
$addDescription .= $langs -> trans ( 'ProspectCustomer' );
} else {
2020-11-30 14:47:07 +01:00
$addDescription .= " Unknown status " . GETPOST ( " filter_client " );
2020-10-31 14:32:18 +01:00
}
}
2020-11-30 14:47:07 +01:00
if ( GETPOSTISSET ( " filter_status " )) {
if ( strlen ( $addDescription ) > 0 ) {
2020-10-31 14:32:18 +01:00
$addDescription .= " ; " ;
}
$addDescription .= $langs -> trans ( " Status " ) . " = " ;
2020-11-30 14:47:07 +01:00
if ( GETPOST ( " filter_status " ) == '1' ) {
2020-10-31 14:32:18 +01:00
$addFilter .= " AND s.status=1 " ;
$addDescription .= $langs -> trans ( " Enabled " );
} else {
$addFilter .= " AND s.status=0 " ;
$addDescription .= $langs -> trans ( " Disabled " );
}
}
2022-04-29 19:31:05 +02:00
if ( GETPOST ( 'default_lang' , 'alpha' )) {
2022-04-29 17:56:56 +02:00
$addFilter .= " AND s.default_lang LIKE ' " . $this -> db -> escape ( GETPOST ( 'default_lang' , 'alpha' )) . " %' " ;
$addDescription = $langs -> trans ( 'DefaultLang' ) . " = " ;
}
2022-06-15 03:26:27 +02:00
if ( GETPOST ( 'filter_lang_thirdparties' , 'alpha' )) {
$addFilter .= " AND s.default_lang LIKE ' " . $this -> db -> escape ( GETPOST ( 'filter_lang_thirdparties' , 'alpha' )) . " %' " ;
$addDescription = $langs -> trans ( 'DefaultLang' ) . " = " ;
}
2022-04-29 19:31:05 +02:00
2020-10-31 14:32:18 +01:00
$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 IN ( " . getEntity ( 'societe' ) . " ) " ;
2021-06-09 15:36:47 +02:00
$sql .= " AND s.email NOT IN (SELECT email FROM " . MAIN_DB_PREFIX . " mailing_cibles WHERE fk_mailing= " . (( int ) $mailing_id ) . " ) " ;
2020-10-31 14:32:18 +01:00
$sql .= " AND cs.fk_soc = s.rowid " ;
$sql .= " AND c.rowid = cs.fk_categorie " ;
2022-01-31 12:02:48 +01:00
if ( GETPOST ( 'filter' , 'int' ) > 0 ) {
$sql .= " AND c.rowid= " . (( int ) GETPOST ( 'filter' , 'int' ));
}
2020-10-31 14:32:18 +01:00
$sql .= $addFilter ;
$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 IN ( " . getEntity ( 'societe' ) . " ) " ;
2021-06-09 15:36:47 +02:00
$sql .= " AND s.email NOT IN (SELECT email FROM " . MAIN_DB_PREFIX . " mailing_cibles WHERE fk_mailing= " . (( int ) $mailing_id ) . " ) " ;
2020-10-31 14:32:18 +01:00
$sql .= " AND cs.fk_soc = s.rowid " ;
$sql .= " AND c.rowid = cs.fk_categorie " ;
2022-01-31 12:02:48 +01:00
if ( GETPOST ( 'filter' , 'int' ) > 0 ) {
$sql .= " AND c.rowid= " . (( int ) GETPOST ( 'filter' , 'int' ));
}
2020-10-31 14:32:18 +01:00
$sql .= $addFilter ;
}
$sql .= " ORDER BY email " ;
2022-04-22 15:05:38 +02:00
2020-10-31 14:32:18 +01:00
// Stock recipients emails into targets table
2020-04-10 10:59:32 +02:00
$result = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $result ) {
2009-05-22 17:45:31 +02: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 ) {
2009-05-22 17:45:31 +02:00
$obj = $this -> db -> fetch_object ( $result );
2021-02-23 22:03:23 +01:00
if ( $old <> $obj -> email ) {
2020-04-10 10:59:32 +02:00
$otherTxt = ( $obj -> label ? $langs -> transnoentities ( " Category " ) . '=' . $obj -> label : '' );
2021-02-23 22:03:23 +01:00
if ( strlen ( $addDescription ) > 0 && strlen ( $otherTxt ) > 0 ) {
2020-04-10 10:59:32 +02:00
$otherTxt .= " ; " ;
2018-06-19 11:46:03 +02:00
}
2020-04-10 10:59:32 +02:00
$otherTxt .= $addDescription ;
2009-05-22 17:45:31 +02:00
$cibles [ $j ] = array (
2020-10-31 14:32:18 +01:00
'email' => $obj -> email ,
'fk_contact' => $obj -> fk_contact ,
'lastname' => $obj -> name , // For a thirdparty, we must use name
'firstname' => '' , // For a thirdparty, lastname is ''
'other' => $otherTxt ,
'source_url' => $this -> url ( $obj -> id ),
'source_id' => $obj -> id ,
'source_type' => 'thirdparty'
2009-05-22 17:45:31 +02:00
);
$old = $obj -> email ;
$j ++ ;
}
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2009-05-22 17:45:31 +02:00
dol_syslog ( $this -> db -> error ());
2020-04-10 10:59:32 +02:00
$this -> error = $this -> db -> error ();
2009-05-22 17:45:31 +02:00
return - 1 ;
}
2019-09-09 14:18:25 +02:00
return parent :: addTargetsToDatabase ( $mailing_id , $cibles );
2009-05-22 17:45:31 +02: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 " .
*
* @ return array Array with SQL requests
2008-10-16 01:36:30 +02:00
*/
2020-10-31 14:32:18 +01:00
public function getSqlArrayForStats ()
2008-10-16 01:36:30 +02:00
{
2009-05-22 17:45:31 +02:00
// CHANGE THIS: Optionnal
2009-01-25 23:04:55 +01:00
2008-10-16 01:36:30 +02:00
//var $statssql=array();
2009-05-22 17:45:31 +02:00
//$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
2008-10-16 01:36:30 +02:00
return array ();
}
2012-08-27 20:21:53 +02: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
2009-05-22 17:45:31 +02:00
*/
2020-10-31 14:32:18 +01:00
public function getNbOfRecipients ( $sql = '' )
2009-05-22 17:45:31 +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 " ;
2022-01-31 12:02:48 +01:00
$sql .= " WHERE s.email <> '' " ;
2020-04-10 10:59:32 +02:00
$sql .= " AND s.entity IN ( " . getEntity ( 'societe' ) . " ) " ;
2009-05-22 17:45:31 +02:00
2022-05-29 09:58:36 +02:00
// La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
2009-05-22 17:45:31 +02:00
return parent :: getNbOfRecipients ( $sql );
}
/**
2012-01-10 10:23:57 +01:00
* 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
2009-05-22 17:45:31 +02:00
*/
2020-10-31 14:32:18 +01:00
public function formFilter ()
2009-05-22 17:45:31 +02:00
{
global $conf , $langs ;
2010-07-15 10:48:54 +02:00
$langs -> load ( " companies " );
2022-01-31 12:02:48 +01:00
$s = '<select id="filter_thirdparties" name="filter" class="flat">' ;
2009-01-25 23:04:55 +01:00
2011-09-20 12:30:56 +02:00
// Show categories
2010-07-16 08:59:05 +02:00
$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
2010-07-16 08:59:05 +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 " ;
2009-05-22 17:45:31 +02:00
2010-07-16 08:59:05 +02:00
//print $sql;
2009-05-22 17:45:31 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2009-05-22 17:45:31 +02:00
$num = $this -> db -> num_rows ( $resql );
2010-08-13 00:23:44 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $conf -> categorie -> enabled )) {
$num = 0 ; // Force empty list if category module is not enabled
}
2012-08-27 20:21:53 +02:00
2021-02-23 22:03:23 +01:00
if ( $num ) {
2022-01-31 12:02:48 +01:00
$s .= '<option value="-1">' . $langs -> trans ( " Categories " ) . '</option>' ;
2021-02-23 22:03:23 +01:00
} else {
$s .= '<option value="0">' . $langs -> trans ( " ContactsAllShort " ) . '</option>' ;
}
2010-08-13 00:23:44 +02:00
2009-05-22 17:45:31 +02:00
$i = 0 ;
2021-02-23 22:03:23 +01:00
while ( $i < $num ) {
2009-05-22 17:45:31 +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>' ;
2008-10-16 01:36:30 +02:00
$i ++ ;
}
2022-01-31 12:02:48 +01:00
$s .= ajax_combobox ( " filter_thirdparties " );
2020-05-21 15:05:19 +02:00
} else {
2011-12-05 19:03:36 +01:00
dol_print_error ( $this -> db );
2010-07-16 08:59:05 +02:00
}
2008-10-16 01:36:30 +02:00
2020-04-10 10:59:32 +02:00
$s .= '</select> ' ;
2022-01-31 12:02:48 +01:00
$s .= '<select id="filter_client_thirdparties" name="filter_client_thirdparties" class="flat">' ;
$s .= '<option value="-1">' . $langs -> trans ( 'ProspectCustomer' ) . '</option>' ;
2020-10-31 14:32:18 +01:00
if ( empty ( $conf -> global -> SOCIETE_DISABLE_PROSPECTS )) {
$s .= '<option value="2">' . $langs -> trans ( 'Prospect' ) . '</option>' ;
}
if ( empty ( $conf -> global -> SOCIETE_DISABLE_PROSPECTS ) && empty ( $conf -> global -> SOCIETE_DISABLE_CUSTOMERS ) && empty ( $conf -> global -> SOCIETE_DISABLE_PROSPECTSCUSTOMERS )) {
$s .= '<option value="3">' . $langs -> trans ( 'ProspectCustomer' ) . '</option>' ;
}
if ( empty ( $conf -> global -> SOCIETE_DISABLE_CUSTOMERS )) {
$s .= '<option value="1">' . $langs -> trans ( 'Customer' ) . '</option>' ;
}
$s .= '<option value="0">' . $langs -> trans ( 'NorProspectNorCustomer' ) . '</option>' ;
$s .= '</select> ' ;
2022-01-31 12:02:48 +01:00
$s .= ajax_combobox ( " filter_client_thirdparties " );
2020-10-31 14:32:18 +01:00
2022-01-31 12:02:48 +01:00
$s .= ' <select id="filter_status_thirdparties" name="filter_status" class="flat">' ;
$s .= '<option value="-1">' . $langs -> trans ( " Status " ) . '</option>' ;
$s .= '<option value="1">' . $langs -> trans ( " Enabled " ) . '</option>' ;
2020-10-31 14:32:18 +01:00
$s .= '<option value="0">' . $langs -> trans ( " Disabled " ) . '</option>' ;
$s .= '</select>' ;
2022-01-31 12:02:48 +01:00
$s .= ajax_combobox ( " filter_status_thirdparties " );
2022-04-29 19:31:05 +02:00
2023-01-11 21:06:55 +01:00
// Choose language if multilangue active
if ( @ $conf -> global -> MAIN_MULTILANGS == 1 ) {
require_once DOL_DOCUMENT_ROOT . '/core/class/html.formadmin.class.php' ;
$formadmin = new FormAdmin ( $this -> db );
$s .= '<span class="opacitymedium">' . $langs -> trans ( " DefaultLang " ) . ':</span> ' ;
$s .= $formadmin -> select_language ( $langs -> getDefaultLang ( 1 ), 'filter_lang_thirdparties' , 0 , null , 1 , 0 , 0 , '' , 0 , 0 , 0 , null , 1 );
}
2009-05-22 17:45:31 +02:00
return $s ;
}
2020-10-31 14:32:18 +01:00
/**
2012-01-10 10:23:57 +01:00
* Can include an URL link on each record provided by selector shown on target page .
*
2020-10-31 14:32:18 +01:00
* @ param int $id ID
2012-01-10 10:23:57 +01:00
* @ return string Url link
2009-05-22 17:45:31 +02:00
*/
2020-10-31 14:32:18 +01:00
public function url ( $id )
2009-05-22 17:45:31 +02:00
{
2019-01-27 11:55:16 +01:00
return '<a href="' . DOL_URL_ROOT . '/societe/card.php?socid=' . $id . '">' . img_object ( '' , " company " ) . '</a>' ;
2009-05-22 17:45:31 +02:00
}
2008-10-16 01:36:30 +02:00
}