dolibarr  18.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@uers.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 
25 if (!defined('NOSTYLECHECK')) {
26  define('NOSTYLECHECK', '1');
27 }
28 
29 // Load Dolibarr environment
30 require '../../main.inc.php';
31 
32 require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/lib/emailing.lib.php';
34 require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/advtargetemailing.class.php';
35 require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/html.formadvtargetemailing.class.php';
36 require_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/advthirdparties.modules.php';
37 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
38 require_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'));
42 if (isModEnabled('categorie')) {
43  $langs->load("categories");
44 }
45 
46 // Load variable for pagination
47 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
48 $sortfield = GETPOST('sortfield', 'aZ09comma');
49 $sortorder = GETPOST('sortorder', 'aZ09comma');
50 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
51 if (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;
57 if (!$sortorder) {
58  $sortorder = "ASC";
59 }
60 if (!$sortfield) {
61  $sortfield = "email";
62 }
63 
64 $id = GETPOST('id', 'int');
65 $rowid = GETPOST('rowid', 'int');
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 = GETPOST('template_id', 'int');
71 
72 // Do we click on purge search criteria ?
73 if (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 
82 if (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 
90 if ($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
99 if (!$user->rights->mailing->lire || (empty($conf->global->EXTERNAL_USERS_ARE_AUTHORIZED) && $user->socid > 0)) {
100  accessforbidden();
101 }
102 //$result = restrictedArea($user, 'mailing');
103 
104 
105 /*
106  * Actions
107  */
108 
109 if ($action == 'loadfilter') {
110  if (!empty($template_id)) {
111  $result = $advTarget->fetch($template_id);
112  if ($result < 0) {
113  setEventMessages($advTarget->error, $advTarget->errors, 'errors');
114  } else {
115  if (!empty($advTarget->id)) {
116  $array_query = json_decode($advTarget->filtervalue, true);
117  }
118  }
119  }
120 }
121 
122 if ($action == 'add') {
123  $user_contact_query = false;
124 
125  $array_query = array();
126 
127  // Get extra fields
128 
129  foreach ($_POST as $key => $value) {
130  // print '$key='.$key.' $value='.$value.'<BR>';
131  if (preg_match("/^options_.*(?<!_cnct)$/", $key)) {
132  // Special case for start date come with 3 inputs day, month, year
133  if (preg_match("/st_dt/", $key)) {
134  $dtarr = array();
135  $dtarr = explode('_', $key);
136  if (!array_key_exists('options_'.$dtarr[1].'_st_dt', $array_query)) {
137  $array_query['options_'.$dtarr[1].'_st_dt'] = dol_mktime(0, 0, 0, GETPOST('options_'.$dtarr[1].'_st_dtmonth', 'int'), GETPOST('options_'.$dtarr[1].'_st_dtday', 'int'), GETPOST('options_'.$dtarr[1].'_st_dtyear', 'int'));
138  }
139  } elseif (preg_match("/end_dt/", $key)) {
140  // Special case for end date come with 3 inputs day, month, year
141  $dtarr = array();
142  $dtarr = explode('_', $key);
143  if (!array_key_exists('options_'.$dtarr[1].'_end_dt', $array_query)) {
144  $array_query['options_'.$dtarr[1].'_end_dt'] = dol_mktime(0, 0, 0, GETPOST('options_'.$dtarr[1].'_end_dtmonth', 'int'), GETPOST('options_'.$dtarr[1].'_end_dtday', 'int'), GETPOST('options_'.$dtarr[1].'_end_dtyear', 'int'));
145  }
146  } else {
147  $array_query[$key] = GETPOST($key);
148  }
149  }
150  if (preg_match("/^options_.*_cnct/", $key)) {
151  $user_contact_query = true;
152  // Special case for start date come with 3 inputs day, month, year
153  if (preg_match("/st_dt/", $key)) {
154  $dtarr = array();
155  $dtarr = explode('_', $key);
156  if (!array_key_exists('options_'.$dtarr[1].'_st_dt_cnct', $array_query)) {
157  $array_query['options_'.$dtarr[1].'_st_dt_cnct'] = dol_mktime(0, 0, 0, GETPOST('options_'.$dtarr[1].'_st_dtmonth_cnct', 'int'), GETPOST('options_'.$dtarr[1].'_st_dtday_cnct', 'int'), GETPOST('options_'.$dtarr[1].'_st_dtyear_cnct', 'int'));
158  }
159  } elseif (preg_match("/end_dt/", $key)) {
160  // Special case for end date come with 3 inputs day, month, year
161  $dtarr = array();
162  $dtarr = explode('_', $key);
163  if (!array_key_exists('options_'.$dtarr[1].'_end_dt_cnct', $array_query)) {
164  $array_query['options_'.$dtarr[1].'_end_dt_cnct'] = dol_mktime(0, 0, 0, GETPOST('options_'.$dtarr[1].'_end_dtmonth_cnct', 'int'), GETPOST('options_'.$dtarr[1].'_end_dtday_cnct', 'int'), GETPOST('options_'.$dtarr[1].'_end_dtyear_cnct', 'int'));
165  }
166  } else {
167  $array_query[$key] = GETPOST($key);
168  }
169  }
170 
171  if (preg_match("/^cust_/", $key)) {
172  $array_query[$key] = GETPOST($key);
173  }
174 
175  if (preg_match("/^contact_/", $key)) {
176  $array_query[$key] = GETPOST($key);
177 
178  $specials_date_key = array(
179  'contact_update_st_dt',
180  'contact_update_end_dt',
181  'contact_create_st_dt',
182  'contact_create_end_dt'
183  );
184  foreach ($specials_date_key as $date_key) {
185  if ($key == $date_key) {
186  $dt = GETPOST($date_key);
187  if (!empty($dt)) {
188  $array_query[$key] = dol_mktime(0, 0, 0, GETPOST($date_key.'month', 'int'), GETPOST($date_key.'day', 'int'), GETPOST($date_key.'year', 'int'));
189  } else {
190  $array_query[$key] = '';
191  }
192  }
193  }
194 
195  if (!empty($array_query[$key])) {
196  $user_contact_query = true;
197  }
198  }
199 
200  if ($array_query['type_of_target'] == 2 || $array_query['type_of_target'] == 4) {
201  $user_contact_query = true;
202  }
203 
204  if (preg_match("/^type_of_target/", $key)) {
205  $array_query[$key] = GETPOST($key);
206  }
207  }
208 
209  // if ($array_query ['type_of_target'] == 1 || $array_query ['type_of_target'] == 3) {
210  $result = $advTarget->query_thirdparty($array_query);
211  if ($result < 0) {
212  setEventMessages($advTarget->error, $advTarget->errors, 'errors');
213  }
214  /*} else {
215  $advTarget->thirdparty_lines = array ();
216  }*/
217 
218  if ($user_contact_query && ($array_query['type_of_target'] == 1 || $array_query['type_of_target'] == 2 || $array_query['type_of_target'] == 4)) {
219  $result = $advTarget->query_contact($array_query, 1);
220  if ($result < 0) {
221  setEventMessages($advTarget->error, $advTarget->errors, 'errors');
222  }
223  // If use contact but no result use artefact to so not use socid into add_to_target
224  if (count($advTarget->contact_lines) == 0) {
225  $advTarget->contact_lines = array(
226  0
227  );
228  }
229  } else {
230  $advTarget->contact_lines = array();
231  }
232 
233  if ((count($advTarget->thirdparty_lines) > 0) || (count($advTarget->contact_lines) > 0)) {
234  // Add targets into database
235  $obj = new mailing_advthirdparties($db);
236  $result = $obj->add_to_target_spec($id, $advTarget->thirdparty_lines, $array_query['type_of_target'], $advTarget->contact_lines);
237  } else {
238  $result = 0;
239  }
240 
241  if ($result > 0) {
242  $query_temlate_id = '';
243  if (!empty($template_id)) {
244  $query_temlate_id = '&template_id='.$template_id;
245  }
246  setEventMessages($langs->trans("XTargetsAdded", $result), null, 'mesgs');
247  header("Location: ".$_SERVER['PHP_SELF']."?id=".$id.$query_temlate_id);
248  exit();
249  }
250  if ($result == 0) {
251  setEventMessages($langs->trans("WarningNoEMailsAdded"), null, 'warnings');
252  }
253  if ($result < 0) {
254  setEventMessages($obj->error, $obj->errors, 'errors');
255  }
256 }
257 
258 if ($action == 'clear') {
259  // Chargement de la classe
260  $classname = "MailingTargets";
261  $obj = new $classname($db);
262  $obj->clear_target($id);
263 
264  header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
265  exit();
266 }
267 
268 if ($action == 'savefilter' || $action == 'createfilter') {
269  $template_name = GETPOST('template_name');
270  $error = 0;
271 
272  if ($action == 'createfilter' && empty($template_name)) {
273  setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('AdvTgtOrCreateNewFilter')), null, 'errors');
274  $error++;
275  }
276 
277  if (empty($error)) {
278  $array_query = array();
279 
280  // Get extra fields
281  foreach ($_POST as $key => $value) {
282  if (preg_match("/^options_.*(?<!_cnct)$/", $key)) {
283  // Special case for start date come with 3 inputs day, month, year
284  if (preg_match("/st_dt/", $key)) {
285  $dtarr = array();
286  $dtarr = explode('_', $key);
287  if (!array_key_exists('options_'.$dtarr[1].'_st_dt', $array_query)) {
288  $array_query['options_'.$dtarr[1].'_st_dt'] = dol_mktime(0, 0, 0, GETPOST('options_'.$dtarr[1].'_st_dtmonth', 'int'), GETPOST('options_'.$dtarr[1].'_st_dtday', 'int'), GETPOST('options_'.$dtarr[1].'_st_dtyear', 'int'));
289  }
290  } elseif (preg_match("/end_dt/", $key)) {
291  // Special case for end date come with 3 inputs day, month, year
292  $dtarr = array();
293  $dtarr = explode('_', $key);
294  if (!array_key_exists('options_'.$dtarr[1].'_end_dt', $array_query)) {
295  $array_query['options_'.$dtarr[1].'_end_dt'] = dol_mktime(0, 0, 0, GETPOST('options_'.$dtarr[1].'_end_dtmonth', 'int'), GETPOST('options_'.$dtarr[1].'_end_dtday', 'int'), GETPOST('options_'.$dtarr[1].'_end_dtyear', 'int'));
296  // print $array_query['options_'.$dtarr[1].'_end_dt'];
297  // 01/02/1013=1361228400
298  }
299  } else {
300  $array_query[$key] = GETPOST($key);
301  }
302  }
303  if (preg_match("/^options_.*_cnct/", $key)) {
304  // Special case for start date come with 3 inputs day, month, year
305  if (preg_match("/st_dt/", $key)) {
306  $dtarr = array();
307  $dtarr = explode('_', $key);
308  if (!array_key_exists('options_'.$dtarr[1].'_st_dt_cnct', $array_query)) {
309  $array_query['options_'.$dtarr[1].'_st_dt_cnct'] = dol_mktime(0, 0, 0, GETPOST('options_'.$dtarr[1].'_st_dtmonth_cnct', 'int'), GETPOST('options_'.$dtarr[1].'_st_dtday_cnct', 'int'), GETPOST('options_'.$dtarr[1].'_st_dtyear_cnct', 'int'));
310  }
311  } elseif (preg_match("/end_dt/", $key)) {
312  // Special case for end date come with 3 inputs day, month, year
313  $dtarr = array();
314  $dtarr = explode('_', $key);
315  if (!array_key_exists('options_'.$dtarr[1].'_end_dt_cnct', $array_query)) {
316  $array_query['options_'.$dtarr[1].'_end_dt_cnct'] = dol_mktime(0, 0, 0, GETPOST('options_'.$dtarr[1].'_end_dtmonth_cnct', 'int'), GETPOST('options_'.$dtarr[1].'_end_dtday_cnct', 'int'), GETPOST('options_'.$dtarr[1].'_end_dtyear_cnct', 'int'));
317  // print $array_query['cnct_options_'.$dtarr[1].'_end_dt'];
318  // 01/02/1013=1361228400
319  }
320  } else {
321  $array_query[$key] = GETPOST($key);
322  }
323  }
324 
325  if (preg_match("/^cust_/", $key)) {
326  $array_query[$key] = GETPOST($key);
327  }
328 
329  if (preg_match("/^contact_/", $key)) {
330  $array_query[$key] = GETPOST($key);
331 
332  $specials_date_key = array(
333  'contact_update_st_dt',
334  'contact_update_end_dt',
335  'contact_create_st_dt',
336  'contact_create_end_dt'
337  );
338  foreach ($specials_date_key as $date_key) {
339  if ($key == $date_key) {
340  $dt = GETPOST($date_key);
341  if (!empty($dt)) {
342  $array_query[$key] = dol_mktime(0, 0, 0, GETPOST($date_key.'month', 'int'), GETPOST($date_key.'day', 'int'), GETPOST($date_key.'year', 'int'));
343  } else {
344  $array_query[$key] = '';
345  }
346  }
347  }
348  }
349 
350  if (preg_match("/^type_of_target/", $key)) {
351  $array_query[$key] = GETPOST($key);
352  }
353  }
354  $advTarget->filtervalue = json_encode($array_query);
355 
356  if ($action == 'createfilter') {
357  $advTarget->name = $template_name;
358  $result = $advTarget->create($user);
359  if ($result < 0) {
360  setEventMessages($advTarget->error, $advTarget->errors, 'errors');
361  }
362  } elseif ($action == 'savefilter') {
363  $result = $advTarget->update($user);
364  if ($result < 0) {
365  setEventMessages($advTarget->error, $advTarget->errors, 'errors');
366  }
367  }
368  $template_id = $advTarget->id;
369  }
370 }
371 
372 if ($action == 'deletefilter') {
373  $result = $advTarget->delete($user);
374  if ($result < 0) {
375  setEventMessages($advTarget->error, $advTarget->errors, 'errors');
376  }
377  header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
378  exit();
379 }
380 
381 if ($action == 'delete') {
382  // Ici, rowid indique le destinataire et id le mailing
383  $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE rowid = ".((int) $rowid);
384  $resql = $db->query($sql);
385  if ($resql) {
386  if (!empty($id)) {
387  $classname = "MailingTargets";
388  $obj = new $classname($db);
389  $obj->update_nb($id);
390 
391  header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
392  exit();
393  } else {
394  header("Location: liste.php");
395  exit();
396  }
397  } else {
398  dol_print_error($db);
399  }
400 }
401 
402 if (GETPOST("button_removefilter")) {
403  $search_nom = '';
404  $search_prenom = '';
405  $search_email = '';
406 }
407 
408 /*
409  * View
410  */
411 
412 llxHeader('', $langs->trans("MailAdvTargetRecipients"));
413 
414 $form = new Form($db);
415 $formadvtargetemaling = new FormAdvTargetEmailing($db);
416 $formcompany = new FormCompany($db);
417 $formother = new FormOther($db);
418 
419 if ($object->fetch($id) >= 0) {
420  $head = emailing_prepare_head($object);
421 
422  print dol_get_fiche_head($head, 'advtargets', $langs->trans("Mailing"), 0, 'email');
423 
424  print '<table class="border centpercent">';
425 
426  $linkback = '<a href="'.DOL_URL_ROOT.'/comm/mailing/liste.php">'.$langs->trans("BackToList").'</a>';
427 
428  print '<tr><td>'.$langs->trans("Ref").'</td>';
429  print '<td colspan="3">';
430  print $form->showrefnav($object, 'id', $linkback);
431  print '</td></tr>';
432 
433  print '<tr><td>'.$langs->trans("MailTitle").'</td><td colspan="3">'.$object->title.'</td></tr>';
434 
435  print '<tr><td>'.$langs->trans("MailFrom").'</td><td colspan="3">'.dol_print_email($object->email_from, 0, 0, 0, 0, 1).'</td></tr>';
436 
437  // Errors to
438  print '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td colspan="3">'.dol_print_email($object->email_errorsto, 0, 0, 0, 0, 1);
439  print '</td></tr>';
440 
441  // Status
442  print '<tr><td>'.$langs->trans("Status").'</td><td colspan="3">'.$object->getLibStatut(4).'</td></tr>';
443 
444  // Nb of distinct emails
445  print '<tr><td>';
446  print $langs->trans("TotalNbOfDistinctRecipients");
447  print '</td><td colspan="3">';
448  $nbemail = ($object->nbemail ? $object->nbemail : '0');
449  if (!empty($conf->global->MAILING_LIMIT_SENDBYWEB) && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail) {
450  $text = $langs->trans('LimitSendingEmailing', $conf->global->MAILING_LIMIT_SENDBYWEB);
451  print $form->textwithpicto($nbemail, $text, 1, 'warning');
452  } else {
453  print $nbemail;
454  }
455  print '</td></tr>';
456 
457  print '</table>';
458 
459  print "</div>";
460 
461  // Show email selectors
462  if ($object->statut == 0 && $user->rights->mailing->creer) {
463  include DOL_DOCUMENT_ROOT.'/core/tpl/advtarget.tpl.php';
464  }
465 }
466 
467 // End of page
468 llxFooter();
469 $db->close();
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:70
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:609
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:5107
emailing_prepare_head
emailing_prepare_head(Mailing $object)
Prepare array with list of tabs.
Definition: emailing.lib.php:30
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
FormOther
Classe permettant la generation de composants html autre Only common components are here.
Definition: html.formother.class.php:39
FormCompany
Class to build HTML component for third parties management Only common components are here.
Definition: html.formcompany.class.php:40
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
mailing_advthirdparties
Class to manage a list of personalised recipients for mailing feature.
Definition: advthirdparties.modules.php:26
Mailing
Class to manage emailings module.
Definition: mailing.class.php:32
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
Definition: functions.lib.php:8673
$sql
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:207
dol_get_fiche_head
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.
Definition: functions.lib.php:1979
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:509
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:53
dol_print_email
dol_print_email($email, $cid=0, $socid=0, $addlink=0, $max=64, $showinvalid=1, $withpicto=0)
Show EMail link formatted for HTML output.
Definition: functions.lib.php:3177
FormAdvTargetEmailing
Class to manage building of HTML components.
Definition: html.formadvtargetemailing.class.php:28
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
Definition: security.lib.php:1164
AdvanceTargetingMailing
Class to manage advanced emailing target selector.
Definition: advtargetemailing.class.php:29
dol_mktime
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
Definition: functions.lib.php:2968