dolibarr  16.0.5
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 
44  public $enabled = '$conf->adherent->enabled';
45 
49  public $picto = 'user';
50 
54  public $db;
55 
61  public function __construct($db)
62  {
63  $this->db = $db;
64  }
65 
66 
75  public function getSqlArrayForStats()
76  {
77  global $langs;
78 
79  $langs->load("members");
80 
81  // Array for requests for statistics board
82  $statssql = array();
83 
84  $statssql[0] = "SELECT '".$this->db->escape($langs->trans("FundationMembers"))."' as label, count(*) as nb";
85  $statssql[0] .= " FROM ".MAIN_DB_PREFIX."adherent where statut = 1 and entity IN (".getEntity('member').")";
86 
87  return $statssql;
88  }
89 
90 
99  public function getNbOfRecipients($sql = '')
100  {
101  $sql = "SELECT count(distinct(a.email)) as nb";
102  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
103  $sql .= " WHERE (a.email IS NOT NULL AND a.email != '') AND a.entity IN (".getEntity('member').")";
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>';
205  $s .= $langs->trans("DateEndSubscription").': &nbsp;';
206  $s .= $langs->trans("After").' > '.$form->selectDate(-1, 'subscriptionafter', 0, 0, 1, 'fraise', 1, 0, 0);
207  $s .= ' &nbsp; ';
208  $s .= $langs->trans("Before").' < '.$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 $langs, $_POST;
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(GETPOST('subscriptionafterhour', 'int'), GETPOST('subscriptionaftermin', 'int'), GETPOST('subscriptionaftersec', 'int'), GETPOST('subscriptionaftermonth', 'int'), GETPOST('subscriptionafterday', 'int'), GETPOST('subscriptionafteryear', 'int'));
245  $dateendsubscriptionbefore = dol_mktime(GETPOST('subscriptionbeforehour', 'int'), GETPOST('subscriptionbeforemin', 'int'), GETPOST('subscriptionbeforesec', 'int'), GETPOST('subscriptionbeforemonth', 'int'), GETPOST('subscriptionbeforeday', 'int'), GETPOST('subscriptionbeforeyear', 'int'));
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 (GETPOST('filter_category', 'int') > 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 = ".((int) GETPOST('filter_category', 'int'));
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 (GETPOST('filter_type', 'int') > 0) {
279  $sql .= " AND ta.rowid = ".((int) GETPOST('filter_type', 'int'));
280  }
281  $sql .= " ORDER BY a.email";
282  //print $sql;
283 
284  // Add targets into table
285  dol_syslog(get_class($this)."::add_to_target", LOG_DEBUG);
286  $result = $this->db->query($sql);
287  if ($result) {
288  $num = $this->db->num_rows($result);
289  $i = 0;
290  $j = 0;
291 
292  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
293 
294  $old = '';
295  while ($i < $num) {
296  $obj = $this->db->fetch_object($result);
297  if ($old <> $obj->email) {
298  $cibles[$j] = array(
299  'email' => $obj->email,
300  'fk_contact' => $obj->fk_contact,
301  'lastname' => $obj->lastname,
302  'firstname' => $obj->firstname,
303  'other' =>
304  ($langs->transnoentities("Login").'='.$obj->login).';'.
305  ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
306  ($langs->transnoentities("DateEnd").'='.dol_print_date($this->db->jdate($obj->datefin), 'day')).';'.
307  ($langs->transnoentities("Company").'='.$obj->societe),
308  'source_url' => $this->url($obj->id),
309  'source_id' => $obj->id,
310  'source_type' => 'member'
311  );
312  $old = $obj->email;
313  $j++;
314  }
315 
316  $i++;
317  }
318  } else {
319  dol_syslog($this->db->error());
320  $this->error = $this->db->error();
321  return -1;
322  }
323 
324  return parent::addTargetsToDatabase($mailing_id, $cibles);
325  }
326 }
mailing_fraise\add_to_target
add_to_target($mailing_id)
Ajoute destinataires dans table des cibles.
Definition: fraise.modules.php:233
mailing_fraise\__construct
__construct($db)
Constructor.
Definition: fraise.modules.php:61
mailing_fraise\url
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
Definition: fraise.modules.php:220
ajax_combobox
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:438
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_trunc
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.
Definition: functions.lib.php:3805
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
MailingTargets
Parent class of emailing target selectors modules.
Definition: modules_mailings.php:32
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
mailing_fraise\getNbOfRecipients
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
Definition: fraise.modules.php:99
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:148
mailing_fraise\getSqlArrayForStats
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
Definition: fraise.modules.php:75
mailing_fraise
Class to generate target according to rule Fraise.
Definition: fraise.modules.php:34
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
mailing_fraise\formFilter
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings.
Definition: fraise.modules.php:115
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
dol_mktime
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...
Definition: functions.lib.php:2757