notification.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2015 Laurent Destailleur <eldy@users.sourceforge.org>
  4. * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2015 Bahfir Abbes <contact@dolibarrpar.org>
  6. * Copyright (C) 2020 Thibault FOUCART <suport@ptibogxiv.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/admin/notification.php
  23. * \ingroup notification
  24. * \brief Page to setup notification module
  25. */
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/triggers/interface_50_modNotification_Notification.class.php';
  30. // Load translation files required by the page
  31. $langs->loadLangs(array('admin', 'other', 'orders', 'propal', 'bills', 'errors', 'mails'));
  32. // Security check
  33. if (!$user->admin) {
  34. accessforbidden();
  35. }
  36. $action = GETPOST('action', 'aZ09');
  37. $error = 0;
  38. /*
  39. * Actions
  40. */
  41. // Action to update or add a constant
  42. if ($action == 'settemplates' && $user->admin) {
  43. $db->begin();
  44. if (!$error && is_array($_POST)) {
  45. $reg = array();
  46. foreach ($_POST as $key => $val) {
  47. if (!preg_match('/^constvalue_(.*)_TEMPLATE/', $key, $reg)) {
  48. continue;
  49. }
  50. $triggername = $reg[1];
  51. $constvalue = GETPOST($key, 'alpha');
  52. $consttype = 'emailtemplate:xxx';
  53. $tmparray = explode(':', $constvalue);
  54. if (!empty($tmparray[0]) && !empty($tmparray[1])) {
  55. $constvalue = $tmparray[0];
  56. $consttype = 'emailtemplate:'.$tmparray[1];
  57. //var_dump($constvalue);
  58. //var_dump($consttype);
  59. $res = dolibarr_set_const($db, $triggername.'_TEMPLATE', $constvalue, $consttype, 0, '', $conf->entity);
  60. if ($res < 0) {
  61. $error++;
  62. break;
  63. }
  64. } else {
  65. $res = dolibarr_del_const($db, $triggername.'_TEMPLATE', $conf->entity);
  66. }
  67. }
  68. }
  69. if (!$error) {
  70. $db->commit();
  71. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  72. } else {
  73. $db->rollback();
  74. setEventMessages($langs->trans("Error"), null, 'errors');
  75. }
  76. }
  77. if ($action == 'setvalue' && $user->admin) {
  78. $db->begin();
  79. $result = dolibarr_set_const($db, "NOTIFICATION_EMAIL_FROM", GETPOST("email_from", "alphawithlgt"), 'chaine', 0, '', $conf->entity);
  80. if ($result < 0) {
  81. $error++;
  82. }
  83. if (!$error) {
  84. $db->commit();
  85. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  86. } else {
  87. $db->rollback();
  88. setEventMessages($langs->trans("Error"), null, 'errors');
  89. }
  90. }
  91. if ($action == 'setfixednotif' && $user->admin) {
  92. $db->begin();
  93. if (!$error && is_array($_POST)) {
  94. $reg = array();
  95. foreach ($_POST as $key => $val) {
  96. if (!preg_match('/^NOTIF_(.*)_key$/', $key, $reg)) {
  97. continue;
  98. }
  99. $newval = '';
  100. $newkey = '';
  101. $shortkey = preg_replace('/_key$/', '', $key);
  102. //print $shortkey.'<br>';
  103. if (preg_match('/^NOTIF_(.*)_old_(.*)_key/', $key, $reg)) {
  104. dolibarr_del_const($db, 'NOTIFICATION_FIXEDEMAIL_'.$reg[1].'_THRESHOLD_HIGHER_'.$reg[2], $conf->entity);
  105. $newkey = 'NOTIFICATION_FIXEDEMAIL_'.$reg[1].'_THRESHOLD_HIGHER_'.((int) GETPOST($shortkey.'_amount'));
  106. $newval = GETPOST($shortkey.'_key');
  107. //print $newkey.' - '.$newval.'<br>';
  108. } elseif (preg_match('/^NOTIF_(.*)_new_key/', $key, $reg)) {
  109. // Add a new entry
  110. $newkey = 'NOTIFICATION_FIXEDEMAIL_'.$reg[1].'_THRESHOLD_HIGHER_'.((int) GETPOST($shortkey.'_amount'));
  111. $newval = GETPOST($shortkey.'_key');
  112. }
  113. if ($newkey && $newval) {
  114. $result = dolibarr_set_const($db, $newkey, $newval, 'chaine', 0, '', $conf->entity);
  115. }
  116. }
  117. }
  118. if (!$error) {
  119. $db->commit();
  120. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  121. } else {
  122. $db->rollback();
  123. setEventMessages($langs->trans("Error"), null, 'errors');
  124. }
  125. }
  126. /*
  127. * View
  128. */
  129. $form = new Form($db);
  130. $notify = new Notify($db);
  131. llxHeader('', $langs->trans("NotificationSetup"));
  132. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  133. print load_fiche_titre($langs->trans("NotificationSetup"), $linkback, 'title_setup');
  134. print '<span class="opacitymedium">';
  135. print $langs->trans("NotificationsDesc").'<br>';
  136. print $langs->trans("NotificationsDescUser").'<br>';
  137. if (!empty($conf->societe->enabled)) {
  138. print $langs->trans("NotificationsDescContact").'<br>';
  139. }
  140. print $langs->trans("NotificationsDescGlobal").'<br>';
  141. print '</span>';
  142. print '<br>';
  143. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
  144. print '<input type="hidden" name="token" value="'.newToken().'">';
  145. print '<input type="hidden" name="action" value="setvalue">';
  146. print '<table class="noborder centpercent">';
  147. print '<tr class="liste_titre">';
  148. print '<td>'.$langs->trans("Parameter").'</td>';
  149. print '<td>'.$langs->trans("Value").'</td>';
  150. print "</tr>\n";
  151. print '<tr class="oddeven"><td>';
  152. print $langs->trans("NotificationEMailFrom").'</td>';
  153. print '<td>';
  154. print img_picto('', 'email', 'class="pictofixedwidth"');
  155. print '<input class="width300" type="email" name="email_from" value="'.$conf->global->NOTIFICATION_EMAIL_FROM.'">';
  156. if (!empty($conf->global->NOTIFICATION_EMAIL_FROM) && !isValidEmail($conf->global->NOTIFICATION_EMAIL_FROM)) {
  157. print ' '.img_warning($langs->trans("ErrorBadEMail"));
  158. }
  159. print '</td>';
  160. print '</tr>';
  161. print '</table>';
  162. print '<div class="center"><input type="submit" class="button button-save" value="'.$langs->trans("Save").'"></div>';
  163. print '</form>';
  164. print '<br><br>';
  165. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
  166. print '<input type="hidden" name="token" value="'.newToken().'">';
  167. print '<input type="hidden" name="action" value="settemplates">';
  168. // Notification per contacts
  169. $title = $langs->trans("ListOfNotificationsPerUser");
  170. if (!empty($conf->societe->enabled)) {
  171. $title = $langs->trans("ListOfNotificationsPerUserOrContact");
  172. }
  173. print load_fiche_titre($title, '', '');
  174. // Load array of available notifications
  175. $notificationtrigger = new InterfaceNotification($db);
  176. $listofnotifiedevents = $notificationtrigger->getListOfManagedEvents();
  177. if ($conf->global->MAIN_FEATURES_LEVEL >= 2) {
  178. // Editing global variables not related to a specific theme
  179. $constantes = array();
  180. foreach ($listofnotifiedevents as $notifiedevent) {
  181. $label = $langs->trans("Notify_".$notifiedevent['code']); //!=$langs->trans("Notify_".$notifiedevent['code'])?$langs->trans("Notify_".$notifiedevent['code']):$notifiedevent['label'];
  182. $elementLabel = $langs->trans(ucfirst($notifiedevent['elementtype']));
  183. if ($notifiedevent['elementtype'] == 'order_supplier') {
  184. $elementLabel = $langs->trans('SupplierOrder');
  185. } elseif ($notifiedevent['elementtype'] == 'propal') {
  186. $elementLabel = $langs->trans('Proposal');
  187. } elseif ($notifiedevent['elementtype'] == 'facture') {
  188. $elementLabel = $langs->trans('Bill');
  189. } elseif ($notifiedevent['elementtype'] == 'commande') {
  190. $elementLabel = $langs->trans('Order');
  191. } elseif ($notifiedevent['elementtype'] == 'ficheinter') {
  192. $elementLabel = $langs->trans('Intervention');
  193. } elseif ($notifiedevent['elementtype'] == 'shipping') {
  194. $elementLabel = $langs->trans('Shipping');
  195. } elseif ($notifiedevent['elementtype'] == 'expensereport' || $notifiedevent['elementtype'] == 'expense_report') {
  196. $elementLabel = $langs->trans('ExpenseReport');
  197. }
  198. if ($notifiedevent['elementtype'] == 'propal') {
  199. $model = 'propal_send';
  200. } elseif ($notifiedevent['elementtype'] == 'commande') {
  201. $model = 'order_send';
  202. } elseif ($notifiedevent['elementtype'] == 'facture') {
  203. $model = 'facture_send';
  204. } elseif ($notifiedevent['elementtype'] == 'shipping') {
  205. $model = 'shipping_send';
  206. } elseif ($notifiedevent['elementtype'] == 'ficheinter') {
  207. $model = 'fichinter_send';
  208. } elseif ($notifiedevent['elementtype'] == 'expensereport') {
  209. $model = 'expensereport_send';
  210. } elseif ($notifiedevent['elementtype'] == 'order_supplier') {
  211. $model = 'order_supplier_send';
  212. // } elseif ($notifiedevent['elementtype'] == 'invoice_supplier') $model = 'invoice_supplier_send';
  213. } elseif ($notifiedevent['elementtype'] == 'member') {
  214. $model = 'member';
  215. }
  216. $constantes[$notifiedevent['code'].'_TEMPLATE'] = array('type'=>'emailtemplate:'.$model, 'label'=>$label);
  217. }
  218. $helptext = '';
  219. form_constantes($constantes, 3, $helptext, 'EmailTemplate');
  220. print '<div class="opacitymedium">';
  221. print '* '.$langs->trans("GoOntoUserCardToAddMore").'<br>';
  222. if (!empty($conf->societe->enabled)) {
  223. print '** '.$langs->trans("GoOntoContactCardToAddMore").'<br>';
  224. }
  225. print '</div>';
  226. print '<div class="center"><input type="submit" class="button button-save" value="'.$langs->trans("Save").'"></div>';
  227. } else {
  228. print '<table class="noborder centpercent">';
  229. print '<tr class="liste_titre">';
  230. print '<td>'.$langs->trans("Label").'</td>';
  231. /*print '<td>'.$langs->trans("Code").'</td>';
  232. print '<td>'.$langs->trans("Label").'</td>';*/
  233. //print '<td class="right">'.$langs->trans("NbOfTargetedContacts").'</td>';
  234. print "</tr>\n";
  235. print '<tr class="oddeven">';
  236. print '<td>';
  237. $i = 0;
  238. foreach ($listofnotifiedevents as $notifiedevent) {
  239. $label = $langs->trans("Notify_".$notifiedevent['code']); //!=$langs->trans("Notify_".$notifiedevent['code'])?$langs->trans("Notify_".$notifiedevent['code']):$notifiedevent['label'];
  240. $elementLabel = $langs->trans(ucfirst($notifiedevent['elementtype']));
  241. if ($notifiedevent['elementtype'] == 'order_supplier') {
  242. $elementLabel = $langs->trans('SupplierOrder');
  243. } elseif ($notifiedevent['elementtype'] == 'propal') {
  244. $elementLabel = $langs->trans('Proposal');
  245. } elseif ($notifiedevent['elementtype'] == 'facture') {
  246. $elementLabel = $langs->trans('Bill');
  247. } elseif ($notifiedevent['elementtype'] == 'commande') {
  248. $elementLabel = $langs->trans('Order');
  249. } elseif ($notifiedevent['elementtype'] == 'ficheinter') {
  250. $elementLabel = $langs->trans('Intervention');
  251. } elseif ($notifiedevent['elementtype'] == 'shipping') {
  252. $elementLabel = $langs->trans('Shipping');
  253. } elseif ($notifiedevent['elementtype'] == 'expensereport' || $notifiedevent['elementtype'] == 'expense_report') {
  254. $elementLabel = $langs->trans('ExpenseReport');
  255. }
  256. if ($i) {
  257. print ', ';
  258. }
  259. print $label;
  260. $i++;
  261. }
  262. print '</td></tr>';
  263. print '</table>';
  264. print '<div class="opacitymedium">';
  265. print '* '.$langs->trans("GoOntoUserCardToAddMore").'<br>';
  266. if (!empty($conf->societe->enabled)) {
  267. print '** '.$langs->trans("GoOntoContactCardToAddMore").'<br>';
  268. }
  269. print '</div>';
  270. }
  271. print '</form>';
  272. print '<br><br>';
  273. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
  274. print '<input type="hidden" name="token" value="'.newToken().'">';
  275. print '<input type="hidden" name="action" value="setfixednotif">';
  276. print '<input type="hidden" name="page_y" value="">';
  277. print load_fiche_titre($langs->trans("ListOfFixedNotifications"), '', '');
  278. print '<table class="noborder centpercent">';
  279. print '<tr class="liste_titre">';
  280. print '<td>'.$langs->trans("Module").'</td>';
  281. print '<td>'.$langs->trans("Code").'</td>';
  282. print '<td>'.$langs->trans("Label").'</td>';
  283. print '<td>'.$langs->trans("FixedEmailTarget").'</td>';
  284. print '<td>'.$langs->trans("Threshold").'</td>';
  285. print '<td></td>';
  286. print "</tr>\n";
  287. foreach ($listofnotifiedevents as $notifiedevent) {
  288. $label = $langs->trans("Notify_".$notifiedevent['code']); //!=$langs->trans("Notify_".$notifiedevent['code'])?$langs->trans("Notify_".$notifiedevent['code']):$notifiedevent['label'];
  289. $elementPicto = $notifiedevent['elementtype'];
  290. $elementLabel = $langs->trans(ucfirst($notifiedevent['elementtype']));
  291. // Special cases
  292. if ($notifiedevent['elementtype'] == 'order_supplier') {
  293. $elementPicto = 'supplier_order';
  294. $elementLabel = $langs->trans('SupplierOrder');
  295. } elseif ($notifiedevent['elementtype'] == 'propal') {
  296. $elementLabel = $langs->trans('Proposal');
  297. } elseif ($notifiedevent['elementtype'] == 'facture') {
  298. $elementPicto = 'bill';
  299. $elementLabel = $langs->trans('Bill');
  300. } elseif ($notifiedevent['elementtype'] == 'commande') {
  301. $elementPicto = 'order';
  302. $elementLabel = $langs->trans('Order');
  303. } elseif ($notifiedevent['elementtype'] == 'ficheinter') {
  304. $elementPicto = 'intervention';
  305. $elementLabel = $langs->trans('Intervention');
  306. } elseif ($notifiedevent['elementtype'] == 'shipping') {
  307. $elementPicto = 'shipment';
  308. $elementLabel = $langs->trans('Shipping');
  309. } elseif ($notifiedevent['elementtype'] == 'expensereport' || $notifiedevent['elementtype'] == 'expense_report') {
  310. $elementPicto = 'expensereport';
  311. $elementLabel = $langs->trans('ExpenseReport');
  312. }
  313. $labelfortrigger = 'AmountHT';
  314. $codehasnotrigger = 0;
  315. if (preg_match('/^HOLIDAY/', $notifiedevent['code'])) {
  316. $codehasnotrigger++;
  317. }
  318. print '<tr class="oddeven">';
  319. print '<td>';
  320. print img_picto('', $elementPicto, 'class="pictofixedwidth"');
  321. print $elementLabel;
  322. print '</td>';
  323. print '<td>'.$notifiedevent['code'].'</td>';
  324. print '<td><span class="opacitymedium">'.$label.'</span></td>';
  325. print '<td>';
  326. $inputfieldalreadyshown = 0;
  327. // Notification with threshold
  328. foreach ($conf->global as $key => $val) {
  329. if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifiedevent['code'].'_THRESHOLD_HIGHER_(.*)/', $key, $reg)) {
  330. continue;
  331. }
  332. $param = 'NOTIFICATION_FIXEDEMAIL_'.$notifiedevent['code'].'_THRESHOLD_HIGHER_'.$reg[1];
  333. $value = GETPOST('NOTIF_'.$notifiedevent['code'].'_old_'.$reg[1].'_key') ?GETPOST('NOTIF_'.$notifiedevent['code'].'_old_'.$reg[1].'_key', 'alpha') : $conf->global->$param;
  334. $s = '<input type="text" size="32" name="NOTIF_'.$notifiedevent['code'].'_old_'.$reg[1].'_key" value="'.dol_escape_htmltag($value).'">'; // Do not use type="email" here, we must be able to enter a list of email with , separator.
  335. $arrayemail = explode(',', $value);
  336. $showwarning = 0;
  337. foreach ($arrayemail as $keydet => $valuedet) {
  338. $valuedet = trim($valuedet);
  339. if (!empty($valuedet) && !isValidEmail($valuedet, 1)) {
  340. $showwarning++;
  341. }
  342. }
  343. if ((!empty($conf->global->$param)) && $showwarning) {
  344. $s .= ' '.img_warning($langs->trans("ErrorBadEMail"));
  345. }
  346. print $form->textwithpicto($s, $langs->trans("YouCanUseCommaSeparatorForSeveralRecipients").'<br>'.$langs->trans("YouCanAlsoUseSupervisorKeyword"), 1, 'help', '', 0, 2);
  347. print '<br>';
  348. $inputfieldalreadyshown++;
  349. }
  350. // New entry input fields
  351. if (empty($inputfieldalreadyshown) || !$codehasnotrigger) {
  352. $s = '<input type="text" size="32" name="NOTIF_'.$notifiedevent['code'].'_new_key" value="">'; // Do not use type="email" here, we must be able to enter a list of email with , separator.
  353. print $form->textwithpicto($s, $langs->trans("YouCanUseCommaSeparatorForSeveralRecipients").'<br>'.$langs->trans("YouCanAlsoUseSupervisorKeyword"), 1, 'help', '', 0, 2);
  354. }
  355. print '</td>';
  356. print '<td>';
  357. // Notification with threshold
  358. $inputfieldalreadyshown = 0;
  359. foreach ($conf->global as $key => $val) {
  360. if ($val == '' || !preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifiedevent['code'].'_THRESHOLD_HIGHER_(.*)/', $key, $reg)) {
  361. continue;
  362. }
  363. if (!$codehasnotrigger) {
  364. print $langs->trans($labelfortrigger).' >= <input type="text" size="4" name="NOTIF_'.$notifiedevent['code'].'_old_'.$reg[1].'_amount" value="'.dol_escape_htmltag($reg[1]).'">';
  365. print '<br>';
  366. $inputfieldalreadyshown++;
  367. }
  368. }
  369. // New entry input fields
  370. if (!$codehasnotrigger) {
  371. print $langs->trans($labelfortrigger).' >= <input type="text" size="4" name="NOTIF_'.$notifiedevent['code'].'_new_amount" value="">';
  372. }
  373. print '</td>';
  374. print '<td>';
  375. // TODO Add link to show message content
  376. print '</td>';
  377. print '</tr>';
  378. }
  379. print '</table>';
  380. print '<br>';
  381. print '<div class="center"><input type="submit" class="button button-save reposition" value="'.$langs->trans("Save").'"></div>';
  382. print '</form>';
  383. // End of page
  384. llxFooter();
  385. $db->close();