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