dolibarr  9.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 <http://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->societe_id > 0)
46 
47 // Load variable for pagination
48 $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
49 $sortfield = GETPOST('sortfield','alpha');
50 $sortorder = GETPOST('sortorder','alpha');
51 $page = 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 }
91 else
92 {
93  if (! empty($advTarget->id)) {
94  $array_query = json_decode($advTarget->filtervalue, true);
95  }
96 }
97 
98 
99 /*
100  * Actions
101  */
102 
103 if ($action == 'loadfilter') {
104  if (! empty($template_id)) {
105  $result = $advTarget->fetch($template_id);
106  if ($result < 0) {
107  setEventMessages($advTarget->error, $advTarget->errors, 'errors');
108  } else {
109  if (! empty($advTarget->id)) {
110  $array_query = json_decode($advTarget->filtervalue, true);
111  }
112  }
113  }
114 }
115 
116 if ($action == 'add') {
117 
118  $user_contact_query = false;
119 
120  $array_query = array ();
121 
122  // Get extra fields
123 
124  foreach ( $_POST as $key => $value ) {
125  // print '$key='.$key.' $value='.$value.'<BR>';
126  if (preg_match("/^options_.*(?<!_cnct)$/", $key)) {
127  // Special case for start date come with 3 inputs day, month, year
128  if (preg_match("/st_dt/", $key)) {
129  $dtarr = array ();
130  $dtarr = explode('_', $key);
131  if (! array_key_exists('options_' . $dtarr[1] . '_st_dt', $array_query)) {
132  $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'));
133  }
134  } elseif (preg_match("/end_dt/", $key)) {
135  // Special case for end date come with 3 inputs day, month, year
136  $dtarr = array ();
137  $dtarr = explode('_', $key);
138  if (! array_key_exists('options_' . $dtarr[1] . '_end_dt', $array_query)) {
139  $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'));
140  }
141  } else {
142  $array_query[$key] = GETPOST($key);
143  }
144  }
145  if (preg_match("/^options_.*_cnct/", $key)) {
146  $user_contact_query = true;
147  // Special case for start date come with 3 inputs day, month, year
148  if (preg_match("/st_dt/", $key)) {
149  $dtarr = array ();
150  $dtarr = explode('_', $key);
151  if (! array_key_exists('options_' . $dtarr[1] . '_st_dt' . '_cnct', $array_query)) {
152  $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'));
153  }
154  } elseif (preg_match("/end_dt/", $key)) {
155  // Special case for end date come with 3 inputs day, month, year
156  $dtarr = array ();
157  $dtarr = explode('_', $key);
158  if (! array_key_exists('options_' . $dtarr[1] . '_end_dt' . '_cnct', $array_query)) {
159  $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'));
160  }
161  } else {
162  $array_query[$key] = GETPOST($key);
163  }
164  }
165 
166  if (preg_match("/^cust_/", $key)) {
167  $array_query[$key] = GETPOST($key);
168  }
169 
170  if (preg_match("/^contact_/", $key)) {
171 
172  $array_query[$key] = GETPOST($key);
173 
174  $specials_date_key = array (
175  'contact_update_st_dt',
176  'contact_update_end_dt',
177  'contact_create_st_dt',
178  'contact_create_end_dt'
179  );
180  foreach ( $specials_date_key as $date_key ) {
181  if ($key == $date_key) {
182  $dt = GETPOST($date_key);
183  if (! empty($dt)) {
184  $array_query[$key] = dol_mktime(0, 0, 0, GETPOST($date_key . 'month', 'int'), GETPOST($date_key . 'day', 'int'), GETPOST($date_key . 'year', 'int'));
185  } else {
186  $array_query[$key] = '';
187  }
188  }
189  }
190 
191  if (! empty($array_query[$key])) {
192  $user_contact_query = true;
193  }
194  }
195 
196  if ($array_query['type_of_target'] == 2 || $array_query['type_of_target'] == 4) {
197  $user_contact_query = true;
198  }
199 
200  if (preg_match("/^type_of_target/", $key)) {
201  $array_query[$key] = GETPOST($key);
202  }
203  }
204 
205  // if ($array_query ['type_of_target'] == 1 || $array_query ['type_of_target'] == 3) {
206  $result = $advTarget->query_thirdparty($array_query);
207  if ($result < 0) {
208  setEventMessages($advTarget->error, $advTarget->errors, 'errors');
209  }
210  /*} else {
211  $advTarget->thirdparty_lines = array ();
212  }*/
213 
214  if ($user_contact_query && ($array_query['type_of_target'] == 1 || $array_query['type_of_target'] == 2 || $array_query['type_of_target'] == 4)) {
215  $result = $advTarget->query_contact($array_query, 1);
216  if ($result < 0) {
217  setEventMessages($advTarget->error, $advTarget->errors, 'errors');
218  }
219  // If use contact but no result use artefact to so not use socid into add_to_target
220  if (count($advTarget->contact_lines) == 0) {
221  $advTarget->contact_lines = array (
222  0
223  );
224  }
225  } else {
226  $advTarget->contact_lines = array ();
227  }
228 
229  if ((count($advTarget->thirdparty_lines) > 0) || (count($advTarget->contact_lines) > 0)) {
230  // Add targets into database
231  $obj = new mailing_advthirdparties($db);
232  $result = $obj->add_to_target_spec($id, $advTarget->thirdparty_lines, $array_query['type_of_target'], $advTarget->contact_lines);
233  } else {
234  $result = 0;
235  }
236 
237  if ($result > 0) {
238  $query_temlate_id = '';
239  if (! empty($template_id)) {
240  $query_temlate_id = '&template_id=' . $template_id;
241  }
242  setEventMessages($langs->trans("XTargetsAdded",$result), null, 'mesgs');
243  header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $id . $query_temlate_id);
244  exit();
245  }
246  if ($result == 0) {
247  setEventMessages($langs->trans("WarningNoEMailsAdded"), null, 'warnings');
248  }
249  if ($result < 0) {
250  setEventMessages($obj->error, $obj->errors, 'errors');
251  }
252 }
253 
254 if ($action == 'clear') {
255  // Chargement de la classe
256  $classname = "MailingTargets";
257  $obj = new $classname($db);
258  $obj->clear_target($id);
259 
260  header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $id);
261  exit();
262 }
263 
264 if ($action == 'savefilter' || $action == 'createfilter') {
265 
266  $template_name = GETPOST('template_name');
267  $error = 0;
268 
269  if ($action == 'createfilter' && empty($template_name)) {
270  setEventMessages($langs->trans('ErrorFieldRequired', $langs->trans('AdvTgtOrCreateNewFilter')), null, 'errors');
271  $error ++;
272  }
273 
274  if (empty($error)) {
275 
276  $array_query = array ();
277 
278  // Get extra fields
279  foreach ( $_POST as $key => $value ) {
280  if (preg_match("/^options_.*(?<!_cnct)$/", $key)) {
281  // Special case for start date come with 3 inputs day, month, year
282  if (preg_match("/st_dt/", $key)) {
283  $dtarr = array ();
284  $dtarr = explode('_', $key);
285  if (! array_key_exists('options_' . $dtarr[1] . '_st_dt', $array_query)) {
286  $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'));
287  }
288  } elseif (preg_match("/end_dt/", $key)) {
289  // Special case for end date come with 3 inputs day, month, year
290  $dtarr = array ();
291  $dtarr = explode('_', $key);
292  if (! array_key_exists('options_' . $dtarr[1] . '_end_dt', $array_query)) {
293  $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'));
294  // print $array_query['options_'.$dtarr[1].'_end_dt'];
295  // 01/02/1013=1361228400
296  }
297  } else {
298  $array_query[$key] = GETPOST($key);
299  }
300  }
301  if (preg_match("/^options_.*_cnct/", $key)) {
302  // Special case for start date come with 3 inputs day, month, year
303  if (preg_match("/st_dt/", $key)) {
304  $dtarr = array ();
305  $dtarr = explode('_', $key);
306  if (! array_key_exists('options_' . $dtarr[1] . '_st_dt' . '_cnct', $array_query)) {
307  $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'));
308  }
309  } elseif (preg_match("/end_dt/", $key)) {
310  // Special case for end date come with 3 inputs day, month, year
311  $dtarr = array ();
312  $dtarr = explode('_', $key);
313  if (! array_key_exists('options_' . $dtarr[1] . '_end_dt' . '_cnct', $array_query)) {
314  $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'));
315  // print $array_query['cnct_options_'.$dtarr[1].'_end_dt'];
316  // 01/02/1013=1361228400
317  }
318  } else {
319  $array_query[$key] = GETPOST($key);
320  }
321  }
322 
323  if (preg_match("/^cust_/", $key)) {
324  $array_query[$key] = GETPOST($key);
325  }
326 
327  if (preg_match("/^contact_/", $key)) {
328 
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 
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=" . $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 ($_POST["button_removefilter"]) {
403  $search_nom = '';
404  $search_prenom = '';
405  $search_email = '';
406 }
407 
408 
409 /*
410  * View
411  */
412 
413 
414 llxHeader('', $langs->trans("MailAdvTargetRecipients"));
415 
416 
417 
418 $form = new Form($db);
419 $formadvtargetemaling = new FormAdvTargetEmailing($db);
420 $formcompany = new FormCompany($db);
421 $formother = new FormOther($db);
422 
423 if ($object->fetch($id) >= 0) {
424 
425  $head = emailing_prepare_head($object);
426 
427  dol_fiche_head($head, 'advtargets', $langs->trans("Mailing"), 0, 'email');
428 
429  print '<table class="border" width="100%">';
430 
431  $linkback = '<a href="' . DOL_URL_ROOT . '/comm/mailing/liste.php">' . $langs->trans("BackToList") . '</a>';
432 
433  print '<tr><td width="25%">' . $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 width="25%">' . $langs->trans("MailTitle") . '</td><td colspan="3">' . $object->titre . '</td></tr>';
439 
440  print '<tr><td width="25%">' . $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 width="25%">' . $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 width="25%">' . $langs->trans("Status") . '</td><td colspan="3">' . $object->getLibStatut(4) . '</td></tr>';
448 
449  // Nb of distinct emails
450  print '<tr><td width="25%">';
451  print $langs->trans("TotalNbOfDistinctRecipients");
452  print '</td><td colspan="3">';
453  $nbemail = ($object->nbemail ? $object->nbemail : '0');
454  if (! empty($conf->global->MAILING_LIMIT_SENDBYWEB) && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail) {
455  $text = $langs->trans('LimitSendingEmailing', $conf->global->MAILING_LIMIT_SENDBYWEB);
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->statut == 0 && $user->rights->mailing->creer) {
468 
469  include DOL_DOCUMENT_ROOT . '/core/tpl/advtarget.tpl.php';
470  }
471 }
472 
473 // End of page
474 llxFooter();
475 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
Class to manage a list of personalised recipients for mailing feature.
Class to manage building of HTML components.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class to build HTML component for third parties management Only common components are here...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
emailing_prepare_head(Mailing $object)
Prepare array with list of tabs.
Classe permettant la generation de composants html autre Only common components are here...
llxHeader()
Empty header.
Definition: wrapper.php:44
Class to manage emailings module.
Class to manage advanced emailing target selector.
dol_print_email($email, $cid=0, $socid=0, $addlink=0, $max=64, $showinvalid=1, $withpicto=0)
Show EMail link.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.