2010-07-16 09:18:52 +02:00
< ? php
/* Copyright ( C ) 2005 - 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:11:07 +01:00
* Copyright ( C ) 2005 - 2009 Regis Houssin < regis . houssin @ capnetworks . com >
2010-07-16 09:18:52 +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 / framboise . modules . php
2010-07-16 09:18:52 +02:00
* \ingroup mailing
* \brief Example file to provide a list of recipients for mailing module
*/
2011-10-24 14:11:49 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/modules/mailings/modules_mailings.php' ;
2010-07-16 09:18:52 +02:00
/**
* \class mailing_framboise
* \brief Class to manage a list of personalised recipients for mailing feature
*/
class mailing_framboise extends MailingTargets
{
// CHANGE THIS: Put here a name not already used
var $name = 'MembersCategories' ;
// CHANGE THIS: Put here a description of your selector module.
// This label is used if no translation found for key MailingModuleDescXXX where XXX=name is found
var $desc = " Foundation members with emails (by categories) " ;
// CHANGE THIS: Set to 1 if selector is available for admin users only
var $require_admin = 0 ;
var $require_module = array ( " adherent " , " categorie " );
var $picto = 'user' ;
var $db ;
2011-09-11 20:35:38 +02:00
/**
* Constructor
*
2011-11-13 11:45:25 +01:00
* @ param DoliDB $db Database handler
2011-09-11 20:35:38 +02:00
*/
2012-03-14 11:49:11 +01:00
function __construct ( $db )
2010-07-16 09:18:52 +02:00
{
2011-11-13 11:45:25 +01:00
$this -> db = $db ;
2010-07-16 09:18:52 +02:00
}
/**
2012-01-05 12:59:24 +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 .
* @ param array $filtersarray If you used the formFilter function . Empty otherwise .
* @ return int < 0 if error , number of emails added if ok
2010-07-16 09:18:52 +02:00
*/
function add_to_target ( $mailing_id , $filtersarray = array ())
{
global $conf , $langs ;
$cibles = array ();
// Select the members from category
2013-02-23 16:21:49 +01:00
$sql = " SELECT a.rowid as id, a.email as email, a.lastname, null as fk_contact, a.firstname, " ;
2011-11-23 21:25:02 +01:00
if ( $_POST [ 'filter' ]) $sql .= " c.label " ;
2010-07-16 09:18:52 +02:00
else $sql .= " null as label " ;
2012-02-01 11:32:55 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " adherent as a " ;
2011-11-23 21:25:02 +01:00
if ( $_POST [ 'filter' ])
{
2012-02-01 11:32:55 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " categorie_member as cm ON cm.fk_member = a.rowid " ;
2011-11-23 21:25:02 +01:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " categorie as c ON c.rowid = cm.fk_categorie " ;
2012-01-05 12:59:24 +01:00
}
2012-02-01 11:32:55 +01:00
$sql .= " WHERE a.email != '' " ;
$sql .= " AND a.entity = " . $conf -> entity ;
2012-01-05 12:59:24 +01:00
if ( $_POST [ 'filter' ]) $sql .= " AND c.rowid=' " . $_POST [ 'filter' ] . " ' " ;
2012-02-01 11:32:55 +01:00
$sql .= " ORDER BY a.email " ;
2010-07-16 09:18:52 +02:00
// Stocke destinataires dans cibles
$result = $this -> db -> query ( $sql );
if ( $result )
{
$num = $this -> db -> num_rows ( $result );
$i = 0 ;
$j = 0 ;
dol_syslog ( get_class ( $this ) . " ::add_to_target mailing " . $num . " targets found " );
$old = '' ;
while ( $i < $num )
{
$obj = $this -> db -> fetch_object ( $result );
if ( $old <> $obj -> email )
{
$cibles [ $j ] = array (
'email' => $obj -> email ,
'fk_contact' => $obj -> fk_contact ,
2013-03-09 20:34:38 +01:00
'lastname' => $obj -> lastname ,
2010-07-16 09:18:52 +02:00
'firstname' => $obj -> firstname ,
'other' => ( $obj -> label ? $langs -> transnoentities ( " Category " ) . '=' . $obj -> label : '' ),
2010-12-01 22:51:13 +01:00
'source_url' => $this -> url ( $obj -> id ),
'source_id' => $obj -> id ,
'source_type' => 'member'
2010-07-16 09:18:52 +02:00
);
$old = $obj -> email ;
$j ++ ;
}
$i ++ ;
}
}
else
{
dol_syslog ( $this -> db -> error ());
$this -> error = $this -> db -> error ();
return - 1 ;
}
return parent :: add_to_target ( $mailing_id , $cibles );
}
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
2010-07-16 09:18:52 +02:00
*/
function getSqlArrayForStats ()
{
// 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 ();
}
2012-01-05 12:59:24 +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.
*
2013-04-05 09:23:31 +02:00
* @ param string $sql Requete sql de comptage
2012-01-05 12:59:24 +01:00
* @ return int Nb of recipients
2010-07-16 09:18:52 +02:00
*/
2013-04-05 09:23:31 +02:00
function getNbOfRecipients ( $sql = '' )
2010-07-16 09:18:52 +02:00
{
global $conf ;
2012-02-01 11:32:55 +01:00
$sql = " SELECT count(distinct(a.email)) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " adherent as a " ;
$sql .= " WHERE a.email != '' " ;
$sql .= " AND a.entity = " . $conf -> entity ;
2010-07-16 09:18:52 +02:00
// La requete doit retourner un champ "nb" pour etre comprise
// par parent::getNbOfRecipients
return parent :: getNbOfRecipients ( $sql );
}
/**
2012-01-05 12:59:24 +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
2010-07-16 09:18:52 +02:00
*/
function formFilter ()
{
global $conf , $langs ;
$langs -> load ( " companies " );
2010-12-13 19:40:04 +01:00
$langs -> load ( " categories " );
2010-07-16 09:18:52 +02:00
$s = '' ;
$s .= '<select name="filter" class="flat">' ;
2011-09-20 12:30:56 +02:00
// Show categories
2010-07-16 09:18:52 +02:00
$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 );
2010-08-13 00:23:44 +02:00
2010-12-13 19:40:04 +01:00
$s .= '<option value="0"> </option>' ;
if ( ! $num ) $s .= '<option value="0" disabled="disabled">' . $langs -> trans ( " NoCategoriesDefined " ) . '</option>' ;
2010-08-13 00:23:44 +02:00
2010-07-16 09:18:52 +02:00
$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
{
2011-12-05 19:03:36 +01:00
dol_print_error ( $this -> db );
2010-07-16 09:18:52 +02:00
}
$s .= '</select>' ;
return $s ;
}
/**
2012-01-05 12:59:24 +01:00
* Can include an URL link on each record provided by selector shown on target page .
*
* @ param int $id Id of member
* @ return string Url link
2010-07-16 09:18:52 +02:00
*/
function url ( $id )
{
//$companystatic=new Societe($this->db);
//$companystatic->id=$id;
//$companystatic->nom='';
//return $companystatic->getNomUrl(1); // Url too long
return '<a href="' . DOL_URL_ROOT . '/adherents/fiche.php?rowid=' . $id . '">' . img_object ( '' , " user " ) . '</a>' ;
}
}
?>