fiche.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/societe/notify/fiche.php
  21. * \ingroup societe notification
  22. * \brief Tab for notifications of third party
  23. */
  24. require("../../main.inc.php");
  25. require_once(DOL_DOCUMENT_ROOT."/core/lib/company.lib.php");
  26. require_once(DOL_DOCUMENT_ROOT."/contact/class/contact.class.php");
  27. require_once(DOL_DOCUMENT_ROOT."/core/triggers/interface_50_modNotification_Notification.class.php");
  28. $langs->load("companies");
  29. $langs->load("mails");
  30. $langs->load("admin");
  31. $langs->load("other");
  32. $socid = GETPOST("socid",'int');
  33. $action = GETPOST('action');
  34. $contactid=GETPOST('contactid'); // May be an int or 'thirdparty'
  35. $actionid=GETPOST('actionid');
  36. // Security check
  37. if ($user->societe_id) $socid=$user->societe_id;
  38. $result = restrictedArea($user, 'societe','','');
  39. $sortfield = GETPOST("sortfield",'alpha');
  40. $sortorder = GETPOST("sortorder",'alpha');
  41. $page = GETPOST("page",'int');
  42. if ($page == -1) { $page = 0; }
  43. $offset = $conf->liste_limit * $page;
  44. $pageprev = $page - 1;
  45. $pagenext = $page + 1;
  46. if (! $sortorder) $sortorder="ASC";
  47. if (! $sortfield) $sortfield="c.name";
  48. $now=dol_now();
  49. /*
  50. * Actions
  51. */
  52. // Add a notification
  53. if ($action == 'add')
  54. {
  55. $error=0;
  56. if (empty($contactid))
  57. {
  58. $mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Contact")).'</div>';
  59. $error++;
  60. }
  61. if ($actionid <= 0)
  62. {
  63. $mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Action")).'</div>';
  64. $error++;
  65. }
  66. if (! $error)
  67. {
  68. $db->begin();
  69. $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def";
  70. $sql .= " WHERE fk_soc=".$socid." AND fk_contact=".$contactid." AND fk_action=".$actionid;
  71. if ($db->query($sql))
  72. {
  73. $sql = "INSERT INTO ".MAIN_DB_PREFIX."notify_def (datec,fk_soc, fk_contact, fk_action)";
  74. $sql .= " VALUES ('".$db->idate($now)."',".$socid.",".$contactid.",".$actionid.")";
  75. if (! $db->query($sql))
  76. {
  77. $error++;
  78. dol_print_error($db);
  79. }
  80. }
  81. else
  82. {
  83. dol_print_error($db);
  84. }
  85. if (! $error)
  86. {
  87. $db->commit();
  88. }
  89. else
  90. {
  91. $db->rollback();
  92. }
  93. }
  94. }
  95. // Remove a notification
  96. if ($action == 'delete')
  97. {
  98. $sql = "DELETE FROM ".MAIN_DB_PREFIX."notify_def where rowid=".$_GET["actid"];
  99. $db->query($sql);
  100. }
  101. /*
  102. * View
  103. */
  104. $form = new Form($db);
  105. llxHeader();
  106. $object = new Societe($db);
  107. $result=$object->fetch($socid);
  108. if ($result > 0)
  109. {
  110. $form = new Form($db);
  111. $langs->load("other");
  112. $head = societe_prepare_head($object);
  113. dol_fiche_head($head, 'notify', $langs->trans("ThirdParty"),0,'company');
  114. print '<table class="border"width="100%">';
  115. print '<tr><td width="20%">'.$langs->trans("ThirdPartyName").'</td><td colspan="3">';
  116. print $form->showrefnav($object,'socid','',($user->societe_id?0:1),'rowid','nom');
  117. print '</td></tr>';
  118. // Prefix
  119. if (! empty($conf->global->SOCIETE_USEPREFIX)) // Old not used prefix field
  120. {
  121. print '<tr><td>'.$langs->trans('Prefix').'</td><td colspan="3">'.$object->prefix_comm.'</td></tr>';
  122. }
  123. if ($object->client)
  124. {
  125. print '<tr><td>';
  126. print $langs->trans('CustomerCode').'</td><td colspan="3">';
  127. print $object->code_client;
  128. if ($object->check_codeclient() <> 0) print ' <font class="error">('.$langs->trans("WrongCustomerCode").')</font>';
  129. print '</td></tr>';
  130. }
  131. if ($object->fournisseur)
  132. {
  133. print '<tr><td>';
  134. print $langs->trans('SupplierCode').'</td><td colspan="3">';
  135. print $object->code_fournisseur;
  136. if ($object->check_codefournisseur() <> 0) print ' <font class="error">('.$langs->trans("WrongSupplierCode").')</font>';
  137. print '</td></tr>';
  138. }
  139. print '<tr><td width="30%">'.$langs->trans("NbOfActiveNotifications").'</td>';
  140. print '<td colspan="3">';
  141. $sql = "SELECT COUNT(n.rowid) as nb";
  142. $sql.= " FROM ".MAIN_DB_PREFIX."notify_def as n";
  143. $sql.= " WHERE fk_soc = ".$object->id;
  144. $resql=$db->query($sql);
  145. if ($resql)
  146. {
  147. $num = $db->num_rows($resql);
  148. $i = 0;
  149. while ($i < $num)
  150. {
  151. $obj = $db->fetch_object($resql);
  152. $nb=$obj->nb;
  153. $i++;
  154. }
  155. }
  156. else {
  157. dol_print_error($db);
  158. }
  159. print $nb;
  160. print '</td></tr>';
  161. print '</table>';
  162. // Help
  163. print '<br>'.$langs->trans("NotificationsDesc").'<br>';
  164. dol_fiche_end();
  165. print "\n";
  166. // Add notification form
  167. print_fiche_titre($langs->trans("AddNewNotification"),'','');
  168. print '<form action="fiche.php?socid='.$socid.'" method="post">';
  169. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  170. print '<input type="hidden" name="action" value="add">';
  171. $param="&socid=".$socid;
  172. // Line with titles
  173. print '<table width="100%" class="noborder">';
  174. print '<tr class="liste_titre">';
  175. print_liste_field_titre($langs->trans("Contact"),"fiche.php","c.name",'',$param,'"width="45%"',$sortfield,$sortorder);
  176. print_liste_field_titre($langs->trans("Action"),"fiche.php","a.titre",'',$param,'"width="35%"',$sortfield,$sortorder);
  177. print_liste_field_titre($langs->trans("Type"),"fiche.php","",'',$param,'"width="10%"',$sortfield,$sortorder);
  178. print_liste_field_titre('');
  179. print '</tr>';
  180. $var=false;
  181. $listofemails=$object->thirdparty_and_contact_email_array();
  182. if (count($listofemails) > 0)
  183. {
  184. $actions=array();
  185. // Load array of available notifications
  186. $notificationtrigger=new InterfaceNotification($db);
  187. $listofnotifiedevents=$notificationtrigger->getListOfManagedEvents();
  188. foreach($listofnotifiedevents as $notifiedevent)
  189. {
  190. $label=$langs->trans("Notify_".$notifiedevent['code'])!=$langs->trans("Notify_".$notifiedevent['code'])?$langs->trans("Notify_".$notifiedevent['code']):$notifiedevent['label'];
  191. $actions[$notifiedevent['rowid']]=$label;
  192. }
  193. print '<tr '.$bc[$var].'><td>';
  194. print $form->selectarray("contactid",$listofemails);
  195. print '</td>';
  196. print '<td>';
  197. print $form->selectarray("actionid",$actions,'',1);
  198. print '</td>';
  199. print '<td>';
  200. $type=array('email'=>$langs->trans("EMail"));
  201. print $form->selectarray("typeid",$type);
  202. print '</td>';
  203. print '<td align="right"><input type="submit" class="button" value="'.$langs->trans("Add").'"></td>';
  204. print '</tr>';
  205. }
  206. else
  207. {
  208. print '<tr '.$bc[$var].'><td colspan="4">';
  209. print $langs->trans("YouMustCreateContactFirst");
  210. print '</td></tr>';
  211. }
  212. print '</table>';
  213. print '</form>';
  214. print '<br>';
  215. dol_htmloutput_mesg($mesg);
  216. // List of active notifications
  217. print_fiche_titre($langs->trans("ListOfActiveNotifications"),'','');
  218. $var=true;
  219. // Line with titles
  220. print '<table width="100%" class="noborder">';
  221. print '<tr class="liste_titre">';
  222. print_liste_field_titre($langs->trans("Contact"),"fiche.php","c.name",'',$param,'"width="45%"',$sortfield,$sortorder);
  223. print_liste_field_titre($langs->trans("Action"),"fiche.php","a.titre",'',$param,'"width="35%"',$sortfield,$sortorder);
  224. print_liste_field_titre($langs->trans("Type"),"fiche.php","",'',$param,'"width="10%"',$sortfield,$sortorder);
  225. print_liste_field_titre('','','');
  226. print '</tr>';
  227. // List of notifications for contacts
  228. $sql = "SELECT n.rowid, n.type,";
  229. $sql.= " a.code, a.label,";
  230. $sql.= " c.rowid as contactid, c.name, c.firstname, c.email";
  231. $sql.= " FROM ".MAIN_DB_PREFIX."c_action_trigger as a,";
  232. $sql.= " ".MAIN_DB_PREFIX."notify_def as n,";
  233. $sql.= " ".MAIN_DB_PREFIX."socpeople c";
  234. $sql.= " WHERE a.rowid = n.fk_action";
  235. $sql.= " AND c.rowid = n.fk_contact";
  236. $sql.= " AND c.fk_soc = ".$object->id;
  237. $resql=$db->query($sql);
  238. if ($resql)
  239. {
  240. $num = $db->num_rows($resql);
  241. $i = 0;
  242. $contactstatic=new Contact($db);
  243. while ($i < $num)
  244. {
  245. $var = !$var;
  246. $obj = $db->fetch_object($resql);
  247. $contactstatic->id=$obj->contactid;
  248. $contactstatic->name=$obj->name;
  249. $contactstatic->firstname=$obj->firstname;
  250. print '<tr '.$bc[$var].'><td>'.$contactstatic->getNomUrl(1);
  251. if ($obj->type == 'email')
  252. {
  253. if (isValidEmail($obj->email))
  254. {
  255. print ' &lt;'.$obj->email.'&gt;';
  256. }
  257. else
  258. {
  259. $langs->load("errors");
  260. print ' &nbsp; '.img_warning().' '.$langs->trans("ErrorBadEMail",$obj->email);
  261. }
  262. }
  263. print '</td>';
  264. print '<td>';
  265. $label=($langs->trans("Notify_".$obj->code)!="Notify_".$obj->code?$langs->trans("Notify_".$obj->code):$obj->label);
  266. print $label;
  267. print '</td>';
  268. print '<td>';
  269. if ($obj->type == 'email') print $langs->trans("Email");
  270. if ($obj->type == 'sms') print $langs->trans("SMS");
  271. print '</td>';
  272. print '<td align="right"><a href="fiche.php?socid='.$socid.'&action=delete&actid='.$obj->rowid.'">'.img_delete().'</a></td>';
  273. print '</tr>';
  274. $i++;
  275. }
  276. $db->free($resql);
  277. }
  278. else
  279. {
  280. dol_print_error($db);
  281. }
  282. print '</table>';
  283. print '<br>';
  284. // List of notifications done
  285. print_fiche_titre($langs->trans("ListOfNotificationsDone"),'','');
  286. $var=true;
  287. // Line with titles
  288. print '<table width="100%" class="noborder">';
  289. print '<tr class="liste_titre">';
  290. print_liste_field_titre($langs->trans("Contact"),"fiche.php","c.name",'',$param,'',$sortfield,$sortorder);
  291. print_liste_field_titre($langs->trans("Action"),"fiche.php","a.titre",'',$param,'',$sortfield,$sortorder);
  292. print_liste_field_titre($langs->trans("Date"),"fiche.php","a.daten",'',$param,'align="right"',$sortfield,$sortorder);
  293. print '</tr>';
  294. // List
  295. $sql = "SELECT n.rowid, n.daten, n.email, n.objet_type, n.objet_id,";
  296. $sql.= " c.rowid as id, c.name, c.firstname, c.email,";
  297. $sql.= " a.code, a.label";
  298. $sql.= " FROM ".MAIN_DB_PREFIX."c_action_trigger as a,";
  299. $sql.= " ".MAIN_DB_PREFIX."notify as n, ";
  300. $sql.= " ".MAIN_DB_PREFIX."socpeople as c";
  301. $sql.= " WHERE a.rowid = n.fk_action";
  302. $sql.= " AND c.rowid = n.fk_contact";
  303. $sql.= " AND c.fk_soc = ".$object->id;
  304. $resql=$db->query($sql);
  305. if ($resql)
  306. {
  307. $num = $db->num_rows($resql);
  308. $i = 0;
  309. $contactstatic=new Contact($db);
  310. while ($i < $num)
  311. {
  312. $var = !$var;
  313. $obj = $db->fetch_object($resql);
  314. $contactstatic->id=$obj->id;
  315. $contactstatic->name=$obj->name;
  316. $contactstatic->firstname=$obj->firstname;
  317. print '<tr '.$bc[$var].'><td>'.$contactstatic->getNomUrl(1);
  318. print $obj->email?' &lt;'.$obj->email.'&gt;':$langs->trans("NoMail");
  319. print '</td>';
  320. print '<td>';
  321. $label=($langs->trans("Notify_".$obj->code)!="Notify_".$obj->code?$langs->trans("Notify_".$obj->code):$obj->label);
  322. print $label;
  323. print '</td>';
  324. // TODO Add link to object here
  325. // print
  326. print'<td align="right">'.dol_print_date($db->jdate($obj->daten), 'dayhour').'</td>';
  327. print '</tr>';
  328. $i++;
  329. }
  330. $db->free($resql);
  331. }
  332. else
  333. {
  334. dol_print_error($db);
  335. }
  336. print '</table>';
  337. }
  338. else dol_print_error('','RecordNotFound');
  339. $db->close();
  340. llxFooter();
  341. ?>