advthirdparties.modules.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This file is an example to follow to add your own email selector inside
  6. * the Dolibarr email tool.
  7. * Follow instructions given in README file to know what to change to build
  8. * your own emailing list selector.
  9. * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
  10. */
  11. /**
  12. * \file advtargetingemaling/modules/mailings/advthirdparties.modules.php
  13. * \ingroup advtargetingemaling
  14. * \brief Example file to provide a list of recipients for mailing module
  15. */
  16. include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  17. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  18. include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  19. /**
  20. * Class to manage a list of personalised recipients for mailing feature
  21. */
  22. class mailing_advthirdparties extends MailingTargets
  23. {
  24. var $name='ThirdPartyAdvancedTargeting';
  25. var $desc="Third parties";
  26. var $require_admin=0;
  27. var $require_module=array("none"); // This module should not be displayed as Selector in mailling
  28. var $picto='company';
  29. var $db;
  30. /**
  31. * Constructor
  32. *
  33. * @param DoliDB $db Database handler
  34. */
  35. function __construct($db)
  36. {
  37. global $conf;
  38. $this->db=$db;
  39. }
  40. /**
  41. * This is the main function that returns the array of emails
  42. *
  43. * @param int $mailing_id Id of mailing. No need to use it.
  44. * @param array $socid Array of id soc to add
  45. * @param int $type_of_target Defined in advtargetemailing.class.php
  46. * @param array $contactid Array of contact id to add
  47. * @return int <0 if error, number of emails added if ok
  48. */
  49. function add_to_target($mailing_id,$socid,$type_of_target, $contactid)
  50. {
  51. global $conf, $langs;
  52. dol_syslog(get_class($this)."::add_to_target socid=".var_export($socid,true).' contactid='.var_export($contactid,true));
  53. $cibles = array();
  54. if (($type_of_target==1) || ($type_of_target==3)) {
  55. // Select the third parties from category
  56. if (count($socid)>0)
  57. {
  58. $sql= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact";
  59. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object=s.rowid";
  60. $sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
  61. $sql.= " AND s.rowid IN (".implode(',',$socid).")";
  62. $sql.= " ORDER BY email";
  63. // Stock recipients emails into targets table
  64. $result=$this->db->query($sql);
  65. if ($result)
  66. {
  67. $num = $this->db->num_rows($result);
  68. $i = 0;
  69. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found", LOG_DEBUG);
  70. $old = '';
  71. while ($i < $num)
  72. {
  73. $obj = $this->db->fetch_object($result);
  74. if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
  75. if (!array_key_exists($obj->email, $cibles)) {
  76. $cibles[$obj->email] = array(
  77. 'email' => $obj->email,
  78. 'fk_contact' => $obj->fk_contact,
  79. 'name' => $obj->name,
  80. 'firstname' => $obj->firstname,
  81. 'other' => '',
  82. 'source_url' => $this->url($obj->id,'thirdparty'),
  83. 'source_id' => $obj->id,
  84. 'source_type' => 'thirdparty'
  85. );
  86. }
  87. }
  88. $i++;
  89. }
  90. }
  91. else
  92. {
  93. dol_syslog($this->db->error());
  94. $this->error=$this->db->error();
  95. return -1;
  96. }
  97. }
  98. }
  99. if (($type_of_target==1) || ($type_of_target==2)) {
  100. // Select the third parties from category
  101. if (count($socid)>0 || count($contactid)>0)
  102. {
  103. $sql= "SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname";
  104. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as socp";
  105. $sql.= " WHERE socp.entity IN (".getEntity('societe', 1).")";
  106. if (count($contactid)>0) {
  107. $sql.= " AND socp.rowid IN (".implode(',',$contactid).")";
  108. }
  109. if (count($socid)>0) {
  110. $sql.= " AND socp.fk_soc IN (".implode(',',$socid).")";
  111. }
  112. $sql.= " ORDER BY email";
  113. // Stock recipients emails into targets table
  114. $result=$this->db->query($sql);
  115. if ($result)
  116. {
  117. $num = $this->db->num_rows($result);
  118. $i = 0;
  119. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  120. $old = '';
  121. while ($i < $num)
  122. {
  123. $obj = $this->db->fetch_object($result);
  124. if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
  125. if (!array_key_exists($obj->email, $cibles)) {
  126. $cibles[$obj->email] = array(
  127. 'email' => $obj->email,
  128. 'fk_contact' =>$obj->id,
  129. 'lastname' => $obj->lastname,
  130. 'firstname' => $obj->firstname,
  131. 'other' => '',
  132. 'source_url' => $this->url($obj->id,'contact'),
  133. 'source_id' => $obj->id,
  134. 'source_type' => 'contact'
  135. );
  136. }
  137. }
  138. $i++;
  139. }
  140. }
  141. else
  142. {
  143. dol_syslog($this->db->error());
  144. $this->error=$this->db->error();
  145. return -1;
  146. }
  147. }
  148. }
  149. dol_syslog(get_class($this)."::add_to_target mailing cibles=".var_export($cibles,true), LOG_DEBUG);
  150. return parent::add_to_target($mailing_id, $cibles);
  151. }
  152. /**
  153. * On the main mailing area, there is a box with statistics.
  154. * If you want to add a line in this report you must provide an
  155. * array of SQL request that returns two field:
  156. * One called "label", One called "nb".
  157. *
  158. * @return array Array with SQL requests
  159. */
  160. function getSqlArrayForStats()
  161. {
  162. // CHANGE THIS: Optionnal
  163. //var $statssql=array();
  164. //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
  165. return array();
  166. }
  167. /**
  168. * Return here number of distinct emails returned by your selector.
  169. * For example if this selector is used to extract 500 different
  170. * emails from a text file, this function must return 500.
  171. *
  172. * @return int Nb of recipients
  173. */
  174. function getNbOfRecipients()
  175. {
  176. global $conf;
  177. $sql = "SELECT count(distinct(s.email)) as nb";
  178. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  179. $sql.= " WHERE s.email != ''";
  180. $sql.= " AND s.entity IN (".getEntity('societe', 1).")";
  181. // La requete doit retourner un champ "nb" pour etre comprise
  182. // par parent::getNbOfRecipients
  183. return parent::getNbOfRecipients($sql);
  184. }
  185. /**
  186. * This is to add a form filter to provide variant of selector
  187. * If used, the HTML select must be called "filter"
  188. *
  189. * @return string A html select zone
  190. */
  191. function formFilter()
  192. {
  193. global $conf, $langs;
  194. $langs->load("companies");
  195. $s='';
  196. $s.='<select name="filter" class="flat">';
  197. // Show categories
  198. $sql = "SELECT rowid, label, type, visible";
  199. $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
  200. $sql.= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
  201. // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
  202. $sql.= " AND entity = ".$conf->entity;
  203. $sql.= " ORDER BY label";
  204. //print $sql;
  205. $resql = $this->db->query($sql);
  206. if ($resql)
  207. {
  208. $num = $this->db->num_rows($resql);
  209. if (empty($conf->categorie->enabled)) $num=0; // Force empty list if category module is not enabled
  210. if ($num) $s.='<option value="0">&nbsp;</option>';
  211. else $s.='<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
  212. $i = 0;
  213. while ($i < $num)
  214. {
  215. $obj = $this->db->fetch_object($resql);
  216. $type='';
  217. if ($obj->type == 1) $type=$langs->trans("Supplier");
  218. if ($obj->type == 2) $type=$langs->trans("Customer");
  219. $s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->label,38,'middle');
  220. if ($type) $s.=' ('.$type.')';
  221. $s.='</option>';
  222. $i++;
  223. }
  224. }
  225. else
  226. {
  227. dol_print_error($this->db);
  228. }
  229. $s.='</select>';
  230. return $s;
  231. }
  232. /**
  233. * Can include an URL link on each record provided by selector shown on target page.
  234. *
  235. * @param int $id ID
  236. * @param string $type type
  237. * @return string Url link
  238. */
  239. function url($id,$type)
  240. {
  241. if ($type=='thirdparty') {
  242. $companystatic=new Societe($this->db);
  243. $companystatic->fetch($id);
  244. return $companystatic->getNomUrl(0);
  245. } elseif ($type=='contact') {
  246. $contactstatic=new Contact($this->db);
  247. $contactstatic->fetch($id);
  248. return $contactstatic->getNomUrl(0);
  249. }
  250. }
  251. }