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