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