contacts2.modules.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /* Copyright (C) 2011 François Cerbelle <francois@cerbelle.net>
  3. * Copyright (C) 2013 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. * or see http://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/modules/mailings/contacts2.modules.php
  21. * \ingroup mailing
  22. * \brief Provides a list of recipients for mailing module
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  25. /**
  26. * \class mailing_contacts2
  27. * \brief Class to manage a list of personalised recipients for mailing feature
  28. */
  29. class mailing_contacts2 extends MailingTargets
  30. {
  31. var $name='ContactsByFunction';
  32. // This label is used if no translation is found for key MailingModuleDescXXX where XXX=name is found
  33. var $desc='Add contacts by function';
  34. var $require_admin=0;
  35. var $require_module=array();
  36. var $picto='contact';
  37. var $db;
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. */
  43. function __construct($db)
  44. {
  45. $this->db=$db;
  46. }
  47. /**
  48. * Renvoie url lien vers fiche de la source du destinataire du mailing
  49. *
  50. * @param int $id ID
  51. * @return string Url lien
  52. */
  53. function url($id)
  54. {
  55. return '<a href="'.DOL_URL_ROOT.'/contact/fiche.php?id='.$id.'">'.img_object('',"contact").'</a>';
  56. }
  57. /**
  58. * This is the main function that returns the array of emails
  59. *
  60. * @param int $mailing_id Id of mailing. No need to use it.
  61. * @param array $filtersarray Function
  62. * @return int <0 if error, number of emails added if ok
  63. */
  64. function add_to_target($mailing_id,$filtersarray=array())
  65. {
  66. global $conf,$langs;
  67. $target = array();
  68. // La requete doit retourner: id, email, fk_contact, name, firstname, other
  69. $sql = "SELECT sp.rowid as id, sp.email as email, sp.rowid as fk_contact,";
  70. $sql.= " sp.lastname, sp.firstname as firstname, sp.civilite,";
  71. $sql.= " s.nom as companyname";
  72. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
  73. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
  74. $sql.= " WHERE sp.entity IN (".getEntity('societe', 1).")";
  75. $sql.= " AND sp.email != ''"; // Note that null != '' is false
  76. $sql.= " AND sp.no_email = 0";
  77. //$sql.= " AND sp.poste != ''";
  78. if ($filtersarray[0]<>'all') $sql.= " AND sp.poste ='".$this->db->escape($filtersarray[0])."'";
  79. $sql.= " ORDER BY sp.lastname, sp.firstname";
  80. $resql = $this->db->query($sql);
  81. if ($resql)
  82. {
  83. $num = $this->db->num_rows($resql);
  84. $i = 0;
  85. while ($i < $num)
  86. {
  87. $obj= $this->db->fetch_object($resql);
  88. $target[] = array(
  89. 'email' => $obj->email,
  90. 'fk_contact' => $obj->fk_contact,
  91. 'lastname' => $obj->lastname,
  92. 'firstname' => $obj->firstname,
  93. 'other' =>
  94. ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
  95. ($langs->transnoentities("UserTitle").'='.($obj->civilite?$langs->transnoentities("Civility".$obj->civilite):'')),
  96. 'source_url' => $this->url($obj->id),
  97. 'source_id' => $obj->id,
  98. 'source_type' => 'contact'
  99. );
  100. $i++;
  101. }
  102. }
  103. return parent::add_to_target($mailing_id, $target);
  104. }
  105. /**
  106. * On the main mailing area, there is a box with statistics.
  107. * If you want to add a line in this report you must provide an
  108. * array of SQL request that returns two field:
  109. * One called "label", One called "nb".
  110. *
  111. * @return array Array with SQL requests
  112. */
  113. function getSqlArrayForStats()
  114. {
  115. global $conf;
  116. $statssql=array();
  117. /*for ($i=0; $i<5; $i++) {
  118. $statssql[$i] = "SELECT sp.poste as label";
  119. $statssql[$i].= ", count(distinct(sp.email)) as nb";
  120. $statssql[$i].= " FROM ".MAIN_DB_PREFIX."socpeople as sp,";
  121. $statssql[$i].= " ".MAIN_DB_PREFIX."societe as s";
  122. $statssql[$i].= " WHERE s.rowid = sp.fk_soc";
  123. $statssql[$i].= " AND sp.email != ''"; // Note that null != '' is false
  124. $statssql[$i].= " AND (sp.poste IS NOT NULL AND sp.poste != '')";
  125. $statssql[$i].= " AND sp.entity IN (".getEntity('societe', 1).")";
  126. $statssql[$i].= " GROUP BY label";
  127. $statssql[$i].= " ORDER BY nb DESC";
  128. $statssql[$i].= " LIMIT $i,1";
  129. }*/
  130. return $statssql;
  131. }
  132. /**
  133. * Return here number of distinct emails returned by your selector.
  134. *
  135. * @param string $sql Requete sql de comptage
  136. * @return int
  137. */
  138. function getNbOfRecipients($sql='')
  139. {
  140. global $conf;
  141. // We must report here number of contacts when absolutely no filter selected (so all contacts).
  142. // Number with a filter are show in the combo list for each filter.
  143. // If we want a filter "a position is defined", we must add it into formFilter
  144. $sql = "SELECT count(distinct(sp.email)) as nb";
  145. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
  146. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
  147. $sql.= " WHERE sp.entity IN (".getEntity('societe', 1).")";
  148. $sql.= " AND sp.email != ''"; // Note that null != '' is false
  149. $sql.= " AND sp.no_email = 0";
  150. //$sql.= " AND sp.poste != ''";
  151. // La requete doit retourner un champ "nb" pour etre comprise
  152. // par parent::getNbOfRecipients
  153. return parent::getNbOfRecipients($sql);
  154. }
  155. /**
  156. * This is to add a form filter to provide variant of selector
  157. * If used, the HTML select must be called "filter"
  158. *
  159. * @return string A html select zone
  160. */
  161. function formFilter()
  162. {
  163. global $conf, $langs;
  164. $langs->load("companies");
  165. $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
  166. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
  167. $sql.= " WHERE sp.entity IN (".getEntity('societe', 1).")";
  168. $sql.= " AND sp.email != ''"; // Note that null != '' is false
  169. $sql.= " AND sp.no_email = 0";
  170. $sql.= " AND (sp.poste IS NOT NULL AND sp.poste != '')";
  171. $sql.= " GROUP BY sp.poste";
  172. $sql.= " ORDER BY sp.poste";
  173. $resql = $this->db->query($sql);
  174. $s='';
  175. $s.='<select name="filter" class="flat">';
  176. $s.='<option value="all"></option>';
  177. if ($resql)
  178. {
  179. $num = $this->db->num_rows($resql);
  180. $i = 0;
  181. while ($i < $num)
  182. {
  183. $obj = $this->db->fetch_object($resql);
  184. $s.='<option value="'.$obj->poste.'">'.$obj->poste.' ('.$obj->nb.')</option>';
  185. $i++;
  186. }
  187. }
  188. $s.='</select>';
  189. return $s;
  190. }
  191. }
  192. ?>