thirdparties.modules.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. public $enabled = 'isModEnabled("societe")';
  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. * Constructor
  35. *
  36. * @param DoliDB $db Database handler
  37. */
  38. public function __construct($db)
  39. {
  40. global $conf, $langs;
  41. $langs->load("companies");
  42. $this->db = $db;
  43. }
  44. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  45. /**
  46. * This is the main function that returns the array of emails
  47. *
  48. * @param int $mailing_id Id of mailing. No need to use it.
  49. * @return int Return integer <0 if error, number of emails added if ok
  50. */
  51. public function add_to_target($mailing_id)
  52. {
  53. // phpcs:enable
  54. global $conf, $langs;
  55. $cibles = array();
  56. $addDescription = "";
  57. $addFilter = "";
  58. if (GETPOSTISSET("filter_client_thirdparties") && GETPOST("filter_client_thirdparties") != '-1') {
  59. $addFilter .= " AND s.client=".((int) GETPOST("filter_client_thirdparties", 'int'));
  60. $addDescription = $langs->trans('ProspectCustomer')."=";
  61. if (GETPOST("filter_client_thirdparties") == 0) {
  62. $addDescription .= $langs->trans('NorProspectNorCustomer');
  63. } elseif (GETPOST("filter_client_thirdparties") == 1) {
  64. $addDescription .= $langs->trans('Customer');
  65. } elseif (GETPOST("filter_client_thirdparties") == 2) {
  66. $addDescription .= $langs->trans('Prospect');
  67. } elseif (GETPOST("filter_client_thirdparties") == 3) {
  68. $addDescription .= $langs->trans('ProspectCustomer');
  69. } else {
  70. $addDescription .= "Unknown status ".GETPOST("filter_client_thirdparties");
  71. }
  72. }
  73. if (GETPOSTISSET("filter_supplier_thirdparties") && GETPOST("filter_supplier_thirdparties") != '-1') {
  74. $addFilter .= " AND s.fournisseur = ".((int) GETPOST("filter_supplier_thirdparties", 'int'));
  75. $addDescription = $langs->trans('Supplier')."=";
  76. if (GETPOST("filter_supplier_thirdparties") == 0) {
  77. $addDescription .= $langs->trans('No');
  78. } elseif (GETPOST("filter_supplier_thirdparties") == 1) {
  79. $addDescription .= $langs->trans('Yes');
  80. } else {
  81. $addDescription .= "Unknown status ".GETPOST("filter_supplier_thirdparties");
  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. } elseif (GETPOST("filter_status") == '0') {
  93. $addFilter .= " AND s.status=0";
  94. $addDescription .= $langs->trans("Disabled");
  95. }
  96. }
  97. if (GETPOSTISSET("filter_status")) {
  98. if (strlen($addDescription) > 0) {
  99. $addDescription .= ";";
  100. }
  101. $addDescription .= $langs->trans("Status")."=";
  102. if (GETPOST("filter_status") == '1') {
  103. $addFilter .= " AND s.status=1";
  104. $addDescription .= $langs->trans("Enabled");
  105. } elseif (GETPOST("filter_status") == '0') {
  106. $addFilter .= " AND s.status=0";
  107. $addDescription .= $langs->trans("Disabled");
  108. }
  109. }
  110. if (GETPOST('default_lang', 'alpha') && GETPOST('default_lang', 'alpha') != '-1') {
  111. $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('default_lang', 'alpha'))."%'";
  112. $addDescription = $langs->trans('DefaultLang')."=";
  113. }
  114. if (GETPOST('filter_lang_thirdparties', 'alpha') && GETPOST('filter_lang_thirdparties', 'alpha') != '-1') {
  115. $addFilter .= " AND s.default_lang LIKE '".$this->db->escape(GETPOST('filter_lang_thirdparties', 'alpha'))."%'";
  116. $addDescription = $langs->trans('DefaultLang')."=";
  117. }
  118. // Select the third parties from category
  119. if (!GETPOST('filter_thirdparties') || GETPOST('filter_thirdparties') == '-1') {
  120. $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
  121. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  122. $sql .= " WHERE s.email <> ''";
  123. $sql .= " AND s.entity IN (".getEntity('societe').")";
  124. $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  125. if (empty($this->evenunsubscribe)) {
  126. $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
  127. }
  128. $sql .= $addFilter;
  129. } else {
  130. $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";
  131. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
  132. $sql .= " WHERE s.email <> ''";
  133. $sql .= " AND s.entity IN (".getEntity('societe').")";
  134. $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  135. $sql .= " AND cs.fk_soc = s.rowid";
  136. $sql .= " AND c.rowid = cs.fk_categorie";
  137. if (GETPOST('filter_thirdparties', 'int') > 0) {
  138. $sql .= " AND c.rowid=".((int) GETPOST('filter_thirdparties', 'int'));
  139. }
  140. if (empty($this->evenunsubscribe)) {
  141. $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
  142. }
  143. $sql .= $addFilter;
  144. $sql .= " UNION ";
  145. $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";
  146. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
  147. $sql .= " WHERE s.email <> ''";
  148. $sql .= " AND s.entity IN (".getEntity('societe').")";
  149. $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  150. $sql .= " AND cs.fk_soc = s.rowid";
  151. $sql .= " AND c.rowid = cs.fk_categorie";
  152. if (GETPOST('filter_thirdparties', 'int') > 0) {
  153. $sql .= " AND c.rowid=".((int) GETPOST('filter_thirdparties', 'int'));
  154. }
  155. if (empty($this->evenunsubscribe)) {
  156. $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = s.email) = 0";
  157. }
  158. $sql .= $addFilter;
  159. }
  160. $sql .= " ORDER BY email";
  161. //print $sql;exit;
  162. // Stock recipients emails into targets table
  163. $result = $this->db->query($sql);
  164. if ($result) {
  165. $num = $this->db->num_rows($result);
  166. $i = 0;
  167. $j = 0;
  168. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  169. $old = '';
  170. while ($i < $num) {
  171. $obj = $this->db->fetch_object($result);
  172. if ($old != $obj->email) {
  173. $otherTxt = ($obj->label ? $langs->transnoentities("Category").'='.$obj->label : '');
  174. if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
  175. $otherTxt .= ";";
  176. }
  177. $otherTxt .= $addDescription;
  178. $cibles[$j] = array(
  179. 'email' => $obj->email,
  180. 'fk_contact' => $obj->fk_contact,
  181. 'lastname' => $obj->name, // For a thirdparty, we must use name
  182. 'firstname' => '', // For a thirdparty, lastname is ''
  183. 'other' => $otherTxt,
  184. 'source_url' => $this->url($obj->id),
  185. 'source_id' => $obj->id,
  186. 'source_type' => 'thirdparty'
  187. );
  188. $old = $obj->email;
  189. $j++;
  190. }
  191. $i++;
  192. }
  193. } else {
  194. dol_syslog($this->db->error());
  195. $this->error = $this->db->error();
  196. return -1;
  197. }
  198. return parent::addTargetsToDatabase($mailing_id, $cibles);
  199. }
  200. /**
  201. * On the main mailing area, there is a box with statistics.
  202. * If you want to add a line in this report you must provide an
  203. * array of SQL request that returns two field:
  204. * One called "label", One called "nb".
  205. *
  206. * @return array Array with SQL requests
  207. */
  208. public function getSqlArrayForStats()
  209. {
  210. // CHANGE THIS: Optionnal
  211. //var $statssql=array();
  212. //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
  213. return array();
  214. }
  215. /**
  216. * Return here number of distinct emails returned by your selector.
  217. * For example if this selector is used to extract 500 different
  218. * emails from a text file, this function must return 500.
  219. *
  220. * @param string $sql Requete sql de comptage
  221. * @return int|string Nb of recipient, or <0 if error, or '' if NA
  222. */
  223. public function getNbOfRecipients($sql = '')
  224. {
  225. global $conf;
  226. $sql = "SELECT count(distinct(s.email)) as nb";
  227. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  228. $sql .= " WHERE s.email <> ''";
  229. $sql .= " AND s.entity IN (".getEntity('societe').")";
  230. if (empty($this->evenunsubscribe)) {
  231. $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int) $conf->entity).")";
  232. }
  233. // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
  234. return parent::getNbOfRecipients($sql);
  235. }
  236. /**
  237. * This is to add a form filter to provide variant of selector
  238. * If used, the HTML select must be called "filter"
  239. *
  240. * @return string A html select zone
  241. */
  242. public function formFilter()
  243. {
  244. global $conf, $langs;
  245. $langs->load("companies");
  246. // filter
  247. $s = '<select id="filter_thirdparties" name="filter_thirdparties" class="flat maxwidth200">';
  248. // Show categories
  249. $sql = "SELECT rowid, label, type, visible";
  250. $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
  251. $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
  252. // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
  253. $sql .= " AND entity = ".$conf->entity;
  254. $sql .= " ORDER BY label";
  255. //print $sql;
  256. $resql = $this->db->query($sql);
  257. if ($resql) {
  258. $num = $this->db->num_rows($resql);
  259. if (!isModEnabled("categorie")) {
  260. $num = 0; // Force empty list if category module is not enabled
  261. }
  262. if ($num) {
  263. $s .= '<option value="-1">'.$langs->trans("Categories").'</option>';
  264. } else {
  265. $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
  266. }
  267. $i = 0;
  268. while ($i < $num) {
  269. $obj = $this->db->fetch_object($resql);
  270. $type = '';
  271. if ($obj->type == 1) {
  272. $type = $langs->trans("Supplier");
  273. }
  274. if ($obj->type == 2) {
  275. $type = $langs->trans("Customer");
  276. }
  277. $labeltoshow = $obj->label;
  278. if ($type) {
  279. $labeltoshow .= ' <span class="opacitymedium">('.$type.')</span>';
  280. }
  281. $s .= '<option value="'.$obj->rowid.'" data-html="'.dol_escape_htmltag($labeltoshow).'">';
  282. $s .= $labeltoshow;
  283. $s .= '</option>';
  284. $i++;
  285. }
  286. $s .= ajax_combobox("filter_thirdparties");
  287. } else {
  288. dol_print_error($this->db);
  289. }
  290. $s .= '</select> ';
  291. // filter_client_thirdparties
  292. $s .= '<select id="filter_client_thirdparties" name="filter_client_thirdparties" class="flat minwidth100">';
  293. $s .= '<option value="-1">'.$langs->trans('ProspectCustomer').'</option>';
  294. if (!getDolGlobalString('SOCIETE_DISABLE_PROSPECTS')) {
  295. $s .= '<option value="2">'.$langs->trans('Prospect').'</option>';
  296. }
  297. if (!getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') && !getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS') && !getDolGlobalString('SOCIETE_DISABLE_PROSPECTSCUSTOMERS')) {
  298. $s .= '<option value="3">'.$langs->trans('ProspectCustomer').'</option>';
  299. }
  300. if (!getDolGlobalString('SOCIETE_DISABLE_CUSTOMERS')) {
  301. $s .= '<option value="1">'.$langs->trans('Customer').'</option>';
  302. }
  303. $s .= '<option value="0">'.$langs->trans('NorProspectNorCustomer').'</option>';
  304. $s .= '</select> ';
  305. $s .= ajax_combobox("filter_client_thirdparties");
  306. // filter_supplier_thirdparties
  307. $s .= ' <select id="filter_supplier_thirdparties" name="filter_supplier_thirdparties" class="flat minwidth100">';
  308. $s .= '<option value="-1">'.$langs->trans("Supplier").'</option>';
  309. $s .= '<option value="1">'.$langs->trans("Yes").'</option>';
  310. $s .= '<option value="0">'.$langs->trans("No").'</option>';
  311. $s .= '</select>';
  312. $s .= ajax_combobox("filter_supplier_thirdparties");
  313. // filter_status_thirdparties
  314. $s .= ' <select id="filter_status_thirdparties" name="filter_status" class="flat">';
  315. $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
  316. $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
  317. $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
  318. $s .= '</select>';
  319. $s .= ajax_combobox("filter_status_thirdparties");
  320. // filter_lang_thirdparties
  321. if (getDolGlobalInt('MAIN_MULTILANGS')) {
  322. // Choose language
  323. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  324. $formadmin = new FormAdmin($this->db);
  325. $s .= img_picto($langs->trans("DefaultLang"), 'language', 'class="pictofixedwidth"');
  326. //$s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
  327. $s .= $formadmin->select_language(GETPOST('filter_lang_thirdparties', 'aZ09'), 'filter_lang_thirdparties', 0, null, $langs->trans("DefaultLang"), 0, 0, '', 0, 0, 0, null, 1);
  328. }
  329. return $s;
  330. }
  331. /**
  332. * Can include an URL link on each record provided by selector shown on target page.
  333. *
  334. * @param int $id ID
  335. * @return string Url link
  336. */
  337. public function url($id)
  338. {
  339. return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('', "company").'</a>';
  340. }
  341. }