card.php 9.3 KB

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