thirdparties.modules.php 12 KB

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