dolibarr  20.0.0-alpha
fraise.modules.php
Go to the documentation of this file.
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-2023 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 
27 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
28 include_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
29 
30 
35 {
36  public $name = 'FundationMembers'; // Identifiant du module mailing
37  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
38  public $desc = 'Foundation members with emails';
39  // Set to 1 if selector is available for admin users only
40  public $require_admin = 0;
41 
42  public $require_module = array('adherent');
43 
44  public $enabled = 'isModEnabled("member")';
45 
49  public $picto = 'user';
50 
51 
57  public function __construct($db)
58  {
59  $this->db = $db;
60  }
61 
62 
71  public function getSqlArrayForStats()
72  {
73  global $langs;
74 
75  $langs->load("members");
76 
77  // Array for requests for statistics board
78  $statssql = array();
79 
80  $statssql[0] = "SELECT '".$this->db->escape($langs->trans("FundationMembers"))."' as label, count(*) as nb";
81  $statssql[0] .= " FROM ".MAIN_DB_PREFIX."adherent where statut = 1 and entity IN (".getEntity('member').")";
82 
83  return $statssql;
84  }
85 
86 
95  public function getNbOfRecipients($sql = '')
96  {
97  global $conf;
98  $sql = "SELECT count(distinct(a.email)) as nb";
99  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
100  $sql .= " WHERE (a.email IS NOT NULL AND a.email != '') AND a.entity IN (".getEntity('member').")";
101  if (empty($this->evenunsubscribe)) {
102  $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = a.email and mu.entity = ".((int) $conf->entity).")";
103  }
104 
105  // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
106  return parent::getNbOfRecipients($sql);
107  }
108 
109 
115  public function formFilter()
116  {
117  global $conf, $langs;
118 
119  // Load translation files required by the page
120  $langs->loadLangs(array("members", "companies", "categories"));
121 
122  $form = new Form($this->db);
123 
124  $s = '';
125 
126  // Status
127  $s .= '<select id="filter_fraise" name="filter" class="flat">';
128  $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
129  $s .= '<option value="draft">'.$langs->trans("MemberStatusDraft").'</option>';
130  $s .= '<option value="1a">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusPaidShort").')</option>';
131  $s .= '<option value="1b">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusActiveLateShort").')</option>';
132  $s .= '<option value="0">'.$langs->trans("MemberStatusResiliatedShort").'</option>';
133  $s .= '</select> ';
134  $s .= ajax_combobox("filter_fraise");
135 
136  $s .= '<select id="filter_type_fraise" name="filter_type" class="flat">';
137  $sql = "SELECT rowid, libelle as label, statut";
138  $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type";
139  $sql .= " WHERE entity IN (".getEntity('member_type').")";
140  $sql .= " ORDER BY rowid";
141  $resql = $this->db->query($sql);
142  if ($resql) {
143  $num = $this->db->num_rows($resql);
144 
145  $s .= '<option value="-1">'.$langs->trans("Type").'</option>';
146  if (!$num) {
147  $s .= '<option value="0" disabled="disabled">'.$langs->trans("NoCategoriesDefined").'</option>';
148  }
149 
150  $i = 0;
151  while ($i < $num) {
152  $obj = $this->db->fetch_object($resql);
153 
154  $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
155  $s .= '</option>';
156  $i++;
157  }
158  $s .= ajax_combobox("filter_type");
159  } else {
160  dol_print_error($this->db);
161  }
162 
163  $s .= '</select>';
164  $s .= ajax_combobox("filter_type_fraise");
165 
166  $s .= ' ';
167 
168  $s .= '<select id="filter_category_fraise" name="filter_category" class="flat">';
169 
170  // Show categories
171  $sql = "SELECT rowid, label, type, visible";
172  $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
173  $sql .= " WHERE type = 3"; // We keep only categories for members
174  // $sql.= " AND visible > 0"; // We ignore the property visible because member's categories does not use this property (only products categories use it).
175  $sql .= " AND entity = ".$conf->entity;
176  $sql .= " ORDER BY label";
177 
178  //print $sql;
179  $resql = $this->db->query($sql);
180  if ($resql) {
181  $num = $this->db->num_rows($resql);
182 
183  $s .= '<option value="-1">'.$langs->trans("Category").'</option>';
184  if (!$num) {
185  $s .= '<option value="0" disabled>'.$langs->trans("NoCategoriesDefined").'</option>';
186  }
187 
188  $i = 0;
189  while ($i < $num) {
190  $obj = $this->db->fetch_object($resql);
191 
192  $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
193  $s .= '</option>';
194  $i++;
195  }
196  $s .= ajax_combobox("filter_category_fraise");
197  } else {
198  dol_print_error($this->db);
199  }
200 
201  $s .= '</select>';
202 
203 
204  $s .= '<br><span class="opacitymedium">';
205  $s .= $langs->trans("DateEndSubscription").': &nbsp;';
206  $s .= $langs->trans("After").' > </span>'.$form->selectDate(-1, 'subscriptionafter', 0, 0, 1, 'fraise', 1, 0, 0);
207  $s .= ' &nbsp; ';
208  $s .= '<span class="opacitymedium">'.$langs->trans("Before").' < </span>'.$form->selectDate(-1, 'subscriptionbefore', 0, 0, 1, 'fraise', 1, 0, 0);
209 
210  return $s;
211  }
212 
213 
220  public function url($id)
221  {
222  return '<a href="'.DOL_URL_ROOT.'/adherents/card.php?rowid='.$id.'">'.img_object('', "user").'</a>';
223  }
224 
225 
226  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
233  public function add_to_target($mailing_id)
234  {
235  // phpcs:enable
236  global $conf, $langs;
237 
238  // Load translation files required by the page
239  $langs->loadLangs(array("members", "companies"));
240 
241  $cibles = array();
242  $now = dol_now();
243 
244  $dateendsubscriptionafter = dol_mktime(GETPOSTINT('subscriptionafterhour'), GETPOSTINT('subscriptionaftermin'), GETPOSTINT('subscriptionaftersec'), GETPOSTINT('subscriptionaftermonth'), GETPOSTINT('subscriptionafterday'), GETPOSTINT('subscriptionafteryear'));
245  $dateendsubscriptionbefore = dol_mktime(GETPOSTINT('subscriptionbeforehour'), GETPOSTINT('subscriptionbeforemin'), GETPOSTINT('subscriptionbeforesec'), GETPOSTINT('subscriptionbeforemonth'), GETPOSTINT('subscriptionbeforeday'), GETPOSTINT('subscriptionbeforeyear'));
246 
247  // La requete doit retourner: id, email, fk_contact, name, firstname
248  $sql = "SELECT a.rowid as id, a.email as email, null as fk_contact, ";
249  $sql .= " a.lastname, a.firstname,";
250  $sql .= " a.datefin, a.civility as civility_id, a.login, a.societe"; // Other fields
251  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
252  if (GETPOSTINT('filter_category') > 0) {
253  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie_member as cm ON cm.fk_member = a.rowid";
254  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie as c ON c.rowid = cm.fk_categorie AND c.rowid = ".(GETPOSTINT('filter_category'));
255  }
256  $sql .= " , ".MAIN_DB_PREFIX."adherent_type as ta";
257  $sql .= " WHERE a.entity IN (".getEntity('member').") AND a.email <> ''"; // Note that null != '' is false
258  $sql .= " AND a.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
259  // Filter on status
260  if (GETPOST("filter", 'aZ09') == 'draft') {
261  $sql .= " AND a.statut = -1";
262  } elseif (GETPOST("filter", 'aZ09') == '1a') {
263  $sql .= " AND a.statut=1 AND (a.datefin >= '".$this->db->idate($now)."' OR ta.subscription = 0)";
264  } elseif (GETPOST("filter", 'aZ09') == '1b') {
265  $sql .= " AND a.statut=1 AND ((a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."') AND ta.subscription = 1)";
266  } elseif (GETPOST("filter", 'aZ09') === '0') {
267  $sql .= " AND a.statut=0";
268  }
269  // Filter on date
270  if ($dateendsubscriptionafter > 0) {
271  $sql .= " AND datefin > '".$this->db->idate($dateendsubscriptionafter)."'";
272  }
273  if ($dateendsubscriptionbefore > 0) {
274  $sql .= " AND datefin < '".$this->db->idate($dateendsubscriptionbefore)."'";
275  }
276  $sql .= " AND a.fk_adherent_type = ta.rowid";
277  // Filter on type
278  if (GETPOSTINT('filter_type') > 0) {
279  $sql .= " AND ta.rowid = ".(GETPOSTINT('filter_type'));
280  }
281  if (empty($this->evenunsubscribe)) {
282  $sql .= " AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = a.email and mu.entity = ".((int) $conf->entity).")";
283  }
284  $sql .= " ORDER BY a.email";
285  //print $sql;
286 
287  // Add targets into table
288  dol_syslog(get_class($this)."::add_to_target", LOG_DEBUG);
289  $result = $this->db->query($sql);
290  if ($result) {
291  $num = $this->db->num_rows($result);
292  $i = 0;
293  $j = 0;
294 
295  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
296 
297  $old = '';
298  while ($i < $num) {
299  $obj = $this->db->fetch_object($result);
300  if ($old != $obj->email) {
301  $cibles[$j] = array(
302  'email' => $obj->email,
303  'fk_contact' => $obj->fk_contact,
304  'lastname' => $obj->lastname,
305  'firstname' => $obj->firstname,
306  'other' =>
307  ($langs->transnoentities("Login").'='.$obj->login).';'.
308  ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
309  ($langs->transnoentities("DateEnd").'='.dol_print_date($this->db->jdate($obj->datefin), 'day')).';'.
310  ($langs->transnoentities("Company").'='.$obj->societe),
311  'source_url' => $this->url($obj->id),
312  'source_id' => $obj->id,
313  'source_type' => 'member'
314  );
315  $old = $obj->email;
316  $j++;
317  }
318 
319  $i++;
320  }
321  } else {
322  dol_syslog($this->db->error());
323  $this->error = $this->db->error();
324  return -1;
325  }
326 
327  return parent::addTargetsToDatabase($mailing_id, $cibles);
328  }
329 }
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:463
Class to manage generation of HTML components Only common components must be here.
Parent class of emailing target selectors modules.
Class to generate target according to rule Fraise.
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
add_to_target($mailing_id)
Ajoute destinataires dans table des cibles.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings.
__construct($db)
Constructor.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.