dolibarr  9.0.0
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 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 <http://www.gnu.org/licenses/>.
18  * or see http://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  var $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  var $desc='Foundation members with emails';
39  // Set to 1 if selector is available for admin users only
40  var $require_admin=0;
41 
42  var $require_module=array('adherent');
43  var $picto='user';
44 
48  public $db;
49 
55  function __construct($db)
56  {
57  $this->db = $db;
58  }
59 
60 
70  {
71  global $langs;
72 
73  $langs->load("members");
74 
75  // Array for requests for statistics board
76  $statssql=array();
77 
78  $statssql[0] ="SELECT '".$this->db->escape($langs->trans("FundationMembers"))."' as label, count(*) as nb";
79  $statssql[0].=" FROM ".MAIN_DB_PREFIX."adherent where statut = 1 and entity IN (".getEntity('member').")";
80 
81  return $statssql;
82  }
83 
84 
93  function getNbOfRecipients($sql='')
94  {
95  $sql = "SELECT count(distinct(a.email)) as nb";
96  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
97  $sql .= " WHERE (a.email IS NOT NULL AND a.email != '') AND a.entity IN (".getEntity('member').")";
98 
99  // La requete doit retourner un champ "nb" pour etre comprise
100  // par parent::getNbOfRecipients
101  return parent::getNbOfRecipients($sql);
102  }
103 
104 
110  function formFilter()
111  {
112  global $conf, $langs;
113 
114  // Load translation files required by the page
115  $langs->loadLangs(array("members","companies","categories"));
116 
117  $form=new Form($this->db);
118 
119  $s='';
120 
121  // Status
122  $s.=$langs->trans("Status").': ';
123  $s.='<select name="filter" class="flat">';
124  $s.='<option value="none">&nbsp;</option>';
125  $s.='<option value="-1">'.$langs->trans("MemberStatusDraft").'</option>';
126  $s.='<option value="1a">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusPaidShort").')</option>';
127  $s.='<option value="1b">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusActiveLateShort").')</option>';
128  $s.='<option value="0">'.$langs->trans("MemberStatusResiliatedShort").'</option>';
129  $s.='</select> ';
130  $s.=$langs->trans("Type").': ';
131  $s.='<select name="filter_type" class="flat">';
132  $sql = "SELECT rowid, libelle, statut";
133  $sql.= " FROM ".MAIN_DB_PREFIX."adherent_type";
134  $sql.= " WHERE entity IN (".getEntity('member_type').")";
135  $sql.= " ORDER BY rowid";
136  $resql = $this->db->query($sql);
137  if ($resql)
138  {
139  $num = $this->db->num_rows($resql);
140 
141  $s.='<option value="0">&nbsp;</option>';
142  if (! $num) $s.='<option value="0" disabled="disabled">'.$langs->trans("NoCategoriesDefined").'</option>';
143 
144  $i = 0;
145  while ($i < $num)
146  {
147  $obj = $this->db->fetch_object($resql);
148 
149  $s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->libelle,38,'middle');
150  $s.='</option>';
151  $i++;
152  }
153  }
154  else
155  {
156  dol_print_error($this->db);
157  }
158 
159  $s.='</select>';
160 
161  $s.=' ';
162 
163  $s.=$langs->trans("Category").': ';
164  $s.='<select name="filter_category" class="flat">';
165 
166  // Show categories
167  $sql = "SELECT rowid, label, type, visible";
168  $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
169  $sql.= " WHERE type = 3"; // We keep only categories for members
170  // $sql.= " AND visible > 0"; // We ignore the property visible because member's categories does not use this property (only products categories use it).
171  $sql.= " AND entity = ".$conf->entity;
172  $sql.= " ORDER BY label";
173 
174  //print $sql;
175  $resql = $this->db->query($sql);
176  if ($resql)
177  {
178  $num = $this->db->num_rows($resql);
179 
180  $s.='<option value="0">&nbsp;</option>';
181  if (! $num) $s.='<option value="0" disabled>'.$langs->trans("NoCategoriesDefined").'</option>';
182 
183  $i = 0;
184  while ($i < $num)
185  {
186  $obj = $this->db->fetch_object($resql);
187 
188  $s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->label,38,'middle');
189  $s.='</option>';
190  $i++;
191  }
192  }
193  else
194  {
195  dol_print_error($this->db);
196  }
197 
198  $s.='</select>';
199 
200 
201  $s.='<br>';
202  $s.=$langs->trans("DateEndSubscription").': &nbsp;';
203  $s.=$langs->trans("After").' > '.$form->selectDate(-1,'subscriptionafter',0,0,1,'fraise',1,0,0);
204  $s.=' &nbsp; ';
205  $s.=$langs->trans("Before").' < '.$form->selectDate(-1,'subscriptionbefore',0,0,1,'fraise',1,0,0);
206 
207  return $s;
208  }
209 
210 
217  function url($id)
218  {
219  return '<a href="'.DOL_URL_ROOT.'/adherents/card.php?rowid='.$id.'">'.img_object('',"user").'</a>';
220  }
221 
222 
223  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
231  function add_to_target($mailing_id,$filtersarray=array())
232  {
233  // phpcs:enable
234  // Deprecation warning
235  if ($filtersarray) {
236  dol_syslog(__METHOD__ . ": filtersarray parameter is deprecated", LOG_WARNING);
237  }
238 
239  global $langs,$_POST;
240 
241  // Load translation files required by the page
242  $langs->loadLangs(array("members","companies"));
243 
244  $cibles = array();
245  $now=dol_now();
246 
247  $dateendsubscriptionafter=dol_mktime($_POST['subscriptionafterhour'],$_POST['subscriptionaftermin'],$_POST['subscriptionaftersec'],$_POST['subscriptionaftermonth'],$_POST['subscriptionafterday'],$_POST['subscriptionafteryear']);
248  $dateendsubscriptionbefore=dol_mktime($_POST['subscriptionbeforehour'],$_POST['subscriptionbeforemin'],$_POST['subscriptionbeforesec'],$_POST['subscriptionbeforemonth'],$_POST['subscriptionbeforeday'],$_POST['subscriptionbeforeyear']);
249 
250  // La requete doit retourner: id, email, fk_contact, name, firstname
251  $sql = "SELECT a.rowid as id, a.email as email, null as fk_contact, ";
252  $sql.= " a.lastname, a.firstname,";
253  $sql.= " a.datefin, a.civility as civility_id, a.login, a.societe"; // Other fields
254  $sql.= " FROM ".MAIN_DB_PREFIX."adherent as a";
255  if ($_POST['filter_category'])
256  {
257  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_member as cm ON cm.fk_member = a.rowid";
258  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie as c ON c.rowid = cm.fk_categorie";
259  }
260  $sql.= " , ".MAIN_DB_PREFIX."adherent_type as ta";
261  $sql.= " WHERE a.entity IN (".getEntity('member').") AND a.email <> ''"; // Note that null != '' is false
262  $sql.= " AND a.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$this->db->escape($mailing_id).")";
263  // Filter on status
264  if (isset($_POST["filter"]) && $_POST["filter"] == '-1') $sql.= " AND a.statut=-1";
265  if (isset($_POST["filter"]) && $_POST["filter"] == '1a') $sql.= " AND a.statut=1 AND a.datefin >= '".$this->db->idate($now)."'";
266  if (isset($_POST["filter"]) && $_POST["filter"] == '1b') $sql.= " AND a.statut=1 AND (a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."')";
267  if (isset($_POST["filter"]) && $_POST["filter"] == '0') $sql.= " AND a.statut=0";
268  // Filter on date
269  if ($dateendsubscriptionafter > 0) $sql.=" AND datefin > '".$this->db->idate($dateendsubscriptionafter)."'";
270  if ($dateendsubscriptionbefore > 0) $sql.=" AND datefin < '".$this->db->idate($dateendsubscriptionbefore)."'";
271  $sql.= " AND a.fk_adherent_type = ta.rowid";
272  // Filter on type
273  if ($_POST['filter_type']) $sql.= " AND ta.rowid='".$_POST['filter_type']."'";
274  // Filter on category
275  if ($_POST['filter_category']) $sql.= " AND c.rowid='".$_POST['filter_category']."'";
276  $sql.= " ORDER BY a.email";
277  //print $sql;
278 
279  // Add targets into table
280  dol_syslog(get_class($this)."::add_to_target", LOG_DEBUG);
281  $result=$this->db->query($sql);
282  if ($result)
283  {
284  $num = $this->db->num_rows($result);
285  $i = 0;
286  $j = 0;
287 
288  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
289 
290  $old = '';
291  while ($i < $num)
292  {
293  $obj = $this->db->fetch_object($result);
294  if ($old <> $obj->email)
295  {
296  $cibles[$j] = array(
297  'email' => $obj->email,
298  'fk_contact' => $obj->fk_contact,
299  'lastname' => $obj->lastname,
300  'firstname' => $obj->firstname,
301  'other' =>
302  ($langs->transnoentities("Login").'='.$obj->login).';'.
303  ($langs->transnoentities("UserTitle").'='.($obj->civility_id?$langs->transnoentities("Civility".$obj->civility_id):'')).';'.
304  ($langs->transnoentities("DateEnd").'='.dol_print_date($this->db->jdate($obj->datefin),'day')).';'.
305  ($langs->transnoentities("Company").'='.$obj->societe),
306  'source_url' => $this->url($obj->id),
307  'source_id' => $obj->id,
308  'source_type' => 'member'
309  );
310  $old = $obj->email;
311  $j++;
312  }
313 
314  $i++;
315  }
316  }
317  else
318  {
319  dol_syslog($this->db->error());
320  $this->error=$this->db->error();
321  return -1;
322  }
323 
324  return parent::add_to_target($mailing_id, $cibles);
325  }
326 }
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
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...
add_to_target($mailing_id, $filtersarray=array())
Ajoute destinataires dans table des cibles.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_now($mode='gmt')
Return date for now.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Class to generate target according to rule Fraise.
__construct($db)
Constructor.
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Parent class of emailing target selectors modules.