advthirdparties.modules.php 8.9 KB

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