card.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2010-2014 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  6. * Copyright (C) 2016 Abbes Bahfir <contact@dolibarrpar.com>
  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/user/notify/card.php
  23. * \ingroup user notification
  24. * \brief Tab for notifications of third party
  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/usergroups.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/triggers/interface_50_modNotification_Notification.class.php';
  31. // Load translation files required by page
  32. $langs->loadLangs(array('companies', 'mails', 'admin', 'other', 'errors'));
  33. $id = GETPOST("id", 'int');
  34. $ref = GETPOST('ref', 'alpha');
  35. $action = GETPOST('action', 'aZ09');
  36. $actionid = GETPOST('actionid');
  37. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  38. $sortfield = GETPOST("sortfield", 'alpha');
  39. $sortorder = GETPOST("sortorder", 'alpha');
  40. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  41. if (!$sortorder) {
  42. $sortorder = "DESC";
  43. }
  44. if (!$sortfield) {
  45. $sortfield = "n.daten";
  46. }
  47. if (empty($page) || $page == -1) {
  48. $page = 0;
  49. }
  50. $offset = $limit * $page;
  51. $pageprev = $page - 1;
  52. $pagenext = $page + 1;
  53. $now = dol_now();
  54. // Security check
  55. $object = new User($db);
  56. if ($id > 0 || !empty($ref)) {
  57. $result = $object->fetch($id, $ref, '', 1);
  58. $object->getrights();
  59. }
  60. $permissiontoadd = (($object->id == $user->id) || (!empty($user->rights->user->user->lire)));
  61. // Security check
  62. if ($user->socid) {
  63. $id = $user->socid;
  64. }
  65. $result = restrictedArea($user, 'user', '', '', 'user');
  66. /*
  67. * Actions
  68. */
  69. if (GETPOST('cancel', 'alpha')) {
  70. $action = 'list';
  71. }
  72. // Add a notification
  73. if ($action == 'add') {
  74. $error = 0;
  75. if ($actionid <= 0) {
  76. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Action")), null, 'errors');
  77. $error++;
  78. $action = 'create';
  79. }
  80. if (!$error) {
  81. $db->begin();
  82. $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def";
  83. $sql .= " WHERE fk_user=".((int) $id)." AND fk_action=".((int) $actionid);
  84. if ($db->query($sql)) {
  85. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify_def (datec,fk_user, fk_action)";
  86. $sql .= " VALUES ('".$db->idate($now)."',".$id.",".$actionid.")";
  87. if (!$db->query($sql)) {
  88. $error++;
  89. dol_print_error($db);
  90. }
  91. } else {
  92. dol_print_error($db);
  93. }
  94. if (!$error) {
  95. $db->commit();
  96. } else {
  97. $db->rollback();
  98. $action = 'create';
  99. }
  100. }
  101. }
  102. // Remove a notification
  103. if ($action == 'delete') {
  104. $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def where rowid=".GETPOST("actid", "int");
  105. $db->query($sql);
  106. }
  107. /*
  108. * View
  109. */
  110. $form = new Form($db);
  111. $object = new User($db);
  112. $result = $object->fetch($id, '', '', 1);
  113. $object->getrights();
  114. $title = $langs->trans("ThirdParty").' - '.$langs->trans("Notification");
  115. if (!empty($conf->global->MAIN_HTML_TITLE) && preg_match('/thirdpartynameonly/', $conf->global->MAIN_HTML_TITLE) && $object->name) {
  116. $title = $object->name.' - '.$langs->trans("Notification");
  117. }
  118. $help_url = 'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
  119. llxHeader('', $title, $help_url);
  120. if ($result > 0) {
  121. $langs->load("other");
  122. $head = user_prepare_head($object);
  123. print dol_get_fiche_head($head, 'notify', $langs->trans("User"), -1, 'user');
  124. $linkback = '<a href="'.DOL_URL_ROOT.'/user/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  125. dol_banner_tab($object, 'id', $linkback, $user->rights->user->user->lire || $user->admin, 'rowid', 'ref', '', '', 0, '', '', 0, '');
  126. print '<div class="fichecenter">';
  127. print '<div class="underbanner clearboth"></div>';
  128. print '<table class="border centpercent tableforfield">';
  129. // Login
  130. print '<tr><td class="titlefield">'.$langs->trans("Login").'</td>';
  131. if (!empty($object->ldap_sid) && $object->statut == 0) {
  132. print '<td class="error">'.$langs->trans("LoginAccountDisableInDolibarr").'</td>';
  133. } else {
  134. print '<td>'.$object->login.'</td>';
  135. }
  136. print '</tr>'."\n";
  137. /*print '<tr><td class="titlefield">'.$langs->trans("NbOfActiveNotifications").'</td>'; // Notification for this thirdparty
  138. print '<td colspan="3">';
  139. $nbofrecipientemails=0;
  140. $notify=new Notify($db);
  141. $tmparray = $notify->getNotificationsArray('', 0, null, $object->id, array('user'));
  142. foreach($tmparray as $tmpkey => $tmpval)
  143. {
  144. $nbofrecipientemails++;
  145. }
  146. print $nbofrecipientemails;
  147. print '</td></tr>';*/
  148. print '</table>';
  149. print '</div>';
  150. print dol_get_fiche_end();
  151. print "\n";
  152. // Help
  153. print '<span class="opacitymedium">';
  154. print '<br>'.$langs->trans("NotificationsDesc");
  155. print '<br>'.$langs->trans("NotificationsDescUser");
  156. print '<br>'.$langs->trans("NotificationsDescContact");
  157. print '<br>'.$langs->trans("NotificationsDescGlobal");
  158. print '</span>';
  159. print '<br><br><br>'."\n";
  160. // Add notification form
  161. // print load_fiche_titre($langs->trans("AddNewNotification"), '', '');
  162. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.urlencode($id).'" method="POST">';
  163. print '<input type="hidden" name="token" value="'.newToken().'">';
  164. print '<input type="hidden" name="action" value="add">';
  165. $param = "&id=".urlencode($id);
  166. // Line with titles
  167. /* print '<table width="100%" class="noborder">';
  168. print '<tr class="liste_titre">';
  169. print_liste_field_titre("Target", $_SERVER["PHP_SELF"], "c.lastname,c.firstname", '', $param, 'width="45%"', $sortfield, $sortorder);
  170. print_liste_field_titre("Action", $_SERVER["PHP_SELF"], "", '', $param, 'width="35%"', $sortfield, $sortorder);
  171. print_liste_field_titre("Type", $_SERVER["PHP_SELF"], "n.type", '', $param, 'width="10%"', $sortfield, $sortorder);
  172. print_liste_field_titre('');
  173. print "</tr>\n";
  174. print '</table>';
  175. print '<br>';
  176. */
  177. // List of notifications enabled for contacts
  178. $sql = "SELECT n.rowid, n.type,";
  179. $sql .= " a.code, a.label,";
  180. $sql .= " c.rowid as userid, c.entity, c.login, c.lastname, c.firstname, c.email, c.statut as status";
  181. $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger as a,";
  182. $sql .= " ".MAIN_DB_PREFIX."notify_def as n,";
  183. $sql .= " ".MAIN_DB_PREFIX."user c";
  184. $sql .= " WHERE a.rowid = n.fk_action";
  185. $sql .= " AND c.rowid = n.fk_user";
  186. $sql .= " AND c.rowid = ".$object->id;
  187. $sql .= " AND c.entity IN (".getEntity('user').')';
  188. $resql = $db->query($sql);
  189. if ($resql) {
  190. $num = $db->num_rows($resql);
  191. } else {
  192. dol_print_error($db);
  193. }
  194. $newcardbutton = dolGetButtonTitle($langs->trans('New'), '', 'fa fa-plus-circle', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=create&backtopage='.urlencode($_SERVER['PHP_SELF']), '', $permissiontoadd);
  195. $title = $langs->trans("ListOfActiveNotifications");
  196. // List of active notifications
  197. //print load_fiche_titre($langs->trans("ListOfActiveNotifications").' ('.$num.')', '', '');
  198. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $num, 'email', 0, $newcardbutton, '', $limit, 0, 0, 1);
  199. // Line with titles
  200. print '<table width="100%" class="noborder">';
  201. print '<tr class="liste_titre">';
  202. print_liste_field_titre("Target", $_SERVER["PHP_SELF"], "c.lastname,c.firstname", '', $param, 'width="45%"', $sortfield, $sortorder);
  203. print_liste_field_titre("Action", $_SERVER["PHP_SELF"], "", '', $param, 'width="35%"', $sortfield, $sortorder);
  204. print_liste_field_titre("Type", $_SERVER["PHP_SELF"], "n.type", '', $param, 'width="10%"', $sortfield, $sortorder);
  205. print_liste_field_titre('', '', '');
  206. print '</tr>';
  207. if ($action == 'create') {
  208. // $listofemails=$object->thirdparty_and_contact_email_array();
  209. if ($object->email) {
  210. $actions = array();
  211. // Load array of available notifications
  212. $notificationtrigger = new InterfaceNotification($db);
  213. $listofnotifiedevents = $notificationtrigger->getListOfManagedEvents();
  214. foreach ($listofnotifiedevents as $notifiedevent) {
  215. $label = ($langs->trans("Notify_".$notifiedevent['code']) != "Notify_".$notifiedevent['code'] ? $langs->trans("Notify_".$notifiedevent['code']) : $notifiedevent['label']);
  216. $actions[$notifiedevent['rowid']] = $label;
  217. }
  218. print '<tr class="oddeven nohover"><td>';
  219. print $object->getNomUrl(1);
  220. if (isValidEmail($object->email)) {
  221. print ' &lt;'.$object->email.'&gt;';
  222. } else {
  223. $langs->load("errors");
  224. print ' &nbsp; '.img_warning().' '.$langs->trans("ErrorBadEMail", $object->email);
  225. }
  226. print '</td>';
  227. print '<td>';
  228. print img_picto('', 'object_action', '', false, 0, 0, '', 'paddingright').$form->selectarray("actionid", $actions, '', 1);
  229. print '</td>';
  230. print '<td>';
  231. $type = array('email'=>$langs->trans("EMail"));
  232. print $form->selectarray("typeid", $type);
  233. print '</td>';
  234. print '<td class="nowraponall">';
  235. print '<input type="submit" class="button" value="'.$langs->trans("Add").'">';
  236. print '&nbsp;';
  237. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
  238. print '</td>';
  239. print '</tr>';
  240. } else {
  241. print '<tr class="oddeven"><td colspan="4">';
  242. print $langs->trans("YouMustAssignUserMailFirst");
  243. print '</td></tr>';
  244. }
  245. } else {
  246. if ($num) {
  247. $i = 0;
  248. $userstatic = new user($db);
  249. while ($i < $num) {
  250. $obj = $db->fetch_object($resql);
  251. $userstatic->id = $obj->userid;
  252. $userstatic->lastname = $obj->lastname;
  253. $userstatic->firstname = $obj->firstname;
  254. $userstatic->email = $obj->email;
  255. $userstatic->statut = $obj->status;
  256. print '<tr class="oddeven"><td>'.$userstatic->getNomUrl(1);
  257. if ($obj->type == 'email') {
  258. if (isValidEmail($obj->email)) {
  259. print ' &lt;'.$obj->email.'&gt;';
  260. } else {
  261. $langs->load("errors");
  262. print ' &nbsp; '.img_warning().' '.$langs->trans("ErrorBadEMail", $obj->email);
  263. }
  264. }
  265. print '</td>';
  266. print '<td>';
  267. $label = ($langs->trans("Notify_".$obj->code) != "Notify_".$obj->code ? $langs->trans("Notify_".$obj->code) : $obj->label);
  268. print img_picto('', 'object_action', '', false, 0, 0, '', 'paddingright').$label;
  269. print '</td>';
  270. print '<td>';
  271. if ($obj->type == 'email') {
  272. print $langs->trans("Email");
  273. }
  274. if ($obj->type == 'sms') {
  275. print $langs->trans("SMS");
  276. }
  277. print '</td>';
  278. print '<td class="right"><a href="card.php?id='.$id.'&amp;action=delete&amp;token='.newToken().'&amp;actid='.$obj->rowid.'">'.img_delete().'</a></td>';
  279. print '</tr>';
  280. $i++;
  281. }
  282. $db->free($resql);
  283. }
  284. // List of notifications enabled for fixed email
  285. /*
  286. foreach($conf->global as $key => $val) {
  287. if (! preg_match('/^NOTIFICATION_FIXEDEMAIL_(.*)/', $key, $reg)) continue;
  288. print '<tr class="oddeven"><td>';
  289. $listtmp=explode(',',$val);
  290. $first=1;
  291. foreach($listtmp as $keyemail => $valemail)
  292. {
  293. if (! $first) print ', ';
  294. $first=0;
  295. $valemail=trim($valemail);
  296. //print $keyemail.' - '.$valemail.' - '.$reg[1].'<br>';
  297. if (isValidEmail($valemail, 1))
  298. {
  299. if ($valemail == '__SUPERVISOREMAIL__') print $valemail;
  300. else print ' &lt;'.$valemail.'&gt;';
  301. }
  302. else
  303. {
  304. print ' '.img_warning().' '.$langs->trans("ErrorBadEMail",$valemail);
  305. }
  306. }
  307. print '</td>';
  308. print '<td>';
  309. $notifcode=preg_replace('/_THRESHOLD_.*$/','',$reg[1]);
  310. $notifcodecond=preg_replace('/^.*_(THRESHOLD_)/','$1',$reg[1]);
  311. $label=($langs->trans("Notify_".$notifcode)!="Notify_".$notifcode?$langs->trans("Notify_".$notifcode):$notifcode);
  312. print $label;
  313. if (preg_match('/^THRESHOLD_HIGHER_(.*)$/',$notifcodecond,$regcond) && ($regcond[1] > 0))
  314. {
  315. print ' - '.$langs->trans("IfAmountHigherThan",$regcond[1]);
  316. }
  317. print '</td>';
  318. print '<td>';
  319. print $langs->trans("Email");
  320. print '</td>';
  321. print '<td class="right">'.$langs->trans("SeeModuleSetup", $langs->transnoentitiesnoconv("Module600Name")).'</td>';
  322. print '</tr>';
  323. }*/
  324. /*if ($user->admin)
  325. {
  326. $var = ! $var;
  327. print '<tr class="oddeven"><td colspan="4">';
  328. print '+ <a href="'.DOL_URL_ROOT.'/admin/notification.php">'.$langs->trans("SeeModuleSetup", $langs->transnoentitiesnoconv("Module600Name")).'</a>';
  329. print '</td></tr>';
  330. }*/
  331. }
  332. print '</table>';
  333. print '</form>';
  334. print '<br><br>'."\n";
  335. // List
  336. $sql = "SELECT n.rowid, n.daten, n.email, n.objet_type as object_type, n.objet_id as object_id, n.type,";
  337. $sql .= " c.rowid as id, c.lastname, c.firstname, c.email as contactemail,";
  338. $sql .= " a.code, a.label";
  339. $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger as a,";
  340. $sql .= " ".MAIN_DB_PREFIX."notify as n";
  341. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as c ON n.fk_user = c.rowid";
  342. $sql .= " WHERE a.rowid = n.fk_action";
  343. $sql .= " AND n.fk_user = ".$object->id;
  344. $sql .= $db->order($sortfield, $sortorder);
  345. // Count total nb of records
  346. $nbtotalofrecords = '';
  347. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  348. $result = $db->query($sql);
  349. $nbtotalofrecords = $db->num_rows($result);
  350. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  351. $page = 0;
  352. $offset = 0;
  353. }
  354. }
  355. $sql .= $db->plimit($limit + 1, $offset);
  356. $resql = $db->query($sql);
  357. if ($resql) {
  358. $num = $db->num_rows($resql);
  359. } else {
  360. dol_print_error($db);
  361. }
  362. $param = '&id='.$object->id;
  363. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  364. $param .= '&contextpage='.$contextpage;
  365. }
  366. if ($limit > 0 && $limit != $conf->liste_limit) {
  367. $param .= '&limit='.$limit;
  368. }
  369. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'" name="formfilter">';
  370. if (isset($optioncss) && $optioncss != '') {
  371. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  372. }
  373. print '<input type="hidden" name="token" value="'.newToken().'">';
  374. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  375. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  376. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  377. print '<input type="hidden" name="page" value="'.$page.'">';
  378. print '<input type="hidden" name="id" value="'.$object->id.'">';
  379. // List of notifications done
  380. print_barre_liste($langs->trans("ListOfNotificationsDone"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'email', 0, '', '', $limit);
  381. // Line with titles
  382. print '<table width="100%" class="noborder">';
  383. print '<tr class="liste_titre">';
  384. print_liste_field_titre("Target", $_SERVER["PHP_SELF"], "c.lastname,c.firstname", '', $param, '', $sortfield, $sortorder);
  385. print_liste_field_titre("Action", $_SERVER["PHP_SELF"], "", '', $param, '', $sortfield, $sortorder);
  386. print_liste_field_titre("Type", $_SERVER["PHP_SELF"], "n.type", '', $param, '', $sortfield, $sortorder);
  387. //print_liste_field_titre("Object",$_SERVER["PHP_SELF"],"",'',$param,'"',$sortfield,$sortorder);
  388. print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "n.daten", '', $param, '', $sortfield, $sortorder, 'right ');
  389. print '</tr>';
  390. if ($num) {
  391. $i = 0;
  392. $userstatic = new User($db);
  393. while ($i < $num) {
  394. $obj = $db->fetch_object($resql);
  395. print '<tr class="oddeven"><td>';
  396. if ($obj->id > 0) {
  397. $userstatic->id = $obj->id;
  398. $userstatic->lastname = $obj->lastname;
  399. $userstatic->firstname = $obj->firstname;
  400. print $userstatic->getNomUrl(1);
  401. print $obj->email ? ' &lt;'.$obj->email.'&gt;' : $langs->trans("NoMail");
  402. } else {
  403. print $obj->email;
  404. }
  405. print '</td>';
  406. print '<td>';
  407. $label = ($langs->trans("Notify_".$obj->code) != "Notify_".$obj->code ? $langs->trans("Notify_".$obj->code) : $obj->label);
  408. print $label;
  409. print '</td>';
  410. print '<td>';
  411. if ($obj->type == 'email') {
  412. print $langs->trans("Email");
  413. }
  414. if ($obj->type == 'sms') {
  415. print $langs->trans("Sms");
  416. }
  417. print '</td>';
  418. // TODO Add link to object here for other types
  419. /*print '<td>';
  420. if ($obj->object_type == 'order')
  421. {
  422. $orderstatic->id=$obj->object_id;
  423. $orderstatic->ref=...
  424. print $orderstatic->getNomUrl(1);
  425. }
  426. print '</td>';*/
  427. // print
  428. print'<td class="right">'.dol_print_date($db->jdate($obj->daten), 'dayhour').'</td>';
  429. print '</tr>';
  430. $i++;
  431. }
  432. $db->free($resql);
  433. } else {
  434. print '<tr><td colspan="4"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  435. }
  436. print '</table>';
  437. print '</form>';
  438. } else {
  439. dol_print_error('', 'RecordNotFound');
  440. }
  441. // End of page
  442. llxFooter();
  443. $db->close();