PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/modules/mailings/framboise.modules.php

https://github.com/asterix14/dolibarr
PHP | 227 lines | 118 code | 37 blank | 72 comment | 13 complexity | 81e09f8a6cd0d3a747fe1b412af602ec MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
  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/framboise.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 mailing_framboise
  19. * \brief Class to manage a list of personalised recipients for mailing feature
  20. */
  21. class mailing_framboise extends MailingTargets
  22. {
  23. // CHANGE THIS: Put here a name not already used
  24. var $name='MembersCategories';
  25. // CHANGE THIS: Put here a description of your selector module.
  26. // This label is used if no translation found for key MailingModuleDescXXX where XXX=name is found
  27. var $desc="Foundation members with emails (by categories)";
  28. // CHANGE THIS: Set to 1 if selector is available for admin users only
  29. var $require_admin=0;
  30. var $require_module=array("adherent","categorie");
  31. var $picto='user';
  32. var $db;
  33. /**
  34. * Constructor
  35. *
  36. * @param DoliDB $db Database handler
  37. */
  38. function mailing_framboise($db)
  39. {
  40. $this->db=$db;
  41. }
  42. /**
  43. * \brief This is the main function that returns the array of emails
  44. * \param mailing_id Id of mailing. No need to use it.
  45. * \param filterarray If you used the formFilter function. Empty otherwise.
  46. * \return int <0 if error, number of emails added if ok
  47. */
  48. function add_to_target($mailing_id,$filtersarray=array())
  49. {
  50. global $conf, $langs;
  51. $cibles = array();
  52. // CHANGE THIS
  53. // Select the members from category
  54. $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname,";
  55. if ($_POST['filter']) $sql.= " llx_categorie.label as label";
  56. else $sql.=" null as label";
  57. $sql.= " FROM llx_adherent as s";
  58. if ($_POST['filter']) $sql.= " LEFT JOIN llx_categorie_member ON llx_categorie_member.fk_member=s.rowid";
  59. if ($_POST['filter']) $sql.= " LEFT JOIN llx_categorie ON llx_categorie.rowid = llx_categorie_member.fk_categorie";
  60. $sql.= " WHERE s.email != ''";
  61. $sql.= " AND s.entity = ".$conf->entity;
  62. if ($_POST['filter']) $sql.= " AND llx_categorie.rowid='".$_POST['filter']."'";
  63. $sql.= " ORDER BY s.email";
  64. // Stocke destinataires dans cibles
  65. $result=$this->db->query($sql);
  66. if ($result)
  67. {
  68. $num = $this->db->num_rows($result);
  69. $i = 0;
  70. $j = 0;
  71. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  72. $old = '';
  73. while ($i < $num)
  74. {
  75. $obj = $this->db->fetch_object($result);
  76. if ($old <> $obj->email)
  77. {
  78. $cibles[$j] = array(
  79. 'email' => $obj->email,
  80. 'fk_contact' => $obj->fk_contact,
  81. 'name' => $obj->name,
  82. 'firstname' => $obj->firstname,
  83. 'other' => ($obj->label?$langs->transnoentities("Category").'='.$obj->label:''),
  84. 'source_url' => $this->url($obj->id),
  85. 'source_id' => $obj->id,
  86. 'source_type' => 'member'
  87. );
  88. $old = $obj->email;
  89. $j++;
  90. }
  91. $i++;
  92. }
  93. }
  94. else
  95. {
  96. dol_syslog($this->db->error());
  97. $this->error=$this->db->error();
  98. return -1;
  99. }
  100. return parent::add_to_target($mailing_id, $cibles);
  101. }
  102. /**
  103. * On the main mailing area, there is a box with statistics.
  104. * If you want to add a line in this report you must provide an
  105. * array of SQL request that returns two field:
  106. * One called "label", One called "nb".
  107. *
  108. * @return array Array with SQL requests
  109. */
  110. function getSqlArrayForStats()
  111. {
  112. // CHANGE THIS: Optionnal
  113. //var $statssql=array();
  114. //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
  115. return array();
  116. }
  117. /*
  118. * \brief Return here number of distinct emails returned by your selector.
  119. * For example if this selector is used to extract 500 different
  120. * emails from a text file, this function must return 500.
  121. * \return int
  122. */
  123. function getNbOfRecipients()
  124. {
  125. global $conf;
  126. $sql = "SELECT count(distinct(s.email)) as nb";
  127. $sql.= " FROM ".MAIN_DB_PREFIX."adherent as s";
  128. $sql.= " WHERE s.email != ''";
  129. $sql.= " AND s.entity = ".$conf->entity;
  130. // La requete doit retourner un champ "nb" pour etre comprise
  131. // par parent::getNbOfRecipients
  132. return parent::getNbOfRecipients($sql);
  133. }
  134. /**
  135. * \brief This is to add a form filter to provide variant of selector
  136. * If used, the HTML select must be called "filter"
  137. * \return string A html select zone
  138. */
  139. function formFilter()
  140. {
  141. global $conf, $langs;
  142. $langs->load("companies");
  143. $langs->load("categories");
  144. $s='';
  145. $s.='<select name="filter" class="flat">';
  146. // Show categories
  147. $sql = "SELECT rowid, label, type, visible";
  148. $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
  149. $sql.= " WHERE type = 3"; // We keep only categories for members
  150. // $sql.= " AND visible > 0"; // We ignore the property visible because member's categories does not use this property (only products categories use it).
  151. $sql.= " AND entity = ".$conf->entity;
  152. $sql.= " ORDER BY label";
  153. //print $sql;
  154. $resql = $this->db->query($sql);
  155. if ($resql)
  156. {
  157. $num = $this->db->num_rows($resql);
  158. $s.='<option value="0">&nbsp;</option>';
  159. if (! $num) $s.='<option value="0" disabled="disabled">'.$langs->trans("NoCategoriesDefined").'</option>';
  160. $i = 0;
  161. while ($i < $num)
  162. {
  163. $obj = $this->db->fetch_object($resql);
  164. $s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->label,38,'middle');
  165. $s.='</option>';
  166. $i++;
  167. }
  168. }
  169. else
  170. {
  171. dol_print_error($db);
  172. }
  173. $s.='</select>';
  174. return $s;
  175. }
  176. /**
  177. * \brief Can include an URL link on each record provided by selector
  178. * shown on target page.
  179. * \return string Url link
  180. */
  181. function url($id)
  182. {
  183. //$companystatic=new Societe($this->db);
  184. //$companystatic->id=$id;
  185. //$companystatic->nom='';
  186. //return $companystatic->getNomUrl(1); // Url too long
  187. return '<a href="'.DOL_URL_ROOT.'/adherents/fiche.php?rowid='.$id.'">'.img_object('',"user").'</a>';
  188. }
  189. }
  190. ?>