dolibarr 21.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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
28include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
29include_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
30
31
36{
37 public $name = 'FundationMembers'; // Identifiant du module mailing
38 // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
39 public $desc = 'Foundation members with emails';
40 // Set to 1 if selector is available for admin users only
41 public $require_admin = 0;
42
43 public $require_module = array('adherent');
44
45 public $enabled = 'isModEnabled("member")';
46
50 public $picto = 'user';
51
52
58 public function __construct($db)
59 {
60 $this->db = $db;
61 }
62
63
72 public function getSqlArrayForStats()
73 {
74 global $langs;
75
76 $langs->load("members");
77
78 // Array for requests for statistics board
79 $statssql = array();
80
81 $statssql[0] = "SELECT '".$this->db->escape($langs->trans("FundationMembers"))."' as label, count(*) as nb";
82 $statssql[0] .= " FROM ".MAIN_DB_PREFIX."adherent where statut = 1 and entity IN (".getEntity('member').")";
83
84 return $statssql;
85 }
86
87
96 public function getNbOfRecipients($sql = '')
97 {
98 global $conf;
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 if (empty($this->evenunsubscribe)) {
103 $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).")";
104 }
105
106 // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
107 return parent::getNbOfRecipients($sql);
108 }
109
110
116 public function formFilter()
117 {
118 global $conf, $langs;
119
120 // Load translation files required by the page
121 $langs->loadLangs(array("members", "companies", "categories"));
122
123 $form = new Form($this->db);
124
125 $s = '';
126
127 // Status
128 $s .= '<select id="filter_fraise" name="filter" class="flat">';
129 $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
130 $s .= '<option value="draft">'.$langs->trans("MemberStatusDraft").'</option>';
131 $s .= '<option value="1a">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusPaidShort").')</option>';
132 $s .= '<option value="1b">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusActiveLateShort").')</option>';
133 $s .= '<option value="0">'.$langs->trans("MemberStatusResiliatedShort").'</option>';
134 $s .= '</select> ';
135 $s .= ajax_combobox("filter_fraise");
136
137 $s .= '<select id="filter_type_fraise" name="filter_type" class="flat">';
138 $sql = "SELECT rowid, libelle as label, statut";
139 $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type";
140 $sql .= " WHERE entity IN (".getEntity('member_type').")";
141 $sql .= " ORDER BY rowid";
142 $resql = $this->db->query($sql);
143 if ($resql) {
144 $num = $this->db->num_rows($resql);
145
146 $s .= '<option value="-1">'.$langs->trans("Type").'</option>';
147 if (!$num) {
148 $s .= '<option value="0" disabled="disabled">'.$langs->trans("NoCategoriesDefined").'</option>';
149 }
150
151 $i = 0;
152 while ($i < $num) {
153 $obj = $this->db->fetch_object($resql);
154
155 $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
156 $s .= '</option>';
157 $i++;
158 }
159 $s .= ajax_combobox("filter_type");
160 } else {
161 dol_print_error($this->db);
162 }
163
164 $s .= '</select>';
165 $s .= ajax_combobox("filter_type_fraise");
166
167 $s .= ' ';
168
169 $s .= '<select id="filter_category_fraise" name="filter_category" class="flat">';
170
171 // Show categories
172 $sql = "SELECT rowid, label, type, visible";
173 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
174 $sql .= " WHERE type = 3"; // We keep only categories for members
175 // $sql.= " AND visible > 0"; // We ignore the property visible because member's categories does not use this property (only products categories use it).
176 $sql .= " AND entity = ".$conf->entity;
177 $sql .= " ORDER BY label";
178
179 //print $sql;
180 $resql = $this->db->query($sql);
181 if ($resql) {
182 $num = $this->db->num_rows($resql);
183
184 $s .= '<option value="-1">'.$langs->trans("Category").'</option>';
185 if (!$num) {
186 $s .= '<option value="0" disabled>'.$langs->trans("NoCategoriesDefined").'</option>';
187 }
188
189 $i = 0;
190 while ($i < $num) {
191 $obj = $this->db->fetch_object($resql);
192
193 $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
194 $s .= '</option>';
195 $i++;
196 }
197 $s .= ajax_combobox("filter_category_fraise");
198 } else {
199 dol_print_error($this->db);
200 }
201
202 $s .= '</select>';
203
204
205 $s .= '<br><span class="opacitymedium">';
206 $s .= $langs->trans("DateEndSubscription").': &nbsp;';
207 $s .= $langs->trans("After").' > </span>'.$form->selectDate(-1, 'subscriptionafter', 0, 0, 1, 'fraise', 1, 0, 0);
208 $s .= ' &nbsp; ';
209 $s .= '<span class="opacitymedium">'.$langs->trans("Before").' < </span>'.$form->selectDate(-1, 'subscriptionbefore', 0, 0, 1, 'fraise', 1, 0, 0);
210
211 return $s;
212 }
213
214
221 public function url($id)
222 {
223 return '<a href="'.DOL_URL_ROOT.'/adherents/card.php?rowid='.$id.'">'.img_object('', "user").'</a>';
224 }
225
226
227 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
234 public function add_to_target($mailing_id)
235 {
236 // phpcs:enable
237 global $conf, $langs;
238
239 // Load translation files required by the page
240 $langs->loadLangs(array("members", "companies"));
241
242 $cibles = array();
243 $now = dol_now();
244
245 $dateendsubscriptionafter = dol_mktime(GETPOSTINT('subscriptionafterhour'), GETPOSTINT('subscriptionaftermin'), GETPOSTINT('subscriptionaftersec'), GETPOSTINT('subscriptionaftermonth'), GETPOSTINT('subscriptionafterday'), GETPOSTINT('subscriptionafteryear'));
246 $dateendsubscriptionbefore = dol_mktime(GETPOSTINT('subscriptionbeforehour'), GETPOSTINT('subscriptionbeforemin'), GETPOSTINT('subscriptionbeforesec'), GETPOSTINT('subscriptionbeforemonth'), GETPOSTINT('subscriptionbeforeday'), GETPOSTINT('subscriptionbeforeyear'));
247
248 // La requete doit retourner: id, email, fk_contact, name, firstname
249 $sql = "SELECT a.rowid as id, a.email as email, null as fk_contact, ";
250 $sql .= " a.lastname, a.firstname,";
251 $sql .= " a.datefin, a.civility as civility_id, a.login, a.societe"; // Other fields
252 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
253 if (GETPOSTINT('filter_category') > 0) {
254 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie_member as cm ON cm.fk_member = a.rowid";
255 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie as c ON c.rowid = cm.fk_categorie AND c.rowid = ".(GETPOSTINT('filter_category'));
256 }
257 $sql .= " , ".MAIN_DB_PREFIX."adherent_type as ta";
258 $sql .= " WHERE a.entity IN (".getEntity('member').") AND a.email <> ''"; // Note that null != '' is false
259 $sql .= " AND a.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
260 // Filter on status
261 if (GETPOST("filter", 'aZ09') == 'draft') {
262 $sql .= " AND a.statut = -1";
263 } elseif (GETPOST("filter", 'aZ09') == '1a') {
264 $sql .= " AND a.statut=1 AND (a.datefin >= '".$this->db->idate($now)."' OR ta.subscription = 0)";
265 } elseif (GETPOST("filter", 'aZ09') == '1b') {
266 $sql .= " AND a.statut=1 AND ((a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."') AND ta.subscription = 1)";
267 } elseif (GETPOST("filter", 'aZ09') === '0') {
268 $sql .= " AND a.statut=0";
269 }
270 // Filter on date
271 if ($dateendsubscriptionafter > 0) {
272 $sql .= " AND datefin > '".$this->db->idate($dateendsubscriptionafter)."'";
273 }
274 if ($dateendsubscriptionbefore > 0) {
275 $sql .= " AND datefin < '".$this->db->idate($dateendsubscriptionbefore)."'";
276 }
277 $sql .= " AND a.fk_adherent_type = ta.rowid";
278 // Filter on type
279 if (GETPOSTINT('filter_type') > 0) {
280 $sql .= " AND ta.rowid = ".(GETPOSTINT('filter_type'));
281 }
282 if (empty($this->evenunsubscribe)) {
283 $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).")";
284 }
285 $sql .= " ORDER BY a.email";
286 //print $sql;
287
288 // Add targets into table
289 dol_syslog(get_class($this)."::add_to_target", LOG_DEBUG);
290 $result = $this->db->query($sql);
291 if ($result) {
292 $num = $this->db->num_rows($result);
293 $i = 0;
294 $j = 0;
295
296 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
297
298 $old = '';
299 while ($i < $num) {
300 $obj = $this->db->fetch_object($result);
301 if ($old != $obj->email) {
302 $cibles[$j] = array(
303 'email' => $obj->email,
304 'fk_contact' => $obj->fk_contact,
305 'lastname' => $obj->lastname,
306 'firstname' => $obj->firstname,
307 'other' =>
308 ($langs->transnoentities("Login").'='.$obj->login).';'.
309 ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
310 ($langs->transnoentities("DateEnd").'='.dol_print_date($this->db->jdate($obj->datefin), 'day')).';'.
311 ($langs->transnoentities("Company").'='.$obj->societe),
312 'source_url' => $this->url($obj->id),
313 'source_id' => $obj->id,
314 'source_type' => 'member'
315 );
316 $old = $obj->email;
317 $j++;
318 }
319
320 $i++;
321 }
322 } else {
323 dol_syslog($this->db->error());
324 $this->error = $this->db->error();
325 return -1;
326 }
327
328 return parent::addTargetsToDatabase($mailing_id, $cibles);
329 }
330}
$id
Definition account.php:39
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:457
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)
Provide the URL to the car of the source information of the recipient for the 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.
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.