dolibarr 21.0.0-alpha
card.php
Go to the documentation of this file.
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
31// Load Dolibarr environment
32require '../main.inc.php';
33require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
35require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
36
37// Load translation files required by the page
38$langs->load("categories");
39
40// Security check
41$socid = GETPOSTINT('socid');
42if (!$user->hasRight('categorie', 'lire')) {
44}
45
46$action = GETPOST('action', 'alpha');
47$cancel = GETPOST('cancel', 'alpha');
48$origin = GETPOST('origin', 'alpha');
49$catorigin = GETPOSTINT('catorigin');
50$type = GETPOST('type', 'aZ09');
51$urlfrom = GETPOST('urlfrom', 'alpha');
52$backtopage = GETPOST('backtopage', 'alpha');
53
54$label = (string) GETPOST('label', 'alphanohtml');
55$description = (string) GETPOST('description', 'restricthtml');
56$color = preg_replace('/[^0-9a-f#]/i', '', (string) GETPOST('color', 'alphanohtml'));
57$position = GETPOSTINT('position');
58$visible = GETPOSTINT('visible');
59$parent = GETPOSTINT('parent');
60
61$idProdOrigin = 0;
62$idSupplierOrigin = 0;
63$idCompanyOrigin = 0;
64$idMemberOrigin = 0;
65$idContactOrigin = 0;
66$idProjectOrigin = 0;
67$idProdOrigin = 0;
68
69if ($origin) {
70 if ($type == Categorie::TYPE_PRODUCT) {
71 $idProdOrigin = $origin;
72 }
73 if ($type == Categorie::TYPE_SUPPLIER) {
74 $idSupplierOrigin = $origin;
75 }
76 if ($type == Categorie::TYPE_CUSTOMER) {
77 $idCompanyOrigin = $origin;
78 }
79 if ($type == Categorie::TYPE_MEMBER) {
80 $idMemberOrigin = $origin;
81 }
82 if ($type == Categorie::TYPE_CONTACT) {
83 $idContactOrigin = $origin;
84 }
85 if ($type == Categorie::TYPE_PROJECT) {
86 $idProjectOrigin = $origin;
87 }
88}
89
90if ($catorigin && $type == Categorie::TYPE_PRODUCT) {
91 $idCatOrigin = $catorigin;
92}
93if (!GETPOSTISSET('parent') && $catorigin) {
94 $parent = $catorigin;
95}
96
97$object = new Categorie($db);
98
99$extrafields = new ExtraFields($db);
100$extrafields->fetch_name_optionals_label($object->table_element);
101
102// Initialize a technical object to manage hooks. Note that conf->hooks_modules contains array array
103$hookmanager->initHooks(array('categorycard'));
104
105$error = 0;
106
107
108/*
109 * Actions
110 */
111
112$parameters = array('socid' => $socid, 'origin' => $origin, 'catorigin' => $catorigin, 'type' => $type, 'urlfrom' => $urlfrom, 'backtopage' => $backtopage, 'label' => $label, 'description' => $description, 'color' => $color, 'position' => $position, 'visible' => $visible, 'parent' => $parent);
113// Note that $action and $object may be modified by some hooks
114$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
115if ($reshook < 0) {
116 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
117}
118
119if (empty($reshook)) {
120 // Add action
121 if ($action == 'add' && $user->hasRight('categorie', 'creer')) {
122 // Action add a category
123 if ($cancel) {
124 if ($urlfrom) {
125 header("Location: ".$urlfrom);
126 exit;
127 } elseif ($backtopage) {
128 header("Location: ".$backtopage);
129 exit;
130 } elseif ($idProdOrigin) {
131 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type);
132 exit;
133 } elseif ($idCompanyOrigin) {
134 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type);
135 exit;
136 } elseif ($idSupplierOrigin) {
137 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type);
138 exit;
139 } elseif ($idMemberOrigin) {
140 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type);
141 exit;
142 } elseif ($idContactOrigin) {
143 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type);
144 exit;
145 } elseif ($idProjectOrigin) {
146 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type);
147 exit;
148 } else {
149 header("Location: ".DOL_URL_ROOT.'/categories/index.php?leftmenu=cat&type='.$type);
150 exit;
151 }
152 }
153 $object->label = $label;
154 $object->color = $color;
155 $object->position = $position;
156 $object->description = dol_htmlcleanlastbr($description);
157 $object->socid = ($socid > 0 ? $socid : 0);
158 $object->visible = $visible;
159 $object->type = $type;
160
161 if ($parent != "-1") {
162 $object->fk_parent = $parent;
163 }
164
165 $ret = $extrafields->setOptionalsFromPost(null, $object);
166 if ($ret < 0) {
167 $error++;
168 }
169
170 if (!$object->label) {
171 $error++;
172 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
173 $action = 'create';
174 }
175
176 // Create category in database
177 if (!$error) {
178 $result = $object->create($user);
179 if ($result > 0) {
180 $action = 'confirmed';
181 } else {
182 setEventMessages($object->error, $object->errors, 'errors');
183 }
184 }
185 }
186 // Action confirmation of creation category
187 if ($action == 'confirmed' && $user->hasRight('categorie', 'creer')) {
188 if ($urlfrom) {
189 header("Location: ".$urlfrom);
190 exit;
191 } elseif ($backtopage) {
192 header("Location: ".$backtopage);
193 exit;
194 } elseif ($idProdOrigin) {
195 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
196 exit;
197 } elseif ($idCompanyOrigin) {
198 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
199 exit;
200 } elseif ($idSupplierOrigin) {
201 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
202 exit;
203 } elseif ($idMemberOrigin) {
204 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
205 exit;
206 } elseif ($idContactOrigin) {
207 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
208 exit;
209 } elseif ($idProjectOrigin) {
210 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
211 exit;
212 }
213
214 header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$result.'&type='.$type);
215 exit;
216 }
217}
218
219
220/*
221 * View
222 */
223
224$form = new Form($db);
225$formother = new FormOther($db);
226
227$help_url = 'EN:Module_Categories|FR:Module_Catégories|DE:Modul_Kategorien';
228
229llxHeader("", $langs->trans("Categories"), $help_url);
230
231if ($user->hasRight('categorie', 'creer')) {
232 // Create or add
233 if ($action == 'create' || $action == 'add') {
234 dol_set_focus('#label');
235
236 print '<form action="'.$_SERVER['PHP_SELF'].'?type='.$type.'" method="POST">';
237 print '<input type="hidden" name="token" value="'.newToken().'">';
238 print '<input type="hidden" name="urlfrom" value="'.$urlfrom.'">';
239 print '<input type="hidden" name="action" value="add">';
240 print '<input type="hidden" name="id" value="'.GETPOST('origin', 'alpha').'">';
241 print '<input type="hidden" name="type" value="'.$type.'">';
242 print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
243 if ($origin) {
244 print '<input type="hidden" name="origin" value="'.$origin.'">';
245 }
246 if ($catorigin) {
247 print '<input type="hidden" name="catorigin" value="'.$catorigin.'">';
248 }
249
250 print load_fiche_titre($langs->trans("CreateCat"));
251
252 print dol_get_fiche_head();
253
254 print '<table class="border centpercent">';
255
256 // Ref
257 print '<tr>';
258 print '<td class="titlefieldcreate fieldrequired">'.$langs->trans("Ref").'</td><td><input id="label" class="minwidth100" name="label" value="'.dol_escape_htmltag($label).'">';
259 print'</td></tr>';
260
261 // Description
262 print '<tr><td class="tdtop">'.$langs->trans("Description").'</td><td>';
263 require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
264 $doleditor = new DolEditor('description', $description, '', 160, 'dolibarr_notes', '', false, true, isModEnabled('fckeditor') && getDolGlobalInt('FCKEDITOR_ENABLE_SOCIETE'), ROWS_5, '90%');
265 $doleditor->Create();
266 print '</td></tr>';
267
268 // Color
269 print '<tr><td>'.$langs->trans("Color").'</td><td>';
270 print $formother->selectColor($color, 'color');
271 print '</td></tr>';
272
273 // Position
274 print '<tr>';
275 print '<td class="titlefieldcreate">'.$langs->trans("Position").'</td><td><input id="position" type="number" class="minwidth50 maxwidth50" name="position" value="'.$position.'">';
276 print'</td></tr>';
277
278 // Parent category
279 print '<tr><td>'.$langs->trans("AddIn").'</td><td>';
280 print img_picto($langs->trans("ParentCategory"), 'category', 'class="pictofixedwidth"');
281 print $form->select_all_categories($type, $parent, 'parent');
282 print ajax_combobox('parent');
283 print '</td></tr>';
284
285 $parameters = array();
286 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
287 print $hookmanager->resPrint;
288 if (empty($reshook)) {
289 print $object->showOptionals($extrafields, 'create', $parameters);
290 }
291
292 print '</table>';
293
294 print dol_get_fiche_end();
295
296 print '<div class="center">';
297 print '<input type="submit" class="button b" value="'.$langs->trans("CreateThisCat").'" name="creation" />';
298 print '&nbsp; &nbsp; &nbsp;';
299 print '<input type="submit" class="button button-cancel" value="'.$langs->trans("Cancel").'" name="cancel" />';
300 print '</div>';
301
302 print '</form>';
303 }
304}
305
306// End of page
307llxFooter();
308$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:457
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:70
Class to manage categories.
Class to manage a WYSIWYG editor.
Class to manage standard extra fields.
Class to manage generation of HTML components Only common components must be here.
Class permettant la generation de composants html autre Only common components are here.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_set_focus($selector)
Set focus onto field with selector (similar behaviour of 'autofocus' HTML5 tag)
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.