* Copyright (C) 2006-2021 Laurent Destailleur * Copyright (C) 2005-2014 Regis Houssin * Copyright (C) 2007 Patrick Raguin * Copyright (C) 2013 Florian Henry * Copyright (C) 2015 Raphaël Doursenaud * Copyright (C) 2020 Frédéric France * Copyright (C) 2024 MDW * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/categories/card.php * \ingroup category * \brief Page to create a new category */ // Load Dolibarr environment require '../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; // Load translation files required by the page $langs->load("categories"); // Security check $socid = GETPOSTINT('socid'); if (!$user->hasRight('categorie', 'lire')) { accessforbidden(); } $action = GETPOST('action', 'alpha'); $cancel = GETPOST('cancel', 'alpha'); $origin = GETPOST('origin', 'alpha'); $catorigin = GETPOSTINT('catorigin'); $type = GETPOST('type', 'aZ09'); $urlfrom = GETPOST('urlfrom', 'alpha'); $backtopage = GETPOST('backtopage', 'alpha'); $label = (string) GETPOST('label', 'alphanohtml'); $description = (string) GETPOST('description', 'restricthtml'); $color = preg_replace('/[^0-9a-f#]/i', '', (string) GETPOST('color', 'alphanohtml')); $position = GETPOSTINT('position'); $visible = GETPOSTINT('visible'); $parent = GETPOSTINT('parent'); if ($origin) { if ($type == Categorie::TYPE_PRODUCT) { $idProdOrigin = $origin; } if ($type == Categorie::TYPE_SUPPLIER) { $idSupplierOrigin = $origin; } if ($type == Categorie::TYPE_CUSTOMER) { $idCompanyOrigin = $origin; } if ($type == Categorie::TYPE_MEMBER) { $idMemberOrigin = $origin; } if ($type == Categorie::TYPE_CONTACT) { $idContactOrigin = $origin; } if ($type == Categorie::TYPE_PROJECT) { $idProjectOrigin = $origin; } } if ($catorigin && $type == Categorie::TYPE_PRODUCT) { $idCatOrigin = $catorigin; } if (!GETPOSTISSET('parent') && $catorigin) { $parent = $catorigin; } $object = new Categorie($db); $extrafields = new ExtraFields($db); $extrafields->fetch_name_optionals_label($object->table_element); // Initialize a technical object to manage hooks. Note that conf->hooks_modules contains array array $hookmanager->initHooks(array('categorycard')); $error = 0; /* * Actions */ $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); // Note that $action and $object may be modified by some hooks $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); if ($reshook < 0) { setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); } if (empty($reshook)) { // Add action if ($action == 'add' && $user->hasRight('categorie', 'creer')) { // Action add a category if ($cancel) { if ($urlfrom) { header("Location: ".$urlfrom); exit; } elseif ($backtopage) { header("Location: ".$backtopage); exit; } elseif ($idProdOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type); exit; } elseif ($idCompanyOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type); exit; } elseif ($idSupplierOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type); exit; } elseif ($idMemberOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type); exit; } elseif ($idContactOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type); exit; } elseif ($idProjectOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type); exit; } else { header("Location: ".DOL_URL_ROOT.'/categories/index.php?leftmenu=cat&type='.$type); exit; } } $object->label = $label; $object->color = $color; $object->position = $position; $object->description = dol_htmlcleanlastbr($description); $object->socid = ($socid > 0 ? $socid : 0); $object->visible = $visible; $object->type = $type; if ($parent != "-1") { $object->fk_parent = $parent; } $ret = $extrafields->setOptionalsFromPost(null, $object); if ($ret < 0) { $error++; } if (!$object->label) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors'); $action = 'create'; } // Create category in database if (!$error) { $result = $object->create($user); if ($result > 0) { $action = 'confirmed'; } else { setEventMessages($object->error, $object->errors, 'errors'); } } } // Action confirmation of creation category if ($action == 'confirmed' && $user->hasRight('categorie', 'creer')) { if ($urlfrom) { header("Location: ".$urlfrom); exit; } elseif ($backtopage) { header("Location: ".$backtopage); exit; } elseif ($idProdOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProdOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated"))); exit; } elseif ($idCompanyOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idCompanyOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated"))); exit; } elseif ($idSupplierOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idSupplierOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated"))); exit; } elseif ($idMemberOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idMemberOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated"))); exit; } elseif ($idContactOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idContactOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated"))); exit; } elseif ($idProjectOrigin) { header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$idProjectOrigin.'&type='.$type.'&mesg='.urlencode($langs->trans("CatCreated"))); exit; } header("Location: ".DOL_URL_ROOT.'/categories/viewcat.php?id='.$result.'&type='.$type); exit; } } /* * View */ $form = new Form($db); $formother = new FormOther($db); $help_url = 'EN:Module_Categories|FR:Module_Catégories|DE:Modul_Kategorien'; llxHeader("", $langs->trans("Categories"), $help_url); if ($user->hasRight('categorie', 'creer')) { // Create or add if ($action == 'create' || $action == 'add') { dol_set_focus('#label'); print '
'; print ''; print ''; print ''; print ''; print ''; print ''; if ($origin) { print ''; } if ($catorigin) { print ''; } print load_fiche_titre($langs->trans("CreateCat")); print dol_get_fiche_head(); print ''; // Ref print ''; print ''; // Description print ''; // Color print ''; // Position print ''; print ''; // Parent category print ''; $parameters = array(); $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook print $hookmanager->resPrint; if (empty($reshook)) { print $object->showOptionals($extrafields, 'create', $parameters); } print '
'.$langs->trans("Ref").''; print'
'.$langs->trans("Description").''; require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; $doleditor = new DolEditor('description', $description, '', 160, 'dolibarr_notes', '', false, true, isModEnabled('fckeditor') && getDolGlobalInt('FCKEDITOR_ENABLE_SOCIETE'), ROWS_5, '90%'); $doleditor->Create(); print '
'.$langs->trans("Color").''; print $formother->selectColor($color, 'color'); print '
'.$langs->trans("Position").''; print'
'.$langs->trans("AddIn").''; print img_picto($langs->trans("ParentCategory"), 'category', 'class="pictofixedwidth"'); print $form->select_all_categories($type, $parent, 'parent'); print ajax_combobox('parent'); print '
'; print dol_get_fiche_end(); print '
'; print ''; print '     '; print ''; print '
'; print '
'; } } // End of page llxFooter(); $db->close();