card.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
  3. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  4. * Copyright (C) 2022 Open-Dsi <support@open-dsi.fr>
  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 3 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 <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/variants/card.php
  21. * \ingroup variants
  22. * \brief Page to show product attribute
  23. */
  24. require '../main.inc.php';
  25. require 'class/ProductAttribute.class.php';
  26. require 'class/ProductAttributeValue.class.php';
  27. require 'lib/variants.lib.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array('products'));
  30. $id = GETPOST('id', 'int');
  31. $ref = GETPOST('ref', 'alpha');
  32. $action = GETPOST('action', 'aZ09');
  33. $confirm = GETPOST('confirm', 'alpha');
  34. $cancel = GETPOST('cancel', 'alpha');
  35. $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'productattribute'; // To manage different context of search
  36. $backtopage = GETPOST('backtopage', 'alpha');
  37. $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');
  38. $lineid = GETPOST('lineid', 'alpha');
  39. // Security check
  40. if (empty($conf->variants->enabled)) {
  41. accessforbidden('Module not enabled');
  42. }
  43. if ($user->socid > 0) { // Protection if external user
  44. accessforbidden();
  45. }
  46. $result = restrictedArea($user, 'variants');
  47. $object = new ProductAttribute($db);
  48. // Load object
  49. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once
  50. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  51. $hookmanager->initHooks(array('productattributecard', 'globalcard'));
  52. $permissiontoread = $user->rights->variants->read;
  53. $permissiontoadd = $user->rights->variants->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
  54. $permissiontoedit = $user->rights->variants->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
  55. $permissiontodelete = $user->rights->variants->delete;
  56. $error = 0;
  57. /*
  58. * Actions
  59. */
  60. $parameters = array();
  61. // Note that $action and $object may be modified by some hooks
  62. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
  63. if ($reshook < 0) {
  64. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  65. }
  66. if (empty($reshook)) {
  67. $error = 0;
  68. $backurlforlist = dol_buildpath('/variants/list.php', 1);
  69. if (empty($backtopage) || ($cancel && empty($id))) {
  70. if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
  71. if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
  72. $backtopage = $backurlforlist;
  73. } else {
  74. $backtopage = dol_buildpath('/variants/card.php', 1).'?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
  75. }
  76. }
  77. }
  78. // Action to move up and down lines of object
  79. include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
  80. if ($cancel) {
  81. if (!empty($backtopage)) {
  82. header("Location: " . $backtopage);
  83. exit;
  84. }
  85. $action = '';
  86. }
  87. // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
  88. include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
  89. // Action to move up and down lines of object
  90. if ($action == 'up' && $permissiontoedit) {
  91. $object->line_up(GETPOST('rowid'), false);
  92. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'#'.GETPOST('rowid'));
  93. exit();
  94. } elseif ($action == 'down' && $permissiontoedit) {
  95. $object->line_down(GETPOST('rowid'), false);
  96. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.'#'.GETPOST('rowid'));
  97. exit();
  98. }
  99. if ($action == 'addline' && $permissiontoedit) {
  100. $line_ref = GETPOST('line_ref', 'alpha');
  101. $line_value = GETPOST('line_value', 'alpha');
  102. $result = $object->addLine($line_ref, $line_value);
  103. if ($result > 0) {
  104. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  105. header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
  106. exit();
  107. } else {
  108. setEventMessages($object->error, $object->errors, 'errors');
  109. $action = '';
  110. }
  111. } elseif ($action == 'updateline' && $permissiontoedit) {
  112. $line_ref = GETPOST('line_ref', 'alpha');
  113. $line_value = GETPOST('line_value', 'alpha');
  114. $result = $object->updateLine($lineid, $line_ref, $line_value);
  115. if ($result > 0) {
  116. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  117. header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
  118. exit();
  119. } else {
  120. setEventMessages($object->error, $object->errors, 'errors');
  121. $action = 'editline';
  122. }
  123. }
  124. }
  125. /*
  126. * View
  127. */
  128. $title = $langs->trans('ProductAttributeName', dol_htmlentities($object->label));
  129. $help_url = 'EN:Module_Products#Variants';
  130. llxHeader('', $title, $help_url);
  131. // Part to create
  132. if ($action == 'create') {
  133. print load_fiche_titre($langs->trans("NewObject", $langs->transnoentitiesnoconv("ProductAttribute")), '', 'object_' . $object->picto);
  134. print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
  135. print '<input type="hidden" name="token" value="' . newToken() . '">';
  136. print '<input type="hidden" name="action" value="add">';
  137. if ($backtopage) {
  138. print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
  139. }
  140. if ($backtopageforcancel) {
  141. print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
  142. }
  143. print dol_get_fiche_head(array(), '');
  144. print '<table class="border centpercent tableforfieldcreate">' . "\n";
  145. // Common attributes
  146. include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_add.tpl.php';
  147. print '</table>' . "\n";
  148. print dol_get_fiche_end();
  149. print '<div class="center">';
  150. print '<input type="submit" class="button" name="add" value="' . dol_escape_htmltag($langs->trans("Create")) . '">';
  151. print '&nbsp; ';
  152. print '<input type="' . ($backtopage ? "submit" : "button") . '" class="button button-cancel" name="cancel" value="' . dol_escape_htmltag($langs->trans("Cancel")) . '"' . ($backtopage ? '' : ' onclick="javascript:history.go(-1)"') . '>'; // Cancel for create does not post form if we don't know the backtopage
  153. print '</div>';
  154. print '</form>';
  155. dol_set_focus('input[name="label"]');
  156. } elseif (($id || $ref) && $action == 'edit') {
  157. // Part to edit record
  158. print load_fiche_titre($langs->trans("ProductAttribute"), '', 'object_' . $object->picto);
  159. print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
  160. print '<input type="hidden" name="token" value="' . newToken() . '">';
  161. print '<input type="hidden" name="action" value="update">';
  162. print '<input type="hidden" name="id" value="' . $object->id . '">';
  163. if ($backtopage) {
  164. print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
  165. }
  166. if ($backtopageforcancel) {
  167. print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
  168. }
  169. print dol_get_fiche_head();
  170. print '<table class="border centpercent tableforfieldedit">' . "\n";
  171. // Common attributes
  172. include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_edit.tpl.php';
  173. print '</table>';
  174. print dol_get_fiche_end();
  175. print '<div class="center"><input type="submit" class="button button-save" name="save" value="' . $langs->trans("Save") . '">';
  176. print ' &nbsp; <input type="submit" class="button button-cancel" name="cancel" value="' . $langs->trans("Cancel") . '">';
  177. print '</div>';
  178. print '</form>';
  179. } elseif ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
  180. // Part to show record
  181. $res = $object->fetch_optionals();
  182. $head = productAttributePrepareHead($object);
  183. print dol_get_fiche_head($head, 'card', $langs->trans("ProductAttribute"), -1, $object->picto);
  184. $formconfirm = '';
  185. // Confirmation to delete
  186. if ($action == 'delete') {
  187. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteMyObject'), $langs->trans('ProductAttributeDeleteDialog'), 'confirm_delete', '', 0, 1);
  188. } elseif ($action == 'ask_deleteline') {
  189. // Confirmation to delete line
  190. $object_value = new ProductAttributeValue($db);
  191. if ($object_value->fetch($lineid) > 0) {
  192. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id . '&lineid=' . $lineid, $langs->trans('DeleteLine'), $langs->trans('ProductAttributeValueDeleteDialog', dol_htmlentities($object_value->value), dol_htmlentities($object_value->ref)), 'confirm_deleteline', '', 0, 1);
  193. }
  194. }
  195. // Call Hook formConfirm
  196. $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
  197. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  198. if (empty($reshook)) {
  199. $formconfirm .= $hookmanager->resPrint;
  200. } elseif ($reshook > 0) {
  201. $formconfirm = $hookmanager->resPrint;
  202. }
  203. // Print form confirm
  204. print $formconfirm;
  205. // Object card
  206. // ------------------------------------------------------------
  207. $backtolist = (GETPOST('backtolist') ? GETPOST('backtolist') : DOL_URL_ROOT . '/variants/list.php?leftmenu=?restore_lastsearch_values=1');
  208. $linkback = '<a href="' . dol_sanitizeUrl($backtolist) . '">' . $langs->trans("BackToList") . '</a>';
  209. dol_banner_tab($object, 'id', $linkback);
  210. print '<div class="fichecenter">';
  211. print '<div class="fichehalfleft">';
  212. print '<div class="underbanner clearboth"></div>';
  213. print '<table class="border centpercent tableforfield">' . "\n";
  214. // Common attributes
  215. include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_view.tpl.php';
  216. print '</table>';
  217. print '</div>';
  218. print '</div>';
  219. print '<div class="clearboth"></div>';
  220. print dol_get_fiche_end();
  221. // Buttons for actions
  222. if ($action != 'editline') {
  223. print '<div class="tabsAction">' . "\n";
  224. $parameters = array();
  225. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  226. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  227. if (empty($reshook)) {
  228. // Modify
  229. print dolGetButtonAction($langs->trans('Modify'), '', 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=edit', '', $permissiontoedit);
  230. // Delete (need delete permission, or if draft, just need create/modify permission)
  231. print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=delete', '', $permissiontodelete);
  232. }
  233. print '</div>' . "\n";
  234. }
  235. /*
  236. * Lines
  237. */
  238. if (!empty($object->table_element_line)) {
  239. // Show object lines
  240. $result = $object->getLinesArray();
  241. print load_fiche_titre($langs->trans("PossibleValues") . (!empty($object->lines) ? ' (' . count($object->lines) . ')' : ''));
  242. print ' <form name="addproduct" id="addproduct" action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . (($action != 'editline') ? '' : '#line_' . GETPOST('lineid', 'int')) . '" method="POST">
  243. <input type="hidden" name="token" value="' . newToken() . '">
  244. <input type="hidden" name="action" value="' . (($action != 'editline') ? 'addline' : 'updateline') . '">
  245. <input type="hidden" name="mode" value="">
  246. <input type="hidden" name="page_y" value="">
  247. <input type="hidden" name="id" value="' . $object->id . '">
  248. ';
  249. if ($backtopage) {
  250. print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
  251. }
  252. if ($backtopageforcancel) {
  253. print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
  254. }
  255. if (!empty($conf->use_javascript_ajax)) {
  256. include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php';
  257. }
  258. print '<div class="div-table-responsive-no-min">';
  259. if (!empty($object->lines) || ($permissiontoedit && $action != 'selectlines' && $action != 'editline')) {
  260. print '<table id="tablelines" class="noborder noshadow" width="100%">';
  261. }
  262. // Form to add new line
  263. if ($permissiontoedit && $action != 'selectlines') {
  264. if ($action != 'editline') {
  265. // Add products/services form
  266. $parameters = array();
  267. $reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  268. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  269. if (empty($reshook))
  270. $object->formAddObjectLine(1, $mysoc, $soc);
  271. }
  272. }
  273. if (!empty($object->lines)) {
  274. $object->printObjectLines($action, $mysoc, null, GETPOST('lineid', 'int'), 1);
  275. }
  276. if (!empty($object->lines) || ($permissiontoedit && $action != 'selectlines' && $action != 'editline')) {
  277. print '</table>';
  278. }
  279. print '</div>';
  280. print "</form>\n";
  281. }
  282. }
  283. // End of page
  284. llxFooter();
  285. $db->close();