Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Fixes category selection and creation of random categories #32

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,6 @@ protected function loadFormData()
*/
protected function preprocessForm(Form $form, $data, $group = 'content')
{
if ($this->canCreateCategory())
{
$form->setFieldAttribute('catid', 'allowAdd', 'true');

// Add a prefix for categories created on the fly.
$form->setFieldAttribute('catid', 'customPrefix', '#new#');
}

parent::preprocessForm($form, $data, $group);
}

Expand Down Expand Up @@ -203,55 +195,6 @@ public function save($data)
{
$input = Factory::getApplication()->input;

// Create new category, if needed.
$createCategory = true;

// If category ID is provided, check if it's valid.
if (is_numeric($data['catid']) && $data['catid'])
{
$createCategory = !CategoriesHelper::validateCategoryId($data['catid'], 'com_cookiemanger');
}

// Save New Category
if ($createCategory && $this->canCreateCategory())
{
$category = [
// Remove #new# prefix, if exists.
'title' => strpos($data['catid'], '#new#') === 0 ? substr($data['catid'], 5) : $data['catid'],
'parent_id' => 1,
'extension' => 'com_cookiemanager',
'language' => $data['language'],
'published' => 1,
];

/** @var \Joomla\Component\Categories\Administrator\Model\CategoryModel $categoryModel */
$categoryModel = Factory::getApplication()->bootComponent('com_categories')
->getMVCFactory()->createModel('Category', 'Administrator', ['ignore_request' => true]);

// Create new category.
if (!$categoryModel->save($category))
{
$this->setError($categoryModel->getError());

return false;
}

// Get the new category ID.
$data['catid'] = $categoryModel->getState('category.id');
}

return parent::save($data);
}

/**
* Is the user allowed to create an on the fly category?
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
private function canCreateCategory()
{
return Factory::getUser()->authorise('core.create', 'com_cookiemanager');
}
}