2005-02-12 18:42:01 +01:00
< ? php
2011-03-24 01:35:45 +01:00
/* Copyright ( C ) 2005 - 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2009 Regis Houssin < regis . houssin @ inodbox . com >
2024-08-07 01:20:43 +02:00
* Copyright ( C ) 2024 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 >
2005-02-12 18:42:01 +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
2005-02-12 18:42:01 +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 /
2005-02-12 18:42:01 +01:00
*/
2005-04-07 01:23:48 +02:00
/**
2011-10-24 14:11:49 +02:00
* \file htdocs / core / modules / mailings / pomme . modules . php
2010-07-21 14:16:25 +02:00
* \ingroup mailing
2021-07-31 17:28:43 +02:00
* \brief File of class to offer a selector of emailing targets of users .
2009-05-22 17:45:31 +02:00
*/
2011-10-24 14:11:49 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/modules/mailings/modules_mailings.php' ;
2005-02-12 18:42:01 +01:00
2005-04-07 01:23:48 +02:00
/**
2021-07-31 17:28:43 +02:00
* Class to offer a selector of emailing targets with Rule 'Pomme' .
2009-05-22 17:45:31 +02:00
*/
2005-02-12 18:42:01 +01:00
class mailing_pomme extends MailingTargets
{
2024-11-13 15:27:26 +01:00
/**
* @ var string name of mailing module
*/
public $name = 'DolibarrUsers' ;
/**
* @ var string This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX = name is found
*/
public $desc = 'Dolibarr users with emails' ; // Libelle utilise si aucune traduction pour MailingModuleDescXXX ou XXX=name trouvée
/**
* @ var string [] Module mailing actif si modules require_module actifs
*/
public $require_module = array ();
/**
* @ var int Module mailing actif pour user admin ou non
*/
public $require_admin = 1 ;
2019-10-30 09:58:19 +01: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 = 'user' ;
2005-02-12 18:42:01 +01:00
2011-11-13 11:45:25 +01:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2020-10-31 14:32:18 +01:00
public function __construct ( $db )
2009-05-22 17:45:31 +02:00
{
2020-04-10 10:59:32 +02:00
$this -> db = $db ;
2009-05-22 17:45:31 +02:00
}
2006-06-25 17:52:12 +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 " .
*
2015-03-17 00:21:17 +01: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 ()
2006-06-25 17:52:12 +02:00
{
2009-04-28 22:35:01 +02:00
global $conf , $langs ;
2009-05-22 17:45:31 +02:00
2009-04-28 22:35:01 +02:00
$langs -> load ( " users " );
2009-05-22 17:45:31 +02:00
2020-04-10 10:59:32 +02:00
$statssql = array ();
2020-09-19 22:41:05 +02:00
$sql = " SELECT ' " . $this -> db -> escape ( $langs -> trans ( " DolibarrUsers " )) . " ' as label, " ;
2020-04-10 10:59:32 +02:00
$sql .= " count(distinct(u.email)) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
2024-01-13 19:48:20 +01:00
$sql .= " WHERE u.email != '' " ; // u.email IS NOT NULL est implicit dans ce test
2020-04-10 10:59:32 +02:00
$sql .= " AND u.entity IN (0, " . $conf -> entity . " ) " ;
2023-04-08 17:16:22 +02:00
2020-04-10 10:59:32 +02:00
$statssql [ 0 ] = $sql ;
2006-06-25 17:52:12 +02:00
return $statssql ;
}
2009-01-25 23:04:55 +01:00
2020-10-31 14:32:18 +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 SQL request to use to count
* @ return int | string Nb of recipient , or < 0 if error , or '' if NA
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(u.email)) as nb " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
2024-01-13 19:48:20 +01:00
$sql .= " WHERE u.email != '' " ; // u.email IS NOT NULL est implicit dans ce test
2020-04-10 10:59:32 +02:00
$sql .= " AND u.entity IN (0, " . $conf -> entity . " ) " ;
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 = u.email and mu.entity = " . (( int ) $conf -> entity ) . " ) " ;
}
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 );
}
2009-02-18 21:30:33 +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
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 $langs ;
$langs -> load ( " users " );
2020-04-10 10:59:32 +02:00
$s = '' ;
2023-02-23 13:50:25 +01:00
$s .= '<select id="filter_pomme"" name="filter" class="flat minwidth100">' ;
2022-01-31 12:02:48 +01:00
$s .= '<option value="-1">' . $langs -> trans ( " Status " ) . '</option>' ;
2020-04-10 10:59:32 +02:00
$s .= '<option value="1">' . $langs -> trans ( " Enabled " ) . '</option>' ;
$s .= '<option value="0">' . $langs -> trans ( " Disabled " ) . '</option>' ;
$s .= '</select>' ;
2022-01-31 12:02:48 +01:00
$s .= ajax_combobox ( " filter_pomme " );
2020-04-10 10:59:32 +02:00
$s .= ' ' ;
2023-02-23 13:50:25 +01:00
$s .= '<select id="filteremployee_pomme" name="filteremployee" class="flat minwidth100">' ;
2022-01-31 12:02:48 +01:00
$s .= '<option value="-1">' . $langs -> trans ( " Employee " ) . '</option>' ;
2020-04-10 10:59:32 +02:00
$s .= '<option value="1">' . $langs -> trans ( " Yes " ) . '</option>' ;
$s .= '<option value="0">' . $langs -> trans ( " No " ) . '</option>' ;
$s .= '</select>' ;
2022-01-31 12:02:48 +01:00
$s .= ajax_combobox ( " filteremployee_pomme " );
2018-08-13 17:26:32 +02:00
2009-05-22 17:45:31 +02:00
return $s ;
}
/**
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
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 . '/user/card.php?id=' . $id . '">' . img_object ( '' , " user " ) . '</a>' ;
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
/**
2012-01-10 10:23:57 +01:00
* Ajoute destinataires dans table des cibles
*
* @ 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
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
global $conf , $langs ;
2014-05-17 19:43:22 +02:00
$langs -> load ( " companies " );
2009-05-22 17:45:31 +02:00
$cibles = array ();
2014-05-17 19:43:22 +02:00
// La requete doit retourner: id, email, fk_contact, lastname, firstname
2009-05-22 17:45:31 +02:00
$sql = " SELECT u.rowid as id, u.email as email, null as fk_contact, " ;
2020-04-10 10:59:32 +02:00
$sql .= " u.lastname, u.firstname as firstname, u.civility as civility_id, u.login, u.office_phone " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
2024-01-13 19:48:20 +01:00
$sql .= " WHERE u.email <> '' " ; // u.email IS NOT NULL est implicit dans ce test
2020-04-10 10:59:32 +02:00
$sql .= " AND u.entity IN (0, " . $conf -> entity . " ) " ;
2021-06-09 15:36:47 +02:00
$sql .= " AND u.email NOT IN (SELECT email FROM " . MAIN_DB_PREFIX . " mailing_cibles WHERE fk_mailing= " . (( int ) $mailing_id ) . " ) " ;
2021-02-23 22:03:23 +01:00
if ( GETPOSTISSET ( " filter " ) && GETPOST ( " filter " ) == '1' ) {
$sql .= " AND u.statut=1 " ;
}
if ( GETPOSTISSET ( " filter " ) && GETPOST ( " filter " ) == '0' ) {
$sql .= " AND u.statut=0 " ;
}
2024-01-12 20:40:34 +01:00
if ( GETPOSTISSET ( " filteremployee " ) && GETPOST ( " filteremployee " ) == '1' ) {
2021-02-23 22:03:23 +01:00
$sql .= " AND u.employee=1 " ;
}
if ( GETPOSTISSET ( " filteremployee " ) && GETPOST ( " filteremployee " ) == '0' ) {
$sql .= " AND u.employee=0 " ;
}
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 = u.email and mu.entity = " . (( int ) $conf -> entity ) . " ) " ;
}
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY u.email " ;
2009-05-22 17:45:31 +02:00
// Stocke destinataires dans cibles
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 );
2023-12-27 12:12:20 +01:00
if ( $old != $obj -> email ) {
2009-05-22 17:45:31 +02:00
$cibles [ $j ] = array (
2020-10-31 14:32:18 +01:00
'email' => $obj -> email ,
2024-11-08 14:43:12 +01:00
'fk_contact' => ( int ) $obj -> fk_contact ,
2020-10-31 14:32:18 +01:00
'lastname' => $obj -> lastname ,
'firstname' => $obj -> firstname ,
'other' =>
( $langs -> transnoentities ( " Login " ) . '=' . $obj -> login ) . ';' .
( $langs -> transnoentities ( " UserTitle " ) . '=' . $obj -> civility_id ) . ';' .
( $langs -> transnoentities ( " PhonePro " ) . '=' . $obj -> office_phone ),
'source_url' => $this -> url ( $obj -> id ),
2024-11-08 14:43:12 +01:00
'source_id' => ( int ) $obj -> id ,
2020-10-31 14:32:18 +01:00
'source_type' => 'user'
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 );
2020-10-31 14:32:18 +01:00
}
2005-02-12 18:42:01 +01:00
}