card.php 18 KB

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