card.php 16 KB

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