thirdparties.modules.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This file is an example to follow to add your own email selector inside
  6. * the Dolibarr email tool.
  7. * Follow instructions given in README file to know what to change to build
  8. * your own emailing list selector.
  9. * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
  10. */
  11. /**
  12. * \file htdocs/core/modules/mailings/thirdparties.modules.php
  13. * \ingroup mailing
  14. * \brief Example file to provide a list of recipients for mailing module
  15. */
  16. include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  17. /**
  18. * Class to manage a list of personalised recipients for mailing feature
  19. */
  20. class mailing_thirdparties extends MailingTargets
  21. {
  22. var $name='ContactsCategories';
  23. var $desc="Third parties (by categories)";
  24. var $require_admin=0;
  25. var $require_module=array("societe"); // This module allows to select by categories must be also enabled if category module is not activated
  26. var $picto='company';
  27. var $db;
  28. /**
  29. * Constructor
  30. *
  31. * @param DoliDB $db Database handler
  32. */
  33. function __construct($db)
  34. {
  35. global $conf;
  36. $this->db=$db;
  37. }
  38. /**
  39. * This is the main function that returns the array of emails
  40. *
  41. * @param int $mailing_id Id of mailing. No need to use it.
  42. * @param array $filtersarray If you used the formFilter function. Empty otherwise.
  43. * @return int <0 if error, number of emails added if ok
  44. */
  45. function add_to_target($mailing_id,$filtersarray=array())
  46. {
  47. global $conf, $langs;
  48. $cibles = array();
  49. // Select the third parties from category
  50. if (empty($_POST['filter']))
  51. {
  52. $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
  53. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  54. $sql.= " WHERE s.email != ''";
  55. $sql.= " AND s.entity IN (".getEntity('societe', 1).")";
  56. }
  57. else
  58. {
  59. $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";
  60. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
  61. $sql.= " WHERE s.email != ''";
  62. $sql.= " AND s.entity IN (".getEntity('societe', 1).")";
  63. $sql.= " AND cs.fk_societe = s.rowid";
  64. $sql.= " AND c.rowid = cs.fk_categorie";
  65. $sql.= " AND c.rowid='".$this->db->escape($_POST['filter'])."'";
  66. $sql.= " UNION ";
  67. $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";
  68. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
  69. $sql.= " WHERE s.email != ''";
  70. $sql.= " AND s.entity IN (".getEntity('societe', 1).")";
  71. $sql.= " AND cs.fk_societe = s.rowid";
  72. $sql.= " AND c.rowid = cs.fk_categorie";
  73. $sql.= " AND c.rowid='".$this->db->escape($_POST['filter'])."'";
  74. }
  75. $sql.= " ORDER BY email";
  76. // Stock recipients emails into targets table
  77. $result=$this->db->query($sql);
  78. if ($result)
  79. {
  80. $num = $this->db->num_rows($result);
  81. $i = 0;
  82. $j = 0;
  83. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  84. $old = '';
  85. while ($i < $num)
  86. {
  87. $obj = $this->db->fetch_object($result);
  88. if ($old <> $obj->email)
  89. {
  90. $cibles[$j] = array(
  91. 'email' => $obj->email,
  92. 'fk_contact' => $obj->fk_contact,
  93. 'lastname' => $obj->lastname,
  94. 'firstname' => $obj->firstname,
  95. 'other' => ($obj->label?$langs->transnoentities("Category").'='.$obj->label:''),
  96. 'source_url' => $this->url($obj->id),
  97. 'source_id' => $obj->id,
  98. 'source_type' => 'thirdparty'
  99. );
  100. $old = $obj->email;
  101. $j++;
  102. }
  103. $i++;
  104. }
  105. }
  106. else
  107. {
  108. dol_syslog($this->db->error());
  109. $this->error=$this->db->error();
  110. return -1;
  111. }
  112. return parent::add_to_target($mailing_id, $cibles);
  113. }
  114. /**
  115. * On the main mailing area, there is a box with statistics.
  116. * If you want to add a line in this report you must provide an
  117. * array of SQL request that returns two field:
  118. * One called "label", One called "nb".
  119. *
  120. * @return array Array with SQL requests
  121. */
  122. function getSqlArrayForStats()
  123. {
  124. // CHANGE THIS: Optionnal
  125. //var $statssql=array();
  126. //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
  127. return array();
  128. }
  129. /**
  130. * Return here number of distinct emails returned by your selector.
  131. * For example if this selector is used to extract 500 different
  132. * emails from a text file, this function must return 500.
  133. *
  134. * @param string $sql Requete sql de comptage
  135. * @return int Nb of recipients
  136. */
  137. function getNbOfRecipients($sql='')
  138. {
  139. global $conf;
  140. $sql = "SELECT count(distinct(s.email)) as nb";
  141. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  142. $sql.= " WHERE s.email != ''";
  143. $sql.= " AND s.entity IN (".getEntity('societe', 1).")";
  144. // La requete doit retourner un champ "nb" pour etre comprise
  145. // par parent::getNbOfRecipients
  146. return parent::getNbOfRecipients($sql);
  147. }
  148. /**
  149. * This is to add a form filter to provide variant of selector
  150. * If used, the HTML select must be called "filter"
  151. *
  152. * @return string A html select zone
  153. */
  154. function formFilter()
  155. {
  156. global $conf, $langs;
  157. $langs->load("companies");
  158. $s='';
  159. $s.='<select name="filter" class="flat">';
  160. // Show categories
  161. $sql = "SELECT rowid, label, type, visible";
  162. $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
  163. $sql.= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
  164. // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
  165. $sql.= " AND entity = ".$conf->entity;
  166. $sql.= " ORDER BY label";
  167. //print $sql;
  168. $resql = $this->db->query($sql);
  169. if ($resql)
  170. {
  171. $num = $this->db->num_rows($resql);
  172. if (empty($conf->categorie->enabled)) $num=0; // Force empty list if category module is not enabled
  173. if ($num) $s.='<option value="0">&nbsp;</option>';
  174. else $s.='<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
  175. $i = 0;
  176. while ($i < $num)
  177. {
  178. $obj = $this->db->fetch_object($resql);
  179. $type='';
  180. if ($obj->type == 1) $type=$langs->trans("Supplier");
  181. if ($obj->type == 2) $type=$langs->trans("Customer");
  182. $s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->label,38,'middle');
  183. if ($type) $s.=' ('.$type.')';
  184. $s.='</option>';
  185. $i++;
  186. }
  187. }
  188. else
  189. {
  190. dol_print_error($this->db);
  191. }
  192. $s.='</select>';
  193. return $s;
  194. }
  195. /**
  196. * Can include an URL link on each record provided by selector shown on target page.
  197. *
  198. * @param int $id ID
  199. * @return string Url link
  200. */
  201. function url($id)
  202. {
  203. //$companystatic=new Societe($this->db);
  204. //$companystatic->id=$id;
  205. //$companystatic->nom='';
  206. //return $companystatic->getNomUrl(1); // Url too long
  207. return '<a href="'.DOL_URL_ROOT.'/societe/soc.php?socid='.$id.'">'.img_object('',"company").'</a>';
  208. }
  209. }
  210. ?>