dolibarr  13.0.2
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 <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 
47  public $picto = 'user';
48 
52  public $db;
53 
59  public function __construct($db)
60  {
61  $this->db = $db;
62  }
63 
64 
73  public function getSqlArrayForStats()
74  {
75  global $langs;
76 
77  $langs->load("members");
78 
79  // Array for requests for statistics board
80  $statssql = array();
81 
82  $statssql[0] = "SELECT '".$this->db->escape($langs->trans("FundationMembers"))."' as label, count(*) as nb";
83  $statssql[0] .= " FROM ".MAIN_DB_PREFIX."adherent where statut = 1 and entity IN (".getEntity('member').")";
84 
85  return $statssql;
86  }
87 
88 
97  public function getNbOfRecipients($sql = '')
98  {
99  $sql = "SELECT count(distinct(a.email)) as nb";
100  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
101  $sql .= " WHERE (a.email IS NOT NULL AND a.email != '') AND a.entity IN (".getEntity('member').")";
102 
103  // La requete doit retourner un champ "nb" pour etre comprise
104  // par parent::getNbOfRecipients
105  return parent::getNbOfRecipients($sql);
106  }
107 
108 
114  public function formFilter()
115  {
116  global $conf, $langs;
117 
118  // Load translation files required by the page
119  $langs->loadLangs(array("members", "companies", "categories"));
120 
121  $form = new Form($this->db);
122 
123  $s = '';
124 
125  // Status
126  $s .= $langs->trans("Status").': ';
127  $s .= '<select name="filter" class="flat">';
128  $s .= '<option value="none">&nbsp;</option>';
129  $s .= '<option value="-1">'.$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 .= $langs->trans("Type").': ';
135  $s .= '<select name="filter_type" class="flat">';
136  $sql = "SELECT rowid, libelle as label, statut";
137  $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type";
138  $sql .= " WHERE entity IN (".getEntity('member_type').")";
139  $sql .= " ORDER BY rowid";
140  $resql = $this->db->query($sql);
141  if ($resql)
142  {
143  $num = $this->db->num_rows($resql);
144 
145  $s .= '<option value="0">&nbsp;</option>';
146  if (!$num) $s .= '<option value="0" disabled="disabled">'.$langs->trans("NoCategoriesDefined").'</option>';
147 
148  $i = 0;
149  while ($i < $num)
150  {
151  $obj = $this->db->fetch_object($resql);
152 
153  $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
154  $s .= '</option>';
155  $i++;
156  }
157  } else {
158  dol_print_error($this->db);
159  }
160 
161  $s .= '</select>';
162 
163  $s .= ' ';
164 
165  $s .= $langs->trans("Category").': ';
166  $s .= '<select name="filter_category" class="flat">';
167 
168  // Show categories
169  $sql = "SELECT rowid, label, type, visible";
170  $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
171  $sql .= " WHERE type = 3"; // We keep only categories for members
172  // $sql.= " AND visible > 0"; // We ignore the property visible because member's categories does not use this property (only products categories use it).
173  $sql .= " AND entity = ".$conf->entity;
174  $sql .= " ORDER BY label";
175 
176  //print $sql;
177  $resql = $this->db->query($sql);
178  if ($resql)
179  {
180  $num = $this->db->num_rows($resql);
181 
182  $s .= '<option value="0">&nbsp;</option>';
183  if (!$num) $s .= '<option value="0" disabled>'.$langs->trans("NoCategoriesDefined").'</option>';
184 
185  $i = 0;
186  while ($i < $num)
187  {
188  $obj = $this->db->fetch_object($resql);
189 
190  $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
191  $s .= '</option>';
192  $i++;
193  }
194  } else {
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  public 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.ScopeNotCamelCaps
230  public function add_to_target($mailing_id)
231  {
232  // phpcs:enable
233  global $langs, $_POST;
234 
235  // Load translation files required by the page
236  $langs->loadLangs(array("members", "companies"));
237 
238  $cibles = array();
239  $now = dol_now();
240 
241  $dateendsubscriptionafter = dol_mktime(GETPOST('subscriptionafterhour', 'int'), GETPOST('subscriptionaftermin', 'int'), GETPOST('subscriptionaftersec', 'int'), GETPOST('subscriptionaftermonth', 'int'), GETPOST('subscriptionafterday', 'int'), GETPOST('subscriptionafteryear', 'int'));
242  $dateendsubscriptionbefore = dol_mktime(GETPOST('subscriptionbeforehour', 'int'), GETPOST('subscriptionbeforemin', 'int'), GETPOST('subscriptionbeforesec', 'int'), GETPOST('subscriptionbeforemonth', 'int'), GETPOST('subscriptionbeforeday', 'int'), GETPOST('subscriptionbeforeyear', 'int'));
243 
244  // La requete doit retourner: id, email, fk_contact, name, firstname
245  $sql = "SELECT a.rowid as id, a.email as email, null as fk_contact, ";
246  $sql .= " a.lastname, a.firstname,";
247  $sql .= " a.datefin, a.civility as civility_id, a.login, a.societe"; // Other fields
248  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
249  if (GETPOST('filter_category'))
250  {
251  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie_member as cm ON cm.fk_member = a.rowid";
252  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie as c ON c.rowid = cm.fk_categorie AND c.rowid = ".((int) GETPOST('filter_category', 'int'));
253  }
254  $sql .= " , ".MAIN_DB_PREFIX."adherent_type as ta";
255  $sql .= " WHERE a.entity IN (".getEntity('member').") AND a.email <> ''"; // Note that null != '' is false
256  $sql .= " AND a.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$this->db->escape($mailing_id).")";
257  // Filter on status
258  if (GETPOST("filter") == '-1') {
259  $sql .= " AND a.statut=-1";
260  }
261  if (GETPOST("filter", 'aZ09') == '1a') $sql .= " AND a.statut=1 AND (a.datefin >= '".$this->db->idate($now)."' OR ta.subscription = 0)";
262  if (GETPOST("filter", 'aZ09') == '1b') $sql .= " AND a.statut=1 AND ((a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."') AND ta.subscription = 1)";
263  if (GETPOST("filter", 'aZ09') === '0') $sql .= " AND a.statut=0";
264  // Filter on date
265  if ($dateendsubscriptionafter > 0) $sql .= " AND datefin > '".$this->db->idate($dateendsubscriptionafter)."'";
266  if ($dateendsubscriptionbefore > 0) $sql .= " AND datefin < '".$this->db->idate($dateendsubscriptionbefore)."'";
267  $sql .= " AND a.fk_adherent_type = ta.rowid";
268  // Filter on type
269  if (GETPOST('filter_type', 'int') > 0) $sql .= " AND ta.rowid='".$this->db->escape(GETPOST('filter_type', 'int'))."'";
270  $sql .= " ORDER BY a.email";
271  //print $sql;
272 
273  // Add targets into table
274  dol_syslog(get_class($this)."::add_to_target", LOG_DEBUG);
275  $result = $this->db->query($sql);
276  if ($result)
277  {
278  $num = $this->db->num_rows($result);
279  $i = 0;
280  $j = 0;
281 
282  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
283 
284  $old = '';
285  while ($i < $num)
286  {
287  $obj = $this->db->fetch_object($result);
288  if ($old <> $obj->email)
289  {
290  $cibles[$j] = array(
291  'email' => $obj->email,
292  'fk_contact' => $obj->fk_contact,
293  'lastname' => $obj->lastname,
294  'firstname' => $obj->firstname,
295  'other' =>
296  ($langs->transnoentities("Login").'='.$obj->login).';'.
297  ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
298  ($langs->transnoentities("DateEnd").'='.dol_print_date($this->db->jdate($obj->datefin), 'day')).';'.
299  ($langs->transnoentities("Company").'='.$obj->societe),
300  'source_url' => $this->url($obj->id),
301  'source_id' => $obj->id,
302  'source_type' => 'member'
303  );
304  $old = $obj->email;
305  $j++;
306  }
307 
308  $i++;
309  }
310  } else {
311  dol_syslog($this->db->error());
312  $this->error = $this->db->error();
313  return -1;
314  }
315 
316  return parent::addTargetsToDatabase($mailing_id, $cibles);
317  }
318 }
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
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...
dol_now($mode= 'auto')
Return date for now.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage generation of HTML components Only common components must be here.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_print_date($time, $format= '', $tzoutput= 'auto', $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.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $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 &#39;...&#39; if string larger than length.
__construct($db)
Constructor.
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
getNbOfRecipients($sql= '')
Return here number of distinct emails returned by your selector.
Parent class of emailing target selectors modules.