Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 12 additions & 7 deletions administrator/components/com_banners/models/banner.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ protected function preprocessForm(JForm $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 All @@ -403,20 +406,22 @@ public function save($data)

JLoader::register('CategoriesHelper', JPATH_ADMINISTRATOR . '/components/com_categories/helpers/categories.php');

// Cast catid to integer for comparison
$catid = (int) $data['catid'];
// Create new category, if needed.
$createCategory = true;

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

// Save New Category
if ($catid == 0 && $this->canCreateCategory())
if ($createCategory && $this->canCreateCategory())
Copy link
Contributor

@bembelimen bembelimen Jul 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the PR solves the problem, thank you @SharkyKZ very good work.
I have one general thought here (which I'm not sure if related to this PR or needs a new one):

Let's assume we transfer a string as catid (so $createCategory is true) but $this->canCreateCategory() is false, what is the saved value for catid then? I assume "0"?

So tltr: PR is fine imho and can be merged. If this special (edge) case would also be handled, it would be nice, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UI wouldn't allow to create category, but if you circumvent it, the ID would be 0. It's the same before this PR though.

{
$table = array();
$table['title'] = $data['catid'];

// Remove #new# prefix, if exists.
$table['title'] = strpos($data['catid'], '#new#') === 0 ? substr($data['catid'], 5) : $data['catid'];
$table['parent_id'] = 1;
$table['extension'] = 'com_banners';
$table['language'] = $data['language'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class JFormFieldCategoryEdit extends JFormFieldList
*/
protected $allowAdd;

/**
* Optional prefix for new categories.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $customPrefix;

/**
* A flexible category list that respects access controls
*
Expand Down Expand Up @@ -57,6 +65,7 @@ public function setup(SimpleXMLElement $element, $value, $group = null)
if ($return)
{
$this->allowAdd = isset($this->element['allowAdd']) ? $this->element['allowAdd'] : '';
$this->customPrefix = (string) $this->element['customPrefix'];
}

return $return;
Expand All @@ -76,6 +85,7 @@ public function __get($name)
switch ($name)
{
case 'allowAdd':
case 'customPrefix':
return $this->$name;
}

Expand All @@ -102,6 +112,9 @@ public function __set($name, $value)
$value = (string) $value;
$this->$name = ($value === 'true' || $value === $name || $value === '1');
break;
case 'customPrefix':
$this->$name = (string) $value;
break;
default:
parent::__set($name, $value);
}
Expand Down Expand Up @@ -349,6 +362,11 @@ protected function getInput()
$attr .= ' data-custom_group_text="' . $customGroupText . '" '
. 'data-no_results_text="' . JText::_('JGLOBAL_ADD_CUSTOM_CATEGORY') . '" '
. 'data-placeholder="' . JText::_('JGLOBAL_TYPE_OR_SELECT_CATEGORY') . '" ';

if ($this->customPrefix !== '')
{
$attr .= 'data-custom_value_prefix="' . $this->customPrefix . '" ';
}
}

if ($class)
Expand Down
19 changes: 12 additions & 7 deletions administrator/components/com_contact/models/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,22 @@ public function save($data)

JLoader::register('CategoriesHelper', JPATH_ADMINISTRATOR . '/components/com_categories/helpers/categories.php');

// Cast catid to integer for comparison
$catid = (int) $data['catid'];
// Create new category, if needed.
$createCategory = true;

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

// Save New Category
if ($catid == 0 && $this->canCreateCategory())
if ($createCategory && $this->canCreateCategory())
{
$table = array();
$table['title'] = $data['catid'];

// Remove #new# prefix, if exists.
$table['title'] = strpos($data['catid'], '#new#') === 0 ? substr($data['catid'], 5) : $data['catid'];
$table['parent_id'] = 1;
$table['extension'] = 'com_contact';
$table['language'] = $data['language'];
Expand Down Expand Up @@ -438,6 +440,9 @@ protected function preprocessForm(JForm $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#');
}

// Association contact items
Expand Down
19 changes: 12 additions & 7 deletions administrator/components/com_content/models/article.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,20 +573,22 @@ public function save($data)

JLoader::register('CategoriesHelper', JPATH_ADMINISTRATOR . '/components/com_categories/helpers/categories.php');

// Cast catid to integer for comparison
$catid = (int) $data['catid'];
// Create new category, if needed.
$createCategory = true;

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

// Save New Category
if ($catid == 0 && $this->canCreateCategory())
if ($createCategory && $this->canCreateCategory())
{
$table = array();
$table['title'] = $data['catid'];

// Remove #new# prefix, if exists.
$table['title'] = strpos($data['catid'], '#new#') === 0 ? substr($data['catid'], 5) : $data['catid'];
$table['parent_id'] = 1;
$table['extension'] = 'com_content';
$table['language'] = $data['language'];
Expand Down Expand Up @@ -810,6 +812,9 @@ protected function preprocessForm(JForm $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#');
}

// Association content items
Expand Down
19 changes: 12 additions & 7 deletions administrator/components/com_newsfeeds/models/newsfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,22 @@ public function save($data)

JLoader::register('CategoriesHelper', JPATH_ADMINISTRATOR . '/components/com_categories/helpers/categories.php');

// Cast catid to integer for comparison
$catid = (int) $data['catid'];
// Create new category, if needed.
$createCategory = true;

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

// Save New Category
if ($catid == 0 && $this->canCreateCategory())
if ($createCategory && $this->canCreateCategory())
{
$table = array();
$table['title'] = $data['catid'];

// Remove #new# prefix, if exists.
$table['title'] = strpos($data['catid'], '#new#') === 0 ? substr($data['catid'], 5) : $data['catid'];
$table['parent_id'] = 1;
$table['extension'] = 'com_newsfeeds';
$table['language'] = $data['language'];
Expand Down Expand Up @@ -413,6 +415,9 @@ protected function preprocessForm(JForm $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#');
}

// Association newsfeeds items
Expand Down
4 changes: 3 additions & 1 deletion media/jui/js/chosen.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ This file is generated by `grunt build`, do not edit it by hand.
/*<JUI>*/
/* Original: not exist */
this.allow_custom_value = false;
this.custom_value_prefix = '';
/*</JUI>*/
this.allow_single_deselect = (this.options.allow_single_deselect != null) && (this.form_field.options[0] != null) && this.form_field.options[0].text === "" ? this.options.allow_single_deselect : false;
this.disable_search_threshold = this.options.disable_search_threshold || 0;
Expand Down Expand Up @@ -627,6 +628,7 @@ This file is generated by `grunt build`, do not edit it by hand.
/*<JUI>*/
/* Original: not exist */
this.allow_custom_value = this.form_field_jq.hasClass("chzn-custom-value") || this.options.allow_custom_value;
this.custom_value_prefix = this.form_field_jq.attr("data-custom_value_prefix") || this.custom_value_prefix;
/*</JUI>*/
return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl");
};
Expand Down Expand Up @@ -1115,7 +1117,7 @@ This file is generated by `grunt build`, do not edit it by hand.
else if ((!this.is_multiple) && this.allow_custom_value) {
value = this.search_field.val();
group = this.add_unique_custom_group();
option = $('<option value="' + value + '">' + value + '</option>');
option = $('<option value="' + this.custom_value_prefix + value + '">' + value + '</option>');
group.append(option);
this.form_field_jq.append(group);
this.form_field.options[this.form_field.options.length - 1].selected = true;
Expand Down
3 changes: 1 addition & 2 deletions media/jui/js/chosen.jquery.min.js

Large diffs are not rendered by default.