thirdparties.modules.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /* Copyright (C) 2018-2018 Andre Schild <a.schild@aarboard.ch>
  3. * Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. *
  6. * This file is an example to follow to add your own email selector inside
  7. * the Dolibarr email tool.
  8. * Follow instructions given in README file to know what to change to build
  9. * your own emailing list selector.
  10. * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
  11. */
  12. /**
  13. * \file htdocs/core/modules/mailings/thirdparties.modules.php
  14. * \ingroup mailing
  15. * \brief Example file to provide a list of recipients for mailing module
  16. */
  17. include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  18. /**
  19. * Class to manage a list of personalised recipients for mailing feature
  20. */
  21. class mailing_thirdparties extends MailingTargets
  22. {
  23. public $name = 'ThirdPartiesByCategories';
  24. // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
  25. public $desc = "Third parties (by categories)";
  26. public $require_admin = 0;
  27. public $require_module = array("societe"); // This module allows to select by categories must be also enabled if category module is not activated
  28. /**
  29. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  30. */
  31. public $picto = 'company';
  32. /**
  33. * @var DoliDB Database handler.
  34. */
  35. public $db;
  36. /**
  37. * Constructor
  38. *
  39. * @param DoliDB $db Database handler
  40. */
  41. public function __construct($db)
  42. {
  43. global $conf, $langs;
  44. $langs->load("companies");
  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. * @return int <0 if error, number of emails added if ok
  53. */
  54. public function add_to_target($mailing_id)
  55. {
  56. // phpcs:enable
  57. global $conf, $langs;
  58. $cibles = array();
  59. $addDescription = "";
  60. // Select the third parties from category
  61. if (!GETPOST('filter')) {
  62. $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
  63. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  64. $sql .= " WHERE s.email <> ''";
  65. $sql .= " AND s.entity IN (".getEntity('societe').")";
  66. $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  67. } else {
  68. $addFilter = "";
  69. if (GETPOSTISSET("filter_client") && GETPOST("filter_client") <> '-1') {
  70. $addFilter .= " AND s.client=".((int) GETPOST("filter_client", 'int'));
  71. $addDescription = $langs->trans('ProspectCustomer')."=";
  72. if (GETPOST("filter_client") == 0) {
  73. $addDescription .= $langs->trans('NorProspectNorCustomer');
  74. } elseif (GETPOST("filter_client") == 1) {
  75. $addDescription .= $langs->trans('Customer');
  76. } elseif (GETPOST("filter_client") == 2) {
  77. $addDescription .= $langs->trans('Prospect');
  78. } elseif (GETPOST("filter_client") == 3) {
  79. $addDescription .= $langs->trans('ProspectCustomer');
  80. } else {
  81. $addDescription .= "Unknown status ".GETPOST("filter_client");
  82. }
  83. }
  84. if (GETPOSTISSET("filter_status")) {
  85. if (strlen($addDescription) > 0) {
  86. $addDescription .= ";";
  87. }
  88. $addDescription .= $langs->trans("Status")."=";
  89. if (GETPOST("filter_status") == '1') {
  90. $addFilter .= " AND s.status=1";
  91. $addDescription .= $langs->trans("Enabled");
  92. } else {
  93. $addFilter .= " AND s.status=0";
  94. $addDescription .= $langs->trans("Disabled");
  95. }
  96. }
  97. $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label";
  98. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
  99. $sql .= " WHERE s.email <> ''";
  100. $sql .= " AND s.entity IN (".getEntity('societe').")";
  101. $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  102. $sql .= " AND cs.fk_soc = s.rowid";
  103. $sql .= " AND c.rowid = cs.fk_categorie";
  104. if (GETPOST('filter', 'int') > 0) {
  105. $sql .= " AND c.rowid=".((int) GETPOST('filter', 'int'));
  106. }
  107. $sql .= $addFilter;
  108. $sql .= " UNION ";
  109. $sql .= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label";
  110. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
  111. $sql .= " WHERE s.email <> ''";
  112. $sql .= " AND s.entity IN (".getEntity('societe').")";
  113. $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  114. $sql .= " AND cs.fk_soc = s.rowid";
  115. $sql .= " AND c.rowid = cs.fk_categorie";
  116. if (GETPOST('filter', 'int') > 0) {
  117. $sql .= " AND c.rowid=".((int) GETPOST('filter', 'int'));
  118. }
  119. $sql .= $addFilter;
  120. }
  121. $sql .= " ORDER BY email";
  122. // Stock recipients emails into targets table
  123. $result = $this->db->query($sql);
  124. if ($result) {
  125. $num = $this->db->num_rows($result);
  126. $i = 0;
  127. $j = 0;
  128. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  129. $old = '';
  130. while ($i < $num) {
  131. $obj = $this->db->fetch_object($result);
  132. if ($old <> $obj->email) {
  133. $otherTxt = ($obj->label ? $langs->transnoentities("Category").'='.$obj->label : '');
  134. if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
  135. $otherTxt .= ";";
  136. }
  137. $otherTxt .= $addDescription;
  138. $cibles[$j] = array(
  139. 'email' => $obj->email,
  140. 'fk_contact' => $obj->fk_contact,
  141. 'lastname' => $obj->name, // For a thirdparty, we must use name
  142. 'firstname' => '', // For a thirdparty, lastname is ''
  143. 'other' => $otherTxt,
  144. 'source_url' => $this->url($obj->id),
  145. 'source_id' => $obj->id,
  146. 'source_type' => 'thirdparty'
  147. );
  148. $old = $obj->email;
  149. $j++;
  150. }
  151. $i++;
  152. }
  153. } else {
  154. dol_syslog($this->db->error());
  155. $this->error = $this->db->error();
  156. return -1;
  157. }
  158. return parent::addTargetsToDatabase($mailing_id, $cibles);
  159. }
  160. /**
  161. * On the main mailing area, there is a box with statistics.
  162. * If you want to add a line in this report you must provide an
  163. * array of SQL request that returns two field:
  164. * One called "label", One called "nb".
  165. *
  166. * @return array Array with SQL requests
  167. */
  168. public function getSqlArrayForStats()
  169. {
  170. // CHANGE THIS: Optionnal
  171. //var $statssql=array();
  172. //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
  173. return array();
  174. }
  175. /**
  176. * Return here number of distinct emails returned by your selector.
  177. * For example if this selector is used to extract 500 different
  178. * emails from a text file, this function must return 500.
  179. *
  180. * @param string $sql Requete sql de comptage
  181. * @return int Nb of recipients
  182. */
  183. public function getNbOfRecipients($sql = '')
  184. {
  185. global $conf;
  186. $sql = "SELECT count(distinct(s.email)) as nb";
  187. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  188. $sql .= " WHERE s.email <> ''";
  189. $sql .= " AND s.entity IN (".getEntity('societe').")";
  190. // La requete doit retourner un champ "nb" pour etre comprise
  191. // par parent::getNbOfRecipients
  192. return parent::getNbOfRecipients($sql);
  193. }
  194. /**
  195. * This is to add a form filter to provide variant of selector
  196. * If used, the HTML select must be called "filter"
  197. *
  198. * @return string A html select zone
  199. */
  200. public function formFilter()
  201. {
  202. global $conf, $langs;
  203. $langs->load("companies");
  204. $s = '<select id="filter_thirdparties" name="filter" class="flat">';
  205. // Show categories
  206. $sql = "SELECT rowid, label, type, visible";
  207. $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
  208. $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
  209. // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
  210. $sql .= " AND entity = ".$conf->entity;
  211. $sql .= " ORDER BY label";
  212. //print $sql;
  213. $resql = $this->db->query($sql);
  214. if ($resql) {
  215. $num = $this->db->num_rows($resql);
  216. if (empty($conf->categorie->enabled)) {
  217. $num = 0; // Force empty list if category module is not enabled
  218. }
  219. if ($num) {
  220. $s .= '<option value="-1">'.$langs->trans("Categories").'</option>';
  221. } else {
  222. $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
  223. }
  224. $i = 0;
  225. while ($i < $num) {
  226. $obj = $this->db->fetch_object($resql);
  227. $type = '';
  228. if ($obj->type == 1) {
  229. $type = $langs->trans("Supplier");
  230. }
  231. if ($obj->type == 2) {
  232. $type = $langs->trans("Customer");
  233. }
  234. $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
  235. if ($type) {
  236. $s .= ' ('.$type.')';
  237. }
  238. $s .= '</option>';
  239. $i++;
  240. }
  241. $s .= ajax_combobox("filter_thirdparties");
  242. } else {
  243. dol_print_error($this->db);
  244. }
  245. $s .= '</select> ';
  246. $s .= '<select id="filter_client_thirdparties" name="filter_client_thirdparties" class="flat">';
  247. $s .= '<option value="-1">'.$langs->trans('ProspectCustomer').'</option>';
  248. if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS)) {
  249. $s .= '<option value="2">'.$langs->trans('Prospect').'</option>';
  250. }
  251. if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && empty($conf->global->SOCIETE_DISABLE_PROSPECTSCUSTOMERS)) {
  252. $s .= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
  253. }
  254. if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS)) {
  255. $s .= '<option value="1">'.$langs->trans('Customer').'</option>';
  256. }
  257. $s .= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
  258. $s .= '</select> ';
  259. $s .= ajax_combobox("filter_client_thirdparties");
  260. $s .= ' <select id="filter_status_thirdparties" name="filter_status" class="flat">';
  261. $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
  262. $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
  263. $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
  264. $s .= '</select>';
  265. $s .= ajax_combobox("filter_status_thirdparties");
  266. return $s;
  267. }
  268. /**
  269. * Can include an URL link on each record provided by selector shown on target page.
  270. *
  271. * @param int $id ID
  272. * @return string Url link
  273. */
  274. public function url($id)
  275. {
  276. return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
  277. }
  278. }