dolibarr 21.0.0-beta
advtargetemailing.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
3 * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
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 */
20
28if (!defined('NOSTYLECHECK')) {
29 define('NOSTYLECHECK', '1');
30}
31
32// Load Dolibarr environment
33require '../../main.inc.php';
34
35require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php';
36require_once DOL_DOCUMENT_ROOT.'/core/lib/emailing.lib.php';
37require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/advtargetemailing.class.php';
38require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/html.formadvtargetemailing.class.php';
39require_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/advthirdparties.modules.php';
40require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
41require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
42
51// Load translation files required by the page
52$langs->loadLangs(array('mails', 'companies'));
53if (isModEnabled('category')) {
54 $langs->load("categories");
55}
56
57// Load variable for pagination
58$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
59$sortfield = GETPOST('sortfield', 'aZ09comma');
60$sortorder = GETPOST('sortorder', 'aZ09comma');
61$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
62if (empty($page) || $page == -1) {
63 $page = 0;
64} // If $page is not defined, or '' or -1
65$offset = $limit * $page;
66$pageprev = $page - 1;
67$pagenext = $page + 1;
68if (!$sortorder) {
69 $sortorder = "ASC";
70}
71if (!$sortfield) {
72 $sortfield = "email";
73}
74
75$id = GETPOSTINT('id');
76$rowid = GETPOSTINT('rowid');
77$action = GETPOST('action', 'aZ09');
78$search_nom = GETPOST("search_nom");
79$search_prenom = GETPOST("search_prenom");
80$search_email = GETPOST("search_email");
81$template_id = GETPOSTINT('template_id');
82
83// Do we click on purge search criteria ?
84if (GETPOST('button_removefilter_x', 'alpha')) {
85 $search_nom = '';
86 $search_prenom = '';
87 $search_email = '';
88}
89$array_query = array();
90'@phan-var-force array<string,int|string|string[]> $array_query';
91$object = new Mailing($db);
92$advTarget = new AdvanceTargetingMailing($db);
93
94if (empty($template_id)) {
95 $advTarget->fk_element = $id;
96 $advTarget->type_element = 'mailing';
97 $result = $advTarget->fetch_by_mailing();
98} else {
99 $result = $advTarget->fetch($template_id);
100}
101
102if ($result < 0) {
103 setEventMessages($advTarget->error, $advTarget->errors, 'errors');
104} else {
105 if (!empty($advTarget->id)) {
106 $array_query = json_decode($advTarget->filtervalue, true);
107 }
108}
109
110// Security check
111if (!$user->hasRight('mailing', 'lire') || (!getDolGlobalString('EXTERNAL_USERS_ARE_AUTHORIZED') && $user->socid > 0)) {
113}
114//$result = restrictedArea($user, 'mailing');
115
116$permissiontoread = $user->hasRight('mailing', 'lire');
117$permissiontoadd = $user->hasRight('mailing', 'creer');
118
119
120/*
121 * Actions
122 */
123
124if ($action == 'loadfilter' && $permissiontoread) {
125 if (!empty($template_id)) {
126 $result = $advTarget->fetch($template_id);
127 if ($result < 0) {
128 setEventMessages($advTarget->error, $advTarget->errors, 'errors');
129 } else {
130 if (!empty($advTarget->id)) {
131 $array_query = json_decode($advTarget->filtervalue, true);
132 }
133 }
134 }
135}
136
137if ($action == 'add' && $permissiontoadd) {
138 $user_contact_query = false;
139
140 $array_query = array();
141
142 // Get extra fields
143
144 foreach ($_POST as $key => $value) {
145 // print '$key='.$key.' $value='.$value.'<BR>';
146 if (preg_match("/^options_.*(?<!_cnct)$/", $key)) {
147 // Special case for start date come with 3 inputs day, month, year
148 if (preg_match("/st_dt/", $key)) {
149 $dtarr = array();
150 $dtarr = explode('_', $key);
151 if (!array_key_exists('options_'.$dtarr[1].'_st_dt', $array_query)) {
152 $array_query['options_'.$dtarr[1].'_st_dt'] = dol_mktime(0, 0, 0, GETPOSTINT('options_'.$dtarr[1].'_st_dtmonth'), GETPOSTINT('options_'.$dtarr[1].'_st_dtday'), GETPOSTINT('options_'.$dtarr[1].'_st_dtyear'));
153 }
154 } elseif (preg_match("/end_dt/", $key)) {
155 // Special case for end date come with 3 inputs day, month, year
156 $dtarr = array();
157 $dtarr = explode('_', $key);
158 if (!array_key_exists('options_'.$dtarr[1].'_end_dt', $array_query)) {
159 $array_query['options_'.$dtarr[1].'_end_dt'] = dol_mktime(0, 0, 0, GETPOSTINT('options_'.$dtarr[1].'_end_dtmonth'), GETPOSTINT('options_'.$dtarr[1].'_end_dtday'), GETPOSTINT('options_'.$dtarr[1].'_end_dtyear'));
160 }
161 } else {
162 $array_query[$key] = GETPOST($key);
163 }
164 }
165 if (preg_match("/^options_.*_cnct/", $key)) {
166 $user_contact_query = true;
167 // Special case for start date come with 3 inputs day, month, year
168 if (preg_match("/st_dt/", $key)) {
169 $dtarr = array();
170 $dtarr = explode('_', $key);
171 if (!array_key_exists('options_'.$dtarr[1].'_st_dt_cnct', $array_query)) {
172 $array_query['options_'.$dtarr[1].'_st_dt_cnct'] = dol_mktime(0, 0, 0, GETPOSTINT('options_'.$dtarr[1].'_st_dtmonth_cnct'), GETPOSTINT('options_'.$dtarr[1].'_st_dtday_cnct'), GETPOSTINT('options_'.$dtarr[1].'_st_dtyear_cnct'));
173 }
174 } elseif (preg_match("/end_dt/", $key)) {
175 // Special case for end date come with 3 inputs day, month, year
176 $dtarr = array();
177 $dtarr = explode('_', $key);
178 if (!array_key_exists('options_'.$dtarr[1].'_end_dt_cnct', $array_query)) {
179 $array_query['options_'.$dtarr[1].'_end_dt_cnct'] = dol_mktime(0, 0, 0, GETPOSTINT('options_'.$dtarr[1].'_end_dtmonth_cnct'), GETPOSTINT('options_'.$dtarr[1].'_end_dtday_cnct'), GETPOSTINT('options_'.$dtarr[1].'_end_dtyear_cnct'));
180 }
181 } else {
182 $array_query[$key] = GETPOST($key);
183 }
184 }
185
186 if (preg_match("/^cust_/", $key)) {
187 $array_query[$key] = GETPOST($key);
188 }
189
190 if (preg_match("/^contact_/", $key)) {
191 $array_query[$key] = GETPOST($key);
192
193 $specials_date_key = array(
194 'contact_update_st_dt',
195 'contact_update_end_dt',
196 'contact_create_st_dt',
197 'contact_create_end_dt'
198 );
199 foreach ($specials_date_key as $date_key) {
200 if ($key == $date_key) {
201 $dt = GETPOST($date_key);
202 if (!empty($dt)) {
203 $array_query[$key] = dol_mktime(0, 0, 0, GETPOSTINT($date_key.'month'), GETPOSTINT($date_key.'day'), GETPOSTINT($date_key.'year'));
204 } else {
205 $array_query[$key] = '';
206 }
207 }
208 }
209
210 if (!empty($array_query[$key])) {
211 $user_contact_query = true;
212 }
213 }
214
215 if ($array_query['type_of_target'] == 2 || $array_query['type_of_target'] == 4) {
216 $user_contact_query = true;
217 }
218
219 if (preg_match("/^type_of_target/", $key)) {
220 $array_query[$key] = GETPOST($key);
221 }
222 }
223
224 // if ($array_query ['type_of_target'] == 1 || $array_query ['type_of_target'] == 3) {
225 $result = $advTarget->query_thirdparty($array_query);
226 if ($result < 0) {
227 setEventMessages($advTarget->error, $advTarget->errors, 'errors');
228 }
229 /*} else {
230 $advTarget->thirdparty_lines = array ();
231 }*/
232
233 if ($user_contact_query && ($array_query['type_of_target'] == 1 || $array_query['type_of_target'] == 2 || $array_query['type_of_target'] == 4)) {
234 $result = $advTarget->query_contact($array_query, 1);
235 if ($result < 0) {
236 setEventMessages($advTarget->error, $advTarget->errors, 'errors');
237 }
238 // If use contact but no result use artefact to so not use socid into add_to_target
239 if (count($advTarget->contact_lines) == 0) {
240 $advTarget->contact_lines = array(
241 0
242 );
243 }
244 } else {
245 $advTarget->contact_lines = array();
246 }
247
248 $mailingadvthirdparties = null;
249 if ((count($advTarget->thirdparty_lines) > 0) || (count($advTarget->contact_lines) > 0)) {
250 // Add targets into database
251 $mailingadvthirdparties = new mailing_advthirdparties($db);
252 $result = $mailingadvthirdparties->add_to_target_spec($id, $advTarget->thirdparty_lines, $array_query['type_of_target'], $advTarget->contact_lines);
253 } else {
254 $result = 0;
255 }
256
257 if ($result > 0) {
258 $query_temlate_id = '';
259 if (!empty($template_id)) {
260 $query_temlate_id = '&template_id='.$template_id;
261 }
262 setEventMessages($langs->trans("XTargetsAdded", $result), null, 'mesgs');
263 header("Location: ".$_SERVER['PHP_SELF']."?id=".$id.$query_temlate_id);
264 exit();
265 }
266 if ($result == 0) {
267 setEventMessages($langs->trans("WarningNoEMailsAdded"), null, 'warnings');
268 }
269 if ($result < 0 && is_object($mailingadvthirdparties)) {
270 setEventMessages($mailingadvthirdparties->error, $mailingadvthirdparties->errors, 'errors');
271 }
272}
273
274if ($action == 'clear' && $permissiontoadd) {
275 $mailingtargets = new MailingTargets($db);
276 $mailingtargets->clear_target($id);
277
278 header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
279 exit();
280}
281
282if (($action == 'savefilter' || $action == 'createfilter') && $permissiontoadd) {
283 $template_name = GETPOST('template_name');
284 $error = 0;
285
286 if ($action == 'createfilter' && empty($template_name) && $permissiontoadd) {
287 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('AdvTgtOrCreateNewFilter')), null, 'errors');
288 $error++;
289 }
290
291 if (empty($error)) {
292 $array_query = array();
293
294 // Get extra fields
295 foreach ($_POST as $key => $value) {
296 if (preg_match("/^options_.*(?<!_cnct)$/", $key)) {
297 // Special case for start date come with 3 inputs day, month, year
298 if (preg_match("/st_dt/", $key)) {
299 $dtarr = array();
300 $dtarr = explode('_', $key);
301 if (!array_key_exists('options_'.$dtarr[1].'_st_dt', $array_query)) {
302 $array_query['options_'.$dtarr[1].'_st_dt'] = dol_mktime(0, 0, 0, GETPOSTINT('options_'.$dtarr[1].'_st_dtmonth'), GETPOSTINT('options_'.$dtarr[1].'_st_dtday'), GETPOSTINT('options_'.$dtarr[1].'_st_dtyear'));
303 }
304 } elseif (preg_match("/end_dt/", $key)) {
305 // Special case for end date come with 3 inputs day, month, year
306 $dtarr = array();
307 $dtarr = explode('_', $key);
308 if (!array_key_exists('options_'.$dtarr[1].'_end_dt', $array_query)) {
309 $array_query['options_'.$dtarr[1].'_end_dt'] = dol_mktime(0, 0, 0, GETPOSTINT('options_'.$dtarr[1].'_end_dtmonth'), GETPOSTINT('options_'.$dtarr[1].'_end_dtday'), GETPOSTINT('options_'.$dtarr[1].'_end_dtyear'));
310 // print $array_query['options_'.$dtarr[1].'_end_dt'];
311 // 01/02/1013=1361228400
312 }
313 } else {
314 $array_query[$key] = GETPOST($key);
315 }
316 }
317 if (preg_match("/^options_.*_cnct/", $key)) {
318 // Special case for start date come with 3 inputs day, month, year
319 if (preg_match("/st_dt/", $key)) {
320 $dtarr = array();
321 $dtarr = explode('_', $key);
322 if (!array_key_exists('options_'.$dtarr[1].'_st_dt_cnct', $array_query)) {
323 $array_query['options_'.$dtarr[1].'_st_dt_cnct'] = dol_mktime(0, 0, 0, GETPOSTINT('options_'.$dtarr[1].'_st_dtmonth_cnct'), GETPOSTINT('options_'.$dtarr[1].'_st_dtday_cnct'), GETPOSTINT('options_'.$dtarr[1].'_st_dtyear_cnct'));
324 }
325 } elseif (preg_match("/end_dt/", $key)) {
326 // Special case for end date come with 3 inputs day, month, year
327 $dtarr = array();
328 $dtarr = explode('_', $key);
329 if (!array_key_exists('options_'.$dtarr[1].'_end_dt_cnct', $array_query)) {
330 $array_query['options_'.$dtarr[1].'_end_dt_cnct'] = dol_mktime(0, 0, 0, GETPOSTINT('options_'.$dtarr[1].'_end_dtmonth_cnct'), GETPOSTINT('options_'.$dtarr[1].'_end_dtday_cnct'), GETPOSTINT('options_'.$dtarr[1].'_end_dtyear_cnct'));
331 // print $array_query['cnct_options_'.$dtarr[1].'_end_dt'];
332 // 01/02/1013=1361228400
333 }
334 } else {
335 $array_query[$key] = GETPOST($key);
336 }
337 }
338
339 if (preg_match("/^cust_/", $key)) {
340 $array_query[$key] = GETPOST($key);
341 }
342
343 if (preg_match("/^contact_/", $key)) {
344 $array_query[$key] = GETPOST($key);
345
346 $specials_date_key = array(
347 'contact_update_st_dt',
348 'contact_update_end_dt',
349 'contact_create_st_dt',
350 'contact_create_end_dt'
351 );
352 foreach ($specials_date_key as $date_key) {
353 if ($key == $date_key) {
354 $dt = GETPOST($date_key);
355 if (!empty($dt)) {
356 $array_query[$key] = dol_mktime(0, 0, 0, GETPOSTINT($date_key.'month'), GETPOSTINT($date_key.'day'), GETPOSTINT($date_key.'year'));
357 } else {
358 $array_query[$key] = '';
359 }
360 }
361 }
362 }
363
364 if (preg_match("/^type_of_target/", $key)) {
365 $array_query[$key] = GETPOST($key);
366 }
367 }
368 $advTarget->filtervalue = json_encode($array_query);
369
370 if ($action == 'createfilter') { // Test on permission already done
371 $advTarget->name = $template_name;
372 $result = $advTarget->create($user);
373 if ($result < 0) {
374 setEventMessages($advTarget->error, $advTarget->errors, 'errors');
375 }
376 } elseif ($action == 'savefilter') { // Test on permission already done
377 $result = $advTarget->update($user);
378 if ($result < 0) {
379 setEventMessages($advTarget->error, $advTarget->errors, 'errors');
380 }
381 }
382 $template_id = $advTarget->id;
383 }
384}
385
386if ($action == 'deletefilter' && $permissiontoadd) {
387 $result = $advTarget->delete($user);
388 if ($result < 0) {
389 setEventMessages($advTarget->error, $advTarget->errors, 'errors');
390 }
391 header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
392 exit();
393}
394
395if ($action == 'delete' && $permissiontoadd) {
396 // Ici, rowid indique le destinataire et id le mailing
397 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE rowid = ".((int) $rowid);
398 $resql = $db->query($sql);
399 if ($resql) {
400 if (!empty($id)) {
401 $mailingtargets = new MailingTargets($db);
402 $mailingtargets->update_nb($id);
403
404 header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
405 exit();
406 } else {
407 header("Location: liste.php");
408 exit();
409 }
410 } else {
411 dol_print_error($db);
412 }
413}
414
415if (GETPOST("button_removefilter")) {
416 $search_nom = '';
417 $search_prenom = '';
418 $search_email = '';
419}
420
421
422/*
423 * View
424 */
425
426llxHeader('', $langs->trans("MailAdvTargetRecipients"));
427
428$form = new Form($db);
429$formadvtargetemaling = new FormAdvTargetEmailing($db);
430$formcompany = new FormCompany($db);
431$formother = new FormOther($db);
432
433if ($object->fetch($id) >= 0) {
434 $head = emailing_prepare_head($object);
435
436 print dol_get_fiche_head($head, 'advtargets', $langs->trans("Mailing"), 0, 'email');
437
438 print '<table class="border centpercent">';
439
440 $linkback = '<a href="'.DOL_URL_ROOT.'/comm/mailing/liste.php">'.$langs->trans("BackToList").'</a>';
441
442 print '<tr><td>'.$langs->trans("Ref").'</td>';
443 print '<td colspan="3">';
444 print $form->showrefnav($object, 'id', $linkback);
445 print '</td></tr>';
446
447 print '<tr><td>'.$langs->trans("MailTitle").'</td><td colspan="3">'.$object->title.'</td></tr>';
448
449 print '<tr><td>'.$langs->trans("MailFrom").'</td><td colspan="3">'.dol_print_email($object->email_from, 0, 0, 0, 0, 1).'</td></tr>';
450
451 // Errors to
452 print '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td colspan="3">'.dol_print_email($object->email_errorsto, 0, 0, 0, 0, 1);
453 print '</td></tr>';
454
455 // Status
456 print '<tr><td>'.$langs->trans("Status").'</td><td colspan="3">'.$object->getLibStatut(4).'</td></tr>';
457
458 // Nb of distinct emails
459 print '<tr><td>';
460 print $langs->trans("TotalNbOfDistinctRecipients");
461 print '</td><td colspan="3">';
462 $nbemail = ($object->nbemail ? $object->nbemail : '0');
463 if (getDolGlobalString('MAILING_LIMIT_SENDBYWEB') && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail) {
464 $text = $langs->trans('LimitSendingEmailing', getDolGlobalString('MAILING_LIMIT_SENDBYWEB'));
465 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
466 print $form->textwithpicto($nbemail, $text, 1, 'warning');
467 } else {
468 print $nbemail;
469 }
470 print '</td></tr>';
471
472 print '</table>';
473
474 print "</div>";
475
476 // Show email selectors
477 if ($object->status == 0 && $user->hasRight('mailing', 'creer')) {
478 // @phan-assert FormAdvTargetEmailing $formadvtargetemaling
479 // @phan-assert AdvanceTargetingMailing $advTarget
480
481 // @phan-assert array<string,int|string|string[] $array_query'
482 include DOL_DOCUMENT_ROOT.'/core/tpl/advtarget.tpl.php';
483 }
484}
485
486// End of page
487llxFooter();
488$db->close();
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
Class to manage advanced emailing target selector.
Class to manage building of HTML components.
Class to build HTML component for third parties management Only common components are here.
Class to manage generation of HTML components Only common components must be here.
Class permettant la generation de composants html autre Only common components are here.
Class to manage emailings module.
Parent class of emailing target selectors modules.
Class to manage a list of personalised recipients for mailing feature.
llxFooter()
Footer empty.
Definition document.php:107
emailing_prepare_head(Mailing $object)
Prepare array with list of tabs.
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...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_print_email($email, $cid=0, $socid=0, $addlink=0, $max=64, $showinvalid=1, $withpicto=0, $morecss='paddingrightonly')
Show EMail link formatted for HTML output.
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...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.