fraise.modules.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /* Copyright (C) 2005 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  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/fraise.modules.php
  22. * \ingroup mailing
  23. * \brief File of class to generate target according to rule Fraise
  24. */
  25. include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  26. include_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  27. /**
  28. * Class to generate target according to rule Fraise
  29. */
  30. class mailing_fraise extends MailingTargets
  31. {
  32. public $name = 'FundationMembers'; // Identifiant du module mailing
  33. // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
  34. public $desc = 'Foundation members with emails';
  35. // Set to 1 if selector is available for admin users only
  36. public $require_admin = 0;
  37. public $require_module = array('adherent');
  38. public $enabled = '$conf->adherent->enabled';
  39. /**
  40. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  41. */
  42. public $picto = 'user';
  43. /**
  44. * @var DoliDB Database handler.
  45. */
  46. public $db;
  47. /**
  48. * Constructor
  49. *
  50. * @param DoliDB $db Database handler
  51. */
  52. public function __construct($db)
  53. {
  54. $this->db = $db;
  55. }
  56. /**
  57. * On the main mailing area, there is a box with statistics.
  58. * If you want to add a line in this report you must provide an
  59. * array of SQL request that returns two field:
  60. * One called "label", One called "nb".
  61. *
  62. * @return string[] Array with SQL requests
  63. */
  64. public function getSqlArrayForStats()
  65. {
  66. global $langs;
  67. $langs->load("members");
  68. // Array for requests for statistics board
  69. $statssql = array();
  70. $statssql[0] = "SELECT '".$this->db->escape($langs->trans("FundationMembers"))."' as label, count(*) as nb";
  71. $statssql[0] .= " FROM ".MAIN_DB_PREFIX."adherent where statut = 1 and entity IN (".getEntity('member').")";
  72. return $statssql;
  73. }
  74. /**
  75. * Return here number of distinct emails returned by your selector.
  76. * For example if this selector is used to extract 500 different
  77. * emails from a text file, this function must return 500.
  78. *
  79. * @param string $sql Requete sql de comptage
  80. * @return int|string Nb of recipient, or <0 if error, or '' if NA
  81. */
  82. public function getNbOfRecipients($sql = '')
  83. {
  84. $sql = "SELECT count(distinct(a.email)) as nb";
  85. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
  86. $sql .= " WHERE (a.email IS NOT NULL AND a.email != '') AND a.entity IN (".getEntity('member').")";
  87. // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
  88. return parent::getNbOfRecipients($sql);
  89. }
  90. /**
  91. * Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings
  92. *
  93. * @return string Retourne zone select
  94. */
  95. public function formFilter()
  96. {
  97. global $conf, $langs;
  98. // Load translation files required by the page
  99. $langs->loadLangs(array("members", "companies", "categories"));
  100. $form = new Form($this->db);
  101. $s = '';
  102. // Status
  103. $s .= '<select id="filter_fraise" name="filter" class="flat">';
  104. $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
  105. $s .= '<option value="draft">'.$langs->trans("MemberStatusDraft").'</option>';
  106. $s .= '<option value="1a">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusPaidShort").')</option>';
  107. $s .= '<option value="1b">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusActiveLateShort").')</option>';
  108. $s .= '<option value="0">'.$langs->trans("MemberStatusResiliatedShort").'</option>';
  109. $s .= '</select> ';
  110. $s .= ajax_combobox("filter_fraise");
  111. $s .= '<select id="filter_type_fraise" name="filter_type" class="flat">';
  112. $sql = "SELECT rowid, libelle as label, statut";
  113. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type";
  114. $sql .= " WHERE entity IN (".getEntity('member_type').")";
  115. $sql .= " ORDER BY rowid";
  116. $resql = $this->db->query($sql);
  117. if ($resql) {
  118. $num = $this->db->num_rows($resql);
  119. $s .= '<option value="-1">'.$langs->trans("Type").'</option>';
  120. if (!$num) {
  121. $s .= '<option value="0" disabled="disabled">'.$langs->trans("NoCategoriesDefined").'</option>';
  122. }
  123. $i = 0;
  124. while ($i < $num) {
  125. $obj = $this->db->fetch_object($resql);
  126. $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
  127. $s .= '</option>';
  128. $i++;
  129. }
  130. $s .= ajax_combobox("filter_type");
  131. } else {
  132. dol_print_error($this->db);
  133. }
  134. $s .= '</select>';
  135. $s .= ajax_combobox("filter_type_fraise");
  136. $s .= ' ';
  137. $s .= '<select id="filter_category_fraise" name="filter_category" class="flat">';
  138. // Show categories
  139. $sql = "SELECT rowid, label, type, visible";
  140. $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
  141. $sql .= " WHERE type = 3"; // We keep only categories for members
  142. // $sql.= " AND visible > 0"; // We ignore the property visible because member's categories does not use this property (only products categories use it).
  143. $sql .= " AND entity = ".$conf->entity;
  144. $sql .= " ORDER BY label";
  145. //print $sql;
  146. $resql = $this->db->query($sql);
  147. if ($resql) {
  148. $num = $this->db->num_rows($resql);
  149. $s .= '<option value="-1">'.$langs->trans("Category").'</option>';
  150. if (!$num) {
  151. $s .= '<option value="0" disabled>'.$langs->trans("NoCategoriesDefined").'</option>';
  152. }
  153. $i = 0;
  154. while ($i < $num) {
  155. $obj = $this->db->fetch_object($resql);
  156. $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
  157. $s .= '</option>';
  158. $i++;
  159. }
  160. $s .= ajax_combobox("filter_category_fraise");
  161. } else {
  162. dol_print_error($this->db);
  163. }
  164. $s .= '</select>';
  165. $s .= '<br>';
  166. $s .= $langs->trans("DateEndSubscription").': &nbsp;';
  167. $s .= $langs->trans("After").' > '.$form->selectDate(-1, 'subscriptionafter', 0, 0, 1, 'fraise', 1, 0, 0);
  168. $s .= ' &nbsp; ';
  169. $s .= $langs->trans("Before").' < '.$form->selectDate(-1, 'subscriptionbefore', 0, 0, 1, 'fraise', 1, 0, 0);
  170. return $s;
  171. }
  172. /**
  173. * Renvoie url lien vers fiche de la source du destinataire du mailing
  174. *
  175. * @param int $id ID
  176. * @return string Url lien
  177. */
  178. public function url($id)
  179. {
  180. return '<a href="'.DOL_URL_ROOT.'/adherents/card.php?rowid='.$id.'">'.img_object('', "user").'</a>';
  181. }
  182. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  183. /**
  184. * Ajoute destinataires dans table des cibles
  185. *
  186. * @param int $mailing_id Id of emailing
  187. * @return int < 0 si erreur, nb ajout si ok
  188. */
  189. public function add_to_target($mailing_id)
  190. {
  191. // phpcs:enable
  192. global $langs, $_POST;
  193. // Load translation files required by the page
  194. $langs->loadLangs(array("members", "companies"));
  195. $cibles = array();
  196. $now = dol_now();
  197. $dateendsubscriptionafter = dol_mktime(GETPOST('subscriptionafterhour', 'int'), GETPOST('subscriptionaftermin', 'int'), GETPOST('subscriptionaftersec', 'int'), GETPOST('subscriptionaftermonth', 'int'), GETPOST('subscriptionafterday', 'int'), GETPOST('subscriptionafteryear', 'int'));
  198. $dateendsubscriptionbefore = dol_mktime(GETPOST('subscriptionbeforehour', 'int'), GETPOST('subscriptionbeforemin', 'int'), GETPOST('subscriptionbeforesec', 'int'), GETPOST('subscriptionbeforemonth', 'int'), GETPOST('subscriptionbeforeday', 'int'), GETPOST('subscriptionbeforeyear', 'int'));
  199. // La requete doit retourner: id, email, fk_contact, name, firstname
  200. $sql = "SELECT a.rowid as id, a.email as email, null as fk_contact, ";
  201. $sql .= " a.lastname, a.firstname,";
  202. $sql .= " a.datefin, a.civility as civility_id, a.login, a.societe"; // Other fields
  203. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
  204. if (GETPOST('filter_category', 'int') > 0) {
  205. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie_member as cm ON cm.fk_member = a.rowid";
  206. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie as c ON c.rowid = cm.fk_categorie AND c.rowid = ".((int) GETPOST('filter_category', 'int'));
  207. }
  208. $sql .= " , ".MAIN_DB_PREFIX."adherent_type as ta";
  209. $sql .= " WHERE a.entity IN (".getEntity('member').") AND a.email <> ''"; // Note that null != '' is false
  210. $sql .= " AND a.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  211. // Filter on status
  212. if (GETPOST("filter", 'aZ09') == 'draft') {
  213. $sql .= " AND a.statut = -1";
  214. } elseif (GETPOST("filter", 'aZ09') == '1a') {
  215. $sql .= " AND a.statut=1 AND (a.datefin >= '".$this->db->idate($now)."' OR ta.subscription = 0)";
  216. } elseif (GETPOST("filter", 'aZ09') == '1b') {
  217. $sql .= " AND a.statut=1 AND ((a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."') AND ta.subscription = 1)";
  218. } elseif (GETPOST("filter", 'aZ09') === '0') {
  219. $sql .= " AND a.statut=0";
  220. }
  221. // Filter on date
  222. if ($dateendsubscriptionafter > 0) {
  223. $sql .= " AND datefin > '".$this->db->idate($dateendsubscriptionafter)."'";
  224. }
  225. if ($dateendsubscriptionbefore > 0) {
  226. $sql .= " AND datefin < '".$this->db->idate($dateendsubscriptionbefore)."'";
  227. }
  228. $sql .= " AND a.fk_adherent_type = ta.rowid";
  229. // Filter on type
  230. if (GETPOST('filter_type', 'int') > 0) {
  231. $sql .= " AND ta.rowid = ".((int) GETPOST('filter_type', 'int'));
  232. }
  233. $sql .= " ORDER BY a.email";
  234. //print $sql;
  235. // Add targets into table
  236. dol_syslog(get_class($this)."::add_to_target", LOG_DEBUG);
  237. $result = $this->db->query($sql);
  238. if ($result) {
  239. $num = $this->db->num_rows($result);
  240. $i = 0;
  241. $j = 0;
  242. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  243. $old = '';
  244. while ($i < $num) {
  245. $obj = $this->db->fetch_object($result);
  246. if ($old <> $obj->email) {
  247. $cibles[$j] = array(
  248. 'email' => $obj->email,
  249. 'fk_contact' => $obj->fk_contact,
  250. 'lastname' => $obj->lastname,
  251. 'firstname' => $obj->firstname,
  252. 'other' =>
  253. ($langs->transnoentities("Login").'='.$obj->login).';'.
  254. ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
  255. ($langs->transnoentities("DateEnd").'='.dol_print_date($this->db->jdate($obj->datefin), 'day')).';'.
  256. ($langs->transnoentities("Company").'='.$obj->societe),
  257. 'source_url' => $this->url($obj->id),
  258. 'source_id' => $obj->id,
  259. 'source_type' => 'member'
  260. );
  261. $old = $obj->email;
  262. $j++;
  263. }
  264. $i++;
  265. }
  266. } else {
  267. dol_syslog($this->db->error());
  268. $this->error = $this->db->error();
  269. return -1;
  270. }
  271. return parent::addTargetsToDatabase($mailing_id, $cibles);
  272. }
  273. }