dolibarr 25.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-2026 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024-2026 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
44 public $require_admin = 0;
45
49 public $require_module = array('adherent');
50
54 public $enabled = 'isModEnabled("member")';
55
59 public $picto = 'user';
60
61
67 public function __construct($db)
68 {
69 $this->db = $db;
70 }
71
72
81 public function getSqlArrayForStats()
82 {
83 global $langs;
84
85 $langs->load("members");
86
87 // Array for requests for statistics board
88 $statssql = array();
89
90 $statssql[0] = "SELECT '".$this->db->escape($langs->trans("FundationMembers"))."' as label, count(*) as nb";
91 $statssql[0] .= " FROM ".MAIN_DB_PREFIX."adherent where statut = 1 and entity IN (".getEntity('member').")";
92
93 return $statssql;
94 }
95
96
105 public function getNbOfRecipients($sql = '')
106 {
107 $sql = "SELECT count(distinct(a.email)) as nb";
108 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
109 $sql .= " WHERE (a.email IS NOT NULL AND a.email != '') AND a.entity IN (".getEntity('member').")";
110 $sql .= $this->getSqlToExcludeUnsubscribed('a.email');
111
112 // The query must return a field "nb" to be understood by parent::getNbOfRecipients
113 return parent::getNbOfRecipients($sql);
114 }
115
116
122 public function formFilter()
123 {
124 global $conf, $langs;
125
126 // Load translation files required by the page
127 $langs->loadLangs(array("members", "companies", "categories"));
128
129 $form = new Form($this->db);
130
131 $s = '';
132
133 // Status
134 $s .= '<select id="filter_fraise" name="filter" class="flat">';
135 $s .= '<option value="-1">'.$langs->trans("Status").'</option>';
136 $s .= '<option value="draft">'.$langs->trans("MemberStatusDraft").'</option>';
137 $s .= '<option value="1a">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusPaidShort").')</option>';
138 $s .= '<option value="1b">'.$langs->trans("MemberStatusActiveShort").' ('.$langs->trans("MemberStatusActiveLateShort").')</option>';
139 $s .= '<option value="0">'.$langs->trans("MemberStatusResiliatedShort").'</option>';
140 $s .= '</select> ';
141 $s .= ajax_combobox("filter_fraise");
142
143 $s .= '<select id="filter_type_fraise" name="filter_type" class="flat">';
144 $sql = "SELECT rowid, libelle as label, statut";
145 $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type";
146 $sql .= " WHERE entity IN (".getEntity('member_type').")";
147 $sql .= " ORDER BY rowid";
148 $resql = $this->db->query($sql);
149 if ($resql) {
150 $num = $this->db->num_rows($resql);
151
152 $s .= '<option value="-1">'.$langs->trans("Type").'</option>';
153 if (!$num) {
154 $s .= '<option value="0" disabled="disabled">'.$langs->trans("NoCategoriesDefined").'</option>';
155 }
156
157 $i = 0;
158 while ($i < $num) {
159 $obj = $this->db->fetch_object($resql);
160
161 $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
162 $s .= '</option>';
163 $i++;
164 }
165 $s .= ajax_combobox("filter_type");
166 } else {
167 dol_print_error($this->db);
168 }
169
170 $s .= '</select>';
171 $s .= ajax_combobox("filter_type_fraise");
172
173 $s .= ' ';
174
175 $s .= '<select id="filter_category_fraise" name="filter_category" class="flat">';
176
177 // Show categories
178 $sql = "SELECT rowid, label, type, visible";
179 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
180 $sql .= " WHERE type = 3"; // We keep only categories for members
181 // $sql.= " AND visible > 0"; // We ignore the property visible because member's categories does not use this property (only products categories use it).
182 $sql .= " AND entity = ".((int) $conf->entity);
183 $sql .= " ORDER BY label";
184
185 //print $sql;
186 $resql = $this->db->query($sql);
187 if ($resql) {
188 $num = $this->db->num_rows($resql);
189
190 $s .= '<option value="-1">'.$langs->trans("Category").'</option>';
191 if (!$num) {
192 $s .= '<option value="0" disabled>'.$langs->trans("NoCategoriesDefined").'</option>';
193 }
194
195 $i = 0;
196 while ($i < $num) {
197 $obj = $this->db->fetch_object($resql);
198
199 $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
200 $s .= '</option>';
201 $i++;
202 }
203 $s .= ajax_combobox("filter_category_fraise");
204 } else {
205 dol_print_error($this->db);
206 }
207
208 $s .= '</select>';
209
210
211 $s .= '<br><span class="opacitymedium">';
212 $s .= $langs->trans("DateEndSubscription").': &nbsp;';
213 $s .= $langs->trans("After").' > </span>'.$form->selectDate(-1, 'subscriptionafter', 0, 0, 1, 'fraise', 1, 0, 0);
214 $s .= ' &nbsp; ';
215 $s .= '<span class="opacitymedium">'.$langs->trans("Before").' < </span>'.$form->selectDate(-1, 'subscriptionbefore', 0, 0, 1, 'fraise', 1, 0, 0);
216
217 return $s;
218 }
219
220
227 public function url($id)
228 {
229 return '<a href="'.DOL_URL_ROOT.'/adherents/card.php?rowid='.$id.'">'.img_object('', "user").'</a>';
230 }
231
232
233 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
240 public function add_to_target($mailing_id)
241 {
242 // phpcs:enable
243 global $langs;
244
245 // Load translation files required by the page
246 $langs->loadLangs(array("members", "companies"));
247
248 $cibles = array();
249 $now = dol_now();
250
251 $dateendsubscriptionafter = dol_mktime(GETPOSTINT('subscriptionafterhour'), GETPOSTINT('subscriptionaftermin'), GETPOSTINT('subscriptionaftersec'), GETPOSTINT('subscriptionaftermonth'), GETPOSTINT('subscriptionafterday'), GETPOSTINT('subscriptionafteryear'));
252 $dateendsubscriptionbefore = dol_mktime(GETPOSTINT('subscriptionbeforehour'), GETPOSTINT('subscriptionbeforemin'), GETPOSTINT('subscriptionbeforesec'), GETPOSTINT('subscriptionbeforemonth'), GETPOSTINT('subscriptionbeforeday'), GETPOSTINT('subscriptionbeforeyear'));
253
254 // The query must return: id, email, fk_contact, name, firstname
255 $sql = "SELECT a.rowid as id, a.email as email, null as fk_contact, ";
256 $sql .= " a.lastname, a.firstname,";
257 $sql .= " a.datefin, a.civility as civility_id, a.login, a.societe"; // Other fields
258 $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
259 if (GETPOSTINT('filter_category') > 0) {
260 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie_member as cm ON cm.fk_member = a.rowid";
261 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie as c ON c.rowid = cm.fk_categorie AND c.rowid = ".(GETPOSTINT('filter_category'));
262 }
263 $sql .= " , ".MAIN_DB_PREFIX."adherent_type as ta";
264 $sql .= " WHERE a.entity IN (".getEntity('member').") AND a.email <> ''"; // Note that null != '' is false
265 $sql .= " AND a.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
266 // Filter on status
267 if (GETPOST("filter", 'aZ09') == 'draft') {
268 $sql .= " AND a.statut = -1";
269 } elseif (GETPOST("filter", 'aZ09') == '1a') {
270 $sql .= " AND a.statut=1 AND (a.datefin >= '".$this->db->idate($now)."' OR ta.subscription = 0)";
271 } elseif (GETPOST("filter", 'aZ09') == '1b') {
272 $sql .= " AND a.statut=1 AND ((a.datefin IS NULL or a.datefin < '".$this->db->idate($now)."') AND ta.subscription = 1)";
273 } elseif (GETPOST("filter", 'aZ09') === '0') {
274 $sql .= " AND a.statut=0";
275 }
276 // Filter on date
277 if ($dateendsubscriptionafter > 0) {
278 $sql .= " AND datefin > '".$this->db->idate($dateendsubscriptionafter)."'";
279 }
280 if ($dateendsubscriptionbefore > 0) {
281 $sql .= " AND datefin < '".$this->db->idate($dateendsubscriptionbefore)."'";
282 }
283 $sql .= " AND a.fk_adherent_type = ta.rowid";
284 // Filter on type
285 if (GETPOSTINT('filter_type') > 0) {
286 $sql .= " AND ta.rowid = ".(GETPOSTINT('filter_type'));
287 }
288 $sql .= $this->getSqlToExcludeUnsubscribed('a.email');
289 $sql .= " ORDER BY a.email";
290 //print $sql;
291
292 // Add targets into table
293 dol_syslog(get_class($this)."::add_to_target", LOG_DEBUG);
294 $result = $this->db->query($sql);
295 if ($result) {
296 $num = $this->db->num_rows($result);
297 $i = 0;
298 $j = 0;
299
300 dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
301
302 $old = '';
303 while ($i < $num) {
304 $obj = $this->db->fetch_object($result);
305 if ($old != $obj->email) {
306 $cibles[$j] = array(
307 'email' => $obj->email,
308 'fk_contact' => (int) $obj->fk_contact,
309 'lastname' => $obj->lastname,
310 'firstname' => $obj->firstname,
311 'other' =>
312 ($langs->transnoentities("Login").'='.$obj->login).';'.
313 ($langs->transnoentities("UserTitle").'='.($obj->civility_id ? $langs->transnoentities("Civility".$obj->civility_id) : '')).';'.
314 ($langs->transnoentities("DateEnd").'='.dol_print_date($this->db->jdate($obj->datefin), 'day')).';'.
315 ($langs->transnoentities("Company").'='.$obj->societe),
316 'source_url' => $this->url($obj->id),
317 'source_id' => (int) $obj->id,
318 'source_type' => 'member'
319 );
320 $old = $obj->email;
321 $j++;
322 }
323
324 $i++;
325 }
326 } else {
327 dol_syslog($this->db->error());
328 $this->error = $this->db->error();
329 return -1;
330 }
331
332 return parent::addTargetsToDatabase($mailing_id, $cibles);
333 }
334}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
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:476
Class to manage generation of HTML components Only common components must be here.
Parent class of emailing target selectors modules.
getSqlToExcludeUnsubscribed($emailfield)
Return the SQL fragment that excludes email addresses which opted out of emailings for the current en...
Class to generate targets according to the Fraise rule.
url($id)
Provides the URL to the card of the source information of the recipient for the mailing.
add_to_target($mailing_id)
Adds recipients to the targets table.
getSqlArrayForStats()
In the main mailing area, there is a box with statistics.
formFilter()
Displays the filter form that appears on the mailing recipient selection page.
__construct($db)
Constructor.
getNbOfRecipients($sql='')
Returns the number of distinct emails returned by your selector.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_now($mode='gmt')
Return date for now.
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...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
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.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...