thirdparties.modules.php 13 KB

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