diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index efa69c744b2..2bd621bc0f6 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -8523,8 +8523,68 @@ abstract class CommonObject } } + /* Part for categories/tags */ + /** - * copy related categories to another object + * Sets object to given categories. + * + * Deletes object from existing categories not supplied. + * Adds it to non existing supplied categories. + * Existing categories are left untouch. + * + * @param int[]|int $categories Category ID or array of Categories IDs + * @param string $type_categ Category type ('customer', 'supplier', 'website_page', ...) + * @return int <0 if KO, >0 if OK + */ + public function setCategories($categories, $type_categ) + { + require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; + + // Handle single category + if (!is_array($categories)) { + $categories = array($categories); + } + + // Get current categories + $c = new Categorie($this->db); + $existing = $c->containing($this->id, $type_categ, 'id'); + + // Diff + if (is_array($existing)) { + $to_del = array_diff($existing, $categories); + $to_add = array_diff($categories, $existing); + } else { + $to_del = array(); // Nothing to delete + $to_add = $categories; + } + + $error = 0; + + // Process + foreach ($to_del as $del) { + if ($c->fetch($del) > 0) { + $c->del_type($this, $type_categ); + } + } + foreach ($to_add as $add) { + if ($c->fetch($add) > 0) + { + $result = $c->add_type($this, $type_categ); + if ($result < 0) + { + $error++; + $this->error = $c->error; + $this->errors = $c->errors; + break; + } + } + } + + return $error ? -1 : 1; + } + + /** + * Copy related categories to another object * * @param int $fromId Id object source * @param int $toId Id object cible diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index bc89fb3403d..3f7ab69089f 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -4070,7 +4070,7 @@ class Form * Return list of categories having choosed type * * @param string|int $type Type of category ('customer', 'supplier', 'contact', 'product', 'member'). Old mode (0, 1, 2, ...) is deprecated. - * @param string $selected Id of category preselected or 'auto' (autoselect category if there is only one element) + * @param string $selected Id of category preselected or 'auto' (autoselect category if there is only one element). Not used if $outputmode = 1. * @param string $htmlname HTML field name * @param int $maxlength Maximum length for labels * @param int|string|array $markafterid Keep only or removed all categories including the leaf $markafterid in category tree (exclude) or Keep only of category is inside the leaf starting with this id. diff --git a/htdocs/website/index.php b/htdocs/website/index.php index 969c56dde8c..a6f4f0250fb 100644 --- a/htdocs/website/index.php +++ b/htdocs/website/index.php @@ -39,6 +39,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/html.formwebsite.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; require_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php'; require_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php'; +require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; $langs->loadLangs(array("admin", "other", "website", "errors")); @@ -1558,6 +1559,17 @@ if ($action == 'updatemeta') } } + if (!$error) { + // Supplier categories association + $categoriesarray = GETPOST('categories', 'array'); + $result = $objectpage->setCategories($categoriesarray, Categorie::TYPE_WEBSITE_PAGE); + if ($result < 0) + { + $error++; + setEventMessages($object->error, $object->errors, 'errors'); + } + } + if (!$error) { $db->commit(); @@ -3235,20 +3247,6 @@ if ($action == 'editmeta' || $action == 'createcontainer') if (GETPOST('WEBSITE_LANG', 'aZ09')) $pagelang = GETPOST('WEBSITE_LANG', 'aZ09'); if (GETPOST('htmlheader', 'none')) $pagehtmlheader = GETPOST('htmlheader', 'none'); - // Title - print ''; - print $langs->trans('WEBSITE_TITLE'); - print ''; - print ''; - print ''; - - // Alias - print ''; - print $langs->trans('WEBSITE_PAGENAME'); - print ''; - print ''; - print ''; - // Type of container print ''; print $langs->trans('WEBSITE_TYPE_CONTAINER'); @@ -3265,6 +3263,27 @@ if ($action == 'editmeta' || $action == 'createcontainer') print ''; } + // Title + print ''; + print $langs->trans('WEBSITE_TITLE'); + print ''; + print ''; + print ''; + + // Alias + print ''; + print $langs->trans('WEBSITE_PAGENAME'); + print ''; + print ''; + print ''; + + print ''; + $htmlhelp = $langs->trans("WEBSITE_ALIASALTDesc"); + print $form->textwithpicto($langs->trans('WEBSITE_ALIASALT'), $htmlhelp, 1, 'help', '', 0, 2, 'aliastooltip'); + print ''; + print ''; + print ''; + print ''; print $langs->trans('WEBSITE_DESCRIPTION'); print ''; @@ -3365,12 +3384,28 @@ if ($action == 'editmeta' || $action == 'createcontainer') } print ''; - print ''; - $htmlhelp = $langs->trans("WEBSITE_ALIASALTDesc"); - print $form->textwithpicto($langs->trans('WEBSITE_ALIASALT'), $htmlhelp, 1, 'help', '', 0, 2, 'aliastooltip'); - print ''; - print ''; - print ''; + // Categories + if (!empty($conf->categorie->enabled) && !empty($user->rights->categorie->lire)) + { + $langs->load('categories'); + + if (! GETPOSTISSET('categories')) { + $cate_arbo = $form->select_all_categories(Categorie::TYPE_WEBSITE_PAGE, '', null, null, null, 1); + $c = new Categorie($db); + $cats = $c->containing($objectpage->id, Categorie::TYPE_WEBSITE_PAGE); + $arrayselected = array(); + foreach ($cats as $cat) { + $arrayselected[] = $cat->id; + } + + $cate_arbo = $form->select_all_categories(Categorie::TYPE_WEBSITE_PAGE, '', 'parent', null, null, 1); + } + + print ''.$form->editfieldkey('Categories', 'categories', '', $objectpage, 0).''; + print img_picto('', 'category', 'class="paddingright"').$form->multiselectarray('categories', $cate_arbo, (GETPOSTISSET('categories') ? GETPOST('categories', 'array') : $arrayselected), null, null, null, null, "90%"); + print ""; + } + $fuser = new User($db);