Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[com_fields] Change extension variable to context for custom field groups #13175

Merged
merged 4 commits into from
Dec 18, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -41,7 +41,7 @@ CREATE TABLE IF NOT EXISTS `#__fields` (
CREATE TABLE IF NOT EXISTS `#__fields_groups` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`asset_id` int(10) NOT NULL DEFAULT 0,
`extension` varchar(255) NOT NULL DEFAULT '',
`context` varchar(255) NOT NULL DEFAULT '',
`title` varchar(255) NOT NULL DEFAULT '',
`note` varchar(255) NOT NULL DEFAULT '',
`description` text NOT NULL,
Expand All @@ -60,7 +60,7 @@ CREATE TABLE IF NOT EXISTS `#__fields_groups` (
KEY `idx_state` (`state`),
KEY `idx_created_by` (`created_by`),
KEY `idx_access` (`access`),
KEY `idx_extension` (`extension`),
KEY `idx_context` (`context`),
KEY `idx_language` (`language`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CREATE INDEX "#__fields_idx_language" ON "#__fields" ("language");
CREATE TABLE "#__fields_groups" (
"id" serial NOT NULL,
"asset_id" bigint DEFAULT 0 NOT NULL,
"extension" varchar(255) DEFAULT '' NOT NULL,
"context" varchar(255) DEFAULT '' NOT NULL,
"title" varchar(255) DEFAULT '' NOT NULL,
"note" varchar(255) DEFAULT '' NOT NULL,
"description" text DEFAULT '' NOT NULL,
Expand All @@ -67,7 +67,7 @@ CREATE INDEX "#__fields_idx_checked_out" ON "#__fields_groups" ("checked_out");
CREATE INDEX "#__fields_idx_state" ON "#__fields_groups" ("state");
CREATE INDEX "#__fields_idx_created_by" ON "#__fields_groups" ("created_by");
CREATE INDEX "#__fields_idx_access" ON "#__fields_groups" ("access");
CREATE INDEX "#__fields_idx_extension" ON "#__fields_groups" ("extension");
CREATE INDEX "#__fields_idx_context" ON "#__fields_groups" ("context");
CREATE INDEX "#__fields_idx_language" ON "#__fields_groups" ("language");

--
Expand Down
2 changes: 1 addition & 1 deletion administrator/components/com_contact/helpers/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function addSubmenu($vName)
);
JHtmlSidebar::addEntry(
JText::_('JGLOBAL_FIELD_GROUPS'),
'index.php?option=com_fields&view=groups&extension=com_contact',
'index.php?option=com_fields&view=groups&context=com_contact.contact',
$vName == 'fields.groups'
);
}
Expand Down
2 changes: 1 addition & 1 deletion administrator/components/com_content/helpers/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function addSubmenu($vName)
);
JHtmlSidebar::addEntry(
JText::_('JGLOBAL_FIELD_GROUPS'),
'index.php?option=com_fields&view=groups&extension=com_content',
'index.php?option=com_fields&view=groups&context=com_content.article',
$vName == 'fields.groups'
);
}
Expand Down
19 changes: 12 additions & 7 deletions administrator/components/com_fields/controllers/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class FieldsControllerGroup extends JControllerForm
protected $text_prefix = 'COM_FIELDS_GROUP';

/**
* The extension for which the group applies.
* The component for which the group applies.
*
* @var string
* @since __DEPLOY_VERSION__
*/
private $extension;
private $component = '';

/**
* Class constructor.
Expand All @@ -45,7 +45,12 @@ public function __construct($config = array())
{
parent::__construct($config);

$this->extension = $this->input->getCmd('extension');
$parts = FieldsHelper::extract($this->input->getCmd('context'));

if ($parts)
{
$this->component = $parts[0];
}
}

/**
Expand Down Expand Up @@ -81,7 +86,7 @@ public function batch($model = null)
*/
protected function allowAdd($data = array())
{
return JFactory::getUser()->authorise('core.create', $this->extension);
return JFactory::getUser()->authorise('core.create', $this->component);
}

