card.php 13 KB

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