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