card.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. $parameters = array('socid' => $socid, 'origin' => $origin, 'catorigin' => $catorigin, 'type' => $type, 'urlfrom' => $urlfrom, 'backtopage' => $backtopage, 'label' => $label, 'description' => $description, 'color' => $color, 'visible' => $visible, 'parent' => $parent);
  85. // Note that $action and $object may be modified by some hooks
  86. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
  87. if ($reshook < 0) {
  88. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  89. }
  90. if (empty($reshook)) {
  91. // Add action
  92. if ($action == 'add' && $user->hasRight('categorie', 'creer')) {
  93. // Action add a category
  94. if ($cancel) {
  95. if ($urlfrom) {
  96. header("Location: ".$urlfrom);
  97. exit;
  98. } elseif ($backtopage) {
  99. header("Location: ".$backtopage);
  100. exit;
  101. } elseif ($idProdOrigin) {
  102. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type);
  103. exit;
  104. } elseif ($idCompanyOrigin) {
  105. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type);
  106. exit;
  107. } elseif ($idSupplierOrigin) {
  108. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type);
  109. exit;
  110. } elseif ($idMemberOrigin) {
  111. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type);
  112. exit;
  113. } elseif ($idContactOrigin) {
  114. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type);
  115. exit;
  116. } elseif ($idProjectOrigin) {
  117. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type);
  118. exit;
  119. } else {
  120. header("Location: ".DOL_URL_ROOT.'/categories/index.php?leftmenu=cat&type='.$type);
  121. exit;
  122. }
  123. }
  124. $object->label = $label;
  125. $object->color = $color;
  126. $object->description = dol_htmlcleanlastbr($description);
  127. $object->socid = ($socid > 0 ? $socid : 0);
  128. $object->visible = $visible;
  129. $object->type = $type;
  130. if ($parent != "-1") {
  131. $object->fk_parent = $parent;
  132. }
  133. $ret = $extrafields->setOptionalsFromPost(null, $object);
  134. if ($ret < 0) {
  135. $error++;
  136. }
  137. if (!$object->label) {
  138. $error++;
  139. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
  140. $action = 'create';
  141. }
  142. // Create category in database
  143. if (!$error) {
  144. $result = $object->create($user);
  145. if ($result > 0) {
  146. $action = 'confirmed';
  147. $_POST["addcat"] = '';
  148. } else {
  149. setEventMessages($object->error, $object->errors, 'errors');
  150. }
  151. }
  152. }
  153. // Confirm action
  154. if (($action == 'add' || $action == 'confirmed') && $user->hasRight('categorie', 'creer')) {
  155. // Action confirmation of creation category
  156. if ($action == 'confirmed') {
  157. if ($urlfrom) {
  158. header("Location: ".$urlfrom);
  159. exit;
  160. } elseif ($backtopage) {
  161. header("Location: ".$backtopage);
  162. exit;
  163. } elseif ($idProdOrigin) {
  164. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  165. exit;
  166. } elseif ($idCompanyOrigin) {
  167. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  168. exit;
  169. } elseif ($idSupplierOrigin) {
  170. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  171. exit;
  172. } elseif ($idMemberOrigin) {
  173. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  174. exit;
  175. } elseif ($idContactOrigin) {
  176. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  177. exit;
  178. } elseif ($idProjectOrigin) {
  179. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
  180. exit;
  181. }
  182. header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$result.'&type='.$type);
  183. exit;
  184. }
  185. }
  186. }
  187. /*
  188. * View
  189. */
  190. $form = new Form($db);
  191. $formother = new FormOther($db);
  192. $help_url = 'EN:Module_Categories|FR:Module_Catégories|DE:Modul_Kategorien';
  193. llxHeader("", $langs->trans("Categories"), $help_url);
  194. if ($user->hasRight('categorie', 'creer')) {
  195. // Create or add
  196. if ($action == 'create' || GETPOST("addcat") == 'addcat') {
  197. dol_set_focus('#label');
  198. print '<form action="'.$_SERVER['PHP_SELF'].'?type='.$type.'" method="POST">';
  199. print '<input type="hidden" name="token" value="'.newToken().'">';
  200. print '<input type="hidden" name="urlfrom" value="'.$urlfrom.'">';
  201. print '<input type="hidden" name="action" value="add">';
  202. print '<input type="hidden" name="addcat" value="addcat">';
  203. print '<input type="hidden" name="id" value="'.GETPOST('origin', 'alpha').'">';
  204. print '<input type="hidden" name="type" value="'.$type.'">';
  205. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  206. if ($origin) {
  207. print '<input type="hidden" name="origin" value="'.$origin.'">';
  208. }
  209. if ($catorigin) {
  210. print '<input type="hidden" name="catorigin" value="'.$catorigin.'">';
  211. }
  212. print load_fiche_titre($langs->trans("CreateCat"));
  213. print dol_get_fiche_head('');
  214. print '<table width="100%" class="border">';
  215. // Ref
  216. print '<tr>';
  217. print '<td class="titlefieldcreate fieldrequired">'.$langs->trans("Ref").'</td><td><input id="label" class="minwidth100" name="label" value="'.dol_escape_htmltag($label).'">';
  218. print'</td></tr>';
  219. // Description
  220. print '<tr><td class="tdtop">'.$langs->trans("Description").'</td><td>';
  221. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  222. $doleditor = new DolEditor('description', $description, '', 160, 'dolibarr_notes', '', false, true, getDolGlobalInt('FCKEDITOR_ENABLE_SOCIETE'), ROWS_5, '90%');
  223. $doleditor->Create();
  224. print '</td></tr>';
  225. // Color
  226. print '<tr><td>'.$langs->trans("Color").'</td><td>';
  227. print $formother->selectColor($color, 'color');
  228. print '</td></tr>';
  229. // Parent category
  230. print '<tr><td>'.$langs->trans("AddIn").'</td><td>';
  231. print img_picto($langs->trans("ParentCategory"), 'category', 'class="pictofixedwidth"');
  232. print $form->select_all_categories($type, $catorigin, 'parent');
  233. print ajax_combobox('parent');
  234. print '</td></tr>';
  235. $parameters = array();
  236. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  237. print $hookmanager->resPrint;
  238. if (empty($reshook)) {
  239. print $object->showOptionals($extrafields, 'create', $parameters);
  240. }
  241. print '</table>';
  242. print dol_get_fiche_end('');
  243. print '<div class="center">';
  244. print '<input type="submit" class="button b" value="'.$langs->trans("CreateThisCat").'" name="creation" />';
  245. print '&nbsp; &nbsp; &nbsp;';
  246. print '<input type="submit" class="button button-cancel" value="'.$langs->trans("Cancel").'" name="cancel" />';
  247. print '</div>';
  248. print '</form>';
  249. }
  250. }
  251. // End of page
  252. llxFooter();
  253. $db->close();