dolibarr 21.0.0-beta
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-2024 Frédéric France <frederic.france@free.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
48 public $enabled = 'isModEnabled("member")';
49
53 public $picto = 'user';
54
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 global $conf;
102 $sql = "SELECT count(distinct(a.email)) as nb";
103 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
104 $sql .= " WHERE (a.email IS NOT NULL AND a.email != '') AND a.entity IN (".getEntity('member').")";
105 if (empty($this->evenunsubscribe)) {
106 $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).")";
107 }
108
109 // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
110 return parent::getNbOfRecipients($sql);
111 }
112
113
119 public function formFilter()
120 {
121 global $conf, $langs;
122
123 // Load translation files required by the page
124 $langs->loadLangs(array("members", "companies", "categories"));
125
126 $form = new Form($this->db);
127
128 $s = '';
129
130 // Status
131 $s .= '<select id="filter_fraise" name="filter" class="flat">';
132 $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
133 $s .= '<option value="draft">'.$langs->trans("MemberStatusDraft").'</option>';
134 $s .= '<option value="1a">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusPaidShort").')</option>';
135 $s .= '<option value="1b">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusActiveLateShort").')</option>';
136 $s .= '<option value="0">'.$langs->trans("MemberStatusResiliatedShort").'</option>';
137 $s .= '</select> ';
138 $s .= ajax_combobox("filter_fraise");
139
140 $s .= '<select id="filter_type_fraise" name="filter_type" class="flat">';
141 $sql = "SELECT rowid, libelle as label, statut";
142 $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type";
143 $sql .= " WHERE entity IN (".getEntity('member_type').")";
144 $sql .= " ORDER BY rowid";
145 $resql = $this->db->query($sql);
146 if ($resql) {
147 $num = $this->db->num_rows($resql);
148
149 $s .= '<option value="-1">'.$langs->trans("Type").'</option>';
150 if (!$num) {
151 $s .= '<option value="0" disabled="disabled">'.$langs->trans("NoCategoriesDefined").'</option>';
152 }
153
154 $i = 0;
155 while ($i < $num) {
156 $obj = $this->db->fetch_object($resql);
157
158 $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
159 $s .= '</option>';
160 $i++;
161 }
162 $s .= ajax_combobox("filter_type");
163 } else {
164 dol_print_error($this->db);
165 }
166
167 $s .= '</select>';
168 $s .= ajax_combobox("filter_type_fraise");
169
170 $s .= ' ';
171
172 $s .= '<select id="filter_category_fraise" name="filter_category" class="flat">';
173
174 // Show categories
175 $sql = "SELECT rowid, label, type, visible";
176 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
177 $sql .= " WHERE type = 3"; // We keep only categories for members
178 // $sql.= " AND visible > 0"; // We ignore the property visible because member's categories does not use this property (only products categories use it).
179 $sql .= " AND entity = ".$conf->entity;
180 $sql .= " ORDER BY label";
181
182 //print $sql;
183 $resql = $this->db->query($sql);
184 if ($resql) {
185 $num = $this->db->num_rows($resql);
186
187 $s .= '<option value="-1">'.$langs->trans("Category").'</option>';
188 if (!$num) {
189 $s .= '<option value="0" disabled>'.$langs->trans("NoCategoriesDefined").'</option>';
190 }
191
192 $i = 0;
193 while ($i < $num) {
194 $obj = $this->db->fetch_object($resql);
195
196 $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
197 $s .= '</option>';
198 $i++;
199 }
200 $s .= ajax_combobox("filter_category_fraise");
201 } else {
202 dol_print_error($this->db);
203 }
204
205 $s .= '</select>';
206
207
208 $s .= '<br><span class="opacitymedium">';
209 $s .= $langs->trans("DateEndSubscription").': &nbsp;';
210 $s .= $langs->trans("After").' > </span>'.$form->selectDate(-1, 'subscriptionafter', 0, 0, 1, 'fraise', 1, 0, 0);
211 $s .= ' &nbsp; ';
212 $s .= '<span class="opacitymedium">'.$langs->trans("Before").' < </span>'.$form->selectDate(-1, 'subscriptionbefore', 0, 0, 1, 'fraise', 1, 0, 0);
213
214 return $s;
215 }
216
217
224 public function url($id)
225 {
226 return '<a href="'.DOL_URL_ROOT.'/adherents/card.php?rowid='.$id.'">'.img_object('', "user").'</a>';
227 }
228
229
230 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
237 public function add_to_target($mailing_id)
238 {
239 // phpcs:enable
240 global $conf, $langs;
241
242 // Load translation files required by the page
243 $langs->loadLangs(array("members", "companies"));
244
245 $cibles = array();
246 $now = dol_now();
247
248 $dateendsubscriptionafter = dol_mktime(GETPOSTINT('subscriptionafterhour'), GETPOSTINT('subscriptionaftermin'), GETPOSTINT('subscriptionaftersec'), GETPOSTINT('subscriptionaftermonth'), GETPOSTINT('subscriptionafterday'), GETPOSTINT('subscriptionafteryear'));
249 $dateendsubscriptionbefore = dol_mktime(GETPOSTINT('subscriptionbeforehour'), GETPOSTINT('subscriptionbeforemin'), GETPOSTINT('subscriptionbeforesec'), GETPOSTINT('subscriptionbeforemonth'), GETPOSTINT('subscriptionbeforeday'), GETPOSTINT('subscriptionbeforeyear'));
250
251 // La requete doit retourner: id, email, fk_contact, name, firstname
252 $sql = "SELECT a.rowid as id, a.email as email, null as fk_contact, ";
253 $sql .= " a.lastname, a.firstname,";
254 $sql .= " a.datefin, a.civility as civility_id, a.login, a.societe"; // Other fields
255 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
256 if (GETPOSTINT('filter_category') > 0) {
257 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie_member as cm ON cm.fk_member = a.rowid";
258 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie as c ON c.rowid = cm.fk_categorie AND c.rowid = ".(GETPOSTINT('filter_category'));
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=".((int) $mailing_id).")";
263 // Filter on status
264 if (GETPOST("filter", 'aZ09') == 'draft') {
265 $sql .= " AND a.statut = -1";
266 } elseif (GETPOST("filter", 'aZ09') == '1a') {
267 $sql .= " AND a.statut=1 AND (a.datefin >= '".$this->db->idate($now)."' OR ta.subscription = 0)";
268 } elseif (GETPOST("filter", 'aZ09') == '1b') {
269 $sql .= " AND a.statut=1 AND ((a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."') AND ta.subscription = 1)";
270 } elseif (GETPOST("filter", 'aZ09') === '0') {
271 $sql .= " AND a.statut=0";
272 }
273 // Filter on date
274 if ($dateendsubscriptionafter > 0) {
275 $sql .= " AND datefin > '".$this->db->idate($dateendsubscriptionafter)."'";
276 }
277 if ($dateendsubscriptionbefore > 0) {
278 $sql .= " AND datefin < '".$this->db->idate($dateendsubscriptionbefore)."'";
279 }
280 $sql .= " AND a.fk_adherent_type = ta.rowid";
281 // Filter on type
282 if (GETPOSTINT('filter_type') > 0) {
283 $sql .= " AND ta.rowid = ".(GETPOSTINT('filter_type'));
284 }
285 if (empty($this->evenunsubscribe)) {
286 $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).")";
287 }
288 $sql .= " ORDER BY a.email";
289 //print $sql;
290
291 // Add targets into table
292 dol_syslog(get_class($this)."::add_to_target", LOG_DEBUG);
293 $result = $this->db->query($sql);
294 if ($result) {
295 $num = $this->db->num_rows($result);
296 $i = 0;
297 $j = 0;
298
299 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
300
301 $old = '';
302 while ($i < $num) {
303 $obj = $this->db->fetch_object($result);
304 if ($old != $obj->email) {
305 $cibles[$j] = array(
306 'email' => $obj->email,
307 'fk_contact' => (int) $obj->fk_contact,
308 'lastname' => $obj->lastname,
309 'firstname' => $obj->firstname,
310 'other' =>
311 ($langs->transnoentities("Login").'='.$obj->login).';'.
312 ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
313 ($langs->transnoentities("DateEnd").'='.dol_print_date($this->db->jdate($obj->datefin), 'day')).';'.
314 ($langs->transnoentities("Company").'='.$obj->societe),
315 'source_url' => $this->url($obj->id),
316 'source_id' => (int) $obj->id,
317 'source_type' => 'member'
318 );
319 $old = $obj->email;
320 $j++;
321 }
322
323 $i++;
324 }
325 } else {
326 dol_syslog($this->db->error());
327 $this->error = $this->db->error();
328 return -1;
329 }
330
331 return parent::addTargetsToDatabase($mailing_id, $cibles);
332 }
333}
$id
Definition account.php:48
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:459
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79