card.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file resource/card.php
  19. * \ingroup resource
  20. * \brief Page to manage resource object
  21. */
  22. // Load Dolibarr environment
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/resource/class/dolresource.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/resource/class/html.formresource.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/resource.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  30. // Load translation files required by the page
  31. $langs->loadLangs(array('resource', 'companies', 'other', 'main'));
  32. // Get parameters
  33. $id = GETPOST('id', 'int');
  34. $action = GETPOST('action', 'aZ09');
  35. $cancel = GETPOST('cancel', 'alpha');
  36. $ref = GETPOST('ref', 'alpha');
  37. $description = GETPOST('description', 'restricthtml');
  38. $confirm = GETPOST('confirm', 'aZ09');
  39. $fk_code_type_resource = GETPOST('fk_code_type_resource', 'alpha');
  40. $country_id = GETPOST('country_id', 'int');
  41. // Protection if external user
  42. if ($user->socid > 0) {
  43. accessforbidden();
  44. }
  45. $object = new Dolresource($db);
  46. $extrafields = new ExtraFields($db);
  47. // fetch optionals attributes and labels
  48. $extrafields->fetch_name_optionals_label($object->table_element);
  49. // Load object
  50. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  51. $result = restrictedArea($user, 'resource', $object->id, 'resource');
  52. $permissiontoadd = $user->rights->resource->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
  53. $permissiontodelete = $user->rights->resource->delete;
  54. /*
  55. * Actions
  56. */
  57. $hookmanager->initHooks(array('resource', 'resource_card', 'globalcard'));
  58. $parameters = array('resource_id'=>$id);
  59. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  60. if ($reshook < 0) {
  61. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  62. }
  63. if (empty($reshook)) {
  64. if ($cancel) {
  65. if (!empty($backtopage)) {
  66. header("Location: ".$backtopage);
  67. exit;
  68. }
  69. if ($action == 'add') {
  70. header("Location: ".DOL_URL_ROOT.'/resource/list.php');
  71. exit;
  72. }
  73. $action = '';
  74. }
  75. if ($action == 'add' && $user->rights->resource->write) {
  76. if (!$cancel) {
  77. $error = '';
  78. if (empty($ref)) {
  79. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
  80. $action = 'create';
  81. } else {
  82. $object->ref = $ref;
  83. $object->description = $description;
  84. $object->fk_code_type_resource = $fk_code_type_resource;
  85. $object->country_id = $country_id;
  86. // Fill array 'array_options' with data from add form
  87. $ret = $extrafields->setOptionalsFromPost(null, $object);
  88. if ($ret < 0) {
  89. $error++;
  90. }
  91. $result = $object->create($user);
  92. if ($result > 0) {
  93. // Creation OK
  94. setEventMessages($langs->trans('ResourceCreatedWithSuccess'), null, 'mesgs');
  95. Header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  96. exit;
  97. } else {
  98. // Creation KO
  99. setEventMessages($object->error, $object->errors, 'errors');
  100. $action = 'create';
  101. }
  102. }
  103. } else {
  104. Header("Location: list.php");
  105. exit;
  106. }
  107. }
  108. if ($action == 'update' && !$cancel && $user->rights->resource->write) {
  109. $error = 0;
  110. if (empty($ref)) {
  111. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
  112. $error++;
  113. }
  114. if (!$error) {
  115. $res = $object->fetch($id);
  116. if ($res > 0) {
  117. $object->ref = $ref;
  118. $object->description = $description;
  119. $object->fk_code_type_resource = $fk_code_type_resource;
  120. $object->country_id = $country_id;
  121. // Fill array 'array_options' with data from add form
  122. $ret = $extrafields->setOptionalsFromPost(null, $object, '@GETPOSTISSET');
  123. if ($ret < 0) {
  124. $error++;
  125. }
  126. $result = $object->update($user);
  127. if ($result > 0) {
  128. Header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  129. exit;
  130. } else {
  131. setEventMessages($object->error, $object->errors, 'errors');
  132. $error++;
  133. }
  134. } else {
  135. setEventMessages($object->error, $object->errors, 'errors');
  136. $error++;
  137. }
  138. }
  139. if ($error) {
  140. $action = 'edit';
  141. }
  142. }
  143. if ($action == 'confirm_delete_resource' && $user->rights->resource->delete && $confirm === 'yes') {
  144. $res = $object->fetch($id);
  145. if ($res > 0) {
  146. $result = $object->delete($id);
  147. if ($result >= 0) {
  148. setEventMessages($langs->trans('RessourceSuccessfullyDeleted'), null, 'mesgs');
  149. Header('Location: '.DOL_URL_ROOT.'/resource/list.php');
  150. exit;
  151. } else {
  152. setEventMessages($object->error, $object->errors, 'errors');
  153. }
  154. } else {
  155. setEventMessages($object->error, $object->errors, 'errors');
  156. }
  157. }
  158. }
  159. /*
  160. * View
  161. */
  162. $title = $langs->trans($action == 'create' ? 'AddResource' : 'ResourceSingular');
  163. llxHeader('', $title, '');
  164. $form = new Form($db);
  165. $formresource = new FormResource($db);
  166. if ($action == 'create' || $object->fetch($id, $ref) > 0) {
  167. if ($action == 'create') {
  168. print load_fiche_titre($title, '', 'object_resource');
  169. print dol_get_fiche_head('');
  170. } else {
  171. $head = resource_prepare_head($object);
  172. print dol_get_fiche_head($head, 'resource', $title, -1, 'resource');
  173. }
  174. if ($action == 'create' || $action == 'edit') {
  175. if (!$user->rights->resource->write) {
  176. accessforbidden('', 0, 1);
  177. }
  178. // Create/Edit object
  179. print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">';
  180. print '<input type="hidden" name="token" value="'.newToken().'">';
  181. print '<input type="hidden" name="action" value="'.($action == "create" ? "add" : "update").'">';
  182. print '<table class="border centpercent">';
  183. // Ref
  184. print '<tr><td class="titlefieldcreate fieldrequired">'.$langs->trans("ResourceFormLabel_ref").'</td>';
  185. print '<td><input class="minwidth200" name="ref" value="'.($ref ? $ref : $object->ref).'" autofocus="autofocus"></td></tr>';
  186. // Type
  187. print '<tr><td>'.$langs->trans("ResourceType").'</td>';
  188. print '<td>';
  189. $formresource->select_types_resource($object->fk_code_type_resource, 'fk_code_type_resource', '', 2);
  190. print '</td></tr>';
  191. // Description
  192. print '<tr><td class="tdtop">'.$langs->trans("Description").'</td>';
  193. print '<td>';
  194. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  195. $doleditor = new DolEditor('description', ($description ? $description : $object->description), '', '200', 'dolibarr_notes', false);
  196. $doleditor->Create();
  197. print '</td></tr>';
  198. // Origin country
  199. print '<tr><td>'.$langs->trans("CountryOrigin").'</td><td>';
  200. print $form->select_country($object->country_id, 'country_id');
  201. if ($user->admin) {
  202. print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  203. }
  204. print '</td></tr>';
  205. // Other attributes
  206. $parameters = array();
  207. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  208. print $hookmanager->resPrint;
  209. if (empty($reshook)) {
  210. print $object->showOptionals($extrafields, 'edit');
  211. }
  212. print '</table>';
  213. print dol_get_fiche_end();
  214. $button_label = ($action == "create" ? "Create" : "Modify");
  215. print $form->buttonsSaveCancel($button_label);
  216. print '</div>';
  217. print '</form>';
  218. } else {
  219. $formconfirm = '';
  220. // Confirm deleting resource line
  221. if ($action == 'delete') {
  222. $formconfirm = $form->formconfirm("card.php?&id=".$object->id, $langs->trans("DeleteResource"), $langs->trans("ConfirmDeleteResource"), "confirm_delete_resource", '', '', 1);
  223. }
  224. // Print form confirm
  225. print $formconfirm;
  226. $linkback = '<a href="'.DOL_URL_ROOT.'/resource/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&id='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  227. $morehtmlref = '<div class="refidno">';
  228. $morehtmlref .= '</div>';
  229. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  230. print '<div class="fichecenter">';
  231. print '<div class="underbanner clearboth"></div>';
  232. /*---------------------------------------
  233. * View object
  234. */
  235. print '<table class="border tableforfield centpercent">';
  236. // Resource type
  237. print '<tr>';
  238. print '<td class="titlefield">'.$langs->trans("ResourceType").'</td>';
  239. print '<td>';
  240. print $object->type_label;
  241. print '</td>';
  242. print '</tr>';
  243. // Description
  244. print '<tr>';
  245. print '<td>'.$langs->trans("ResourceFormLabel_description").'</td>';
  246. print '<td>';
  247. print $object->description;
  248. print '</td>';
  249. print '</tr>';
  250. // Origin country code
  251. print '<tr>';
  252. print '<td>'.$langs->trans("CountryOrigin").'</td>';
  253. print '<td>';
  254. print getCountry($object->country_id, 0, $db);
  255. print '</td>';
  256. // Other attributes
  257. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  258. print '</tr>';
  259. print '</table>';
  260. print '</div>';
  261. print '<div class="clearboth"></div><br>';
  262. print dol_get_fiche_end();
  263. }
  264. /*
  265. * Boutons actions
  266. */
  267. print '<div class="tabsAction">';
  268. $parameters = array();
  269. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
  270. // modified by hook
  271. if (empty($reshook)) {
  272. if ($action != "create" && $action != "edit") {
  273. // Edit resource
  274. if ($user->rights->resource->write) {
  275. print '<div class="inline-block divButAction">';
  276. print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&action=edit&token='.newToken().'" class="butAction">'.$langs->trans('Modify').'</a>';
  277. print '</div>';
  278. }
  279. }
  280. if ($action != "delete" && $action != "create" && $action != "edit") {
  281. print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', $permissiontodelete);
  282. }
  283. }
  284. print '</div>';
  285. } else {
  286. dol_print_error();
  287. }
  288. // End of page
  289. llxFooter();
  290. $db->close();