contacts1.modules.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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@capnetworks.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 <http://www.gnu.org/licenses/>.
  18. * or see http://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. var $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. var $desc='Contacts of thirdparties (prospects, customers, suppliers...)';
  34. var $require_module=array("societe"); // Module mailing actif si modules require_module actifs
  35. var $require_admin=0; // Module mailing actif pour user admin ou non
  36. var $picto='contact';
  37. /**
  38. * @var DoliDB Database handler.
  39. */
  40. public $db;
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $db Database handler
  45. */
  46. function __construct($db)
  47. {
  48. $this->db=$db;
  49. }
  50. /**
  51. * On the main mailing area, there is a box with statistics.
  52. * If you want to add a line in this report you must provide an
  53. * array of SQL request that returns two field:
  54. * One called "label", One called "nb".
  55. *
  56. * @return string[] Array with SQL requests
  57. */
  58. function getSqlArrayForStats()
  59. {
  60. global $conf, $langs;
  61. $langs->load("commercial");
  62. $statssql=array();
  63. $statssql[0] = "SELECT '".$langs->trans("NbOfCompaniesContacts")."' as label,";
  64. $statssql[0].= " count(distinct(c.email)) as nb";
  65. $statssql[0].= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  66. $statssql[0].= " WHERE c.entity IN (".getEntity('socpeople').")";
  67. $statssql[0].= " AND c.email != ''"; // Note that null != '' is false
  68. $statssql[0].= " AND c.no_email = 0";
  69. $statssql[0].= " AND c.statut = 1";
  70. return $statssql;
  71. }
  72. /**
  73. * Return here number of distinct emails returned by your selector.
  74. * For example if this selector is used to extract 500 different
  75. * emails from a text file, this function must return 500.
  76. *
  77. * @param string $sql Requete sql de comptage
  78. * @return int
  79. */
  80. function getNbOfRecipients($sql='')
  81. {
  82. global $conf;
  83. $sql = "SELECT count(distinct(c.email)) as nb";
  84. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  85. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc";
  86. $sql.= " WHERE c.entity IN (".getEntity('socpeople').")";
  87. $sql.= " AND c.email != ''"; // Note that null != '' is false
  88. $sql.= " AND c.no_email = 0";
  89. $sql.= " AND c.statut = 1";
  90. // The request must return a field called "nb" to be understandable by parent::getNbOfRecipients
  91. return parent::getNbOfRecipients($sql);
  92. }
  93. /**
  94. * Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings
  95. *
  96. * @return string Retourne zone select
  97. */
  98. function formFilter()
  99. {
  100. global $langs;
  101. // Load translation files required by the page
  102. $langs->loadLangs(array("commercial","companies","suppliers","categories"));
  103. $s='';
  104. // Add filter on job position
  105. $sql = "SELECT sp.poste, count(distinct(sp.email)) AS nb";
  106. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
  107. $sql.= " WHERE sp.entity IN (".getEntity('socpeople').")";
  108. /*$sql.= " AND sp.email != ''"; // Note that null != '' is false
  109. $sql.= " AND sp.no_email = 0";
  110. $sql.= " AND sp.statut = 1";*/
  111. $sql.= " AND (sp.poste IS NOT NULL AND sp.poste != '')";
  112. $sql.= " GROUP BY sp.poste";
  113. $sql.= " ORDER BY sp.poste";
  114. $resql = $this->db->query($sql);
  115. $s.=$langs->trans("PostOrFunction").': ';
  116. $s.='<select name="filter_jobposition" class="flat">';
  117. $s.='<option value="all">&nbsp;</option>';
  118. if ($resql)
  119. {
  120. $num = $this->db->num_rows($resql);
  121. $i = 0;
  122. while ($i < $num)
  123. {
  124. $obj = $this->db->fetch_object($resql);
  125. $s.='<option value="'.dol_escape_htmltag($obj->poste).'">'.dol_escape_htmltag($obj->poste).' ('.$obj->nb.')</option>';
  126. $i++;
  127. }
  128. }
  129. else dol_print_error($this->db);
  130. $s.='</select>';
  131. $s.=' ';
  132. // Filter on contact category
  133. $s .= $langs->trans("ContactCategoriesShort").': ';
  134. $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
  135. $sql.= " FROM ";
  136. $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
  137. $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
  138. $sql.= " ".MAIN_DB_PREFIX."categorie_contact as cs";
  139. $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
  140. //$sql.= " AND sp.no_email = 0";
  141. //$sql.= " AND sp.email != ''";
  142. //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
  143. $sql.= " AND cs.fk_categorie = c.rowid";
  144. $sql.= " AND cs.fk_socpeople = sp.rowid";
  145. $sql.= " GROUP BY c.label";
  146. $sql.= " ORDER BY c.label";
  147. $resql = $this->db->query($sql);
  148. $s.='<select name="filter_category" class="flat">';
  149. $s.='<option value="all">&nbsp;</option>';
  150. if ($resql)
  151. {
  152. $num = $this->db->num_rows($resql);
  153. if ($num)
  154. {
  155. $i = 0;
  156. while ($i < $num)
  157. {
  158. $obj = $this->db->fetch_object($resql);
  159. $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
  160. $i++;
  161. }
  162. }
  163. else
  164. {
  165. $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactWithCategoryFound").'</option>';
  166. }
  167. }
  168. else dol_print_error($this->db);
  169. $s.='</select>';
  170. $s.='<br>';
  171. // Add prospect of a particular level
  172. $s.=$langs->trans("NatureOfThirdParty").': ';
  173. $s.='<select name="filter" class="flat">';
  174. $sql = "SELECT code, label";
  175. $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
  176. $sql.= " WHERE active > 0";
  177. $sql.= " ORDER BY label";
  178. $resql = $this->db->query($sql);
  179. if ($resql)
  180. {
  181. $num = $this->db->num_rows($resql);
  182. if ($num) $s.='<option value="all">&nbsp;</option>';
  183. else $s.='<option value="all">'.$langs->trans("ContactsAllShort").'</option>';
  184. $s.='<option value="prospects">'.$langs->trans("ThirdPartyProspects").'</option>';
  185. $i = 0;
  186. while ($i < $num)
  187. {
  188. $obj = $this->db->fetch_object($resql);
  189. $level=$langs->trans($obj->code);
  190. if ($level == $obj->code) $level=$langs->trans($obj->label);
  191. $s.='<option value="prospectslevel'.$obj->code.'">'.$langs->trans("ThirdPartyProspects").' ('.$langs->trans("ProspectLevelShort").'='.$level.')</option>';
  192. $i++;
  193. }
  194. }
  195. else dol_print_error($this->db);
  196. $s.='<option value="customers">'.$langs->trans("ThirdPartyCustomers").'</option>';
  197. //$s.='<option value="customersidprof">'.$langs->trans("ThirdPartyCustomersWithIdProf12",$langs->trans("ProfId1"),$langs->trans("ProfId2")).'</option>';
  198. $s.='<option value="suppliers">'.$langs->trans("ThirdPartySuppliers").'</option>';
  199. $s.='</select>';
  200. $s.= ' ';
  201. // Filter on thirdparty category
  202. $s .= $langs->trans("CustomersProspectsCategoriesShort").': ';
  203. $sql = "SELECT c.label, count(distinct(sp.email)) AS nb";
  204. $sql.= " FROM ";
  205. $sql.= " ".MAIN_DB_PREFIX."socpeople as sp,";
  206. $sql.= " ".MAIN_DB_PREFIX."categorie as c,";
  207. $sql.= " ".MAIN_DB_PREFIX."categorie_societe as cs";
  208. $sql.= " WHERE sp.statut = 1"; // Note that null != '' is false
  209. //$sql.= " AND sp.no_email = 0";
  210. //$sql.= " AND sp.email != ''";
  211. //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
  212. $sql.= " AND cs.fk_categorie = c.rowid";
  213. $sql.= " AND cs.fk_soc = sp.fk_soc";
  214. $sql.= " GROUP BY c.label";
  215. $sql.= " ORDER BY c.label";
  216. $resql = $this->db->query($sql);
  217. $s.='<select name="filter_category_customer" class="flat">';
  218. $s.='<option value="all">&nbsp;</option>';
  219. if ($resql)
  220. {
  221. $num = $this->db->num_rows($resql);
  222. if ($num)
  223. {
  224. $i = 0;
  225. while ($i < $num)
  226. {
  227. $obj = $this->db->fetch_object($resql);
  228. $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
  229. $i++;
  230. }
  231. }
  232. else
  233. {
  234. $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
  235. }
  236. }
  237. else dol_print_error($this->db);
  238. $s.='</select>';
  239. $s.= ' ';
  240. // Filter on thirdparty category
  241. $s .= $langs->trans("SuppliersCategoriesShort").': ';
  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.statut = 1"; // Note that null != '' is false
  248. //$sql.= " AND sp.no_email = 0";
  249. //$sql.= " AND sp.email != ''";
  250. //$sql.= " AND sp.entity IN (".getEntity('socpeople').")";
  251. $sql.= " AND cs.fk_categorie = c.rowid";
  252. $sql.= " AND cs.fk_soc = sp.fk_soc";
  253. $sql.= " GROUP BY c.label";
  254. $sql.= " ORDER BY c.label";
  255. $resql = $this->db->query($sql);
  256. $s.='<select name="filter_category_supplier" class="flat">';
  257. $s.='<option value="all">&nbsp;</option>';
  258. if ($resql)
  259. {
  260. $num = $this->db->num_rows($resql);
  261. if ($num)
  262. {
  263. $i = 0;
  264. while ($i < $num)
  265. {
  266. $obj = $this->db->fetch_object($resql);
  267. $s.='<option value="'.$obj->label.'">'.$obj->label.' ('.$obj->nb.')</option>';
  268. $i++;
  269. }
  270. }
  271. else
  272. {
  273. $s.='<option value="-1" disabled="disabled">'.$langs->trans("NoContactLinkedToThirdpartieWithCategoryFound").'</option>';
  274. }
  275. }
  276. else dol_print_error($this->db);
  277. $s.='</select>';
  278. return $s;
  279. }
  280. /**
  281. * Renvoie url lien vers fiche de la source du destinataire du mailing
  282. *
  283. * @param int $id ID
  284. * @return string Url lien
  285. */
  286. function url($id)
  287. {
  288. return '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$id.'">'.img_object('',"contact").'</a>';
  289. }
  290. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  291. /**
  292. * Ajoute destinataires dans table des cibles
  293. *
  294. * @param int $mailing_id Id of emailing
  295. * @param array $filtersarray Optional filter data (deprecated)
  296. * @return int <0 si erreur, nb ajout si ok
  297. */
  298. function add_to_target($mailing_id,$filtersarray=array())
  299. {
  300. // phpcs:enable
  301. global $conf, $langs;
  302. $filter = GETPOST('filter','alpha');
  303. $filter_jobposition = GETPOST('filter_jobposition','alpha');
  304. $filter_category = GETPOST('filter_category','alpha');
  305. $filter_category_customer = GETPOST('filter_category_customer','alpha');
  306. $filter_category_supplier = GETPOST('filter_category_supplier','alpha');
  307. $cibles = array();
  308. // List prospects levels
  309. $prospectlevel=array();
  310. $sql = "SELECT code, label";
  311. $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
  312. $sql.= " WHERE active > 0";
  313. $sql.= " ORDER BY label";
  314. $resql = $this->db->query($sql);
  315. if ($resql)
  316. {
  317. $num = $this->db->num_rows($resql);
  318. $i = 0;
  319. while ($i < $num)
  320. {
  321. $obj = $this->db->fetch_object($resql);
  322. $prospectlevel[$obj->code]=$obj->label;
  323. $i++;
  324. }
  325. }
  326. else dol_print_error($this->db);
  327. // Request must return: id, email, fk_contact, lastname, firstname, other
  328. $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,";
  329. $sql.= " s.nom as companyname";
  330. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp";
  331. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = sp.fk_soc";
  332. if ($filter_category <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c";
  333. if ($filter_category <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_contact as cs";
  334. if ($filter_category_customer <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c2";
  335. if ($filter_category_customer <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_societe as c2s";
  336. if ($filter_category_supplier <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie as c3";
  337. if ($filter_category_supplier <> 'all') $sql.= ", ".MAIN_DB_PREFIX."categorie_fournisseur as c3s";
  338. $sql.= " WHERE sp.entity IN (".getEntity('socpeople').")";
  339. $sql.= " AND sp.email <> ''";
  340. $sql.= " AND sp.no_email = 0";
  341. $sql.= " AND sp.statut = 1";
  342. $sql.= " AND sp.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
  343. // Filter on category
  344. if ($filter_category <> 'all') $sql.= " AND cs.fk_categorie = c.rowid AND cs.fk_socpeople = sp.rowid";
  345. if ($filter_category <> 'all') $sql.= " AND c.label = '".$this->db->escape($filter_category)."'";
  346. if ($filter_category_customer <> 'all') $sql.= " AND c2s.fk_categorie = c2.rowid AND c2s.fk_soc = sp.fk_soc";
  347. if ($filter_category_customer <> 'all') $sql.= " AND c2.label = '".$this->db->escape($filter_category_customer)."'";
  348. if ($filter_category_supplier <> 'all') $sql.= " AND c3s.fk_categorie = c3.rowid AND c3s.fk_soc = sp.fk_soc";
  349. if ($filter_category_supplier <> 'all') $sql.= " AND c3.label = '".$this->db->escape($filter_category_supplier)."'";
  350. // Filter on nature
  351. $key = $filter;
  352. {
  353. //print "xx".$key;
  354. if ($key == 'prospects') $sql.= " AND s.client=2";
  355. foreach($prospectlevel as $codelevel=>$valuelevel) if ($key == 'prospectslevel'.$codelevel) $sql.= " AND s.fk_prospectlevel='".$codelevel."'";
  356. if ($key == 'customers') $sql.= " AND s.client=1";
  357. if ($key == 'suppliers') $sql.= " AND s.fournisseur=1";
  358. }
  359. // Filter on job position
  360. $key = $filter_jobposition;
  361. if (! empty($key) && $key != 'all') $sql.= " AND sp.poste ='".$this->db->escape($key)."'";
  362. $sql.= " ORDER BY sp.email";
  363. //print "wwwwwwx".$sql;
  364. // Stocke destinataires dans cibles
  365. $result=$this->db->query($sql);
  366. if ($result)
  367. {
  368. $num = $this->db->num_rows($result);
  369. $i = 0;
  370. $j = 0;
  371. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  372. $old = '';
  373. while ($i < $num)
  374. {
  375. $obj = $this->db->fetch_object($result);
  376. if ($old <> $obj->email)
  377. {
  378. $cibles[$j] = array(
  379. 'email' => $obj->email,
  380. 'fk_contact' => $obj->fk_contact,
  381. 'lastname' => $obj->lastname,
  382. 'firstname' => $obj->firstname,
  383. 'other' =>
  384. ($langs->transnoentities("ThirdParty").'='.$obj->companyname).';'.
  385. ($langs->transnoentities("UserTitle").'='.($obj->civility_id?$langs->transnoentities("Civility".$obj->civility_id):'')).';'.
  386. ($langs->transnoentities("JobPosition").'='.$obj->jobposition),
  387. 'source_url' => $this->url($obj->id),
  388. 'source_id' => $obj->id,
  389. 'source_type' => 'contact'
  390. );
  391. $old = $obj->email;
  392. $j++;
  393. }
  394. $i++;
  395. }
  396. }
  397. else
  398. {
  399. dol_syslog($this->db->error());
  400. $this->error=$this->db->error();
  401. return -1;
  402. }
  403. return parent::add_to_target($mailing_id, $cibles);
  404. }
  405. }