card.php 11 KB

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