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