card.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. *
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. require '../main.inc.php';
  19. require 'class/ProductAttribute.class.php';
  20. require 'class/ProductAttributeValue.class.php';
  21. $id = GETPOST('id','int');
  22. $valueid = GETPOST('valueid','alpha');
  23. $action = GETPOST('action','alpha');
  24. $label = GETPOST('label','alpha');
  25. $ref = GETPOST('ref','alpha');
  26. $confirm = GETPOST('confirm','alpha');
  27. $cancel = GETPOST('cancel','alpha');
  28. $object = new ProductAttribute($db);
  29. $objectval = new ProductAttributeValue($db);
  30. if ($object->fetch($id) < 1) {
  31. dol_print_error($db, $langs->trans('ErrorRecordNotFound'));
  32. exit();
  33. }
  34. /*
  35. * Actions
  36. */
  37. if ($cancel) $action='';
  38. if ($_POST) {
  39. if ($action == 'edit') {
  40. $object->ref = $ref;
  41. $object->label = $label;
  42. if ($object->update($user) < 1) {
  43. setEventMessages($langs->trans('CoreErrorMessage'), $object->errors, 'errors');
  44. } else {
  45. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  46. header('Location: '.dol_buildpath('/variants/card.php?id='.$id, 2));
  47. exit();
  48. }
  49. } elseif ($action == 'update') {
  50. if ($objectval->fetch($valueid) > 0) {
  51. $objectval->ref = $ref;
  52. $objectval->value = GETPOST('value','alpha');
  53. if (empty($objectval->ref))
  54. {
  55. $error++;
  56. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Ref")), null, 'errors');
  57. }
  58. if (empty($objectval->value))
  59. {
  60. $error++;
  61. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Label")), null, 'errors');
  62. }
  63. if (! $error)
  64. {
  65. if ($objectval->update($user) > 0) {
  66. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  67. } else {
  68. setEventMessage($langs->trans('CoreErrorMessage'), $objectval->errors, 'errors');
  69. }
  70. }
  71. }
  72. header('Location: '.dol_buildpath('/variants/card.php?id='.$object->id, 2));
  73. exit();
  74. }
  75. }
  76. if ($confirm == 'yes') {
  77. if ($action == 'confirm_delete') {
  78. $db->begin();
  79. $res = $objectval->deleteByFkAttribute($object->id);
  80. if ($res < 1 || ($object->delete() < 1)) {
  81. $db->rollback();
  82. setEventMessages($langs->trans('CoreErrorMessage'), $object->errors, 'errors');
  83. header('Location: '.dol_buildpath('/variants/card.php?id='.$object->id, 2));
  84. } else {
  85. $db->commit();
  86. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  87. header('Location: '.dol_buildpath('/variants/list.php', 2));
  88. }
  89. exit();
  90. }
  91. elseif ($action == 'confirm_deletevalue')
  92. {
  93. if ($objectval->fetch($valueid) > 0) {
  94. if ($objectval->delete() < 1) {
  95. setEventMessages($langs->trans('CoreErrorMessage'), $objectval->errors, 'errors');
  96. } else {
  97. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  98. }
  99. header('Location: '.dol_buildpath('/variants/card.php?id='.$object->id, 2));
  100. exit();
  101. }
  102. }
  103. }
  104. /*
  105. * View
  106. */
  107. $langs->load('products');
  108. $title = $langs->trans('ProductAttributeName', dol_htmlentities($object->label));
  109. llxHeader('', $title);
  110. //print load_fiche_titre($title);
  111. $h=0;
  112. $head[$h][0] = DOL_URL_ROOT.'/variants/card.php?id='.$object->id;
  113. $head[$h][1] = $langs->trans("Card");
  114. $head[$h][2] = 'variant';
  115. $h++;
  116. dol_fiche_head($head, 'variant', $langs->trans('ProductAttributeName'), -1, 'generic');
  117. if ($action == 'edit') {
  118. print '<form method="POST">';
  119. }
  120. if ($action != 'edit')
  121. {
  122. print '<div class="fichecenter">';
  123. print '<div class="underbanner clearboth"></div>';
  124. }
  125. ?>
  126. <table class="border" style="width: 100%">
  127. <tr>
  128. <td class="titlefield fieldrequired"><?php echo $langs->trans('Ref') ?></td>
  129. <td>
  130. <?php if ($action == 'edit') {
  131. print '<input type="text" name="ref" value="'.$object->ref.'">';
  132. } else {
  133. print dol_htmlentities($object->ref);
  134. } ?>
  135. </td>
  136. </tr>
  137. <tr>
  138. <td class="fieldrequired"><?php echo $langs->trans('Label') ?></td>
  139. <td>
  140. <?php if ($action == 'edit') {
  141. print '<input type="text" name="label" value="'.$object->label.'">';
  142. } else {
  143. print dol_htmlentities($object->label);
  144. } ?>
  145. </td>
  146. </tr>
  147. </table>
  148. <?php
  149. if ($action != 'edit')
  150. {
  151. print '</div>';
  152. }
  153. dol_fiche_end();
  154. if ($action == 'edit') { ?>
  155. <div style="text-align: center;">
  156. <div class="inline-block divButAction">
  157. <input type="submit" class="button" value="<?php echo $langs->trans('Save') ?>">
  158. &nbsp; &nbsp;
  159. <input type="submit" class="button" name="cancel" value="<?php echo $langs->trans('Cancel') ?>">
  160. </div>
  161. </div></form>
  162. <?php } else {
  163. if ($action == 'delete') {
  164. $form = new Form($db);
  165. print $form->formconfirm(
  166. "card.php?id=".$object->id,
  167. $langs->trans('Delete'),
  168. $langs->trans('ProductAttributeDeleteDialog'),
  169. "confirm_delete",
  170. '',
  171. 0,
  172. 1
  173. );
  174. } elseif ($action == 'delete_value') {
  175. if ($objectval->fetch($valueid) > 0) {
  176. $form = new Form($db);
  177. print $form->formconfirm(
  178. "card.php?id=".$object->id."&valueid=".$objectval->id,
  179. $langs->trans('Delete'),
  180. $langs->trans('ProductAttributeValueDeleteDialog', dol_htmlentities($objectval->value), dol_htmlentities($objectval->ref)),
  181. "confirm_deletevalue",
  182. '',
  183. 0,
  184. 1
  185. );
  186. }
  187. }
  188. ?>
  189. <div class="tabsAction">
  190. <div class="inline-block divButAction">
  191. <a href="card.php?id=<?php echo $object->id ?>&action=edit" class="butAction"><?php echo $langs->trans('Modify') ?></a>
  192. <a href="card.php?id=<?php echo $object->id ?>&action=delete" class="butAction"><?php echo $langs->trans('Delete') ?></a>
  193. </div>
  194. </div>
  195. <?php
  196. print load_fiche_titre($langs->trans("PossibleValues"));
  197. if ($action == 'edit_value') {
  198. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  199. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  200. print '<input type="hidden" name="action" value="update">';
  201. print '<input type="hidden" name="id" value="'.$id.'">';
  202. print '<input type="hidden" name="valueid" value="'.$valueid.'">';
  203. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  204. } ?>
  205. <table class="liste">
  206. <tr class="liste_titre">
  207. <th class="liste_titre titlefield"><?php echo $langs->trans('Ref') ?></th>
  208. <th class="liste_titre"><?php echo $langs->trans('Value') ?></th>
  209. <th class="liste_titre"></th>
  210. </tr>
  211. <?php
  212. foreach ($objectval->fetchAllByProductAttribute($object->id) as $attrval) {
  213. ?>
  214. <tr class="oddeven">
  215. <?php if ($action == 'edit_value' && ($valueid == $attrval->id)): ?>
  216. <td><input type="text" name="ref" value="<?php echo $attrval->ref ?>"></td>
  217. <td><input type="text" name="value" value="<?php echo $attrval->value ?>"></td>
  218. <td style="text-align: right">
  219. <input type="submit" value="<?php echo $langs->trans('Save') ?>" class="button">
  220. &nbsp; &nbsp;
  221. <input type="submit" name="cancel" value="<?php echo $langs->trans('Cancel') ?>" class="button">
  222. </td>
  223. <?php else: ?>
  224. <td><?php echo dol_htmlentities($attrval->ref) ?></td>
  225. <td><?php echo dol_htmlentities($attrval->value) ?></td>
  226. <td style="text-align: right">
  227. <a href="card.php?id=<?php echo $object->id ?>&action=edit_value&valueid=<?php echo $attrval->id ?>"><?php echo img_edit() ?></a>
  228. <a href="card.php?id=<?php echo $object->id ?>&action=delete_value&valueid=<?php echo $attrval->id ?>"><?php echo img_delete() ?></a>
  229. </td>
  230. <?php endif; ?>
  231. </tr>
  232. <?php
  233. }
  234. ?>
  235. </table>
  236. <?php if ($action == 'edit_value'): ?>
  237. </form>
  238. <?php endif ?>
  239. <div class="tabsAction">
  240. <div class="inline-block divButAction">
  241. <a href="create_val.php?id=<?php echo $object->id ?>" class="butAction"><?php echo $langs->trans('Create') ?></a>
  242. </div>
  243. </div>
  244. <?php
  245. }
  246. // End of page
  247. llxFooter();
  248. $db->close();