card.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
  3. * Copyright (C) 2006-2021 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. // Load Dolibarr environment
  29. require '../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  33. // Load translation files required by the page
  34. $langs->load("categories");
  35. // Security check
  36. $socid = (int) GETPOST('socid', 'int');
  37. if (!$user->hasRight('categorie', 'lire')) {
  38. accessforbidden();
  39. }
  40. $action = GETPOST('action', 'alpha');
  41. $cancel = GETPOST('cancel', 'alpha');
  42. $origin = GETPOST('origin', 'alpha');
  43. $catorigin = (int) GETPOST('catorigin', 'int');
  44. $type = GETPOST('type', 'aZ09');
  45. $urlfrom = GETPOST('urlfrom', 'alpha');
  46. $backtopage = GETPOST('backtopage', 'alpha');
  47. $label = (string) GETPOST('label', 'alphanohtml');
  48. $description = (string) GETPOST('description', 'restricthtml');
  49. $color = preg_replace('/[^0-9a-f#]/i', '', (string) GETPOST('color', 'alphanohtml'));
  50. $visible = (int) GETPOST('visible', 'int');
  51. $parent = (int) GETPOST('parent', 'int');
  52. if ($origin) {
  53. if ($type == Categorie::TYPE_PRODUCT) {
  54. $idProdOrigin = $origin;
  55. }
  56. if ($type == Categorie::TYPE_SUPPLIER) {
  57. $idSupplierOrigin = $origin;
  58. }
  59. if ($type == Categorie::TYPE_CUSTOMER) {
  60. $idCompanyOrigin = $origin;
  61. }
  62. if ($type == Categorie::TYPE_MEMBER) {
  63. $idMemberOrigin = $origin;
  64. }
  65. if ($type == Categorie::TYPE_CONTACT) {
  66. $idContactOrigin = $origin;
  67. }
  68. if ($type == Categorie::TYPE_PROJECT) {
  69. $idProjectOrigin = $origin;
  70. }
  71. }
  72. if ($catorigin && $type == Categorie::TYPE_PRODUCT) {
  73. $idCatOrigin = $catorigin;
  74. }
  75. $object = new Categorie($db);
  76. $extrafields = new ExtraFields($db);
  77. $extrafields->fetch_name_optionals_label($object->table_element);
  78. // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array array
  79. $hookmanager->initHooks(array('categorycard'));
  80. $error = 0;
  81. /*
  82. * Actions
  83. */
  84. // Add action
  85. if ($action == 'add' && $user->rights->categorie->creer) {
  86. // Action add a category
  87. if ($cancel) {
  88. if ($urlfrom) {
  89. header("Location: ".$urlfrom);
  90. exit;
  91. } elseif ($backtopage) {
  92. header("Location: ".$backtopage);
  93. exit;
  94. } elseif ($idProdOrigin) {
  95. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type);
  96. exit;
  97. } elseif ($idCompanyOrigin) {
  98. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type);
  99. exit;
  100. } elseif ($idSupplierOrigin) {
  101. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type);
  102. exit;
  103. } elseif ($idMemberOrigin) {
  104. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type);
  105. exit;
  106. } elseif ($idContactOrigin) {
  107. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type);
  108. exit;
  109. } elseif ($idProjectOrigin) {
  110. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type);
  111. exit;
  112. } else {
  113. header("Location: ".DOL_URL_ROOT.'/categories/index.php?leftmenu=cat&type='.$type);
  114. exit;
  115. }
  116. }
  117. $object->label = $label;
  118. $object->color = $color;
  119. $object->description = dol_htmlcleanlastbr($description);
  120. $object->socid = ($socid > 0 ? $socid : 0);
  121. $object->visible = $visible;
  122. $object->type = $type;
  123. if ($parent != "-1") {
  124. $object->fk_parent = $parent;
  125. }
  126. $ret = $extrafields->setOptionalsFromPost(null, $object);
  127. if ($ret < 0) {
  128. $error++;
  129. }
  130. if (!$object->label) {
  131. $error++;
  132. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
  133. $action = 'create';
  134. }
  135. // Create category in database
  136. if (!$error) {
  137. $result = $object->create($user);
  138. if ($result > 0) {
  139. $action = 'confirmed';
  140. $_POST["addcat"] = '';
  141. } else {
  142. setEventMessages($object->error, $object->errors, 'errors');
  143. }
  144. }
  145. }
  146. // Confirm action
  147. if (($action == 'add' || $action == 'confirmed') && $user->rights->categorie->creer) {
  148. // Action confirmation of creation category
  149. if ($action == 'confirmed') {
  150. if ($urlfrom) {
  151. header("Location: ".$urlfrom);
  152. exit;
  153. } elseif ($backtopage) {
  154. header("Location: ".$backtopage);
  155. exit;
  156. } elseif ($idProdOrigin) {
  157. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  158. exit;
  159. } elseif ($idCompanyOrigin) {
  160. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  161. exit;
  162. } elseif ($idSupplierOrigin) {
  163. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  164. exit;
  165. } elseif ($idMemberOrigin) {
  166. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  167. exit;
  168. } elseif ($idContactOrigin) {
  169. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  170. exit;
  171. } elseif ($idProjectOrigin) {
  172. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  173. exit;
  174. }
  175. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$result.'&type='.$type);
  176. exit;
  177. }
  178. }
  179. /*
  180. * View
  181. */
  182. $form = new Form($db);
  183. $formother = new FormOther($db);
  184. $help_url = 'EN:Module_Categories|FR:Module_Catégories|DE:Modul_Kategorien';
  185. llxHeader("", $langs->trans("Categories"), $help_url);
  186. if ($user->rights->categorie->creer) {
  187. // Create or add
  188. if ($action == 'create' || GETPOST("addcat") == 'addcat') {
  189. dol_set_focus('#label');
  190. print '<form action="'.$_SERVER['PHP_SELF'].'?type='.$type.'" method="POST">';
  191. print '<input type="hidden" name="token" value="'.newToken().'">';
  192. print '<input type="hidden" name="urlfrom" value="'.$urlfrom.'">';
  193. print '<input type="hidden" name="action" value="add">';
  194. print '<input type="hidden" name="addcat" value="addcat">';
  195. print '<input type="hidden" name="id" value="'.GETPOST('origin', 'alpha').'">';
  196. print '<input type="hidden" name="type" value="'.$type.'">';
  197. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  198. if ($origin) {
  199. print '<input type="hidden" name="origin" value="'.$origin.'">';
  200. }
  201. if ($catorigin) {
  202. print '<input type="hidden" name="catorigin" value="'.$catorigin.'">';
  203. }
  204. print load_fiche_titre($langs->trans("CreateCat"));
  205. print dol_get_fiche_head('');
  206. print '<table width="100%" class="border">';
  207. // Ref
  208. print '<tr>';
  209. print '<td class="titlefieldcreate fieldrequired">'.$langs->trans("Ref").'</td><td><input id="label" class="minwidth100" name="label" value="'.dol_escape_htmltag($label).'">';
  210. print'</td></tr>';
  211. // Description
  212. print '<tr><td class="tdtop">'.$langs->trans("Description").'</td><td>';
  213. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  214. $doleditor = new DolEditor('description', $description, '', 160, 'dolibarr_notes', '', false, true, getDolGlobalInt('FCKEDITOR_ENABLE_SOCIETE'), ROWS_5, '90%');
  215. $doleditor->Create();
  216. print '</td></tr>';
  217. // Color
  218. print '<tr><td>'.$langs->trans("Color").'</td><td>';
  219. print $formother->selectColor($color, 'color');
  220. print '</td></tr>';
  221. // Parent category
  222. print '<tr><td>'.$langs->trans("AddIn").'</td><td>';
  223. print img_picto($langs->trans("ParentCategory"), 'category', 'class="pictofixedwidth"');
  224. print $form->select_all_categories($type, $catorigin, 'parent');
  225. print ajax_combobox('parent');
  226. print '</td></tr>';
  227. $parameters = array();
  228. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  229. print $hookmanager->resPrint;
  230. if (empty($reshook)) {
  231. print $object->showOptionals($extrafields, 'create', $parameters);
  232. }
  233. print '</table>';
  234. print dol_get_fiche_end('');
  235. print '<div class="center">';
  236. print '<input type="submit" class="button b" value="'.$langs->trans("CreateThisCat").'" name="creation" />';
  237. print '&nbsp; &nbsp; &nbsp;';
  238. print '<input type="submit" class="button button-cancel" value="'.$langs->trans("Cancel").'" name="cancel" />';
  239. print '</div>';
  240. print '</form>';
  241. }
  242. }
  243. // End of page
  244. llxFooter();
  245. $db->close();