2005-03-15 09:51:03 +01:00
< ? php
2009-04-28 22:35:01 +02:00
/* Copyright ( C ) 2005 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2009 Regis Houssin < regis . houssin @ capnetworks . com >
2005-03-15 09:51:03 +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-03-15 09:51:03 +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 />.
2005-03-15 09:51:03 +01:00
* or see http :// www . gnu . org /
*/
/**
2011-10-24 14:11:49 +02:00
* \file htdocs / core / modules / mailings / fraise . modules . php
2011-08-27 18:15:06 +02:00
* \ingroup mailing
* \brief File of class to generate target according to rule Fraise
*/
2005-03-15 09:51:03 +01:00
2011-10-24 14:11:49 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/modules/mailings/modules_mailings.php' ;
2010-08-13 00:23:44 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/html.form.class.php' ;
2005-03-15 09:51:03 +01:00
/**
2015-12-03 21:09:09 +01:00
* Class to generate target according to rule Fraise
2010-07-16 09:18:52 +02:00
*/
2005-03-15 09:51:03 +01:00
class mailing_fraise extends MailingTargets
{
2015-12-03 21:09:09 +01:00
var $name = 'FundationMembers' ; // 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
2017-08-25 21:12:22 +02:00
var $desc = 'Foundation members with emails' ;
2017-01-29 14:53:05 +01:00
// Set to 1 if selector is available for admin users only
2010-07-16 09:18:52 +02:00
var $require_admin = 0 ;
var $require_module = array ( 'adherent' );
2005-03-15 09:51:03 +01:00
var $picto = 'user' ;
2009-01-25 23:04:55 +01:00
2005-03-15 09:51:03 +01:00
var $db ;
2015-12-03 21:09:09 +01:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2012-03-14 11:49:11 +01:00
function __construct ( $db )
2005-03-15 09:51:03 +01:00
{
2011-11-13 11:45:25 +01:00
$this -> db = $db ;
2005-03-15 09:51:03 +01:00
}
2009-01-25 23:04:55 +01:00
2006-06-25 17:52:12 +02:00
2011-11-13 11:45:25 +01:00
/**
2015-12-03 21:09:09 +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 " .
*
2016-04-09 15:07:33 +02:00
* @ return string [] Array with SQL requests
2015-12-03 21:09:09 +01:00
*/
2011-11-13 11:45:25 +01:00
function getSqlArrayForStats ()
2015-12-03 21:09:09 +01:00
{
2017-11-28 12:18:01 +01:00
global $langs ;
2011-03-23 17:24:23 +01:00
2006-06-25 17:52:12 +02:00
$langs -> load ( " members " );
2017-11-27 18:12:52 +01:00
2015-12-03 21:09:09 +01:00
// Array for requests for statistics board
$statssql = array ();
2007-10-22 02:29:08 +02:00
2011-02-24 19:36:27 +01:00
$statssql [ 0 ] = " SELECT ' " . $this -> db -> escape ( $langs -> trans ( " FundationMembers " )) . " ' as label, count(*) as nb " ;
2017-11-27 16:07:40 +01:00
$statssql [ 0 ] .= " FROM " . MAIN_DB_PREFIX . " adherent where statut = 1 and entity IN ( " . getEntity ( 'member' ) . " ) " ;
2006-06-25 17:52:12 +02:00
2015-12-03 21:09:09 +01:00
return $statssql ;
}
2006-06-25 17:52:12 +02:00
2013-04-05 09:23:31 +02:00
/**
2015-12-03 21:09:09 +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.
*
* @ param string $sql Requete sql de comptage
* @ return int Nb of recipients
*/
2013-04-05 09:23:31 +02:00
function getNbOfRecipients ( $sql = '' )
2005-03-15 09:51:03 +01:00
{
2005-04-07 01:23:48 +02:00
$sql = " SELECT count(distinct(a.email)) as nb " ;
2005-03-15 09:51:03 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " adherent as a " ;
2017-11-27 17:08:15 +01:00
$sql .= " WHERE (a.email IS NOT NULL AND a.email != '') AND a.entity IN ( " . getEntity ( 'member' ) . " ) " ;
2005-03-15 09:51:03 +01:00
2006-06-25 17:52:12 +02:00
// La requete doit retourner un champ "nb" pour etre comprise
// par parent::getNbOfRecipients
2009-01-25 23:04:55 +01:00
return parent :: getNbOfRecipients ( $sql );
2005-03-15 09:51:03 +01:00
}
2009-01-25 23:04:55 +01:00
2005-09-16 00:44:29 +02: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
2005-09-16 00:44:29 +02:00
*/
function formFilter ()
2005-03-15 09:51:03 +01:00
{
2015-12-03 21:09:09 +01:00
global $conf , $langs ;
2005-11-08 01:21:49 +01:00
$langs -> load ( " members " );
2017-08-25 21:12:22 +02:00
$langs -> load ( " categories " );
$langs -> load ( " companies " );
2009-01-25 23:04:55 +01:00
2010-08-13 00:23:44 +02:00
$form = new Form ( $this -> db );
2005-09-16 00:44:29 +02:00
$s = '' ;
2017-08-25 21:12:22 +02:00
// Status
2010-08-13 00:23:44 +02:00
$s .= $langs -> trans ( " Status " ) . ': ' ;
2005-09-16 00:44:29 +02:00
$s .= '<select name="filter" class="flat">' ;
2010-08-13 00:23:44 +02:00
$s .= '<option value="none"> </option>' ;
2007-10-22 02:29:08 +02:00
$s .= '<option value="-1">' . $langs -> trans ( " MemberStatusDraft " ) . '</option>' ;
2009-08-19 19:16:47 +02:00
$s .= '<option value="1a">' . $langs -> trans ( " MemberStatusActiveShort " ) . ' (' . $langs -> trans ( " MemberStatusPaidShort " ) . ')</option>' ;
2007-10-22 02:29:08 +02:00
$s .= '<option value="1b">' . $langs -> trans ( " MemberStatusActiveShort " ) . ' (' . $langs -> trans ( " MemberStatusActiveLateShort " ) . ')</option>' ;
2007-02-28 00:59:46 +01:00
$s .= '<option value="0">' . $langs -> trans ( " MemberStatusResiliatedShort " ) . '</option>' ;
2015-12-03 21:09:09 +01:00
$s .= '</select> ' ;
$s .= $langs -> trans ( " Type " ) . ': ' ;
2017-08-25 21:12:22 +02:00
$s .= '<select name="filter_type" class="flat">' ;
2015-12-03 21:09:09 +01:00
$sql = " SELECT rowid, libelle, statut " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " adherent_type " ;
2018-02-24 11:13:14 +01:00
$sql .= " WHERE entity IN ( " . getEntity ( 'member_type' ) . " ) " ;
2015-12-03 21:09:09 +01:00
$sql .= " ORDER BY rowid " ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$s .= '<option value="0"> </option>' ;
if ( ! $num ) $s .= '<option value="0" disabled="disabled">' . $langs -> trans ( " NoCategoriesDefined " ) . '</option>' ;
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
$s .= '<option value="' . $obj -> rowid . '">' . dol_trunc ( $obj -> libelle , 38 , 'middle' );
$s .= '</option>' ;
$i ++ ;
}
}
else
{
dol_print_error ( $this -> db );
}
2005-09-16 00:44:29 +02:00
$s .= '</select>' ;
2017-08-25 21:12:22 +02:00
$s .= ' ' ;
$s .= $langs -> trans ( " Category " ) . ': ' ;
$s .= '<select name="filter_category" class="flat">' ;
// Show categories
$sql = " SELECT rowid, label, type, visible " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " categorie " ;
$sql .= " WHERE type = 3 " ; // We keep only categories for members
// $sql.= " AND visible > 0"; // We ignore the property visible because member's categories does not use this property (only products categories use it).
$sql .= " AND entity = " . $conf -> entity ;
$sql .= " ORDER BY label " ;
//print $sql;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$s .= '<option value="0"> </option>' ;
if ( ! $num ) $s .= '<option value="0" disabled>' . $langs -> trans ( " NoCategoriesDefined " ) . '</option>' ;
$i = 0 ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $resql );
$s .= '<option value="' . $obj -> rowid . '">' . dol_trunc ( $obj -> label , 38 , 'middle' );
$s .= '</option>' ;
$i ++ ;
}
}
else
{
dol_print_error ( $this -> db );
}
$s .= '</select>' ;
2010-08-13 00:23:44 +02:00
$s .= '<br>' ;
2011-03-23 17:24:23 +01:00
$s .= $langs -> trans ( " DateEndSubscription " ) . ': ' ;
2016-03-25 15:53:44 +01:00
$s .= $langs -> trans ( " After " ) . ' > ' . $form -> select_date ( - 1 , 'subscriptionafter' , 0 , 0 , 1 , 'fraise' , 1 , 0 , 1 , 0 );
2010-08-13 00:23:44 +02:00
$s .= ' ' ;
2016-03-25 15:53:44 +01:00
$s .= $langs -> trans ( " Before " ) . ' < ' . $form -> select_date ( - 1 , 'subscriptionbefore' , 0 , 0 , 1 , 'fraise' , 1 , 0 , 1 , 0 );
2010-08-13 00:23:44 +02:00
2005-09-16 00:44:29 +02:00
return $s ;
}
2009-01-25 23:04:55 +01:00
2005-09-16 00:44:29 +02:00
/**
2012-01-10 10:23:57 +01:00
* Renvoie url lien vers fiche de la source du destinataire du mailing
*
2015-12-03 21:09:09 +01:00
* @ param int $id ID
2012-01-10 10:23:57 +01:00
* @ return string Url lien
2005-09-16 00:44:29 +02:00
*/
function url ( $id )
{
2014-09-18 21:18:25 +02:00
return '<a href="' . DOL_URL_ROOT . '/adherents/card.php?rowid=' . $id . '">' . img_object ( '' , " user " ) . '</a>' ;
2005-09-16 00:44:29 +02:00
}
2009-01-25 23:04:55 +01:00
2005-09-16 00:44:29 +02:00
/**
2012-01-10 10:23:57 +01:00
* Ajoute destinataires dans table des cibles
*
2015-12-03 21:09:09 +01:00
* @ param int $mailing_id Id of emailing
* @ param array $filtersarray Param to filter sql request . Deprecated . Should use $_POST instead .
* @ return int < 0 si erreur , nb ajout si ok
2005-09-16 00:44:29 +02:00
*/
function add_to_target ( $mailing_id , $filtersarray = array ())
{
2015-04-23 23:21:06 +02:00
// Deprecation warning
if ( $filtersarray ) {
dol_syslog ( __METHOD__ . " : filtersarray parameter is deprecated " , LOG_WARNING );
}
2010-08-13 00:23:44 +02:00
global $langs , $_POST ;
2009-01-25 23:04:55 +01:00
$langs -> load ( " members " );
2010-12-13 19:40:04 +01:00
$langs -> load ( " companies " );
2009-01-25 23:04:55 +01:00
2015-12-03 21:09:09 +01:00
$cibles = array ();
2010-08-13 00:23:44 +02:00
$now = dol_now ();
$dateendsubscriptionafter = dol_mktime ( $_POST [ 'subscriptionafterhour' ], $_POST [ 'subscriptionaftermin' ], $_POST [ 'subscriptionaftersec' ], $_POST [ 'subscriptionaftermonth' ], $_POST [ 'subscriptionafterday' ], $_POST [ 'subscriptionafteryear' ]);
$dateendsubscriptionbefore = dol_mktime ( $_POST [ 'subscriptionbeforehour' ], $_POST [ 'subscriptionbeforemin' ], $_POST [ 'subscriptionbeforesec' ], $_POST [ 'subscriptionbeforemonth' ], $_POST [ 'subscriptionbeforeday' ], $_POST [ 'subscriptionbeforeyear' ]);
2006-06-25 17:52:12 +02:00
2005-09-16 00:44:29 +02:00
// La requete doit retourner: id, email, fk_contact, name, firstname
2009-01-25 23:04:55 +01:00
$sql = " SELECT a.rowid as id, a.email as email, null as fk_contact, " ;
2013-02-23 16:21:49 +01:00
$sql .= " a.lastname, a.firstname, " ;
2015-12-03 21:09:09 +01:00
$sql .= " a.datefin, a.civility as civility_id, a.login, a.societe " ; // Other fields
2017-08-25 21:12:22 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " adherent as a " ;
if ( $_POST [ 'filter_category' ])
{
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " categorie_member as cm ON cm.fk_member = a.rowid " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " categorie as c ON c.rowid = cm.fk_categorie " ;
}
$sql .= " , " . MAIN_DB_PREFIX . " adherent_type as ta " ;
2017-11-29 10:45:16 +01:00
$sql .= " WHERE a.entity IN ( " . getEntity ( 'member' ) . " ) AND a.email <> '' " ; // Note that null != '' is false
2017-12-01 16:25:13 +01:00
$sql .= " AND a.email NOT IN (SELECT email FROM " . MAIN_DB_PREFIX . " mailing_cibles WHERE fk_mailing= " . $this -> db -> escape ( $mailing_id ) . " ) " ;
2017-08-25 21:12:22 +02:00
// Filter on status
2010-08-13 00:23:44 +02:00
if ( isset ( $_POST [ " filter " ]) && $_POST [ " filter " ] == '-1' ) $sql .= " AND a.statut=-1 " ;
2010-12-28 02:34:50 +01:00
if ( isset ( $_POST [ " filter " ]) && $_POST [ " filter " ] == '1a' ) $sql .= " AND a.statut=1 AND a.datefin >= ' " . $this -> db -> idate ( $now ) . " ' " ;
if ( isset ( $_POST [ " filter " ]) && $_POST [ " filter " ] == '1b' ) $sql .= " AND a.statut=1 AND (a.datefin IS NULL or a.datefin < ' " . $this -> db -> idate ( $now ) . " ') " ;
2010-08-13 00:23:44 +02:00
if ( isset ( $_POST [ " filter " ]) && $_POST [ " filter " ] == '0' ) $sql .= " AND a.statut=0 " ;
2017-08-25 21:12:22 +02:00
// Filter on date
2010-08-13 00:23:44 +02:00
if ( $dateendsubscriptionafter > 0 ) $sql .= " AND datefin > ' " . $this -> db -> idate ( $dateendsubscriptionafter ) . " ' " ;
if ( $dateendsubscriptionbefore > 0 ) $sql .= " AND datefin < ' " . $this -> db -> idate ( $dateendsubscriptionbefore ) . " ' " ;
2015-12-03 21:09:09 +01:00
$sql .= " AND a.fk_adherent_type = ta.rowid " ;
2017-08-25 21:12:22 +02:00
// Filter on type
if ( $_POST [ 'filter_type' ]) $sql .= " AND ta.rowid=' " . $_POST [ 'filter_type' ] . " ' " ;
// Filter on category
if ( $_POST [ 'filter_category' ]) $sql .= " AND c.rowid=' " . $_POST [ 'filter_category' ] . " ' " ;
$sql .= " ORDER BY a.email " ;
2010-08-13 00:23:44 +02:00
//print $sql;
2005-03-15 09:51:03 +01:00
2010-08-13 00:23:44 +02:00
// Add targets into table
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::add_to_target " , LOG_DEBUG );
2006-06-25 17:52:12 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
$j = 0 ;
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::add_to_target mailing " . $num . " targets found " );
2006-06-25 17:52:12 +02:00
$old = '' ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $result );
if ( $old <> $obj -> email )
{
$cibles [ $j ] = array (
2015-12-03 21:09:09 +01:00
'email' => $obj -> email ,
'fk_contact' => $obj -> fk_contact ,
'lastname' => $obj -> lastname ,
'firstname' => $obj -> firstname ,
'other' =>
2010-12-13 19:52:19 +01:00
( $langs -> transnoentities ( " Login " ) . '=' . $obj -> login ) . ';' .
2014-05-05 00:23:09 +02:00
( $langs -> transnoentities ( " UserTitle " ) . '=' . ( $obj -> civility_id ? $langs -> transnoentities ( " Civility " . $obj -> civility_id ) : '' )) . ';' .
2010-12-13 19:52:19 +01:00
( $langs -> transnoentities ( " DateEnd " ) . '=' . dol_print_date ( $this -> db -> jdate ( $obj -> datefin ), 'day' )) . ';' .
( $langs -> transnoentities ( " Company " ) . '=' . $obj -> societe ),
2010-12-01 22:51:13 +01:00
'source_url' => $this -> url ( $obj -> id ),
'source_id' => $obj -> id ,
'source_type' => 'member'
2015-12-03 21:09:09 +01:00
);
2006-06-25 17:52:12 +02:00
$old = $obj -> email ;
$j ++ ;
}
$i ++ ;
}
}
else
{
2009-02-20 23:53:15 +01:00
dol_syslog ( $this -> db -> error ());
2006-06-25 17:52:12 +02:00
$this -> error = $this -> db -> error ();
return - 1 ;
}
return parent :: add_to_target ( $mailing_id , $cibles );
2015-12-03 21:09:09 +01:00
}
2005-03-15 09:51:03 +01:00
}