primo commit
This commit is contained in:
1255
administrator/components/com_fields/src/Model/FieldModel.php
Normal file
1255
administrator/components/com_fields/src/Model/FieldModel.php
Normal file
File diff suppressed because it is too large
Load Diff
465
administrator/components/com_fields/src/Model/FieldsModel.php
Normal file
465
administrator/components/com_fields/src/Model/FieldsModel.php
Normal file
@ -0,0 +1,465 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_fields
|
||||
*
|
||||
* @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Fields\Administrator\Model;
|
||||
|
||||
use Joomla\CMS\Categories\CategoryServiceInterface;
|
||||
use Joomla\CMS\Categories\SectionNotFoundException;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
|
||||
use Joomla\CMS\MVC\Model\ListModel;
|
||||
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
|
||||
use Joomla\Database\ParameterType;
|
||||
use Joomla\Database\QueryInterface;
|
||||
use Joomla\Registry\Registry;
|
||||
use Joomla\Utilities\ArrayHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Fields Model
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
class FieldsModel extends ListModel
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request).
|
||||
* @param ?MVCFactoryInterface $factory The factory.
|
||||
*
|
||||
* @since 3.7.0
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct($config = [], ?MVCFactoryInterface $factory = null)
|
||||
{
|
||||
if (empty($config['filter_fields'])) {
|
||||
$config['filter_fields'] = [
|
||||
'id', 'a.id',
|
||||
'title', 'a.title',
|
||||
'type', 'a.type',
|
||||
'name', 'a.name',
|
||||
'state', 'a.state',
|
||||
'access', 'a.access',
|
||||
'access_level',
|
||||
'only_use_in_subform',
|
||||
'language', 'a.language',
|
||||
'ordering', 'a.ordering',
|
||||
'checked_out', 'a.checked_out',
|
||||
'checked_out_time', 'a.checked_out_time',
|
||||
'created_time', 'a.created_time',
|
||||
'created_user_id', 'a.created_user_id',
|
||||
'group_title', 'g.title',
|
||||
'category_id', 'a.category_id',
|
||||
'group_id', 'a.group_id',
|
||||
'assigned_cat_ids',
|
||||
];
|
||||
}
|
||||
|
||||
parent::__construct($config, $factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to auto-populate the model state.
|
||||
*
|
||||
* This method should only be called once per instantiation and is designed
|
||||
* to be called on the first call to the getState() method unless the model
|
||||
* configuration flag to ignore the request is set.
|
||||
*
|
||||
* Note. Calling getState in this method will result in recursion.
|
||||
*
|
||||
* @param string $ordering An optional ordering field.
|
||||
* @param string $direction An optional direction (asc|desc).
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function populateState($ordering = null, $direction = null)
|
||||
{
|
||||
// List state information.
|
||||
parent::populateState('a.ordering', 'asc');
|
||||
|
||||
$context = $this->getUserStateFromRequest($this->context . '.context', 'context', 'com_content.article', 'CMD');
|
||||
$this->setState('filter.context', $context);
|
||||
|
||||
// Split context into component and optional section
|
||||
$parts = FieldsHelper::extract($context);
|
||||
|
||||
if ($parts) {
|
||||
$this->setState('filter.component', $parts[0]);
|
||||
$this->setState('filter.section', $parts[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a store id based on the model configuration state.
|
||||
*
|
||||
* This is necessary because the model is used by the component and
|
||||
* different modules that might need different sets of data or different
|
||||
* ordering requirements.
|
||||
*
|
||||
* @param string $id An identifier string to generate the store id.
|
||||
*
|
||||
* @return string A store id.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function getStoreId($id = '')
|
||||
{
|
||||
// Compile the store id.
|
||||
$id .= ':' . $this->getState('filter.search');
|
||||
$id .= ':' . $this->getState('filter.context');
|
||||
$id .= ':' . serialize($this->getState('filter.assigned_cat_ids'));
|
||||
$id .= ':' . $this->getState('filter.state');
|
||||
$id .= ':' . $this->getState('filter.group_id');
|
||||
$id .= ':' . serialize($this->getState('filter.language'));
|
||||
$id .= ':' . $this->getState('filter.only_use_in_subform');
|
||||
|
||||
return parent::getStoreId($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a QueryInterface object for retrieving the data set from a database.
|
||||
*
|
||||
* @return QueryInterface An object implementing QueryInterface to retrieve the data set.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function getListQuery()
|
||||
{
|
||||
// Create a new query object.
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true);
|
||||
$user = $this->getCurrentUser();
|
||||
$app = Factory::getApplication();
|
||||
|
||||
// Select the required fields from the table.
|
||||
$query->select(
|
||||
$this->getState(
|
||||
'list.select',
|
||||
'DISTINCT a.id, a.title, a.name, a.checked_out, a.checked_out_time, a.note' .
|
||||
', a.state, a.access, a.created_time, a.created_user_id, a.ordering, a.language' .
|
||||
', a.fieldparams, a.params, a.type, a.default_value, a.context, a.group_id' .
|
||||
', a.label, a.description, a.required, a.only_use_in_subform'
|
||||
)
|
||||
);
|
||||
$query->from('#__fields AS a');
|
||||
|
||||
// Join over the language
|
||||
$query->select('l.title AS language_title, l.image AS language_image')
|
||||
->join('LEFT', $db->quoteName('#__languages') . ' AS l ON l.lang_code = a.language');
|
||||
|
||||
// Join over the users for the checked out user.
|
||||
$query->select('uc.name AS editor')->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
|
||||
|
||||
// Join over the asset groups.
|
||||
$query->select('ag.title AS access_level')->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
|
||||
|
||||
// Join over the users for the author.
|
||||
$query->select('ua.name AS author_name')->join('LEFT', '#__users AS ua ON ua.id = a.created_user_id');
|
||||
|
||||
// Join over the field groups.
|
||||
$query->select('g.title AS group_title, g.access as group_access, g.state AS group_state, g.note as group_note');
|
||||
$query->join('LEFT', '#__fields_groups AS g ON g.id = a.group_id');
|
||||
|
||||
// Filter by context
|
||||
if ($context = $this->getState('filter.context')) {
|
||||
$query->where($db->quoteName('a.context') . ' = :context')
|
||||
->bind(':context', $context);
|
||||
}
|
||||
|
||||
// Filter by access level.
|
||||
if ($access = $this->getState('filter.access')) {
|
||||
if (\is_array($access)) {
|
||||
$access = ArrayHelper::toInteger($access);
|
||||
$query->whereIn($db->quoteName('a.access'), $access);
|
||||
} else {
|
||||
$access = (int) $access;
|
||||
$query->where($db->quoteName('a.access') . ' = :access')
|
||||
->bind(':access', $access, ParameterType::INTEGER);
|
||||
}
|
||||
}
|
||||
|
||||
if (($categories = $this->getState('filter.assigned_cat_ids')) && $context) {
|
||||
$categories = (array) $categories;
|
||||
$categories = ArrayHelper::toInteger($categories);
|
||||
$parts = FieldsHelper::extract($context);
|
||||
|
||||
if ($parts) {
|
||||
// Get the categories for this component (and optionally this section, if available)
|
||||
$cat = (
|
||||
function () use ($parts) {
|
||||
// Get the CategoryService for this component
|
||||
$componentObject = $this->bootComponent($parts[0]);
|
||||
|
||||
if (!$componentObject instanceof CategoryServiceInterface) {
|
||||
// No CategoryService -> no categories
|
||||
return null;
|
||||
}
|
||||
|
||||
$cat = null;
|
||||
|
||||
// Try to get the categories for this component and section
|
||||
try {
|
||||
$cat = $componentObject->getCategory([], $parts[1] ?: '');
|
||||
} catch (SectionNotFoundException $e) {
|
||||
// Not found for component and section -> Now try once more without the section, so only component
|
||||
try {
|
||||
$cat = $componentObject->getCategory();
|
||||
} catch (SectionNotFoundException $e) {
|
||||
// If we haven't found it now, return (no categories available for this component)
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// So we found categories for at least the component, return them
|
||||
return $cat;
|
||||
}
|
||||
)();
|
||||
|
||||
if ($cat) {
|
||||
foreach ($categories as $assignedCatIds) {
|
||||
// Check if we have the actual category
|
||||
$parent = $cat->get($assignedCatIds);
|
||||
|
||||
if ($parent) {
|
||||
$categories[] = (int) $parent->id;
|
||||
|
||||
// Traverse the tree up to get all the fields which are attached to a parent
|
||||
while ($parent->getParent() && $parent->getParent()->id != 'root') {
|
||||
$parent = $parent->getParent();
|
||||
$categories[] = (int) $parent->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$categories = array_unique($categories);
|
||||
|
||||
// Join over the assigned categories
|
||||
$query->join('LEFT', $db->quoteName('#__fields_categories') . ' AS fc ON fc.field_id = a.id');
|
||||
|
||||
if (\in_array('0', $categories)) {
|
||||
$query->where(
|
||||
'(' .
|
||||
$db->quoteName('fc.category_id') . ' IS NULL OR ' .
|
||||
$db->quoteName('fc.category_id') . ' IN (' . implode(',', $query->bindArray(array_values($categories), ParameterType::INTEGER)) . ')' .
|
||||
')'
|
||||
);
|
||||
} else {
|
||||
$query->whereIn($db->quoteName('fc.category_id'), $categories);
|
||||
}
|
||||
}
|
||||
|
||||
// Implement View Level Access
|
||||
if (!$app->isClient('administrator') || !$user->authorise('core.admin')) {
|
||||
$groups = $user->getAuthorisedViewLevels();
|
||||
$query->whereIn($db->quoteName('a.access'), $groups);
|
||||
$query->extendWhere(
|
||||
'AND',
|
||||
[
|
||||
$db->quoteName('a.group_id') . ' = 0',
|
||||
$db->quoteName('g.access') . ' IN (' . implode(',', $query->bindArray($groups, ParameterType::INTEGER)) . ')',
|
||||
],
|
||||
'OR'
|
||||
);
|
||||
}
|
||||
|
||||
// Filter by state
|
||||
$state = $this->getState('filter.state');
|
||||
|
||||
// Include group state only when not on on back end list
|
||||
$includeGroupState = !$app->isClient('administrator') ||
|
||||
$app->getInput()->get('option') != 'com_fields' ||
|
||||
$app->getInput()->get('view') != 'fields';
|
||||
|
||||
if (is_numeric($state)) {
|
||||
$state = (int) $state;
|
||||
$query->where($db->quoteName('a.state') . ' = :state')
|
||||
->bind(':state', $state, ParameterType::INTEGER);
|
||||
|
||||
if ($includeGroupState) {
|
||||
$query->extendWhere(
|
||||
'AND',
|
||||
[
|
||||
$db->quoteName('a.group_id') . ' = 0',
|
||||
$db->quoteName('g.state') . ' = :gstate',
|
||||
],
|
||||
'OR'
|
||||
)
|
||||
->bind(':gstate', $state, ParameterType::INTEGER);
|
||||
}
|
||||
} elseif (!$state) {
|
||||
$query->whereIn($db->quoteName('a.state'), [0, 1]);
|
||||
|
||||
if ($includeGroupState) {
|
||||
$query->extendWhere(
|
||||
'AND',
|
||||
[
|
||||
$db->quoteName('a.group_id') . ' = 0',
|
||||
$db->quoteName('g.state') . ' IN (' . implode(',', $query->bindArray([0, 1], ParameterType::INTEGER)) . ')',
|
||||
],
|
||||
'OR'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$groupId = $this->getState('filter.group_id');
|
||||
|
||||
if (is_numeric($groupId)) {
|
||||
$groupId = (int) $groupId;
|
||||
$query->where($db->quoteName('a.group_id') . ' = :groupid')
|
||||
->bind(':groupid', $groupId, ParameterType::INTEGER);
|
||||
}
|
||||
|
||||
$onlyUseInSubForm = $this->getState('filter.only_use_in_subform');
|
||||
|
||||
if (is_numeric($onlyUseInSubForm)) {
|
||||
$onlyUseInSubForm = (int) $onlyUseInSubForm;
|
||||
$query->where($db->quoteName('a.only_use_in_subform') . ' = :only_use_in_subform')
|
||||
->bind(':only_use_in_subform', $onlyUseInSubForm, ParameterType::INTEGER);
|
||||
}
|
||||
|
||||
// Filter by search in title
|
||||
$search = $this->getState('filter.search');
|
||||
|
||||
if (!empty($search)) {
|
||||
if (stripos($search, 'id:') === 0) {
|
||||
$search = (int) substr($search, 3);
|
||||
$query->where($db->quoteName('a.id') . ' = :id')
|
||||
->bind(':id', $search, ParameterType::INTEGER);
|
||||
} elseif (stripos($search, 'author:') === 0) {
|
||||
$search = '%' . substr($search, 7) . '%';
|
||||
$query->where(
|
||||
'(' .
|
||||
$db->quoteName('ua.name') . ' LIKE :name OR ' .
|
||||
$db->quoteName('ua.username') . ' LIKE :username' .
|
||||
')'
|
||||
)
|
||||
->bind(':name', $search)
|
||||
->bind(':username', $search);
|
||||
} else {
|
||||
$search = '%' . str_replace(' ', '%', trim($search)) . '%';
|
||||
$query->where(
|
||||
'(' .
|
||||
$db->quoteName('a.title') . ' LIKE :title OR ' .
|
||||
$db->quoteName('a.name') . ' LIKE :sname OR ' .
|
||||
$db->quoteName('a.note') . ' LIKE :note' .
|
||||
')'
|
||||
)
|
||||
->bind(':title', $search)
|
||||
->bind(':sname', $search)
|
||||
->bind(':note', $search);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter on the language.
|
||||
if ($language = $this->getState('filter.language')) {
|
||||
$language = (array) $language;
|
||||
|
||||
$query->whereIn($db->quoteName('a.language'), $language, ParameterType::STRING);
|
||||
}
|
||||
|
||||
// Add the list ordering clause
|
||||
$listOrdering = $this->state->get('list.ordering', 'a.ordering');
|
||||
$orderDirn = $this->state->get('list.direction', 'ASC');
|
||||
|
||||
$query->order($db->escape($listOrdering) . ' ' . $db->escape($orderDirn));
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of objects from the results of database query.
|
||||
*
|
||||
* @param string $query The query.
|
||||
* @param integer $limitstart Offset.
|
||||
* @param integer $limit The number of records.
|
||||
*
|
||||
* @return array An array of results.
|
||||
*
|
||||
* @since 3.7.0
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
protected function _getList($query, $limitstart = 0, $limit = 0)
|
||||
{
|
||||
$result = parent::_getList($query, $limitstart, $limit);
|
||||
|
||||
if (\is_array($result)) {
|
||||
foreach ($result as $field) {
|
||||
$field->fieldparams = new Registry($field->fieldparams);
|
||||
$field->params = new Registry($field->params);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the filter form
|
||||
*
|
||||
* @param array $data data
|
||||
* @param boolean $loadData load current data
|
||||
*
|
||||
* @return \Joomla\CMS\Form\Form|bool the Form object or false
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
public function getFilterForm($data = [], $loadData = true)
|
||||
{
|
||||
$form = parent::getFilterForm($data, $loadData);
|
||||
|
||||
if ($form) {
|
||||
$form->setValue('context', null, $this->getState('filter.context'));
|
||||
$form->setFieldAttribute('group_id', 'context', $this->getState('filter.context'), 'filter');
|
||||
$form->setFieldAttribute('assigned_cat_ids', 'extension', $this->state->get('filter.component'), 'filter');
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the groups for the batch method
|
||||
*
|
||||
* @return array An array of groups
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
public function getGroups()
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
$viewlevels = ArrayHelper::toInteger($user->getAuthorisedViewLevels());
|
||||
$context = $this->state->get('filter.context');
|
||||
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true);
|
||||
$query->select(
|
||||
[
|
||||
$db->quoteName('title', 'text'),
|
||||
$db->quoteName('id', 'value'),
|
||||
$db->quoteName('state'),
|
||||
]
|
||||
);
|
||||
$query->from($db->quoteName('#__fields_groups'));
|
||||
$query->whereIn($db->quoteName('state'), [0, 1]);
|
||||
$query->where($db->quoteName('context') . ' = :context');
|
||||
$query->whereIn($db->quoteName('access'), $viewlevels);
|
||||
$query->bind(':context', $context);
|
||||
|
||||
$db->setQuery($query);
|
||||
|
||||
return $db->loadObjectList();
|
||||
}
|
||||
}
|
||||
380
administrator/components/com_fields/src/Model/GroupModel.php
Normal file
380
administrator/components/com_fields/src/Model/GroupModel.php
Normal file
@ -0,0 +1,380 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_fields
|
||||
*
|
||||
* @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Fields\Administrator\Model;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Form;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\MVC\Model\AdminModel;
|
||||
use Joomla\CMS\Table\Table;
|
||||
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
|
||||
use Joomla\Filesystem\Path;
|
||||
use Joomla\Registry\Registry;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Group Model
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
class GroupModel extends AdminModel
|
||||
{
|
||||
/**
|
||||
* @var null|string
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
public $typeAlias = null;
|
||||
|
||||
/**
|
||||
* Allowed batch commands
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $batch_commands = [
|
||||
'assetgroup_id' => 'batchAccess',
|
||||
'language_id' => 'batchLanguage',
|
||||
];
|
||||
|
||||
/**
|
||||
* Method to save the form data.
|
||||
*
|
||||
* @param array $data The form data.
|
||||
*
|
||||
* @return boolean True on success, False on error.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
public function save($data)
|
||||
{
|
||||
// Alter the title for save as copy
|
||||
$input = Factory::getApplication()->getInput();
|
||||
|
||||
// Save new group as unpublished
|
||||
if ($input->get('task') == 'save2copy') {
|
||||
$data['state'] = 0;
|
||||
}
|
||||
|
||||
return parent::save($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a table object, load it if necessary.
|
||||
*
|
||||
* @param string $name The table name. Optional.
|
||||
* @param string $prefix The class prefix. Optional.
|
||||
* @param array $options Configuration array for model. Optional.
|
||||
*
|
||||
* @return Table A Table object
|
||||
*
|
||||
* @since 3.7.0
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getTable($name = 'Group', $prefix = 'Administrator', $options = [])
|
||||
{
|
||||
return parent::getTable($name, $prefix, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method for getting the form from the model.
|
||||
*
|
||||
* @param array $data Data for the form.
|
||||
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
|
||||
*
|
||||
* @return mixed A Form object on success, false on failure
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
public function getForm($data = [], $loadData = true)
|
||||
{
|
||||
$context = $this->getState('filter.context');
|
||||
$jinput = Factory::getApplication()->getInput();
|
||||
|
||||
if (empty($context) && isset($data['context'])) {
|
||||
$context = $data['context'];
|
||||
$this->setState('filter.context', $context);
|
||||
}
|
||||
|
||||
// Get the form.
|
||||
$form = $this->loadForm(
|
||||
'com_fields.group.' . $context,
|
||||
'group',
|
||||
[
|
||||
'control' => 'jform',
|
||||
'load_data' => $loadData,
|
||||
]
|
||||
);
|
||||
|
||||
if (empty($form)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Modify the form based on Edit State access controls.
|
||||
if (empty($data['context'])) {
|
||||
$data['context'] = $context;
|
||||
}
|
||||
|
||||
$user = $this->getCurrentUser();
|
||||
|
||||
if (!$user->authorise('core.edit.state', $context . '.fieldgroup.' . $jinput->get('id'))) {
|
||||
// Disable fields for display.
|
||||
$form->setFieldAttribute('ordering', 'disabled', 'true');
|
||||
$form->setFieldAttribute('state', 'disabled', 'true');
|
||||
|
||||
// Disable fields while saving. The controller has already verified this is a record you can edit.
|
||||
$form->setFieldAttribute('ordering', 'filter', 'unset');
|
||||
$form->setFieldAttribute('state', 'filter', 'unset');
|
||||
}
|
||||
|
||||
// Don't allow to change the created_by user if not allowed to access com_users.
|
||||
if (!$user->authorise('core.manage', 'com_users')) {
|
||||
$form->setFieldAttribute('created_by', 'filter', 'unset');
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to test whether a record can be deleted.
|
||||
*
|
||||
* @param object $record A record object.
|
||||
*
|
||||
* @return boolean True if allowed to delete the record. Defaults to the permission for the component.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function canDelete($record)
|
||||
{
|
||||
if (empty($record->id) || $record->state != -2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->getCurrentUser()->authorise('core.delete', $record->context . '.fieldgroup.' . (int) $record->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to test whether a record can have its state changed.
|
||||
*
|
||||
* @param object $record A record object.
|
||||
*
|
||||
* @return boolean True if allowed to change the state of the record. Defaults to the permission for the
|
||||
* component.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function canEditState($record)
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
|
||||
// Check for existing fieldgroup.
|
||||
if (!empty($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->context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto-populate the model state.
|
||||
*
|
||||
* Note. Calling getState in this method will result in recursion.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function populateState()
|
||||
{
|
||||
parent::populateState();
|
||||
|
||||
$context = Factory::getApplication()->getUserStateFromRequest('com_fields.groups.context', 'context', 'com_fields', 'CMD');
|
||||
$this->setState('filter.context', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* A protected method to get a set of ordering conditions.
|
||||
*
|
||||
* @param Table $table A Table object.
|
||||
*
|
||||
* @return array An array of conditions to add to ordering queries.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function getReorderConditions($table)
|
||||
{
|
||||
$db = $this->getDatabase();
|
||||
|
||||
return [
|
||||
$db->quoteName('context') . ' = ' . $db->quote($table->context),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to preprocess the form.
|
||||
*
|
||||
* @param Form $form A Form object.
|
||||
* @param mixed $data The data expected for the form.
|
||||
* @param string $group The name of the plugin group to import (defaults to "content").
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @see \Joomla\CMS\Form\FormField
|
||||
* @since 3.7.0
|
||||
* @throws \Exception if there is an error in the form event.
|
||||
*/
|
||||
protected function preprocessForm(Form $form, $data, $group = 'content')
|
||||
{
|
||||
parent::preprocessForm($form, $data, $group);
|
||||
|
||||
$parts = FieldsHelper::extract($this->state->get('filter.context'));
|
||||
|
||||
// If we don't have a valid context then return early
|
||||
if (!$parts) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract the component name
|
||||
$component = $parts[0];
|
||||
|
||||
// Extract the section name
|
||||
$section = $parts[1];
|
||||
|
||||
// Set the access control rules field component value.
|
||||
$form->setFieldAttribute('rules', 'component', $component);
|
||||
|
||||
// Looking first in the component models/forms folder
|
||||
$path = Path::clean(JPATH_ADMINISTRATOR . '/components/' . $component . '/models/forms/fieldgroup/' . $section . '.xml');
|
||||
|
||||
if (file_exists($path)) {
|
||||
$lang = Factory::getLanguage();
|
||||
$lang->load($component, JPATH_BASE);
|
||||
$lang->load($component, JPATH_BASE . '/components/' . $component);
|
||||
|
||||
if (!$form->loadFile($path, false)) {
|
||||
throw new \Exception(Text::_('JERROR_LOADFILE_FAILED'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to validate the form data.
|
||||
*
|
||||
* @param Form $form The form to validate against.
|
||||
* @param array $data The data to validate.
|
||||
* @param string $group The name of the field group to validate.
|
||||
*
|
||||
* @return array|boolean Array of filtered data if valid, false otherwise.
|
||||
*
|
||||
* @see \Joomla\CMS\Form\FormRule
|
||||
* @see \Joomla\CMS\Filter\InputFilter
|
||||
* @since 3.9.23
|
||||
*/
|
||||
public function validate($form, $data, $group = null)
|
||||
{
|
||||
if (!$this->getCurrentUser()->authorise('core.admin', 'com_fields')) {
|
||||
if (isset($data['rules'])) {
|
||||
unset($data['rules']);
|
||||
}
|
||||
}
|
||||
|
||||
return parent::validate($form, $data, $group);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the data that should be injected in the form.
|
||||
*
|
||||
* @return array The default data is an empty array.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function loadFormData()
|
||||
{
|
||||
// Check the session for previously entered form data.
|
||||
$app = Factory::getApplication();
|
||||
$input = $app->getInput();
|
||||
$data = $app->getUserState('com_fields.edit.group.data', []);
|
||||
|
||||
if (empty($data)) {
|
||||
$data = $this->getItem();
|
||||
|
||||
// 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 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',
|
||||
$input->getInt('state', (!empty($filters['state']) ? $filters['state'] : null))
|
||||
);
|
||||
$data->set(
|
||||
'language',
|
||||
$input->getString('language', (!empty($filters['language']) ? $filters['language'] : null))
|
||||
);
|
||||
$data->set(
|
||||
'access',
|
||||
$input->getInt('access', (!empty($filters['access']) ? $filters['access'] : $app->get('access')))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->preprocessData('com_fields.group', $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a single record.
|
||||
*
|
||||
* @param integer $pk The id of the primary key.
|
||||
*
|
||||
* @return mixed Object on success, false on failure.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
public function getItem($pk = null)
|
||||
{
|
||||
if ($item = parent::getItem($pk)) {
|
||||
// Prime required properties.
|
||||
if (empty($item->id)) {
|
||||
$item->context = $this->getState('filter.context');
|
||||
}
|
||||
|
||||
if (property_exists($item, 'params')) {
|
||||
$item->params = new Registry($item->params);
|
||||
}
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean the cache
|
||||
*
|
||||
* @param string $group The cache group
|
||||
* @param integer $clientId No longer used, will be removed without replacement
|
||||
* @deprecated 4.3 will be removed in 6.0
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function cleanCache($group = null, $clientId = 0)
|
||||
{
|
||||
$context = Factory::getApplication()->getInput()->get('context');
|
||||
|
||||
parent::cleanCache($context);
|
||||
}
|
||||
}
|
||||
241
administrator/components/com_fields/src/Model/GroupsModel.php
Normal file
241
administrator/components/com_fields/src/Model/GroupsModel.php
Normal file
@ -0,0 +1,241 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_fields
|
||||
*
|
||||
* @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Fields\Administrator\Model;
|
||||
|
||||
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
|
||||
use Joomla\CMS\MVC\Model\ListModel;
|
||||
use Joomla\Database\ParameterType;
|
||||
use Joomla\Database\QueryInterface;
|
||||
use Joomla\Registry\Registry;
|
||||
use Joomla\Utilities\ArrayHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Groups Model
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
class GroupsModel extends ListModel
|
||||
{
|
||||
/**
|
||||
* Context string for the model type. This is used to handle uniqueness
|
||||
* when dealing with the getStoreId() method and caching data structures.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected $context = 'com_fields.groups';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request).
|
||||
* @param ?MVCFactoryInterface $factory The factory.
|
||||
*
|
||||
* @since 3.7.0
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct($config = [], ?MVCFactoryInterface $factory = null)
|
||||
{
|
||||
if (empty($config['filter_fields'])) {
|
||||
$config['filter_fields'] = [
|
||||
'id', 'a.id',
|
||||
'title', 'a.title',
|
||||
'type', 'a.type',
|
||||
'state', 'a.state',
|
||||
'access', 'a.access',
|
||||
'access_level',
|
||||
'language', 'a.language',
|
||||
'ordering', 'a.ordering',
|
||||
'checked_out', 'a.checked_out',
|
||||
'checked_out_time', 'a.checked_out_time',
|
||||
'created', 'a.created',
|
||||
'created_by', 'a.created_by',
|
||||
];
|
||||
}
|
||||
|
||||
parent::__construct($config, $factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to auto-populate the model state.
|
||||
*
|
||||
* This method should only be called once per instantiation and is designed
|
||||
* to be called on the first call to the getState() method unless the model
|
||||
* configuration flag to ignore the request is set.
|
||||
*
|
||||
* Note. Calling getState in this method will result in recursion.
|
||||
*
|
||||
* @param string $ordering An optional ordering field.
|
||||
* @param string $direction An optional direction (asc|desc).
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function populateState($ordering = null, $direction = null)
|
||||
{
|
||||
// List state information.
|
||||
parent::populateState('a.ordering', 'asc');
|
||||
|
||||
$context = $this->getUserStateFromRequest($this->context . '.context', 'context', 'com_content', 'CMD');
|
||||
$this->setState('filter.context', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a store id based on the model configuration state.
|
||||
*
|
||||
* This is necessary because the model is used by the component and
|
||||
* different modules that might need different sets of data or different
|
||||
* ordering requirements.
|
||||
*
|
||||
* @param string $id An identifier string to generate the store id.
|
||||
*
|
||||
* @return string A store id.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function getStoreId($id = '')
|
||||
{
|
||||
// Compile the store id.
|
||||
$id .= ':' . $this->getState('filter.search');
|
||||
$id .= ':' . $this->getState('filter.context');
|
||||
$id .= ':' . $this->getState('filter.state');
|
||||
$id .= ':' . print_r($this->getState('filter.language'), true);
|
||||
|
||||
return parent::getStoreId($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a QueryInterface object for retrieving the data set from a database.
|
||||
*
|
||||
* @return QueryInterface An object implementing QueryInterface to retrieve the data set.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function getListQuery()
|
||||
{
|
||||
// Create a new query object.
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true);
|
||||
$user = $this->getCurrentUser();
|
||||
|
||||
// Select the required fields from the table.
|
||||
$query->select($this->getState('list.select', 'a.*'));
|
||||
$query->from('#__fields_groups AS a');
|
||||
|
||||
// Join over the language
|
||||
$query->select('l.title AS language_title, l.image AS language_image')
|
||||
->join('LEFT', $db->quoteName('#__languages') . ' AS l ON l.lang_code = a.language');
|
||||
|
||||
// Join over the users for the checked out user.
|
||||
$query->select('uc.name AS editor')->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
|
||||
|
||||
// Join over the asset groups.
|
||||
$query->select('ag.title AS access_level')->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
|
||||
|
||||
// Join over the users for the author.
|
||||
$query->select('ua.name AS author_name')->join('LEFT', '#__users AS ua ON ua.id = a.created_by');
|
||||
|
||||
// Filter by context
|
||||
if ($context = $this->getState('filter.context', 'com_fields')) {
|
||||
$query->where($db->quoteName('a.context') . ' = :context')
|
||||
->bind(':context', $context);
|
||||
}
|
||||
|
||||
// Filter by access level.
|
||||
if ($access = $this->getState('filter.access')) {
|
||||
if (\is_array($access)) {
|
||||
$access = ArrayHelper::toInteger($access);
|
||||
$query->whereIn($db->quoteName('a.access'), $access);
|
||||
} else {
|
||||
$access = (int) $access;
|
||||
$query->where($db->quoteName('a.access') . ' = :access')
|
||||
->bind(':access', $access, ParameterType::INTEGER);
|
||||
}
|
||||
}
|
||||
|
||||
// Implement View Level Access
|
||||
if (!$user->authorise('core.admin')) {
|
||||
$groups = $user->getAuthorisedViewLevels();
|
||||
$query->whereIn($db->quoteName('a.access'), $groups);
|
||||
}
|
||||
|
||||
// Filter by published state
|
||||
$state = $this->getState('filter.state');
|
||||
|
||||
if (is_numeric($state)) {
|
||||
$state = (int) $state;
|
||||
$query->where($db->quoteName('a.state') . ' = :state')
|
||||
->bind(':state', $state, ParameterType::INTEGER);
|
||||
} elseif (!$state) {
|
||||
$query->whereIn($db->quoteName('a.state'), [0, 1]);
|
||||
}
|
||||
|
||||
// Filter by search in title
|
||||
$search = $this->getState('filter.search');
|
||||
|
||||
if (!empty($search)) {
|
||||
if (stripos($search, 'id:') === 0) {
|
||||
$search = (int) substr($search, 3);
|
||||
$query->where($db->quoteName('a.id') . ' = :search')
|
||||
->bind(':search', $search, ParameterType::INTEGER);
|
||||
} else {
|
||||
$search = '%' . str_replace(' ', '%', trim($search)) . '%';
|
||||
$query->where($db->quoteName('a.title') . ' LIKE :search')
|
||||
->bind(':search', $search);
|
||||
}
|
||||
}
|
||||
|
||||
// Filter on the language.
|
||||
if ($language = $this->getState('filter.language')) {
|
||||
$language = (array) $language;
|
||||
|
||||
$query->whereIn($db->quoteName('a.language'), $language, ParameterType::STRING);
|
||||
}
|
||||
|
||||
// Add the list ordering clause
|
||||
$listOrdering = $this->getState('list.ordering', 'a.ordering');
|
||||
$listDirn = $db->escape($this->getState('list.direction', 'ASC'));
|
||||
|
||||
$query->order($db->escape($listOrdering) . ' ' . $listDirn);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of objects from the results of database query.
|
||||
*
|
||||
* @param string $query The query.
|
||||
* @param integer $limitstart Offset.
|
||||
* @param integer $limit The number of records.
|
||||
*
|
||||
* @return array An array of results.
|
||||
*
|
||||
* @since 3.8.7
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
protected function _getList($query, $limitstart = 0, $limit = 0)
|
||||
{
|
||||
$result = parent::_getList($query, $limitstart, $limit);
|
||||
|
||||
if (\is_array($result)) {
|
||||
foreach ($result as $group) {
|
||||
$group->params = new Registry($group->params);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user