contacts1.modules.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/modules/mailings/contacts1.modules.php
  22. * \ingroup mailing
  23. * \brief File of class to offer a selector of emailing targets with Rule 'Poire'.
  24. */
  25. include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  26. /**
  27. * Class to offer a selector of emailing targets from contacts
  28. */
  29. class mailing_contacts1 extends MailingTargets
  30. {
  31. public $name = 'ContactCompanies'; // Identifiant du module mailing
  32. // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
  33. public $desc = 'Contacts of thirdparties (prospects, customers, suppliers...)';
  34. public $require_module = array("societe"); // Module mailing actif si modules require_module actifs
  35. public $require_admin = 0; // Module mailing actif pour user admin ou non
  36. public $enabled = '$conf->societe->enabled';
  37. /**
  38. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  39. */
  40. public $picto = 'contact';
  41. /**
  42. * @var DoliDB Database handler.
  43. */
  44. public $db;
  45. /**
  46. * Constructor
  47. *
  48. * @param DoliDB $db Database handler
  49. */
  50. public function __construct($db)
  51. {
  52. $this->db = $db;
  53. }
  54. /**
  55. * On the main mailing area, there is a box with statistics.
  56. * If you want to add a line in this report you must provide an
  57. * array of SQL request that returns two field:
  58. * One called "label", One called "nb".
  59. *
  60. * @return string[] Array with SQL requests
  61. */
  62. public function getSqlArrayForStats()
  63. {
  64. global $conf, $langs;
  65. $langs->load("commercial");
  66. $statssql = array();
  67. $statssql[0] = "SELECT '".$this->db->escape($langs->trans("NbOfCompaniesContacts"))."' as label,";
  68. $statssql[0] .= " count(distinct(c.email)) as nb";
  69. $statssql[0] .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  70. $statssql[0] .= " WHERE c.entity IN (".getEntity('contact').")";
  71. $statssql[0] .= " AND c.email <> ''"; // Note that null != '' is false
  72. $statssql[0] .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = c.email) = 0";
  73. $statssql[0] .= " AND c.statut = 1";
  74. return $statssql;
  75. }
  76. /**
  77. * Return here number of distinct emails returned by your selector.
  78. * For example if this selector is used to extract 500 different
  79. * emails from a text file, this function must return 500.
  80. *
  81. * @param string $sql Requete sql de comptage
  82. * @return int|string Nb of recipient, or <0 if error, or '' if NA
  83. */
  84. public function getNbOfRecipients($sql = '')
  85. {
  86. global $conf;
  87. $sql = "SELECT count(distinct(c.email)) as nb";
  88. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  89. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc";
  90. $sql .= " WHERE c.entity IN (".getEntity('contact').")";
  91. $sql .= " AND c.email <> ''"; // Note that null != '' is false
  92. $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = c.email) = 0";
  93. // exclude unsubscribed users
  94. $sql .= " AND c.statut = 1";
  95. // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
  96. return parent::getNbOfRecipients($sql);
  97. }
  98. /**
  99. * Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings
  100. *
  101. * @return string Retourne zone select
  102. */
  103. public function formFilter()
  104. {
  105. global $langs,$conf;
  106. // Load translation files required by the page
  107. $langs->loadLangs(array("commercial", "companies", "suppliers", "categories"));
  108. $s = '';
  109. // Add filter on job position
  110. $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
  111. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
  112. $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
  113. $sql .= " AND sp.email <> ''"; // Note that null != '' is false
  114. $sql .= " AND sp.statut = 1";
  115. $sql .= " AND (sp.poste IS NOT NULL AND sp.poste <> '')";
  116. $sql .= " GROUP BY sp.poste";
  117. $sql .= " ORDER BY sp.poste";
  118. $resql = $this->db->query($sql);
  119. $s .= '<select id="filter_jobposition_contact" name="filter_jobposition" class="flat" placeholder="'.dol_escape_htmltag($langs->trans("PostOrFunction")).'">';
  120. $s .= '<option value="-1">'.$langs->trans("PostOrFunction").'</option>';
  121. if ($resql) {
  122. $num = $this->db->num_rows($resql);
  123. $i = 0;
  124. if ($num > 0) {
  125. while ($i < $num) {
  126. $obj = $this->db->fetch_object($resql);
  127. $s .= '<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
  128. $i++;
  129. }
  130. } else {
  131. $s .= '<option disabled="disabled" value="">'.$langs->trans("None").'</option>';
  132. }
  133. } else {
  134. dol_print_error($this->db);
  135. }
  136. $s .= '</select>';
  137. $s .= ajax_combobox("filter_jobposition_contact");
  138. $s .= ' ';
  139. // Filter on contact category
  140. $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
  141. $sql .= " FROM ";
  142. $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
  143. $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
  144. $sql .= " ".MAIN_DB_PREFIX."categorie_contact as cs";
  145. $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
  146. $sql .= " AND sp.email <> ''"; // Note that null != '' is false
  147. $sql .= " AND sp.statut = 1";
  148. $sql .= " AND cs.fk_categorie = c.rowid";
  149. $sql .= " AND cs.fk_socpeople = sp.rowid";
  150. $sql .= " GROUP BY c.label";
  151. $sql .= " ORDER BY c.label";
  152. $resql = $this->db->query($sql);
  153. $s .= '<select id="filter_category_contact" name="filter_category" class="flat">';
  154. $s .= '<option value="-1">'.$langs->trans("ContactCategoriesShort").'</option>';
  155. if ($resql) {
  156. $num = $this->db->num_rows($resql);
  157. if ($num) {
  158. $i = 0;
  159. while ($i < $num) {
  160. $obj = $this->db->fetch_object($resql);
  161. $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
  162. $i++;
  163. }
  164. } else {
  165. $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
  166. }
  167. } else {
  168. dol_print_error($this->db);
  169. }
  170. $s .= '</select>';
  171. $s .= ajax_combobox("filter_category_contact");
  172. $s .= '<br>';
  173. // Add prospect of a particular level
  174. $s .= '<select id="filter_contact" name="filter" class="flat">';
  175. $sql = "SELECT code, label";
  176. $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
  177. $sql .= " WHERE active > 0";
  178. $sql .= " ORDER BY label";
  179. $resql = $this->db->query($sql);
  180. if ($resql) {
  181. $num = $this->db->num_rows($resql);
  182. if ($num) {
  183. $s .= '<option value="-1">'.$langs->trans("NatureOfThirdParty").'</option>';
  184. } else {
  185. $s .= '<option value="-1">'.$langs->trans("ContactsAllShort").'</option>';
  186. }
  187. $s .= '<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
  188. $i = 0;
  189. while ($i < $num) {
  190. $obj = $this->db->fetch_object($resql);
  191. $level = $langs->trans($obj->code);
  192. if ($level == $obj->code) {
  193. $level = $langs->trans($obj->label);
  194. }
  195. $s .= '<option value="prospectslevel'.$obj->code.'">'.$langs->trans("ThirdPartyProspects").' ('.$langs->trans("ProspectLevelShort").'='.$level.')</option>';
  196. $i++;
  197. }
  198. } else {
  199. dol_print_error($this->db);
  200. }
  201. $s .= '<option value="customers">'.$langs->trans("ThirdPartyCustomers").'</option>';
  202. //$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
  203. $s .= '<option value="suppliers">'.$langs->trans("ThirdPartySuppliers").'</option>';
  204. $s .= '</select>';
  205. $s .= ajax_combobox("filter_contact");
  206. $s .= ' ';
  207. // Filter on thirdparty category
  208. $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
  209. $sql .= " FROM ";
  210. $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
  211. $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
  212. $sql .= " ".MAIN_DB_PREFIX."categorie_societe as cs";
  213. $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
  214. $sql .= " AND sp.email <> ''"; // Note that null != '' is false
  215. $sql .= " AND sp.statut = 1";
  216. $sql .= " AND cs.fk_categorie = c.rowid";
  217. $sql .= " AND cs.fk_soc = sp.fk_soc";
  218. $sql .= " GROUP BY c.label";
  219. $sql .= " ORDER BY c.label";
  220. $resql = $this->db->query($sql);
  221. $s .= '<select id="filter_category_customer_contact" name="filter_category_customer" class="flat maxwidth200">';
  222. $s .= '<option value="-1">'.$langs->trans("CustomersProspectsCategoriesShort").'</option>';
  223. if ($resql) {
  224. $num = $this->db->num_rows($resql);
  225. if ($num) {
  226. $i = 0;
  227. while ($i < $num) {
  228. $obj = $this->db->fetch_object($resql);
  229. $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
  230. $i++;
  231. }
  232. } else {
  233. $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
  234. }
  235. } else {
  236. dol_print_error($this->db);
  237. }
  238. $s .= '</select>';
  239. $s .= ajax_combobox("filter_category_customer_contact");
  240. $s .= ' ';
  241. // Filter on thirdparty category
  242. $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
  243. $sql .= " FROM ";
  244. $sql .= " ".MAIN_DB_PREFIX."socpeople as sp,";
  245. $sql .= " ".MAIN_DB_PREFIX."categorie as c,";
  246. $sql .= " ".MAIN_DB_PREFIX."categorie_fournisseur as cs";
  247. $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
  248. $sql .= " AND sp.email <> ''"; // Note that null != '' is false
  249. $sql .= " AND sp.statut = 1";
  250. $sql .= " AND cs.fk_categorie = c.rowid";
  251. $sql .= " AND cs.fk_soc = sp.fk_soc";
  252. $sql .= " GROUP BY c.label";
  253. $sql .= " ORDER BY c.label";
  254. $resql = $this->db->query($sql);
  255. $s .= '<select id="filter_category_supplier_contact" name="filter_category_supplier" class="flat maxwidth200">';
  256. $s .= '<option value="-1">'.$langs->trans("SuppliersCategoriesShort").'</option>';
  257. if ($resql) {
  258. $num = $this->db->num_rows($resql);
  259. if ($num) {
  260. $i = 0;
  261. while ($i < $num) {
  262. $obj = $this->db->fetch_object($resql);
  263. $s .= '<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
  264. $i++;
  265. }
  266. } else {
  267. $s .= '<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
  268. }
  269. } else {
  270. dol_print_error($this->db);
  271. }
  272. $s .= '</select>';
  273. $s .= ajax_combobox("filter_category_supplier_contact");
  274. // Choose language if multilangue active
  275. if (getDolGlobalInt('MAIN_MULTILANGS') == 1) {
  276. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  277. $formadmin = new FormAdmin($this->db);
  278. $s .= '<span class="opacitymedium">'.$langs->trans("DefaultLang").':</span> ';
  279. $s .= $formadmin->select_language($langs->getDefaultLang(1), 'filter_lang', 0, 0, 1, 0, 0, '', 0, 0, 0, null, 1);
  280. }
  281. return $s;
  282. }
  283. /**
  284. * Renvoie url lien vers fiche de la source du destinataire du mailing
  285. *
  286. * @param int $id ID
  287. * @return string Url lien
  288. */
  289. public function url($id)
  290. {
  291. return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('', "contact").'</a>';
  292. }
  293. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  294. /**
  295. * Ajoute destinataires dans table des cibles
  296. *
  297. * @param int $mailing_id Id of emailing
  298. * @return int <0 si erreur, nb ajout si ok
  299. */
  300. public function add_to_target($mailing_id)
  301. {
  302. // phpcs:enable
  303. global $conf, $langs;
  304. $filter = GETPOST('filter', 'alpha');
  305. $filter_jobposition = GETPOST('filter_jobposition', 'alpha');
  306. $filter_category = GETPOST('filter_category', 'alpha');
  307. $filter_category_customer = GETPOST('filter_category_customer', 'alpha');
  308. $filter_category_supplier = GETPOST('filter_category_supplier', 'alpha');
  309. $filter_lang = GETPOST('filter_lang', 'alpha');
  310. $cibles = array();
  311. // List prospects levels
  312. $prospectlevel = array();
  313. $sql = "SELECT code, label";
  314. $sql .= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
  315. $sql .= " WHERE active > 0";
  316. $sql .= " ORDER BY label";
  317. $resql = $this->db->query($sql);
  318. if ($resql) {
  319. $num = $this->db->num_rows($resql);
  320. $i = 0;
  321. while ($i < $num) {
  322. $obj = $this->db->fetch_object($resql);
  323. $prospectlevel[$obj->code] = $obj->label;
  324. $i++;
  325. }
  326. } else {
  327. dol_print_error($this->db);
  328. }
  329. // Request must return: id, email, fk_contact, lastname, firstname, other
  330. $sql = "SELECT sp.rowid as id, sp.email as email, sp.rowid as fk_contact, sp.lastname, sp.firstname, sp.civility as civility_id, sp.poste as jobposition,";
  331. $sql .= " s.nom as companyname";
  332. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
  333. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
  334. if ($filter_category != 'all' && $filter_category != '-1') {
  335. $sql .= ", ".MAIN_DB_PREFIX."categorie as c";
  336. $sql .= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
  337. }
  338. if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
  339. $sql .= ", ".MAIN_DB_PREFIX."categorie as c2";
  340. $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
  341. }
  342. if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
  343. $sql .= ", ".MAIN_DB_PREFIX."categorie as c3";
  344. $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
  345. }
  346. $sql .= " WHERE sp.entity IN (".getEntity('contact').")";
  347. $sql .= " AND sp.email <> ''";
  348. $sql .= " AND (SELECT count(*) FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE email = sp.email) = 0";
  349. // Exclude unsubscribed email adresses
  350. $sql .= " AND sp.statut = 1";
  351. $sql .= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  352. // Filter on category
  353. if ($filter_category != 'all' && $filter_category != '-1') {
  354. $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
  355. $sql .= " AND c.label = '".$this->db->escape($filter_category)."'";
  356. }
  357. if ($filter_category_customer != 'all' && $filter_category_customer != '-1') {
  358. $sql .= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
  359. $sql .= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
  360. }
  361. if ($filter_category_supplier != 'all' && $filter_category_supplier != '-1') {
  362. $sql .= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
  363. $sql .= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
  364. }
  365. // Filter on language
  366. if (!empty($filter_lang)) {
  367. $sql .= " AND sp.default_lang LIKE '".$this->db->escape($filter_lang)."%'";
  368. }
  369. // Filter on nature
  370. $key = $filter;
  371. //print "xx".$key;
  372. if ($key == 'prospects') {
  373. $sql .= " AND s.client=2";
  374. }
  375. foreach ($prospectlevel as $codelevel => $valuelevel) {
  376. if ($key == 'prospectslevel'.$codelevel) {
  377. $sql .= " AND s.fk_prospectlevel = '".$this->db->escape($codelevel)."'";
  378. }
  379. }
  380. if ($key == 'customers') {
  381. $sql .= " AND s.client=1";
  382. }
  383. if ($key == 'suppliers') {
  384. $sql .= " AND s.fournisseur=1";
  385. }
  386. // Filter on job position
  387. $key = $filter_jobposition;
  388. if (!empty($key) && $key != 'all' && $key != '-1') {
  389. $sql .= " AND sp.poste = '".$this->db->escape($key)."'";
  390. }
  391. $sql .= " ORDER BY sp.email";
  392. // print "wwwwwwx".$sql;
  393. // Stocke destinataires dans cibles
  394. $result = $this->db->query($sql);
  395. if ($result) {
  396. $num = $this->db->num_rows($result);
  397. $i = 0;
  398. $j = 0;
  399. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  400. $old = '';
  401. while ($i < $num) {
  402. $obj = $this->db->fetch_object($result);
  403. if ($old <> $obj->email) {
  404. $cibles[$j] = array(
  405. 'email' => $obj->email,
  406. 'fk_contact' => $obj->fk_contact,
  407. 'lastname' => $obj->lastname,
  408. 'firstname' => $obj->firstname,
  409. 'other' =>
  410. ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
  411. ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
  412. ($langs->transnoentities("JobPosition").'='.$obj->jobposition),
  413. 'source_url' => $this->url($obj->id),
  414. 'source_id' => $obj->id,
  415. 'source_type' => 'contact'
  416. );
  417. $old = $obj->email;
  418. $j++;
  419. }
  420. $i++;
  421. }
  422. } else {
  423. dol_syslog($this->db->error());
  424. $this->error = $this->db->error();
  425. return -1;
  426. }
  427. return parent::addTargetsToDatabase($mailing_id, $cibles);
  428. }
  429. }