card.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/asset/model/card.php
  20. * \ingroup asset
  21. * \brief Page to create/edit/view asset Model
  22. */
  23. // Load Dolibarr environment
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT . '/core/lib/asset.lib.php';
  26. require_once DOL_DOCUMENT_ROOT . '/asset/class/assetmodel.class.php';
  27. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formcompany.class.php';
  28. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array("assets", "other"));
  31. // Get parameters
  32. $id = GETPOST('id', 'int');
  33. $ref = GETPOST('ref', 'alpha');
  34. $action = GETPOST('action', 'aZ09');
  35. $confirm = GETPOST('confirm', 'alpha');
  36. $cancel = GETPOST('cancel', 'aZ09');
  37. $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'assetmodelcard'; // To manage different context of search
  38. $backtopage = GETPOST('backtopage', 'alpha');
  39. $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');
  40. // Initialize technical objects
  41. $object = new AssetModel($db);
  42. $extrafields = new ExtraFields($db);
  43. $diroutputmassaction = $conf->asset->dir_output . '/temp/massgeneration/' . $user->id;
  44. $hookmanager->initHooks(array('assetmodelcard', 'globalcard')); // Note that conf->hooks_modules contains array
  45. // Fetch optionals attributes and labels
  46. $extrafields->fetch_name_optionals_label($object->table_element);
  47. $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
  48. // Initialize array of search criterias
  49. $search_all = GETPOST("search_all", 'alpha');
  50. $search = array();
  51. foreach ($object->fields as $key => $val) {
  52. if (GETPOST('search_' . $key, 'alpha')) {
  53. $search[$key] = GETPOST('search_' . $key, 'alpha');
  54. }
  55. }
  56. if (empty($action) && empty($id) && empty($ref)) {
  57. $action = 'view';
  58. }
  59. // Load object
  60. include DOL_DOCUMENT_ROOT . '/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  61. $permissiontoread = ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'read')) || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'model_advance', 'read')));
  62. $permissiontoadd = ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'write')) || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'model_advance', 'write'))); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
  63. $permissiontodelete = ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'delete')) || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'model_advance', 'delete'))) || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
  64. $permissionnote = $permissiontoadd; // Used by the include of actions_setnotes.inc.php
  65. $permissiondellink = $permissiontoadd; // Used by the include of actions_dellink.inc.php
  66. $upload_dir = $conf->asset->multidir_output[isset($object->entity) ? $object->entity : 1];
  67. // Security check (enable the most restrictive one)
  68. if ($user->socid > 0) {
  69. accessforbidden();
  70. }
  71. if ($user->socid > 0) {
  72. $socid = $user->socid;
  73. }
  74. $isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
  75. restrictedArea($user, 'asset', $object->id, $object->table_element, '', 'fk_soc', 'rowid', $isdraft);
  76. if (!isModEnabled('asset')) {
  77. accessforbidden();
  78. }
  79. if (!$permissiontoread) {
  80. accessforbidden();
  81. }
  82. /*
  83. * Actions
  84. */
  85. $parameters = array();
  86. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  87. if ($reshook < 0) {
  88. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  89. }
  90. if (empty($reshook)) {
  91. $error = 0;
  92. $backurlforlist = DOL_URL_ROOT . '/asset/model/list.php';
  93. if (empty($backtopage) || ($cancel && empty($id))) {
  94. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  95. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  96. $backtopage = $backurlforlist;
  97. } else {
  98. $backtopage = DOL_URL_ROOT . '/asset/model/card.php?id=' . ((!empty($id) && $id > 0) ? $id : '__ID__');
  99. }
  100. }
  101. }
  102. $triggermodname = 'ASSETMODEL_MODIFY'; // Name of trigger action code to execute when we modify record
  103. if (($action == 'edit' && !($permissiontoadd && $object->status == $object::STATUS_DRAFT)) ||
  104. ($action == 'confirm_setdraft' && !($permissiontoadd && $object->status != $object::STATUS_DRAFT)) ||
  105. ($action == 'confirm_validate' && !($permissiontoadd && $object->status != $object::STATUS_VALIDATED)) ||
  106. ($action == 'confirm_close' && !($permissiontoadd && $object->status != $object::STATUS_CANCELED))
  107. ) {
  108. $action = "";
  109. }
  110. // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
  111. include DOL_DOCUMENT_ROOT . '/core/actions_addupdatedelete.inc.php';
  112. }
  113. /*
  114. * View
  115. *
  116. * Put here all code to build page
  117. */
  118. $form = new Form($db);
  119. $formfile = new FormFile($db);
  120. $title = $langs->trans("AssetModel") . ' - ' . $langs->trans("Card");
  121. $help_url = '';
  122. llxHeader('', $title, $help_url);
  123. // Part to create
  124. if ($action == 'create') {
  125. print load_fiche_titre($langs->trans("NewObject", $langs->transnoentitiesnoconv("AssetModel")), '', 'object_' . $object->picto);
  126. print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
  127. print '<input type="hidden" name="token" value="' . newToken() . '">';
  128. print '<input type="hidden" name="action" value="add">';
  129. if ($backtopage) {
  130. print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
  131. }
  132. if ($backtopageforcancel) {
  133. print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
  134. }
  135. print dol_get_fiche_head(array(), '');
  136. // Set some default values
  137. //if (! GETPOSTISSET('fieldname')) $_POST['fieldname'] = 'myvalue';
  138. print '<table class="border centpercent tableforfieldcreate">' . "\n";
  139. // Common attributes
  140. include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_add.tpl.php';
  141. // Other attributes
  142. include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_add.tpl.php';
  143. print '</table>' . "\n";
  144. print dol_get_fiche_end();
  145. print $form->buttonsSaveCancel("Create");
  146. print '</form>';
  147. //dol_set_focus('input[name="ref"]');
  148. }
  149. // Part to edit record
  150. if (($id || $ref) && $action == 'edit') {
  151. print load_fiche_titre($langs->trans("AssetModel"), '', 'object_' . $object->picto);
  152. print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
  153. print '<input type="hidden" name="token" value="' . newToken() . '">';
  154. print '<input type="hidden" name="action" value="update">';
  155. print '<input type="hidden" name="id" value="' . $object->id . '">';
  156. if ($backtopage) {
  157. print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
  158. }
  159. if ($backtopageforcancel) {
  160. print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
  161. }
  162. print dol_get_fiche_head();
  163. print '<table class="border centpercent tableforfieldedit">' . "\n";
  164. // Common attributes
  165. include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_edit.tpl.php';
  166. // Other attributes
  167. include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_edit.tpl.php';
  168. print '</table>';
  169. print dol_get_fiche_end();
  170. print $form->buttonsSaveCancel();
  171. print '</form>';
  172. }
  173. // Part to show record
  174. if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
  175. $res = $object->fetch_optionals();
  176. $head = assetModelPrepareHead($object);
  177. print dol_get_fiche_head($head, 'card', $langs->trans("AssetModel"), -1, $object->picto);
  178. $formconfirm = '';
  179. // Confirmation to delete
  180. if ($action == 'delete') {
  181. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteAssetModel'), $langs->trans('ConfirmDeleteObject'), 'confirm_delete', '', 0, 1);
  182. } elseif ($action == 'clone') {
  183. // Clone confirmation
  184. // Create an array for form
  185. $formquestion = array();
  186. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneAsk', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  187. }
  188. // Call Hook formConfirm
  189. $parameters = array('formConfirm' => $formconfirm);
  190. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  191. if (empty($reshook)) {
  192. $formconfirm .= $hookmanager->resPrint;
  193. } elseif ($reshook > 0) {
  194. $formconfirm = $hookmanager->resPrint;
  195. }
  196. // Print form confirm
  197. print $formconfirm;
  198. // Object card
  199. // ------------------------------------------------------------
  200. $linkback = '<a href="' . DOL_URL_ROOT . '/asset/model/list.php?restore_lastsearch_values=1' . (!empty($socid) ? '&socid=' . $socid : '') . '">' . $langs->trans("BackToList") . '</a>';
  201. $morehtmlref = '<div class="refidno">';
  202. $morehtmlref .= '</div>';
  203. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  204. print '<div class="fichecenter">';
  205. print '<div class="fichehalfleft">';
  206. print '<div class="underbanner clearboth"></div>';
  207. print '<table class="border centpercent tableforfield">' . "\n";
  208. // Common attributes
  209. include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_view.tpl.php';
  210. // Other attributes. Fields from hook formObjectOptions and Extrafields.
  211. include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
  212. print '</table>';
  213. print '</div>';
  214. print '</div>';
  215. print '<div class="clearboth"></div>';
  216. print dol_get_fiche_end();
  217. // Buttons for actions
  218. if ($action != 'editline') {
  219. print '<div class="tabsAction">' . "\n";
  220. $parameters = array();
  221. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  222. if ($reshook < 0) {
  223. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  224. }
  225. if (empty($reshook)) {
  226. if ($object->status == $object::STATUS_DRAFT) {
  227. print dolGetButtonAction($langs->trans('Modify'), '', 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=edit&token=' . newToken(), '', $permissiontoadd);
  228. }
  229. // Back to draft
  230. if ($object->status != $object::STATUS_DRAFT) {
  231. print dolGetButtonAction($langs->trans('SetToDraft'), '', 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=confirm_setdraft&confirm=yes&token=' . newToken(), '', $permissiontoadd);
  232. }
  233. if ($object->status != $object::STATUS_VALIDATED) {
  234. print dolGetButtonAction($langs->trans('Enable'), '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=confirm_validate&confirm=yes&token=' . newToken(), '', $permissiontoadd);
  235. }
  236. if ($object->status != $object::STATUS_CANCELED) {
  237. print dolGetButtonAction($langs->trans('Disable'), '', 'default', $_SERVER['PHP_SELF'].'?id='.$object->id.'&action=confirm_close&confirm=yes&token='.newToken(), '', $permissiontoadd);
  238. }
  239. // Clone
  240. print dolGetButtonAction($langs->trans('ToClone'), '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . (!empty($socid) ? '&socid=' . $socid : '') . '&action=clone&token=' . newToken(), '', $permissiontoadd);
  241. // Delete (need delete permission, or if draft, just need create/modify permission)
  242. print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=delete&token=' . newToken(), '', $permissiontodelete || ($object->status == $object::STATUS_DRAFT && $permissiontoadd));
  243. }
  244. print '</div>' . "\n";
  245. }
  246. print '<div class="fichecenter"><div class="fichehalfleft">';
  247. print '<a name="builddoc"></a>'; // ancre
  248. print '</div><div class="fichehalfright">';
  249. // $MAXEVENT = 10;
  250. //
  251. // $morehtmlcenter = dolGetButtonTitle($langs->trans('SeeAll'), '', 'fa fa-bars imgforviewmode', DOL_URL_ROOT . '/asset/model/agenda.php?id=' . $object->id);
  252. //
  253. // // List of actions on element
  254. // include_once DOL_DOCUMENT_ROOT . '/core/class/html.formactions.class.php';
  255. // $formactions = new FormActions($db);
  256. // $somethingshown = $formactions->showactions($object, $object->element.'@'.$object->module, 0, 1, '', $MAXEVENT, '', $morehtmlright);
  257. print '</div></div>';
  258. }
  259. // End of page
  260. llxFooter();
  261. $db->close();