advthirdparties.modules.php 9.1 KB

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