diff --git a/htdocs/comm/mailing/cibles.php b/htdocs/comm/mailing/cibles.php index 4d28e04f5d8..628ae813822 100644 --- a/htdocs/comm/mailing/cibles.php +++ b/htdocs/comm/mailing/cibles.php @@ -237,7 +237,9 @@ if ($mil->fetch($_GET["id"]) == 0) print ''.($obj->getNbOfRecipients()).''; print ''; - print $obj->formFilter(); + $filter=$obj->formFilter(); + if ($filter) print $filter; + else print $langs->trans("None"); print ''; print ''; diff --git a/htdocs/comm/mailing/fiche.php b/htdocs/comm/mailing/fiche.php index c81fe94259e..2fe90274417 100644 --- a/htdocs/comm/mailing/fiche.php +++ b/htdocs/comm/mailing/fiche.php @@ -307,8 +307,8 @@ else print ''.$langs->trans("Ref").''.$mil->id.''; print ''.$langs->trans("MailTitle").''.$mil->titre.''; print ''.$langs->trans("MailFrom").''.htmlentities($mil->email_from).''; + print ''.$langs->trans("Status").''.$mil->getLibStatut(4).''; print ''.$langs->trans("TotalNbOfDistinctRecipients").''.($mil->nbemail?$mil->nbemail:''.$langs->trans("NoTargetYet").'').''; - print ''.$langs->trans("Status").''.$mil->statuts[$mil->statut].''; $uc = new User($db, $mil->user_creat); $uc->fetch(); diff --git a/htdocs/comm/mailing/index.php b/htdocs/comm/mailing/index.php index 6bba2fea1c5..6bcafd6d79c 100644 --- a/htdocs/comm/mailing/index.php +++ b/htdocs/comm/mailing/index.php @@ -81,6 +81,7 @@ while (($file = readdir($handle))!==false) if (eregi("(.*)\.(.*)\.(.*)",$file,$reg)) { $modulename=$reg[1]; + if ($modulename == 'example') continue; // Chargement de la classe $file = $dir."/".$modulename.".modules.php"; @@ -104,7 +105,7 @@ while (($file = readdir($handle))!==false) { $var = !$var; - foreach ($mailmodule->statssql as $sql) + foreach ($mailmodule->getSqlArrayForStats() as $sql) { print ''; @@ -157,7 +158,7 @@ if ($result) print ''; print ''.$langs->trans("LastMailings",10).''; print ''.$langs->trans("NbOfEMails").''; - print ''.$langs->trans("Status").''; + print ''.$langs->trans("Status").''; $num = $db->num_rows($result); if ($num > 0) @@ -174,8 +175,8 @@ if ($result) print ''.img_object($langs->trans("ShowEMail"),"email").' '.$obj->rowid.''; print ''.$obj->titre.''; print ''.($obj->nbemail?$obj->nbemail:"0").''; - $mail=new Mailing($db); - print ''.$mail->statuts[$obj->statut].''; + $mailstatic=new Mailing($db); + print ''.$mailstatic->LibStatut($obj->statut,5).''; print ''; $i++; } diff --git a/htdocs/comm/mailing/mailing.class.php b/htdocs/comm/mailing/mailing.class.php index 01b4149e1c7..4bed9c460d1 100644 --- a/htdocs/comm/mailing/mailing.class.php +++ b/htdocs/comm/mailing/mailing.class.php @@ -281,7 +281,68 @@ class Mailing $this->db->query($sql); return 1; } - + + + /** + * \brief Retourne le libellé du statut d'un mailing (brouillon, validée, ...) + * \param mode 0=libellé long, 1=libellé court, 2=Picto + Libellé court, 3=Picto, 4=Picto + Libellé long + * \return string Libelle + */ + function getLibStatut($mode=0) + { + return $this->LibStatut($this->statut,$mode); + } + + /** + * \brief Renvoi le libellé d'un statut donné + * \param statut Id statut + * \param mode 0=libellé long, 1=libellé court, 2=Picto + Libellé court, 3=Picto, 4=Picto + Libellé long, 5=Libellé court + Picto + * \return string Libellé du statut + */ + function LibStatut($statut,$mode=0) + { + global $langs; + $langs->load('mails'); + + if ($mode == 0) + { + return $this->statuts[$statut]; + } + if ($mode == 1) + { + return $this->statuts[$statut]; + } + if ($mode == 2) + { + if ($statut == 0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$this->statuts[$statut]; + if ($statut == 1) return img_picto($langs->trans($this->statuts[$statut]),'statut1').' '.$this->statuts[$statut]; + if ($statut == 2) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$this->statuts[$statut]; + if ($statut == 3) return img_picto($langs->trans($this->statuts[$statut]),'statut6').' '.$this->statuts[$statut]; + } + if ($mode == 3) + { + if ($statut == 0) return img_picto($langs->trans($this->statuts[$statut]),'statut0'); + if ($statut == 1) return img_picto($langs->trans($this->statuts[$statut]),'statut1'); + if ($statut == 2) return img_picto($langs->trans($this->statuts[$statut]),'statut3'); + if ($statut == 3) return img_picto($langs->trans($this->statuts[$statut]),'statut6'); + } + if ($mode == 4) + { + if ($statut == 0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$this->statuts[$statut]; + if ($statut == 1) return img_picto($langs->trans($this->statuts[$statut]),'statut1').' '.$this->statuts[$statut]; + if ($statut == 2) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$this->statuts[$statut]; + if ($statut == 3) return img_picto($langs->trans($this->statuts[$statut]),'statut6').' '.$this->statuts[$statut]; + } + if ($mode == 5) + { + if ($statut == 0) return $this->statuts[$statut].' '.img_picto($langs->trans($this->statuts[$statut]),'statut0'); + if ($statut == 1) return $this->statuts[$statut].' '.img_picto($langs->trans($this->statuts[$statut]),'statut1'); + if ($statut == 2) return $this->statuts[$statut].' '.img_picto($langs->trans($this->statuts[$statut]),'statut3'); + if ($statut == 3) return $this->statuts[$statut].' '.img_picto($langs->trans($this->statuts[$statut]),'statut6'); + } + + } + } ?> diff --git a/htdocs/includes/modules/mailings/README b/htdocs/includes/modules/mailings/README new file mode 100644 index 00000000000..fffa52834ce --- /dev/null +++ b/htdocs/includes/modules/mailings/README @@ -0,0 +1,47 @@ + +If you want to add your own emails selector/import module. This is steps to follow +to add you own email predefined selector: + + +***** STEP 1 ***** + +Copy file +htdocs/includes/mailings/example.modules.php +into +htdocs/includes/mailings/myselector.modules.php + +You can choose value of your choice instead of "myselector" in name +of new file. + + +***** STEP 2 ***** + +Edit this file myselector.modules.php and change following text: + +"class mailing_example" into "class mailing_myselector" +"var $name='example';" into var $name='myselector'; +"var $desc='...';" into var $desc='A text to describe selector'; +"function mailing_example" into "function mailing_myselector" + +Then add code inside the "add_to_target" function. What you must do is simply +fill the $target PHP array with one record for each email your selector must return. + +You can choose the way you get data: From a file, a database. You are free, +the only requirement is that the $target array is filled by records that are +themselves array of: ('email', 'name', 'firstname'). + +This is for example how you can fill the $target array: + +// ----- Your code start here ----- +$target[0]=array('email'=>'email_0','name'=>'name_0','firstname'=>'firstname_0'); +... +$target[n]=array('email'=>'email_n','name'=>'name_n','firstname'=>'firstname_n'); +// ----- Your code end here ----- + + + +***** STEP 3 ***** + +Once this file has been edited, you can go to the Dolibarr emailing feature, +you will see a new line selector in the "target" editor of an emailing. + diff --git a/htdocs/includes/modules/mailings/example.modules.php b/htdocs/includes/modules/mailings/example.modules.php new file mode 100644 index 00000000000..a8d47a11076 --- /dev/null +++ b/htdocs/includes/modules/mailings/example.modules.php @@ -0,0 +1,122 @@ + + * + * 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. + */ + +include_once DOL_DOCUMENT_ROOT.'/includes/modules/mailings/modules_mailings.php'; + + +// CHANGE THIS: Class name must be called mailing_xxx with xxx=name of your selector +class mailing_example extends MailingTargets +{ + // CHANGE THIS: Put here a name not already used + var $name='example'; + // CHANGE THIS: Put here a description of your selector module + var $desc='Put here a description'; + // CHANGE THIS: Set to 1 if selector is available for admin users only + var $require_admin=0; + + var $require_module=array(); + var $picto=''; + var $db; + + + // CHANGE THIS: Constructor name must be called mailing_xxx with xxx=name of your selector + function mailing_example($DB) + { + $this->db=$DB; + } + + + /** + * \brief This is the main function that returns the array of emails + * \param mailing_id Id of mailing. No need to use it. + * \param filterarray If you used the formFilter function. Empty otherwise. + * \return int <0 if error, number of emails added if ok + */ + function add_to_target($mailing_id,$filtersarray=array()) + { + $target = array(); + + // CHANGE THIS + // ----- Your code start here ----- + + // You must fill the $target array with record like this + // $target[0]=array('email'=>'email_0','name'=>'name_0','firstname'=>'firstname_0'); + // ... + // $target[n]=array('email'=>'email_n','name'=>'name_n','firstname'=>'firstname_n'); + + // Example: $target[0]=array('email'=>'myemail@mydomain.com','name'=>'Doe','firstname'=>'John'); + + // ----- Your code end here ----- + + return parent::add_to_target($mailing_id, $target); + } + + + /** + * \brief 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 + */ + 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(); + } + + + /* + * \brief 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. + * \return int + */ + function getNbOfRecipients() + { + // CHANGE THIS: Optionnal + + // Example: return parent::getNbOfRecipients("SELECT count(*) as nb from dolibarr_table"); + // Example: return 500; + return '?'; + } + + /** + * \brief 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 + */ + function formFilter() + { + // CHANGE THIS: Optionnal + + $s=''; + return $s; + } + + + /** + * \brief Can include an URL link on each record provided by selector + * shown on target page. + * \return string Url link + */ + function url($id) + { + // CHANGE THIS: Optionnal + + return ''; + } + +} + +?> diff --git a/htdocs/includes/modules/mailings/fraise.modules.php b/htdocs/includes/modules/mailings/fraise.modules.php index b70aad63c0f..43f76f637b5 100644 --- a/htdocs/includes/modules/mailings/fraise.modules.php +++ b/htdocs/includes/modules/mailings/fraise.modules.php @@ -59,30 +59,44 @@ class mailing_fraise extends MailingTargets var $picto='user'; var $db; - var $statssql=array(); function mailing_fraise($DB) { - global $langs; - $langs->load("members"); - $this->db=$DB; - - // Liste des tableaux des stats espace mailing - $this->statssql[0]="SELECT '".addslashes($langs->trans("FundationMembers"))."' as label, count(*) as nb FROM ".MAIN_DB_PREFIX."adherent where statut = 1"; } + + function getSqlArrayForStats() + { + global $langs; + $langs->load("members"); + + $statssql=array(); + $statssql[0]="SELECT '".addslashes($langs->trans("FundationMembers"))."' as label, count(*) as nb FROM ".MAIN_DB_PREFIX."adherent where statut = 1"; + + return $statssql; + } + + + /* + * \brief 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. + * \return int + */ function getNbOfRecipients() { - // La requete doit retourner: nb $sql = "SELECT count(distinct(a.email)) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a"; $sql .= " WHERE a.email IS NOT NULL"; + // La requete doit retourner un champ "nb" pour etre comprise + // par parent::getNbOfRecipients return parent::getNbOfRecipients($sql); } + /** * \brief Affiche formulaire de filtre qui apparait dans page de selection * des destinataires de mailings @@ -121,6 +135,8 @@ class mailing_fraise extends MailingTargets */ function add_to_target($mailing_id,$filtersarray=array()) { + $cibles = array(); + // La requete doit retourner: id, email, fk_contact, name, firstname $sql = "SELECT a.rowid as id, a.email as email, null as fk_contact, a.nom as name, a.prenom as firstname"; $sql.= " FROM ".MAIN_DB_PREFIX."adherent as a"; @@ -133,8 +149,45 @@ class mailing_fraise extends MailingTargets } $sql.= " ORDER BY a.email"; - return parent::add_to_target($mailing_id, $sql); - } + // Stocke destinataires dans cibles + $result=$this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + $i = 0; + $j = 0; + + dolibarr_syslog("fraisemodules.php: mailing $num cibles trouvées"); + + $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, + 'name' => $obj->name, + 'firstname' => $obj->firstname, + 'url' => $this->url($obj->id) + ); + $old = $obj->email; + $j++; + } + + $i++; + } + } + else + { + dolibarr_syslog($this->db->error()); + $this->error=$this->db->error(); + return -1; + } + + return parent::add_to_target($mailing_id, $cibles); + } } diff --git a/htdocs/includes/modules/mailings/modules_mailings.php b/htdocs/includes/modules/mailings/modules_mailings.php index f968852ab70..a267023067d 100644 --- a/htdocs/includes/modules/mailings/modules_mailings.php +++ b/htdocs/includes/modules/mailings/modules_mailings.php @@ -132,41 +132,8 @@ class MailingTargets * \param sql Requete sql de selection des destinataires * \return int < 0 si erreur, nb ajout si ok */ - function add_to_target($mailing_id, $sql) + function add_to_target($mailing_id, $cibles) { - $cibles = array(); - - // Stocke destinataires dans cibles - $result=$this->db->query($sql); - if ($result) - { - $num = $this->db->num_rows($result); - $i = 0; - $j = 0; - - dolibarr_syslog("mailing-prepare: mailing $num cibles trouvées"); - - $old = ''; - while ($i < $num) - { - $obj = $this->db->fetch_object($result); - if ($old <> $obj->email) - { - $cibles[$j] = array($obj->email,$obj->fk_contact,$obj->name,$obj->firstname,$this->url($obj->id)); - $old = $obj->email; - $j++; - } - - $i++; - } - } - else - { - dolibarr_syslog($this->db->error()); - $this->error=$this->db->error(); - return -1; - } - $this->db->begin(); // Insère destinataires de cibles dans table @@ -176,14 +143,14 @@ class MailingTargets { $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles"; $sql .= " (fk_mailing, "; - if ($cibles[$i][1]) $sql .= "fk_contact, "; + if ($cibles[$i]['fk_contact']) $sql .= "fk_contact, "; $sql .= "nom, prenom, email, url)"; $sql .= " VALUES (".$mailing_id.","; - if ($cibles[$i][1]) $sql .= $cibles[$i][1] .","; - $sql .= "'".addslashes($cibles[$i][2])."',"; - $sql .= "'".addslashes($cibles[$i][3])."',"; - $sql .= "'".addslashes($cibles[$i][0])."',"; - $sql .= "'".addslashes($cibles[$i][4])."')"; + if ($cibles[$i]['fk_contact']) $sql .= $cibles[$i]['fk_contact'] .","; + $sql .= "'".addslashes($cibles[$i]['name'])."',"; + $sql .= "'".addslashes($cibles[$i]['firstname'])."',"; + $sql .= "'".addslashes($cibles[$i]['email'])."',"; + $sql .= "'".addslashes($cibles[$i]['url'])."')"; $result=$this->db->query($sql); if ($result) diff --git a/htdocs/includes/modules/mailings/poire.modules.php b/htdocs/includes/modules/mailings/poire.modules.php index c3b45b2d77a..4e7f9dd74b6 100644 --- a/htdocs/includes/modules/mailings/poire.modules.php +++ b/htdocs/includes/modules/mailings/poire.modules.php @@ -53,40 +53,52 @@ include_once DOL_DOCUMENT_ROOT.'/includes/modules/mailings/modules_mailings.php' class mailing_poire extends MailingTargets { var $name='ContactCompanies'; // Identifiant du module mailing - var $desc='Contacts avec emails des sociétés'; // Libellé utilisé si aucune traduction pour MailingModuleDescXXX ou XXX=name trouvée + var $desc='Contacts des sociétés'; // Libellé utilisé si aucune traduction pour MailingModuleDescXXX ou XXX=name trouvée var $require_module=array("commercial"); // Module mailing actif si modules require_module actifs var $require_admin=0; // Module mailing actif pour user admin ou non var $picto='contact'; var $db; - var $statssql=array(); function mailing_poire($DB) { + $this->db=$DB; + } + + + function getSqlArrayForStats() + { global $langs; $langs->load("commercial"); - $this->db=$DB; + $statssql=array(); + $statssql[0]="SELECT '".$langs->trans("NbOfCompaniesContacts")."' as label, count(distinct(c.email)) as nb FROM ".MAIN_DB_PREFIX."socpeople as c, ".MAIN_DB_PREFIX."societe as s WHERE s.idp = c.fk_soc AND s.client = 1 AND c.email != ''"; + + return $statssql; + } - // Liste des tableaux des stats espace mailing - //$this->statssql[0]="SELECT '".$langs->trans("Customers")."' as label, count(*) as nb FROM ".MAIN_DB_PREFIX."societe WHERE client = 1"; - $this->statssql[0]="SELECT '".$langs->trans("NbOfCompaniesContacts")."' as label, count(distinct(c.email)) as nb FROM ".MAIN_DB_PREFIX."socpeople as c, ".MAIN_DB_PREFIX."societe as s WHERE s.idp = c.fk_soc AND s.client = 1 AND c.email != ''"; - } + /* + * \brief 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. + * \return int + */ function getNbOfRecipients() { - // La requete doit retourner: nb $sql = "SELECT count(distinct(c.email)) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c"; $sql .= ", ".MAIN_DB_PREFIX."societe as s"; $sql .= " WHERE s.idp = c.fk_soc"; - $sql .= " AND s.client = 1"; $sql .= " AND c.email != ''"; + // La requete doit retourner un champ "nb" pour etre comprise + // par parent::getNbOfRecipients return parent::getNbOfRecipients($sql); } + /** * \brief Affiche formulaire de filtre qui apparait dans page de selection * des destinataires de mailings @@ -102,9 +114,10 @@ class mailing_poire extends MailingTargets $s=''; $s.=''; return $s; } @@ -124,10 +137,12 @@ class mailing_poire extends MailingTargets * \brief Ajoute destinataires dans table des cibles * \param mailing_id Id du mailing concerné * \param filterarray Requete sql de selection des destinataires - * \return int < 0 si erreur, nb ajout si ok + * \return int <0 si erreur, nb ajout si ok */ function add_to_target($mailing_id,$filtersarray=array()) { + $cibles = array(); + // La requete doit retourner: id, email, fk_contact, name, firstname $sql = "SELECT c.idp as id, c.email as email, c.idp as fk_contact, c.name as name, c.firstname as firstname"; $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c"; @@ -142,7 +157,45 @@ class mailing_poire extends MailingTargets } $sql .= " ORDER BY c.email"; - return parent::add_to_target($mailing_id, $sql); + + // Stocke destinataires dans cibles + $result=$this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + $i = 0; + $j = 0; + + dolibarr_syslog("poire.modules.php: mailing $num cibles trouvées"); + + $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, + 'name' => $obj->name, + 'firstname' => $obj->firstname, + 'url' => $this->url($obj->id) + ); + $old = $obj->email; + $j++; + } + + $i++; + } + } + else + { + dolibarr_syslog($this->db->error()); + $this->error=$this->db->error(); + return -1; + } + + return parent::add_to_target($mailing_id, $cibles); } } diff --git a/htdocs/includes/modules/mailings/pomme.modules.php b/htdocs/includes/modules/mailings/pomme.modules.php index 2d0a6dc3b1d..613ec4492f7 100644 --- a/htdocs/includes/modules/mailings/pomme.modules.php +++ b/htdocs/includes/modules/mailings/pomme.modules.php @@ -58,30 +58,42 @@ class mailing_pomme extends MailingTargets var $picto='user'; var $db; - var $statssql=array(); function mailing_pomme($DB) { + $this->db=$DB; + } + + + function getSqlArrayForStats() + { global $langs; $langs->load("users"); - - $this->db=$DB; - // Liste des tableaux des stats espace mailing + $statssql=array(); $sql = "SELECT '".$langs->trans("DolibarrUsers")."' as label, count(distinct(email)) as nb FROM ".MAIN_DB_PREFIX."user as u"; $sql.= " WHERE u.email != ''"; // u.email IS NOT NULL est implicite dans ce test - $this->statssql[0]=$sql; - - } + $statssql[0]=$sql; + + return $statssql; + } + + /* + * \brief 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. + * \return int + */ function getNbOfRecipients() { - // La requete doit retourner: nb $sql = "SELECT count(distinct(u.email)) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."user as u"; $sql .= " WHERE u.email != ''"; // u.email IS NOT NULL est implicite dans ce test + // La requete doit retourner un champ "nb" pour etre comprise + // par parent::getNbOfRecipients return parent::getNbOfRecipients($sql); } @@ -104,13 +116,52 @@ class mailing_pomme extends MailingTargets */ function add_to_target($mailing_id,$filtersarray=array()) { + $cibles = array(); + // La requete doit retourner: id, email, fk_contact, name, firstname $sql = "SELECT u.rowid as id, u.email as email, null as fk_contact, u.name as name, u.firstname as firstname"; $sql .= " FROM ".MAIN_DB_PREFIX."user as u"; $sql .= " WHERE u.email != ''"; // u.email IS NOT NULL est implicite dans ce test $sql .= " ORDER BY u.email"; - return parent::add_to_target($mailing_id, $sql); + // Stocke destinataires dans cibles + $result=$this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + $i = 0; + $j = 0; + + dolibarr_syslog("pomme.modules.php: mailing $num cibles trouvées"); + + $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, + 'name' => $obj->name, + 'firstname' => $obj->firstname, + 'url' => $this->url($obj->id) + ); + $old = $obj->email; + $j++; + } + + $i++; + } + } + else + { + dolibarr_syslog($this->db->error()); + $this->error=$this->db->error(); + return -1; + } + + return parent::add_to_target($mailing_id, $cibles); } }