card.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. /* Copyright (C) 2013-2014 Jean-François Ferry <jfefe@aternatik.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file resource/card.php
  19. * \ingroup resource
  20. * \brief Page to manage resource object
  21. */
  22. require '../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  24. require_once DOL_DOCUMENT_ROOT.'/resource/class/dolresource.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/resource/class/html.formresource.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/resource.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('resource', 'companies', 'other', 'main'));
  31. // Get parameters
  32. $id = GETPOST('id','int');
  33. $action = GETPOST('action','alpha');
  34. $cancel = GETPOST('cancel','alpha');
  35. $ref = GETPOST('ref','alpha');
  36. $description = GETPOST('description');
  37. $confirm = GETPOST('confirm');
  38. $fk_code_type_resource = GETPOST('fk_code_type_resource','alpha');
  39. $country_id = GETPOST('country_id', 'int');
  40. // Protection if external user
  41. if ($user->socid > 0)
  42. {
  43. accessforbidden();
  44. }
  45. if( ! $user->rights->resource->read)
  46. {
  47. accessforbidden();
  48. }
  49. $object = new Dolresource($db);
  50. $extrafields = new ExtraFields($db);
  51. // fetch optionals attributes and labels
  52. $extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
  53. /*******************************************************************
  54. * ACTIONS
  55. ********************************************************************/
  56. $hookmanager->initHooks(array('resource', 'resource_card','globalcard'));
  57. $parameters=array('resource_id'=>$id);
  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. if ($cancel)
  63. {
  64. if (! empty($backtopage))
  65. {
  66. header("Location: ".$backtopage);
  67. exit;
  68. }
  69. if ($action == 'add')
  70. {
  71. header("Location: ".DOL_URL_ROOT.'/resource/list.php');
  72. exit;
  73. }
  74. $action='';
  75. }
  76. if ($action == 'add' && $user->rights->resource->write)
  77. {
  78. if (! $cancel)
  79. {
  80. $error='';
  81. if (empty($ref))
  82. {
  83. setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("Ref")), null, 'errors');
  84. $action = 'create';
  85. }
  86. else
  87. {
  88. $object->ref = $ref;
  89. $object->description = $description;
  90. $object->fk_code_type_resource = $fk_code_type_resource;
  91. $object->country_id = $country_id;
  92. // Fill array 'array_options' with data from add form
  93. $ret = $extrafields->setOptionalsFromPost($extralabels,$object);
  94. if ($ret < 0) $error++;
  95. $result=$object->create($user);
  96. if ($result > 0)
  97. {
  98. // Creation OK
  99. setEventMessages($langs->trans('ResourceCreatedWithSuccess'), null, 'mesgs');
  100. Header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  101. exit;
  102. }
  103. else
  104. {
  105. // Creation KO
  106. setEventMessages($object->error, $object->errors, 'errors');
  107. $action = 'create';
  108. }
  109. }
  110. }
  111. else
  112. {
  113. Header("Location: list.php");
  114. exit;
  115. }
  116. }
  117. if ($action == 'update' && ! $cancel && $user->rights->resource->write)
  118. {
  119. $error=0;
  120. if (empty($ref))
  121. {
  122. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
  123. $error++;
  124. }
  125. if (! $error)
  126. {
  127. $res = $object->fetch($id);
  128. if ( $res > 0 )
  129. {
  130. $object->ref = $ref;
  131. $object->description = $description;
  132. $object->fk_code_type_resource = $fk_code_type_resource;
  133. $object->country_id = $country_id;
  134. // Fill array 'array_options' with data from add form
  135. $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
  136. if ($ret < 0) {
  137. $error ++;
  138. }
  139. $result=$object->update($user);
  140. if ($result > 0)
  141. {
  142. Header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  143. exit;
  144. }
  145. else
  146. {
  147. setEventMessages($object->error, $object->errors, 'errors');
  148. $error++;
  149. }
  150. }
  151. else
  152. {
  153. setEventMessages($object->error, $object->errors, 'errors');
  154. $error++;
  155. }
  156. }
  157. if ($error)
  158. {
  159. $action='edit';
  160. }
  161. }
  162. if ($action == 'confirm_delete_resource' && $user->rights->resource->delete && $confirm === 'yes')
  163. {
  164. $res = $object->fetch($id);
  165. if($res > 0)
  166. {
  167. $result = $object->delete($id);
  168. if ($result >= 0)
  169. {
  170. setEventMessages($langs->trans('RessourceSuccessfullyDeleted'), null, 'mesgs');
  171. Header('Location: '.DOL_URL_ROOT.'/resource/list.php');
  172. exit;
  173. }
  174. else
  175. {
  176. setEventMessages($object->error, $object->errors, 'errors');
  177. }
  178. }
  179. else
  180. {
  181. setEventMessages($object->error, $object->errors, 'errors');
  182. }
  183. }
  184. }
  185. /***************************************************
  186. * VIEW
  187. *
  188. * Put here all code to build page
  189. ****************************************************/
  190. $title = $langs->trans($action == 'create' ? 'AddResource' : 'ResourceSingular');
  191. llxHeader('',$title,'');
  192. $form = new Form($db);
  193. $formresource = new FormResource($db);
  194. if ($action == 'create' || $object->fetch($id) > 0)
  195. {
  196. if ($action == 'create')
  197. {
  198. print load_fiche_titre($title,'','title_generic');
  199. dol_fiche_head('');
  200. }
  201. else
  202. {
  203. $head = resource_prepare_head($object);
  204. dol_fiche_head($head, 'resource', $title, -1, 'resource');
  205. }
  206. if ($action == 'create' || $action == 'edit')
  207. {
  208. if ( ! $user->rights->resource->write )
  209. accessforbidden('',0);
  210. // Create/Edit object
  211. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">';
  212. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  213. print '<input type="hidden" name="action" value="'.($action == "create"?"add":"update").'">';
  214. print '<table class="border" width="100%">';
  215. // Ref
  216. print '<tr><td class="titlefieldcreate fieldrequired">'.$langs->trans("ResourceFormLabel_ref").'</td>';
  217. print '<td><input class="minwidth200" name="ref" value="'.($ref ? $ref : $object->ref).'" autofocus="autofocus"></td></tr>';
  218. // Type
  219. print '<tr><td>'.$langs->trans("ResourceType").'</td>';
  220. print '<td>';
  221. $ret = $formresource->select_types_resource($object->fk_code_type_resource,'fk_code_type_resource','',2);
  222. print '</td></tr>';
  223. // Description
  224. print '<tr><td class="tdtop">'.$langs->trans("Description").'</td>';
  225. print '<td>';
  226. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  227. $doleditor = new DolEditor('description', ($description ? $description : $object->description), '', '200', 'dolibarr_notes', false);
  228. $doleditor->Create();
  229. print '</td></tr>';
  230. // Origin country
  231. print '<tr><td>'.$langs->trans("CountryOrigin").'</td><td>';
  232. print $form->select_country($object->country_id,'country_id');
  233. if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
  234. print '</td></tr>';
  235. // Other attributes
  236. $parameters=array('objectsrc' => $objectsrc);
  237. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  238. print $hookmanager->resPrint;
  239. if (empty($reshook))
  240. {
  241. print $object->showOptionals($extrafields,'edit');
  242. }
  243. print '</table>';
  244. dol_fiche_end();
  245. print '<div class="center">';
  246. print '<input type="submit" class="button" name="save" value="' . $langs->trans($action == "create"?"Create":"Modify") . '">';
  247. print ' &nbsp; &nbsp; ';
  248. print '<input type="submit" class="button" name="cancel" value="' . $langs->trans("Cancel") . '">';
  249. print '</div>';
  250. print '</div>';
  251. print '</form>';
  252. }
  253. else
  254. {
  255. $formconfirm = '';
  256. // Confirm deleting resource line
  257. if ($action == 'delete')
  258. {
  259. $formconfirm = $form->formconfirm("card.php?&id=".$object->id,$langs->trans("DeleteResource"),$langs->trans("ConfirmDeleteResource"),"confirm_delete_resource",'','',1);
  260. }
  261. // Print form confirm
  262. print $formconfirm;
  263. $linkback = '<a href="' . DOL_URL_ROOT . '/resource/list.php' . (! empty($socid) ? '?id=' . $socid : '') . '">' . $langs->trans("BackToList") . '</a>';
  264. $morehtmlref='<div class="refidno">';
  265. $morehtmlref.='</div>';
  266. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  267. print '<div class="fichecenter">';
  268. print '<div class="underbanner clearboth"></div>';
  269. /*---------------------------------------
  270. * View object
  271. */
  272. print '<table width="100%" class="border">';
  273. // Resource type
  274. print '<tr>';
  275. print '<td class="titlefield">' . $langs->trans("ResourceType") . '</td>';
  276. print '<td>';
  277. print $object->type_label;
  278. print '</td>';
  279. print '</tr>';
  280. // Description
  281. print '<tr>';
  282. print '<td>' . $langs->trans("ResourceFormLabel_description") . '</td>';
  283. print '<td>';
  284. print $object->description;
  285. print '</td>';
  286. // Other attributes
  287. include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
  288. print '</tr>';
  289. // Origin country code
  290. print '<tr>';
  291. print '<td>'.$langs->trans("CountryOrigin").'</td>';
  292. print '<td>';
  293. print getCountry($object->country_id,0,$db);
  294. print '</td>';
  295. print '</tr>';
  296. print '</table>';
  297. print '</div>';
  298. print '<div class="clearboth"></div><br>';
  299. dol_fiche_end();
  300. }
  301. /*
  302. * Boutons actions
  303. */
  304. print '<div class="tabsAction">';
  305. $parameters = array();
  306. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
  307. // modified by hook
  308. if (empty($reshook))
  309. {
  310. if ($action != "create" && $action != "edit" )
  311. {
  312. // Edit resource
  313. if($user->rights->resource->write)
  314. {
  315. print '<div class="inline-block divButAction">';
  316. print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;action=edit" class="butAction">'.$langs->trans('Modify').'</a>';
  317. print '</div>';
  318. }
  319. }
  320. if ($action != "delete" && $action != "create" && $action != "edit")
  321. {
  322. // Delete resource
  323. if($user->rights->resource->delete)
  324. {
  325. print '<div class="inline-block divButAction">';
  326. print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;action=delete" class="butActionDelete">'.$langs->trans('Delete').'</a>';
  327. print '</div>';
  328. }
  329. }
  330. }
  331. print '</div>';
  332. }
  333. else {
  334. dol_print_error();
  335. }
  336. // End of page
  337. llxFooter();
  338. $db->close();