card.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
  3. * Copyright (C) 2006-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2014 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
  6. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  7. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  8. * Copyright (C) 2020 Frédéric France <frederic.france@netlogic.fr>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/categories/card.php
  25. * \ingroup category
  26. * \brief Page to create a new category
  27. */
  28. require '../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  32. // Load translation files required by the page
  33. $langs->load("categories");
  34. // Security check
  35. $socid = (int) GETPOST('socid', 'int');
  36. if (!$user->rights->categorie->lire) {
  37. accessforbidden();
  38. }
  39. $action = GETPOST('action', 'alpha');
  40. $cancel = GETPOST('cancel', 'alpha');
  41. $origin = GETPOST('origin', 'alpha');
  42. $catorigin = (int) GETPOST('catorigin', 'int');
  43. $type = GETPOST('type', 'aZ09');
  44. $urlfrom = GETPOST('urlfrom', 'alpha');
  45. $backtopage = GETPOST('backtopage', 'alpha');
  46. $label = (string) GETPOST('label', 'alphanohtml');
  47. $description = (string) GETPOST('description', 'restricthtml');
  48. $color = preg_replace('/[^0-9a-f#]/i', '', (string) GETPOST('color', 'alphanohtml'));
  49. $visible = (int) GETPOST('visible', 'int');
  50. $parent = (int) GETPOST('parent', 'int');
  51. if ($origin) {
  52. if ($type == Categorie::TYPE_PRODUCT) {
  53. $idProdOrigin = $origin;
  54. }
  55. if ($type == Categorie::TYPE_SUPPLIER) {
  56. $idSupplierOrigin = $origin;
  57. }
  58. if ($type == Categorie::TYPE_CUSTOMER) {
  59. $idCompanyOrigin = $origin;
  60. }
  61. if ($type == Categorie::TYPE_MEMBER) {
  62. $idMemberOrigin = $origin;
  63. }
  64. if ($type == Categorie::TYPE_CONTACT) {
  65. $idContactOrigin = $origin;
  66. }
  67. if ($type == Categorie::TYPE_PROJECT) {
  68. $idProjectOrigin = $origin;
  69. }
  70. }
  71. if ($catorigin && $type == Categorie::TYPE_PRODUCT) {
  72. $idCatOrigin = $catorigin;
  73. }
  74. $object = new Categorie($db);
  75. $extrafields = new ExtraFields($db);
  76. $extrafields->fetch_name_optionals_label($object->table_element);
  77. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array array
  78. $hookmanager->initHooks(array('categorycard'));
  79. $error = 0;
  80. /*
  81. * Actions
  82. */
  83. // Add action
  84. if ($action == 'add' && $user->rights->categorie->creer) {
  85. // Action ajout d'une categorie
  86. if ($cancel) {
  87. if ($urlfrom) {
  88. header("Location: ".$urlfrom);
  89. exit;
  90. } elseif ($backtopage) {
  91. header("Location: ".$backtopage);
  92. exit;
  93. } elseif ($idProdOrigin) {
  94. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type);
  95. exit;
  96. } elseif ($idCompanyOrigin) {
  97. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type);
  98. exit;
  99. } elseif ($idSupplierOrigin) {
  100. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type);
  101. exit;
  102. } elseif ($idMemberOrigin) {
  103. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type);
  104. exit;
  105. } elseif ($idContactOrigin) {
  106. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type);
  107. exit;
  108. } elseif ($idProjectOrigin) {
  109. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type);
  110. exit;
  111. } else {
  112. header("Location: ".DOL_URL_ROOT.'/categories/index.php?leftmenu=cat&type='.$type);
  113. exit;
  114. }
  115. }
  116. $object->label = $label;
  117. $object->color = $color;
  118. $object->description = dol_htmlcleanlastbr($description);
  119. $object->socid = ($socid > 0 ? $socid : 0);
  120. $object->visible = $visible;
  121. $object->type = $type;
  122. if ($parent != "-1") {
  123. $object->fk_parent = $parent;
  124. }
  125. $ret = $extrafields->setOptionalsFromPost(null, $object);
  126. if ($ret < 0) {
  127. $error++;
  128. }
  129. if (!$object->label) {
  130. $error++;
  131. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
  132. $action = 'create';
  133. }
  134. // Create category in database
  135. if (!$error) {
  136. $result = $object->create($user);
  137. if ($result > 0) {
  138. $action = 'confirmed';
  139. $_POST["addcat"] = '';
  140. } else {
  141. setEventMessages($object->error, $object->errors, 'errors');
  142. }
  143. }
  144. }
  145. // Confirm action
  146. if (($action == 'add' || $action == 'confirmed') && $user->rights->categorie->creer) {
  147. // Action confirmation de creation categorie
  148. if ($action == 'confirmed') {
  149. if ($urlfrom) {
  150. header("Location: ".$urlfrom);
  151. exit;
  152. } elseif ($backtopage) {
  153. header("Location: ".$backtopage);
  154. exit;
  155. } elseif ($idProdOrigin) {
  156. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  157. exit;
  158. } elseif ($idCompanyOrigin) {
  159. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  160. exit;
  161. } elseif ($idSupplierOrigin) {
  162. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  163. exit;
  164. } elseif ($idMemberOrigin) {
  165. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  166. exit;
  167. } elseif ($idContactOrigin) {
  168. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  169. exit;
  170. } elseif ($idProjectOrigin) {
  171. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  172. exit;
  173. }
  174. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$result.'&type='.$type);
  175. exit;
  176. }
  177. }
  178. /*
  179. * View
  180. */
  181. $form = new Form($db);
  182. $formother = new FormOther($db);
  183. $helpurl = '';
  184. llxHeader("", $langs->trans("Categories"), $helpurl);
  185. if ($user->rights->categorie->creer) {
  186. // Create or add
  187. if ($action == 'create' || GETPOST("addcat") == 'addcat') {
  188. dol_set_focus('#label');
  189. print '<form action="'.$_SERVER['PHP_SELF'].'?type='.$type.'" method="POST">';
  190. print '<input type="hidden" name="token" value="'.newToken().'">';
  191. print '<input type="hidden" name="urlfrom" value="'.$urlfrom.'">';
  192. print '<input type="hidden" name="action" value="add">';
  193. print '<input type="hidden" name="addcat" value="addcat">';
  194. print '<input type="hidden" name="id" value="'.GETPOST('origin', 'alpha').'">';
  195. print '<input type="hidden" name="type" value="'.$type.'">';
  196. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  197. if ($origin) {
  198. print '<input type="hidden" name="origin" value="'.$origin.'">';
  199. }
  200. if ($catorigin) {
  201. print '<input type="hidden" name="catorigin" value="'.$catorigin.'">';
  202. }
  203. print load_fiche_titre($langs->trans("CreateCat"));
  204. print dol_get_fiche_head('');
  205. print '<table width="100%" class="border">';
  206. // Ref
  207. print '<tr>';
  208. print '<td class="titlefieldcreate fieldrequired">'.$langs->trans("Ref").'</td><td><input id="label" class="minwidth100" name="label" value="'.dol_escape_htmltag($label).'">';
  209. print'</td></tr>';
  210. // Description
  211. print '<tr><td class="tdtop">'.$langs->trans("Description").'</td><td>';
  212. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  213. $doleditor = new DolEditor('description', $description, '', 160, 'dolibarr_notes', '', false, true, $conf->global->FCKEDITOR_ENABLE_PRODUCTDESC, ROWS_5, '90%');
  214. $doleditor->Create();
  215. print '</td></tr>';
  216. // Color
  217. print '<tr><td>'.$langs->trans("Color").'</td><td>';
  218. print $formother->selectColor($color, 'color');
  219. print '</td></tr>';
  220. // Parent category
  221. print '<tr><td>'.$langs->trans("AddIn").'</td><td>';
  222. print $form->select_all_categories($type, $catorigin, 'parent');
  223. print ajax_combobox('parent');
  224. print '</td></tr>';
  225. $parameters = array();
  226. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  227. print $hookmanager->resPrint;
  228. if (empty($reshook)) {
  229. print $object->showOptionals($extrafields, 'edit', $parameters);
  230. }
  231. print '</table>';
  232. print dol_get_fiche_end('');
  233. print '<div class="center">';
  234. print '<input type="submit" class="button" value="'.$langs->trans("CreateThisCat").'" name="creation" />';
  235. print '&nbsp; &nbsp; &nbsp;';
  236. print '<input type="submit" class="button button-cancel" value="'.$langs->trans("Cancel").'" name="cancel" />';
  237. print '</div>';
  238. print '</form>';
  239. }
  240. }
  241. // End of page
  242. llxFooter();
  243. $db->close();