pomme.modules.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 with Rule 'Pomme'.
  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 'Peche'.
  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 Number of recipients
  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
  89. // par parent::getNbOfRecipients
  90. return parent::getNbOfRecipients($sql);
  91. }
  92. /**
  93. * Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings
  94. *
  95. * @return string Retourne zone select
  96. */
  97. public function formFilter()
  98. {
  99. global $langs;
  100. $langs->load("users");
  101. $s = '';
  102. $s .= $langs->trans("Status").': ';
  103. $s .= '<select name="filter" class="flat">';
  104. $s .= '<option value="-1">&nbsp;</option>';
  105. $s .= '<option value="1">'.$langs->trans("Enabled").'</option>';
  106. $s .= '<option value="0">'.$langs->trans("Disabled").'</option>';
  107. $s .= '</select>';
  108. $s .= ' ';
  109. $s .= $langs->trans("Employee").': ';
  110. $s .= '<select name="filteremployee" class="flat">';
  111. $s .= '<option value="-1">&nbsp;</option>';
  112. $s .= '<option value="1">'.$langs->trans("Yes").'</option>';
  113. $s .= '<option value="0">'.$langs->trans("No").'</option>';
  114. $s .= '</select>';
  115. return $s;
  116. }
  117. /**
  118. * Renvoie url lien vers fiche de la source du destinataire du mailing
  119. *
  120. * @param int $id ID
  121. * @return string Url lien
  122. */
  123. public function url($id)
  124. {
  125. return '<a href="'.DOL_URL_ROOT.'/user/card.php?id='.$id.'">'.img_object('', "user").'</a>';
  126. }
  127. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  128. /**
  129. * Ajoute destinataires dans table des cibles
  130. *
  131. * @param int $mailing_id Id of emailing
  132. * @return int < 0 si erreur, nb ajout si ok
  133. */
  134. public function add_to_target($mailing_id)
  135. {
  136. // phpcs:enable
  137. global $conf, $langs;
  138. $langs->load("companies");
  139. $cibles = array();
  140. // La requete doit retourner: id, email, fk_contact, lastname, firstname
  141. $sql = "SELECT u.rowid as id, u.email as email, null as fk_contact,";
  142. $sql .= " u.lastname, u.firstname as firstname, u.civility as civility_id, u.login, u.office_phone";
  143. $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
  144. $sql .= " WHERE u.email <> ''"; // u.email IS NOT NULL est implicite dans ce test
  145. $sql .= " AND u.entity IN (0,".$conf->entity.")";
  146. $sql .= " AND u.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
  147. if (isset($_POST["filter"]) && $_POST["filter"] == '1') $sql .= " AND u.statut=1";
  148. if (isset($_POST["filter"]) && $_POST["filter"] == '0') $sql .= " AND u.statut=0";
  149. if (isset($_POST["filteremployee"]) && $_POST["filteremployee"] == '1') $sql .= " AND u.employee=1";
  150. if (isset($_POST["filteremployee"]) && $_POST["filteremployee"] == '0') $sql .= " AND u.employee=0";
  151. $sql .= " ORDER BY u.email";
  152. // Stocke destinataires dans cibles
  153. $result = $this->db->query($sql);
  154. if ($result)
  155. {
  156. $num = $this->db->num_rows($result);
  157. $i = 0;
  158. $j = 0;
  159. dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
  160. $old = '';
  161. while ($i < $num)
  162. {
  163. $obj = $this->db->fetch_object($result);
  164. if ($old <> $obj->email)
  165. {
  166. $cibles[$j] = array(
  167. 'email' => $obj->email,
  168. 'fk_contact' => $obj->fk_contact,
  169. 'lastname' => $obj->lastname,
  170. 'firstname' => $obj->firstname,
  171. 'other' =>
  172. ($langs->transnoentities("Login").'='.$obj->login).';'.
  173. ($langs->transnoentities("UserTitle").'='.$obj->civility_id).';'.
  174. ($langs->transnoentities("PhonePro").'='.$obj->office_phone),
  175. 'source_url' => $this->url($obj->id),
  176. 'source_id' => $obj->id,
  177. 'source_type' => 'user'
  178. );
  179. $old = $obj->email;
  180. $j++;
  181. }
  182. $i++;
  183. }
  184. } else {
  185. dol_syslog($this->db->error());
  186. $this->error = $this->db->error();
  187. return -1;
  188. }
  189. return parent::addTargetsToDatabase($mailing_id, $cibles);
  190. }
  191. }