2005-02-11 23:41:42 +00:00
< ? php
/* Copyright ( C ) 2003 - 2004 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2008-04-04 11:31:52 +00:00
* Copyright ( C ) 2004 - 2008 Laurent Destailleur < eldy @ users . sourceforge . net >
2005-02-11 23:41:42 +00:00
* Copyright ( C ) 2004 Eric Seigne < eric . seigne @ ryxeo . com >
*
* 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 14:36:08 +00:00
* the Free Software Foundation ; either version 3 of the License , or
2005-02-11 23:41:42 +00: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 19:55:30 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2005-02-11 23:41:42 +00:00
*/
2005-04-06 23:23:48 +00:00
/**
2011-10-24 12:11:49 +00:00
* \file htdocs / core / modules / mailings / modules_mailings . php
2010-07-21 11:57:52 +00:00
* \ingroup mailing
* \brief File with parent class of emailing target selectors modules
*/
2012-08-22 21:11:24 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php' ;
2007-09-13 18:10:51 +00:00
2005-02-11 23:41:42 +00:00
2005-04-06 23:23:48 +00:00
/**
2014-11-29 11:38:36 +00:00
* Parent class of emailing target selectors modules
2010-07-21 11:57:52 +00:00
*/
2018-08-29 20:06:05 +00:00
class MailingTargets // This can't be abstract as it is used for some method
2005-02-11 23:41:42 +00:00
{
2020-10-31 13:32:18 +00:00
/**
* @ var DoliDB Database handler .
*/
public $db ;
2018-09-05 16:49:06 +00:00
2020-10-31 13:32:18 +00:00
/**
2018-08-22 08:37:16 +00:00
* @ var string Error code ( or message )
*/
2020-02-18 22:47:25 +00:00
public $error = '' ;
2018-09-05 16:49:06 +00:00
2020-10-31 13:32:18 +00:00
public $tooltip = '' ;
2018-08-13 15:26:32 +00:00
2009-01-25 22:04:55 +00:00
2020-10-31 13:32:18 +00:00
/**
2011-09-29 16:04:15 +00:00
* Constructor
*
2011-11-13 10:45:25 +00:00
* @ param DoliDB $db Database handler
2011-09-29 16:04:15 +00:00
*/
2020-10-31 13:32:18 +00:00
public function __construct ( $db )
2011-09-29 16:04:15 +00:00
{
2020-10-31 13:32:18 +00:00
$this -> db = $db ;
2011-09-29 16:04:15 +00:00
}
2020-10-31 13:32:18 +00:00
/**
* Return description of email selector
*
* @ return string Return translation of module label . Try translation of $this -> name then translation of 'MailingModuleDesc' . $this -> name , or $this -> desc if not found
*/
public function getDesc ()
{
global $langs , $form ;
2018-08-13 15:26:32 +00:00
2020-10-31 13:32:18 +00:00
$langs -> load ( " mails " );
$transstring = " MailingModuleDesc " . $this -> name ;
$s = '' ;
2017-01-29 13:53:05 +00:00
2021-02-23 21:03:23 +00:00
if ( $langs -> trans ( $this -> name ) != $this -> name ) {
$s = $langs -> trans ( $this -> name );
} elseif ( $langs -> trans ( $transstring ) != $transstring ) {
$s = $langs -> trans ( $transstring );
} else {
$s = $this -> desc ;
}
2016-11-26 11:21:50 +00:00
2021-02-23 21:03:23 +00:00
if ( $this -> tooltip && is_object ( $form )) {
$s .= ' ' . $form -> textwithpicto ( '' , $langs -> trans ( $this -> tooltip ), 1 , 1 );
}
2020-10-31 13:32:18 +00:00
return $s ;
}
2005-02-11 23:41:42 +00:00
2020-10-31 13:32:18 +00:00
/**
2011-09-29 16:04:15 +00:00
* Return number of records for email selector
2020-10-31 13:32:18 +00:00
*
* @ return integer Example
*/
public function getNbOfRecords ()
{
return 0 ;
}
2005-02-12 17:41:13 +00:00
2020-10-31 13:32:18 +00:00
/**
* Retourne nombre de destinataires
*
* @ param string $sql Sql request to count
* @ return int Nb of recipient , or < 0 if error
*/
public function getNbOfRecipients ( $sql )
{
$result = $this -> db -> query ( $sql );
2021-02-23 21:03:23 +00:00
if ( $result ) {
2020-10-31 13:32:18 +00:00
$obj = $this -> db -> fetch_object ( $result );
return $obj -> nb ;
} else {
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2005-02-12 17:41:13 +00:00
2020-10-31 13:32:18 +00:00
/**
* Affiche formulaire de filtre qui apparait dans page de selection
* des destinataires de mailings
*
* @ return string Retourne zone select
*/
public function formFilter ()
{
return '' ;
}
2009-01-25 22:04:55 +00:00
2020-10-31 13:32:18 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Met a jour nombre de destinataires
*
* @ param int $mailing_id Id of emailing
* @ return int < 0 si erreur , nb destinataires si ok
*/
public function update_nb ( $mailing_id )
{
// phpcs:enable
// Mise a jour nombre de destinataire dans table des mailings
$sql = " SELECT COUNT(*) nb FROM " . MAIN_DB_PREFIX . " mailing_cibles " ;
2021-03-30 01:37:54 +00:00
$sql .= " WHERE fk_mailing = " . (( int ) $mailing_id );
2020-10-31 13:32:18 +00:00
$result = $this -> db -> query ( $sql );
2021-02-23 21:03:23 +00:00
if ( $result ) {
2020-10-31 13:32:18 +00:00
$obj = $this -> db -> fetch_object ( $result );
$nb = $obj -> nb ;
2009-01-25 22:04:55 +00:00
2020-10-31 13:32:18 +00:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing " ;
2021-03-30 01:37:54 +00:00
$sql .= " SET nbemail = " . $nb . " WHERE rowid = " . (( int ) $mailing_id );
2021-02-23 21:03:23 +00:00
if ( ! $this -> db -> query ( $sql )) {
2020-10-31 13:32:18 +00:00
dol_syslog ( $this -> db -> error ());
$this -> error = $this -> db -> error ();
return - 1 ;
}
} else {
return - 1 ;
}
return $nb ;
}
2005-02-12 17:41:13 +00:00
2020-10-31 13:32:18 +00:00
/**
* Add a list of targets int the database
*
* @ param int $mailing_id Id of emailing
* @ param array $cibles Array with targets
* @ return int < 0 si erreur , nb ajout si ok
*/
public function addTargetsToDatabase ( $mailing_id , $cibles )
{
global $conf ;
2021-03-05 13:54:17 +00:00
global $dolibarr_main_instance_unique_id ;
2012-05-09 14:45:15 +00:00
2020-10-31 13:32:18 +00:00
$this -> db -> begin ();
2009-01-25 22:04:55 +00:00
2020-10-31 13:32:18 +00:00
// Insert emailing targets from array into database
$j = 0 ;
$num = count ( $cibles );
2021-02-23 21:03:23 +00:00
foreach ( $cibles as $targetarray ) {
if ( ! empty ( $targetarray [ 'email' ])) { // avoid empty email address
2020-10-31 13:32:18 +00:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " mailing_cibles " ;
$sql .= " (fk_mailing, " ;
$sql .= " fk_contact, " ;
$sql .= " lastname, firstname, email, other, source_url, source_id, " ;
2021-02-23 21:03:23 +00:00
$sql .= " tag, " ;
2020-10-31 13:32:18 +00:00
$sql .= " source_type) " ;
$sql .= " VALUES ( " . $mailing_id . " , " ;
$sql .= ( empty ( $targetarray [ 'fk_contact' ]) ? '0' : " ' " . $this -> db -> escape ( $targetarray [ 'fk_contact' ]) . " ' " ) . " , " ;
$sql .= " ' " . $this -> db -> escape ( $targetarray [ 'lastname' ]) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $targetarray [ 'firstname' ]) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $targetarray [ 'email' ]) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $targetarray [ 'other' ]) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $targetarray [ 'source_url' ]) . " ', " ;
$sql .= ( empty ( $targetarray [ 'source_id' ]) ? 'null' : " ' " . $this -> db -> escape ( $targetarray [ 'source_id' ]) . " ' " ) . " , " ;
2021-08-27 23:45:53 +00:00
$sql .= " ' " . $this -> db -> escape ( dol_hash ( $dolibarr_main_instance_unique_id . " ; " . $targetarray [ 'email' ] . " ; " . $targetarray [ 'lastname' ] . " ; " . $mailing_id . " ; " . $conf -> global -> MAILING_EMAIL_UNSUBSCRIBE_KEY , 'md5' )) . " ', " ;
2020-10-31 13:32:18 +00:00
$sql .= " ' " . $this -> db -> escape ( $targetarray [ 'source_type' ]) . " ') " ;
dol_syslog ( __METHOD__ , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:03:23 +00:00
if ( $result ) {
2020-10-31 13:32:18 +00:00
$j ++ ;
} else {
2021-02-23 21:03:23 +00:00
if ( $this -> db -> errno () != 'DB_ERROR_RECORD_ALREADY_EXISTS' ) {
2020-10-31 13:32:18 +00:00
// Si erreur autre que doublon
dol_syslog ( $this -> db -> error () . ' : ' . $targetarray [ 'email' ]);
$this -> error = $this -> db -> error () . ' : ' . $targetarray [ 'email' ];
$this -> db -> rollback ();
return - 1 ;
}
}
}
}
2005-02-12 17:41:13 +00:00
2020-10-31 13:32:18 +00:00
dol_syslog ( __METHOD__ . " : mailing " . $j . " targets added " );
2005-02-12 17:41:13 +00:00
2020-10-31 13:32:18 +00:00
/*
2021-02-23 21:03:23 +00:00
//Update the status to show thirdparty mail that don't want to be contacted anymore'
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing_cibles " ;
$sql .= " SET statut=3 " ;
2021-08-27 16:18:50 +00:00
$sql .= " WHERE fk_mailing = " . (( int ) $mailing_id ) . " AND email in (SELECT email FROM " . MAIN_DB_PREFIX . " societe where fk_stcomm=-1) " ;
2021-02-23 21:03:23 +00:00
$sql .= " AND source_type='thirdparty' " ;
dol_syslog ( __METHOD__ . " : mailing update status to display thirdparty mail that do not want to be contacted " );
$result = $this -> db -> query ( $sql );
2012-03-01 13:51:07 +00:00
2021-02-23 21:03:23 +00:00
//Update the status to show contact mail that don't want to be contacted anymore'
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing_cibles " ;
$sql .= " SET statut=3 " ;
2021-08-27 16:18:50 +00:00
$sql .= " WHERE fk_mailing = " . (( int ) $mailing_id ) . " AND source_type='contact' AND (email in (SELECT sc.email FROM " . MAIN_DB_PREFIX . " socpeople AS sc " ;
2021-02-23 21:03:23 +00:00
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . " societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1)) " ;
dol_syslog ( __METHOD__ . " : mailing update status to display contact mail that do not want to be contacted " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2018-12-11 09:24:57 +00:00
*/
2012-04-04 12:45:48 +00:00
2020-10-31 13:32:18 +00:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " mailing_cibles " ;
$sql .= " SET statut=3 " ;
2021-10-26 11:52:00 +00:00
$sql .= " WHERE fk_mailing = " . (( int ) $mailing_id ) . " AND email IN (SELECT mu.email FROM " . MAIN_DB_PREFIX . " mailing_unsubscribe AS mu WHERE mu.entity IN (' " . getEntity ( 'mailing' ) . " ')) " ;
2018-12-11 09:24:57 +00:00
2020-10-31 13:32:18 +00:00
dol_syslog ( __METHOD__ . " :mailing update status to display emails that do not want to be contacted anymore " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2021-02-23 21:03:23 +00:00
if ( ! $result ) {
2020-10-31 13:32:18 +00:00
dol_print_error ( $this -> db );
}
2012-03-01 13:51:07 +00:00
2020-10-31 13:32:18 +00:00
$this -> update_nb ( $mailing_id );
2005-02-12 17:41:13 +00:00
2020-10-31 13:32:18 +00:00
$this -> db -> commit ();
2018-12-11 09:24:57 +00:00
2020-10-31 13:32:18 +00:00
return $j ;
}
2005-02-12 17:41:13 +00:00
2020-10-31 13:32:18 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Supprime tous les destinataires de la table des cibles
*
* @ param int $mailing_id Id of emailing
* @ return void
*/
public function clear_target ( $mailing_id )
{
// phpcs:enable
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " mailing_cibles " ;
2021-03-30 15:53:25 +00:00
$sql .= " WHERE fk_mailing = " . (( int ) $mailing_id );
2005-02-12 17:41:13 +00:00
2021-02-23 21:03:23 +00:00
if ( ! $this -> db -> query ( $sql )) {
2020-10-31 13:32:18 +00:00
dol_syslog ( $this -> db -> error ());
}
2005-02-12 17:41:13 +00:00
2020-10-31 13:32:18 +00:00
$this -> update_nb ( $mailing_id );
}
2005-02-11 23:41:42 +00:00
}