notification.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/admin/notification.php
  22. * \ingroup notification
  23. * \brief Page to setup notification module
  24. */
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/triggers/interface_50_modNotification_Notification.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('admin', 'other', 'orders', 'propal', 'bills', 'errors', 'mails'));
  31. // Security check
  32. if (!$user->admin)
  33. accessforbidden();
  34. $action = GETPOST('action','aZ09');
  35. /*
  36. * Actions
  37. */
  38. if ($action == 'setvalue' && $user->admin)
  39. {
  40. $db->begin();
  41. $result=dolibarr_set_const($db, "NOTIFICATION_EMAIL_FROM", $_POST["email_from"], 'chaine', 0, '', $conf->entity);
  42. if ($result < 0) $error++;
  43. if (! $error && is_array($_POST))
  44. {
  45. //var_dump($_POST);
  46. foreach($_POST as $key => $val)
  47. {
  48. if (! preg_match('/^NOTIF_(.*)_key$/', $key, $reg)) continue;
  49. $newval='';
  50. $newkey='';
  51. $shortkey=preg_replace('/_key$/','',$key);
  52. //print $shortkey.'<br>';
  53. if (preg_match('/^NOTIF_(.*)_old_(.*)_key/',$key,$reg))
  54. {
  55. dolibarr_del_const($db, 'NOTIFICATION_FIXEDEMAIL_'.$reg[1].'_THRESHOLD_HIGHER_'.$reg[2], $conf->entity);
  56. $newkey='NOTIFICATION_FIXEDEMAIL_'.$reg[1].'_THRESHOLD_HIGHER_'.((int) GETPOST($shortkey.'_amount'));
  57. $newval=GETPOST($shortkey.'_key');
  58. //print $newkey.' - '.$newval.'<br>';
  59. }
  60. else if (preg_match('/^NOTIF_(.*)_new_key/',$key,$reg))
  61. {
  62. // Add a new entry
  63. $newkey='NOTIFICATION_FIXEDEMAIL_'.$reg[1].'_THRESHOLD_HIGHER_'.((int) GETPOST($shortkey.'_amount'));
  64. $newval=GETPOST($shortkey.'_key');
  65. }
  66. if ($newkey && $newval)
  67. {
  68. $result=dolibarr_set_const($db, $newkey, $newval, 'chaine', 0, '', $conf->entity);
  69. }
  70. }
  71. }
  72. if (! $error)
  73. {
  74. $db->commit();
  75. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  76. }
  77. else
  78. {
  79. $db->rollback();
  80. setEventMessages($langs->trans("Error"), null, 'errors');
  81. }
  82. }
  83. /*
  84. * View
  85. */
  86. $form=new Form($db);
  87. $notify = new Notify($db);
  88. llxHeader('',$langs->trans("NotificationSetup"));
  89. $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  90. print load_fiche_titre($langs->trans("NotificationSetup"),$linkback,'title_setup');
  91. print $langs->trans("NotificationsDesc").'<br>';
  92. print $langs->trans("NotificationsDescUser").'<br>';
  93. if (! empty($conf->societe->enabled)) print $langs->trans("NotificationsDescContact").'<br>';
  94. print $langs->trans("NotificationsDescGlobal").'<br>';
  95. print '<br>';
  96. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
  97. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  98. print '<input type="hidden" name="action" value="setvalue">';
  99. print '<table class="noborder" width="100%">';
  100. print '<tr class="liste_titre">';
  101. print '<td>'.$langs->trans("Parameter").'</td>';
  102. print '<td>'.$langs->trans("Value").'</td>';
  103. print "</tr>\n";
  104. print '<tr class="oddeven"><td>';
  105. print $langs->trans("NotificationEMailFrom").'</td>';
  106. print '<td>';
  107. print '<input size="32" type="email" name="email_from" value="'.$conf->global->NOTIFICATION_EMAIL_FROM.'">';
  108. if (! empty($conf->global->NOTIFICATION_EMAIL_FROM) && ! isValidEmail($conf->global->NOTIFICATION_EMAIL_FROM)) print ' '.img_warning($langs->trans("ErrorBadEMail"));
  109. print '</td>';
  110. print '</tr>';
  111. print '</table>';
  112. print '<br><br>';
  113. // Notification per contacts
  114. $title=$langs->trans("ListOfNotificationsPerUser");
  115. if (! empty($conf->societe->enabled)) $title=$langs->trans("ListOfNotificationsPerUserOrContact");
  116. print load_fiche_titre($title,'','');
  117. print '<table class="noborder" width="100%">';
  118. print '<tr class="liste_titre">';
  119. print '<td>'.$langs->trans("Label").'</td>';
  120. /*print '<td>'.$langs->trans("Code").'</td>';
  121. print '<td>'.$langs->trans("Label").'</td>';*/
  122. //print '<td align="right">'.$langs->trans("NbOfTargetedContacts").'</td>';
  123. print "</tr>\n";
  124. // Load array of available notifications
  125. $notificationtrigger=new InterfaceNotification($db);
  126. $listofnotifiedevents=$notificationtrigger->getListOfManagedEvents();
  127. print '<tr class="oddeven">';
  128. print '<td>';
  129. $i=0;
  130. foreach($listofnotifiedevents as $notifiedevent)
  131. {
  132. $label=$langs->trans("Notify_".$notifiedevent['code']); //!=$langs->trans("Notify_".$notifiedevent['code'])?$langs->trans("Notify_".$notifiedevent['code']):$notifiedevent['label'];
  133. $elementLabel = $langs->trans(ucfirst($notifiedevent['elementtype']));
  134. if ($notifiedevent['elementtype'] == 'order_supplier') $elementLabel = $langs->trans('SupplierOrder');
  135. elseif ($notifiedevent['elementtype'] == 'propal') $elementLabel = $langs->trans('Proposal');
  136. elseif ($notifiedevent['elementtype'] == 'facture') $elementLabel = $langs->trans('Bill');
  137. elseif ($notifiedevent['elementtype'] == 'commande') $elementLabel = $langs->trans('Order');
  138. elseif ($notifiedevent['elementtype'] == 'ficheinter') $elementLabel = $langs->trans('Intervention');
  139. elseif ($notifiedevent['elementtype'] == 'shipping') $elementLabel = $langs->trans('Shipping');
  140. elseif ($notifiedevent['elementtype'] == 'expensereport') $elementLabel = $langs->trans('ExpenseReport');
  141. if ($i) print ', ';
  142. print $label;
  143. $i++;
  144. }
  145. print '</td></tr>';
  146. print '</table>';
  147. print '<div class="opacitymedium">';
  148. print '* '.$langs->trans("GoOntoUserCardToAddMore").'<br>';
  149. if (! empty($conf->societe->enabled)) print '** '.$langs->trans("GoOntoContactCardToAddMore").'<br>';
  150. print '</div>';
  151. print '<br><br>';
  152. print load_fiche_titre($langs->trans("ListOfFixedNotifications"),'','');
  153. print '<table class="noborder" width="100%">';
  154. print '<tr class="liste_titre">';
  155. print '<td>'.$langs->trans("Module").'</td>';
  156. print '<td>'.$langs->trans("Code").'</td>';
  157. print '<td>'.$langs->trans("Label").'</td>';
  158. print '<td>'.$langs->trans("FixedEmailTarget").'</td>';
  159. print '<td>'.$langs->trans("Threshold").'</td>';
  160. print '<td>'.'</td>';
  161. print "</tr>\n";
  162. // Load array of available notifications
  163. $notificationtrigger=new InterfaceNotification($db);
  164. $listofnotifiedevents=$notificationtrigger->getListOfManagedEvents();
  165. foreach($listofnotifiedevents as $notifiedevent)
  166. {
  167. $label=$langs->trans("Notify_".$notifiedevent['code']); //!=$langs->trans("Notify_".$notifiedevent['code'])?$langs->trans("Notify_".$notifiedevent['code']):$notifiedevent['label'];
  168. $elementLabel = $langs->trans(ucfirst($notifiedevent['elementtype']));
  169. // Special cases
  170. if ($notifiedevent['elementtype'] == 'order_supplier') $elementLabel = $langs->trans('SupplierOrder');
  171. elseif ($notifiedevent['elementtype'] == 'propal') $elementLabel = $langs->trans('Proposal');
  172. elseif ($notifiedevent['elementtype'] == 'facture') $elementLabel = $langs->trans('Bill');
  173. elseif ($notifiedevent['elementtype'] == 'commande') $elementLabel = $langs->trans('Order');
  174. elseif ($notifiedevent['elementtype'] == 'ficheinter') $elementLabel = $langs->trans('Intervention');
  175. elseif ($notifiedevent['elementtype'] == 'shipping') $elementLabel = $langs->trans('Shipping');
  176. elseif ($notifiedevent['elementtype'] == 'expensereport') $elementLabel = $langs->trans('ExpenseReport');
  177. print '<tr class="oddeven">';
  178. print '<td>'.$elementLabel.'</td>';
  179. print '<td>'.$notifiedevent['code'].'</td>';
  180. print '<td>'.$label.'</td>';
  181. print '<td>';
  182. // Notification with threshold
  183. foreach($conf->global as $key => $val)
  184. {
  185. if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifiedevent['code'].'_THRESHOLD_HIGHER_(.*)/', $key, $reg)) continue;
  186. $param='NOTIFICATION_FIXEDEMAIL_'.$notifiedevent['code'].'_THRESHOLD_HIGHER_'.$reg[1];
  187. $value=GETPOST('NOTIF_'.$notifiedevent['code'].'_old_'.$reg[1].'_key')?GETPOST('NOTIF_'.$notifiedevent['code'].'_old_'.$reg[1].'_key','alpha'):$conf->global->$param;
  188. $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.
  189. $arrayemail=explode(',',$value);
  190. $showwarning=0;
  191. foreach($arrayemail as $key=>$valuedet)
  192. {
  193. $valuedet=trim($valuedet);
  194. if (! empty($valuedet) && ! isValidEmail($valuedet,1)) $showwarning++;
  195. }
  196. if ((! empty($conf->global->$param)) && $showwarning) $s.=' '.img_warning($langs->trans("ErrorBadEMail"));
  197. print $form->textwithpicto($s,$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients").'<br>'.$langs->trans("YouCanAlsoUseSupervisorKeyword"),1,'help','',0,2);
  198. print '<br>';
  199. }
  200. // New entry input fields
  201. $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.
  202. print $form->textwithpicto($s,$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients").'<br>'.$langs->trans("YouCanAlsoUseSupervisorKeyword"),1,'help','',0,2);
  203. print '</td>';
  204. print '<td>';
  205. // Notification with threshold
  206. foreach($conf->global as $key => $val)
  207. {
  208. if ($val == '' || ! preg_match('/^NOTIFICATION_FIXEDEMAIL_'.$notifiedevent['code'].'_THRESHOLD_HIGHER_(.*)/', $key, $reg)) continue;
  209. print $langs->trans("AmountHT").' >= <input type="text" size="4" name="NOTIF_'.$notifiedevent['code'].'_old_'.$reg[1].'_amount" value="'.dol_escape_htmltag($reg[1]).'">';
  210. print '<br>';
  211. }
  212. // New entry input fields
  213. print $langs->trans("AmountHT").' >= <input type="text" size="4" name="NOTIF_'.$notifiedevent['code'].'_new_amount" value="">';
  214. print '</td>';
  215. print '<td>';
  216. // TODO Add link to show message content
  217. print '</td>';
  218. print '</tr>';
  219. }
  220. print '</table>';
  221. print '<br>';
  222. print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Save").'"></div>';
  223. print '</form>';
  224. // End of page
  225. llxFooter();
  226. $db->close();