/**
Expand All @@ -100,19 +105,19 @@ protected function allowEdit($data = array(), $key = 'parent_id')
$user = JFactory::getUser();

// Check general edit permission first.
if ($user->authorise('core.edit', $this->extension))
if ($user->authorise('core.edit', $this->component))
{
return true;
}

// Check edit on the record asset (explicit or inherited)
if ($user->authorise('core.edit', $this->extension . '.fieldgroup.' . $recordId))
if ($user->authorise('core.edit', $this->component . '.fieldgroup.' . $recordId))
{
return true;
}

// Check edit own on the record asset (explicit or inherited)
if ($user->authorise('core.edit.own', $this->extension . '.fieldgroup.' . $recordId) || $user->authorise('core.edit.own', $this->extension))
if ($user->authorise('core.edit.own', $this->component . '.fieldgroup.' . $recordId) || $user->authorise('core.edit.own', $this->component))
{
// Existing record already has an owner, get it
$record = $this->getModel()->getItem($recordId);
Expand Down
9 changes: 5 additions & 4 deletions administrator/components/com_fields/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
/**
* @package Joomla.Administrator
* @subpackage com_fields
*
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;

$app = JFactory::getApplication();
$component = $app->getUserStateFromRequest('com_fields.groups.extension', 'extension', '', 'CMD');
$context = $app->getUserStateFromRequest('com_fields.groups.context', 'context', '', 'CMD');
$component = '';
Copy link
Contributor

Choose a reason for hiding this comment

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

$component isn't populated here and thus the ACL check will break.
Best is probably to move the explode part out of the if clause to the line just before the ACL check.


if (!$component)
if (!$context)
{
$parts = explode('.', $app->getUserStateFromRequest('com_fields.fields.context', 'context', '', 'CMD'));
$parts = explode('.', $app->getUserStateFromRequest('com_fields.fields.context', 'context', '', 'CMD'), 2);
$component = $parts[0];
}

Expand Down
15 changes: 12 additions & 3 deletions administrator/components/com_fields/helpers/internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ class FieldsHelperInternal
/**
* Configure the Linkbar.
*
* @param string $component The component the fields are used for
* @param string $vName The view currently active
* @param string $context The context the fields are used for
* @param string $vName The view currently active
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public static function addSubmenu ($component, $vName)
public static function addSubmenu ($context, $vName)
{
$parts = FieldsHelper::extract($context);

if (!$parts)
{
return;
}

$component = $parts[0];

// Avoid nonsense situation.
if ($component == 'com_fields')
{
Expand Down
12 changes: 8 additions & 4 deletions administrator/components/com_fields/models/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,15 @@ public function getForm($data = array(), $loadData = true)
if (empty($context) && isset($data['context']))
{
$context = $data['context'];
$parts = explode('.', $context);
$parts = FieldsHelper::extract($context);

$this->setState('field.context', $context);
$this->setState('field.component', $parts[0]);
$this->setState('field.section', isset($parts[1]) ? $parts[1] : '');

if ($parts)
{
$this->setState('field.component', $parts[0]);
$this->setState('field.section', $parts[1]);
}
}

// Get the form.
Expand Down Expand Up @@ -768,7 +772,7 @@ protected function preprocessForm(JForm $form, $data, $group = 'content')
}

$form->setFieldAttribute('type', 'component', $component);
$form->setFieldAttribute('group_id', 'extension', $component);
$form->setFieldAttribute('group_id', 'context', $this->state->get('field.context'));
$form->setFieldAttribute('rules', 'component', $component);

// Trigger the default form events.
Expand Down
14 changes: 9 additions & 5 deletions administrator/components/com_fields/models/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ protected function populateState($ordering = null, $direction = null)
$this->setState('filter.context', $context);

// Split context into component and optional section
$parts = explode('.', $context);
$this->setState('filter.component', $parts[0]);
$this->setState('filter.section', (count($parts) > 1) ? $parts[1] : null);
$parts = FieldsHelper::extract($context);

if ($parts)
{
$this->setState('filter.component', $parts[0]);
$this->setState('filter.section', $parts[1]);
}
}

/**
Expand Down Expand Up @@ -340,7 +344,7 @@ public function getFilterForm($data = array(), $loadData = true)
if ($form)
{
$form->setValue('context', null, $this->getState('filter.context'));
$form->setFieldAttribute('group_id', 'extension', $this->getState('filter.component'), 'filter');
$form->setFieldAttribute('group_id', 'context', $this->getState('filter.context'), 'filter');
}

return $form;
Expand All @@ -363,7 +367,7 @@ public function getGroups()
$query->select('title AS text, id AS value, state');
$query->from('#__fields_groups');
$query->where('state IN (0,1)');
$query->where('extension = ' . $db->quote($this->state->get('filter.component')));
$query->where('context = ' . $db->quote($this->state->get('filter.component')));
$query->where('access IN (' . implode(',', $viewlevels) . ')');

$db->setQuery($query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class JFormFieldFieldgroups extends JFormAbstractlist
*/
protected function getOptions()
{
$extension = (string) $this->element['extension'];
$context = (string) $this->element['context'];
$states = $this->element['state'] ? $this->element['state'] : '0,1';
$states = ArrayHelper::toInteger(explode(',', $states));

Expand All @@ -40,7 +40,7 @@ protected function getOptions()
$query->select('title AS text, id AS value, state');
$query->from('#__fields_groups');
$query->where('state IN (' . implode(',', $states) . ')');
$query->where('extension = ' . $db->quote($extension));
$query->where('context = ' . $db->quote($context));
$query->where('access IN (' . implode(',', $viewlevels) . ')');

$db->setQuery($query);
Expand Down
2 changes: 1 addition & 1 deletion administrator/components/com_fields/models/forms/group.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/>

<field
name="extension"
name="context"
type="hidden"
class="readonly"
readonly="true"
Expand Down
51 changes: 27 additions & 24 deletions administrator/components/com_fields/models/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
defined('_JEXEC') or die;

use Joomla\String\StringHelper;

/**
* Group Model
*
Expand Down Expand Up @@ -69,18 +67,18 @@ public function getTable($name = 'Group', $prefix = 'FieldsTable', $options = ar
*/
public function getForm($data = array(), $loadData = true)
{
$extension = $this->getState('filter.extension');
$context = $this->getState('filter.context');
$jinput = JFactory::getApplication()->input;

if (empty($extension) && isset($data['extension']))
if (empty($context) && isset($data['context']))
{
$extension = $data['extension'];
$this->setState('filter.extension', $extension);
$context = $data['context'];
$this->setState('filter.context', $context);
}

// Get the form.
$form = $this->loadForm(
'com_fields.group.' . $extension, 'group',
'com_fields.group.' . $context, 'group',
array(
'control' => 'jform',
'load_data' => $loadData,
Expand All @@ -93,12 +91,12 @@ public function getForm($data = array(), $loadData = true)
}

// Modify the form based on Edit State access controls.
if (empty($data['extension']))
if (empty($data['context']))
{
$data['extension'] = $extension;
$data['context'] = $context;
}

if (!JFactory::getUser()->authorise('core.edit.state', $extension . '.fieldgroup.' . $jinput->get('id')))
if (!JFactory::getUser()->authorise('core.edit.state', $context . '.fieldgroup.' . $jinput->get('id')))
{
// Disable fields for display.
$form->setFieldAttribute('ordering', 'disabled', 'true');
Expand Down Expand Up @@ -128,7 +126,7 @@ protected function canDelete($record)
return false;
}

return JFactory::getUser()->authorise('core.delete', $record->extension . '.fieldgroup.' . (int) $record->id);
return JFactory::getUser()->authorise('core.delete', $record->context . '.fieldgroup.' . (int) $record->id);
}

/**
Expand All @@ -148,11 +146,11 @@ protected function canEditState($record)
// Check for existing fieldgroup.
if (!empty($record->id))
{
return $user->authorise('core.edit.state', $record->extension . '.fieldgroup.' . (int) $record->id);
return $user->authorise('core.edit.state', $record->context . '.fieldgroup.' . (int) $record->id);
}

// Default to component settings.
return $user->authorise('core.edit.state', $record->extension);
return $user->authorise('core.edit.state', $record->context);
}

/**
Expand All @@ -168,8 +166,8 @@ protected function populateState()
{
parent::populateState();

$extension = JFactory::getApplication()->getUserStateFromRequest('com_fields.groups.extension', 'extension', 'com_fields', 'CMD');
$this->setState('filter.extension', $extension);
$context = JFactory::getApplication()->getUserStateFromRequest('com_fields.groups.context', 'context', 'com_fields', 'CMD');
$this->setState('filter.context', $context);
}

/**
Expand All @@ -183,7 +181,7 @@ protected function populateState()
*/
protected function getReorderConditions($table)
{
return 'extension = ' . $this->_db->quote($table->extension);
return 'context = ' . $this->_db->quote($table->context);
}

/**
Expand All @@ -203,8 +201,13 @@ protected function preprocessForm(JForm $form, $data, $group = 'content')
{
parent::preprocessForm($form, $data, $group);

// Set the access control rules field component value.
$form->setFieldAttribute('rules', 'component', $this->state->get('filter.extension'));
$parts = FieldsHelper::extract($this->state->get('filter.context'));

if ($parts)
{
// Set the access control rules field component value.
$form->setFieldAttribute('rules', 'component', $parts[0]);
}
}

/**
Expand All @@ -227,9 +230,9 @@ protected function loadFormData()
// Pre-select some filters (Status, Language, Access) in edit form if those have been selected in Field Group Manager
if (!$data->id)
{
// Check for which extension the Field Group Manager is used and get selected fields
$extension = substr($app->getUserState('com_fields.groups.filter.extension'), 4);
$filters = (array) $app->getUserState('com_fields.groups.' . $extension . '.filter');
// Check for which context the Field Group Manager is used and get selected fields
$context = substr($app->getUserState('com_fields.groups.filter.context'), 4);
$filters = (array) $app->getUserState('com_fields.groups.' . $context . '.filter');

$data->set(
'state',
Expand Down Expand Up @@ -267,7 +270,7 @@ public function getItem($pk = null)
// Prime required properties.
if (empty($item->id))
{
$item->extension = $this->getState('filter.extension');
$item->context = $this->getState('filter.context');
}

// Convert the created and modified dates to local user time for display in the form.
Expand Down Expand Up @@ -311,8 +314,8 @@ public function getItem($pk = null)
*/
protected function cleanCache($group = null, $client_id = 0)
{
$extension = JFactory::getApplication()->input->get('extension');
$context = JFactory::getApplication()->input->get('context');

parent::cleanCache($extension);
parent::cleanCache($context);
}
}
Loading