dolibarr  16.0.5
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  *
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 
30 require '../main.inc.php';
31 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
33 require_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');
40 if (!$user->rights->categorie->lire) {
42 }
43 
44 $action = GETPOST('action', 'alpha');
45 $cancel = GETPOST('cancel', 'alpha');
46 $origin = GETPOST('origin', 'alpha');
47 $catorigin = (int) GETPOST('catorigin', 'int');
48 $type = GETPOST('type', 'aZ09');
49 $urlfrom = GETPOST('urlfrom', 'alpha');
50 $backtopage = GETPOST('backtopage', 'alpha');
51 
52 $label = (string) GETPOST('label', 'alphanohtml');
53 $description = (string) GETPOST('description', 'restricthtml');
54 $color = preg_replace('/[^0-9a-f#]/i', '', (string) GETPOST('color', 'alphanohtml'));
55 $visible = (int) GETPOST('visible', 'int');
56 $parent = (int) GETPOST('parent', 'int');
57 
58 if ($origin) {
59  if ($type == Categorie::TYPE_PRODUCT) {
60  $idProdOrigin = $origin;
61  }
62  if ($type == Categorie::TYPE_SUPPLIER) {
63  $idSupplierOrigin = $origin;
64  }
65  if ($type == Categorie::TYPE_CUSTOMER) {
66  $idCompanyOrigin = $origin;
67  }
68  if ($type == Categorie::TYPE_MEMBER) {
69  $idMemberOrigin = $origin;
70  }
71  if ($type == Categorie::TYPE_CONTACT) {
72  $idContactOrigin = $origin;
73  }
74  if ($type == Categorie::TYPE_PROJECT) {
75  $idProjectOrigin = $origin;
76  }
77 }
78 
79 if ($catorigin && $type == Categorie::TYPE_PRODUCT) {
80  $idCatOrigin = $catorigin;
81 }
82 
83 $object = new Categorie($db);
84 
85 $extrafields = new ExtraFields($db);
86 $extrafields->fetch_name_optionals_label($object->table_element);
87 
88 // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array array
89 $hookmanager->initHooks(array('categorycard'));
90 
91 $error = 0;
92 
93 
94 /*
95  * Actions
96  */
97 
98 // Add action
99 if ($action == 'add' && $user->rights->categorie->creer) {
100  // Action add a category
101  if ($cancel) {
102  if ($urlfrom) {
103  header("Location: ".$urlfrom);
104  exit;
105  } elseif ($backtopage) {
106  header("Location: ".$backtopage);
107  exit;
108  } elseif ($idProdOrigin) {
109  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type);
110  exit;
111  } elseif ($idCompanyOrigin) {
112  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type);
113  exit;
114  } elseif ($idSupplierOrigin) {
115  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type);
116  exit;
117  } elseif ($idMemberOrigin) {
118  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type);
119  exit;
120  } elseif ($idContactOrigin) {
121  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type);
122  exit;
123  } elseif ($idProjectOrigin) {
124  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type);
125  exit;
126  } else {
127  header("Location: ".DOL_URL_ROOT.'/categories/index.php?leftmenu=cat&type='.$type);
128  exit;
129  }
130  }
131 
132 
133 
134  $object->label = $label;
135  $object->color = $color;
136  $object->description = dol_htmlcleanlastbr($description);
137  $object->socid = ($socid > 0 ? $socid : 0);
138  $object->visible = $visible;
139  $object->type = $type;
140 
141  if ($parent != "-1") {
142  $object->fk_parent = $parent;
143  }
144 
145  $ret = $extrafields->setOptionalsFromPost(null, $object);
146  if ($ret < 0) {
147  $error++;
148  }
149 
150  if (!$object->label) {
151  $error++;
152  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
153  $action = 'create';
154  }
155 
156  // Create category in database
157  if (!$error) {
158  $result = $object->create($user);
159  if ($result > 0) {
160  $action = 'confirmed';
161  $_POST["addcat"] = '';
162  } else {
163  setEventMessages($object->error, $object->errors, 'errors');
164  }
165  }
166 }
167 
168 // Confirm action
169 if (($action == 'add' || $action == 'confirmed') && $user->rights->categorie->creer) {
170  // Action confirmation of creation category
171  if ($action == 'confirmed') {
172  if ($urlfrom) {
173  header("Location: ".$urlfrom);
174  exit;
175  } elseif ($backtopage) {
176  header("Location: ".$backtopage);
177  exit;
178  } elseif ($idProdOrigin) {
179  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
180  exit;
181  } elseif ($idCompanyOrigin) {
182  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
183  exit;
184  } elseif ($idSupplierOrigin) {
185  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
186  exit;
187  } elseif ($idMemberOrigin) {
188  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
189  exit;
190  } elseif ($idContactOrigin) {
191  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
192  exit;
193  } elseif ($idProjectOrigin) {
194  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated")));
195  exit;
196  }
197 
198  header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$result.'&type='.$type);
199  exit;
200  }
201 }
202 
203 
204 /*
205  * View
206  */
207 
208 $form = new Form($db);
209 $formother = new FormOther($db);
210 
211 $help_url = 'EN:Module_Categories|FR:Module_Catégories|DE:Modul_Kategorien';
212 
213 llxHeader("", $langs->trans("Categories"), $help_url);
214 
215 if ($user->rights->categorie->creer) {
216  // Create or add
217  if ($action == 'create' || GETPOST("addcat") == 'addcat') {
218  dol_set_focus('#label');
219 
220  print '<form action="'.$_SERVER['PHP_SELF'].'?type='.$type.'" method="POST">';
221  print '<input type="hidden" name="token" value="'.newToken().'">';
222  print '<input type="hidden" name="urlfrom" value="'.$urlfrom.'">';
223  print '<input type="hidden" name="action" value="add">';
224  print '<input type="hidden" name="addcat" value="addcat">';
225  print '<input type="hidden" name="id" value="'.GETPOST('origin', 'alpha').'">';
226  print '<input type="hidden" name="type" value="'.$type.'">';
227  print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
228  if ($origin) {
229  print '<input type="hidden" name="origin" value="'.$origin.'">';
230  }
231  if ($catorigin) {
232  print '<input type="hidden" name="catorigin" value="'.$catorigin.'">';
233  }
234 
235  print load_fiche_titre($langs->trans("CreateCat"));
236 
237  print dol_get_fiche_head('');
238 
239  print '<table width="100%" class="border">';
240 
241  // Ref
242  print '<tr>';
243  print '<td class="titlefieldcreate fieldrequired">'.$langs->trans("Ref").'</td><td><input id="label" class="minwidth100" name="label" value="'.dol_escape_htmltag($label).'">';
244  print'</td></tr>';
245 
246  // Description
247  print '<tr><td class="tdtop">'.$langs->trans("Description").'</td><td>';
248  require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
249  $doleditor = new DolEditor('description', $description, '', 160, 'dolibarr_notes', '', false, true, getDolGlobalInt('FCKEDITOR_ENABLE_PRODUCTDESC'), ROWS_5, '90%');
250  $doleditor->Create();
251  print '</td></tr>';
252 
253  // Color
254  print '<tr><td>'.$langs->trans("Color").'</td><td>';
255  print $formother->selectColor($color, 'color');
256  print '</td></tr>';
257 
258  // Parent category
259  print '<tr><td>'.$langs->trans("AddIn").'</td><td>';
260  print img_picto($langs->trans("ParentCategory"), 'category', 'class="pictofixedwidth"');
261  print $form->select_all_categories($type, $catorigin, 'parent');
262  print ajax_combobox('parent');
263  print '</td></tr>';
264 
265  $parameters = array();
266  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
267  print $hookmanager->resPrint;
268  if (empty($reshook)) {
269  print $object->showOptionals($extrafields, 'create', $parameters);
270  }
271 
272  print '</table>';
273 
274  print dol_get_fiche_end('');
275 
276  print '<div class="center">';
277  print '<input type="submit" class="button b" value="'.$langs->trans("CreateThisCat").'" name="creation" />';
278  print '&nbsp; &nbsp; &nbsp;';
279  print '<input type="submit" class="button button-cancel" value="'.$langs->trans("Cancel").'" name="cancel" />';
280  print '</div>';
281 
282  print '</form>';
283  }
284 }
285 
286 // End of page
287 llxFooter();
288 $db->close();
ajax_combobox
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:438
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
FormOther
Classe permettant la generation de composants html autre Only common components are here.
Definition: html.formother.class.php:39
Categorie
Class to manage categories.
Definition: categorie.class.php:47
$help_url
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $help_url
View.
Definition: agenda.php:116
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_htmlcleanlastbr
dol_htmlcleanlastbr($stringtodecode)
This function remove all ending and br at end.
Definition: functions.lib.php:7036
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
ExtraFields
Class to manage standard extra fields.
Definition: extrafields.class.php:39
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
dol_set_focus
dol_set_focus($selector)
Set focus onto field with selector (similar behaviour of 'autofocus' HTML5 tag)
Definition: functions.lib.php:9379
$parameters
$parameters
Actions.
Definition: card.php:78
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
DolEditor
Class to manage a WYSIWYG editor.
Definition: doleditor.class.php:30