card.php 9.0 KB

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