advtargetemailing.php 15 KB

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