advtargetemailing.php 15 KB

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