pomme.modules.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /* Copyright (C) 2005-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/modules/mailings/pomme.modules.php
  21. * \ingroup mailing
  22. * \brief File of class to offer a selector of emailing targets of users.
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  25. /**
  26. * Class to offer a selector of emailing targets with Rule 'Pomme'.
  27. */
  28. class mailing_pomme extends MailingTargets
  29. {
  30. public $name = 'DolibarrUsers'; // Identifiant du module mailing
  31. // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
  32. public $desc = 'Dolibarr users with emails'; // Libelle utilise si aucune traduction pour MailingModuleDescXXX ou XXX=name trouv�e
  33. public $require_module = array(); // Module mailing actif si modules require_module actifs
  34. public $require_admin = 1; // Module mailing actif pour user admin ou non
  35. /**
  36. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  37. */
  38. public $picto = 'user';
  39. /**
  40. * @var DoliDB Database handler.
  41. */
  42. public $db;
  43. /**
  44. * Constructor
  45. *
  46. * @param DoliDB $db Database handler
  47. */
  48. public function __construct($db)
  49. {
  50. $this->db = $db;
  51. }
  52. /**
  53. * On the main mailing area, there is a box with statistics.
  54. * If you want to add a line in this report you must provide an
  55. * array of SQL request that returns two field:
  56. * One called "label", One called "nb".
  57. *
  58. * @return string[] Array with SQL requests
  59. */
  60. public function getSqlArrayForStats()
  61. {
  62. global $conf, $langs;
  63. $langs->load("users");
  64. $statssql = array();
  65. $sql = "SELECT '".$this->db->escape($langs->trans("DolibarrUsers"))."' as label,";
  66. $sql .= " count(distinct(u.email)) as nb";
  67. $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
  68. $sql .= " WHERE u.email != ''"; // u.email IS NOT NULL est implicite dans ce test
  69. $sql .= " AND u.entity IN (0,".$conf->entity.")";
  70. $statssql[0] = $sql;
  71. return $statssql;
  72. }
  73. /**
  74. * Return here number of distinct emails returned by your selector.
  75. * For example if this selector is used to extract 500 different
  76. * emails from a text file, this function must return 500.
  77. *
  78. * @param string $sql SQL request to use to count
  79. * @return int|string Nb of recipient, or <0 if error, or '' if NA
  80. */
  81. public function getNbOfRecipients($sql = '')
  82. {
  83. global $conf;
  84. $sql = "SELECT count(distinct(u.email)) as nb";
  85. $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
  86. $sql .= " WHERE u.email != ''"; // u.email IS NOT NULL est implicite dans ce test
  87. $sql .= " AND u.entity IN (0,".$conf->entity.")";
  88. // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
  89. return parent::getNbOfRecipients($sql);
  90. }
  91. /**
  92. * Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings
  93. *
  94. * @return string Retourne zone select
  95. */
  96. public function formFilter()
  97. {
  98. global $langs;
  99. $langs->load("users");
  100. $s = '';
  101. $s .= '<select id="filter_pomme"" name="filter" class="flat">';
  102. $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
  103. $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
  104. $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
  105. $s .= '</select>';
  106. $s .= ajax_combobox("filter_pomme");
  107. $s .= ' ';
  108. $s .= '<select id="filteremployee_pomme" name="filteremployee" class="flat">';
  109. $s .= '<option value="-1">'.$langs->trans("Employee").'</option>';
  110. $s .= '<option value="1">'.$langs->trans("Yes").'</option>';
  111. $s .= '<option value="0">'.$langs->trans("No").'</option>';
  112. $s .= '</select>';
  113. $s .= ajax_combobox("filteremployee_pomme");
  114. return $s;
  115. }
  116. /**
  117. * Renvoie url lien vers fiche de la source du destinataire du mailing
  118. *
  119. * @param int $id ID
  120. * @return string Url lien
  121. */
  122. public function url($id)
  123. {
  124. return '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$id.'">'.img_object('', "user").'</a>';
  125. }
  126. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  127. /**
  128. * Ajoute destinataires dans table des cibles
  129. *
  130. * @param int $mailing_id Id of emailing
  131. * @return int < 0 si erreur, nb ajout si ok
  132. */
  133. public function add_to_target($mailing_id)
  134. {
  135. // phpcs:enable
  136. global $conf, $langs;
  137. $langs->load("companies");
  138. $cibles = array();
  139. // La requete doit retourner: id, email, fk_contact, lastname, firstname
  140. $sql = "SELECT u.rowid as id, u.email as email, null as fk_contact,";
  141. $sql .= " u.lastname, u.firstname as firstname, u.civility as civility_id, u.login, u.office_phone";
  142. $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
  143. $sql .= " WHERE u.email <> ''"; // u.email IS NOT NULL est implicite dans ce test
  144. $sql .= " AND u.entity IN (0,".$conf->entity.")";
  145. $sql .= " AND u.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
  146. if (GETPOSTISSET("filter") && GETPOST("filter") == '1') {
  147. $sql .= " AND u.statut=1";
  148. }
  149. if (GETPOSTISSET("filter") && GETPOST("filter") == '0') {
  150. $sql .= " AND u.statut=0";
  151. }
  152. if (GETPOSTISSET("filteremployee") && GETPOSt("filteremployee") == '1') {
  153. $sql .= " AND u.employee=1";
  154. }
  155. if (GETPOSTISSET("filteremployee") && GETPOST("filteremployee") == '0') {
  156. $sql .= " AND u.employee=0";
  157. }
  158. $sql .= " ORDER BY u.email";
  159. // Stocke destinataires dans cibles
  160. $result = $this->db->query($sql);
  161. if ($result) {
  162. $num = $this->db->num_rows($result);
  163. $i = 0;
  164. $j = 0;
  165. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  166. $old = '';
  167. while ($i < $num) {
  168. $obj = $this->db->fetch_object($result);
  169. if ($old <> $obj->email) {
  170. $cibles[$j] = array(
  171. 'email' => $obj->email,
  172. 'fk_contact' => $obj->fk_contact,
  173. 'lastname' => $obj->lastname,
  174. 'firstname' => $obj->firstname,
  175. 'other' =>
  176. ($langs->transnoentities("Login").'='.$obj->login).';'.
  177. ($langs->transnoentities("UserTitle").'='.$obj->civility_id).';'.
  178. ($langs->transnoentities("PhonePro").'='.$obj->office_phone),
  179. 'source_url' => $this->url($obj->id),
  180. 'source_id' => $obj->id,
  181. 'source_type' => 'user'
  182. );
  183. $old = $obj->email;
  184. $j++;
  185. }
  186. $i++;
  187. }
  188. } else {
  189. dol_syslog($this->db->error());
  190. $this->error = $this->db->error();
  191. return -1;
  192. }
  193. return parent::addTargetsToDatabase($mailing_id, $cibles);
  194. }
  195. }