From d96a86f47ffd97faf9467551a50add3887ecb582 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Mon, 6 Aug 2018 16:26:01 +0200 Subject: [PATCH 1/4] Change com_fields to services --- .../com_content/Model/ArticleModel.php | 11 +- .../Controller/DisplayController.php | 1 + .../com_fields/Controller/FieldController.php | 1 + .../Controller/FieldsController.php | 1 + .../com_fields/Controller/GroupController.php | 1 + .../Controller/GroupsController.php | 1 + .../Dispatcher.php} | 26 +- .../com_fields/Field/FieldcontextsField.php | 1 + .../com_fields/Field/FieldgroupsField.php | 1 + .../com_fields/Field/Modal/FieldField.php | 1 + .../com_fields/Field/SectionField.php | 1 + .../components/com_fields/Field/TypeField.php | 1 + .../com_fields/Helper/FieldsHelper.php | 752 ++++++++++++++++++ .../com_fields/Model/FieldModel.php | 1 + .../com_fields/Model/FieldsModel.php | 1 + .../com_fields/Model/GroupModel.php | 1 + .../com_fields/Model/GroupsModel.php | 1 + .../com_fields/Plugin/FieldsListPlugin.php | 1 + .../com_fields/Plugin/FieldsPlugin.php | 1 + .../com_fields/Table/FieldTable.php | 1 + .../com_fields/Table/GroupTable.php | 1 + .../com_fields/View/Field/HtmlView.php | 1 + .../com_fields/View/Fields/HtmlView.php | 1 + .../com_fields/View/Group/HtmlView.php | 1 + .../com_fields/View/Groups/HtmlView.php | 1 + .../components/com_fields/helpers/fields.php | 735 +---------------- .../com_fields/services/provider.php | 54 ++ .../com_contact/layouts/fields/render.php | 4 +- .../Controller/DisplayController.php | 1 + .../com_fields/Dispatcher/Dispatcher.php | 49 ++ components/com_fields/dispatcher.php | 43 - components/com_fields/fields.php | 33 - .../com_fields/layouts/field/render.php | 4 +- .../com_fields/layouts/fields/render.php | 5 +- plugins/content/fields/fields.php | 4 +- plugins/editors-xtd/fields/fields.php | 4 +- plugins/system/fields/fields.php | 3 +- 37 files changed, 902 insertions(+), 848 deletions(-) rename administrator/components/com_fields/{dispatcher.php => Dispatcher/Dispatcher.php} (51%) create mode 100644 administrator/components/com_fields/Helper/FieldsHelper.php create mode 100644 administrator/components/com_fields/services/provider.php create mode 100644 components/com_fields/Dispatcher/Dispatcher.php delete mode 100644 components/com_fields/dispatcher.php delete mode 100644 components/com_fields/fields.php diff --git a/administrator/components/com_content/Model/ArticleModel.php b/administrator/components/com_content/Model/ArticleModel.php index fa18852b62a3c..e3baf37352d8c 100644 --- a/administrator/components/com_content/Model/ArticleModel.php +++ b/administrator/components/com_content/Model/ArticleModel.php @@ -12,6 +12,7 @@ defined('_JEXEC') or die; use Joomla\CMS\Dispatcher\DispatcherFactory; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; use Joomla\Registry\Registry; use Joomla\Utilities\ArrayHelper; use Joomla\CMS\MVC\Model\AdminModel; @@ -79,9 +80,6 @@ protected function batchCopy($value, $pks, $contexts) PluginHelper::importPlugin('system'); - // Register FieldsHelper - \JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); - // Parent exists so we let's proceed while (!empty($pks)) { @@ -108,7 +106,7 @@ protected function batchCopy($value, $pks, $contexts) } } - $fields = \FieldsHelper::getFields('com_content.article', $this->table, true); + $fields = FieldsHelper::getFields('com_content.article', $this->table, true); $fieldsData = array(); if (!empty($fields)) @@ -219,9 +217,6 @@ protected function batchMove($value, $pks, $contexts) PluginHelper::importPlugin('system'); - // Register FieldsHelper - \JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); - // Parent exists so we proceed foreach ($pks as $pk) { @@ -250,7 +245,7 @@ protected function batchMove($value, $pks, $contexts) } } - $fields = \FieldsHelper::getFields('com_content.article', $this->table, true); + $fields = FieldsHelper::getFields('com_content.article', $this->table, true); $fieldsData = array(); diff --git a/administrator/components/com_fields/Controller/DisplayController.php b/administrator/components/com_fields/Controller/DisplayController.php index 4a93e44cc59ac..0ac67aea0e1bc 100644 --- a/administrator/components/com_fields/Controller/DisplayController.php +++ b/administrator/components/com_fields/Controller/DisplayController.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Controller; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Controller/FieldController.php b/administrator/components/com_fields/Controller/FieldController.php index cdd32abbb4fa9..7fbde0e9ade3c 100644 --- a/administrator/components/com_fields/Controller/FieldController.php +++ b/administrator/components/com_fields/Controller/FieldController.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Controller; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Controller/FieldsController.php b/administrator/components/com_fields/Controller/FieldsController.php index 99eb350f5d724..484d5cf58cc6b 100644 --- a/administrator/components/com_fields/Controller/FieldsController.php +++ b/administrator/components/com_fields/Controller/FieldsController.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Controller; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Controller/GroupController.php b/administrator/components/com_fields/Controller/GroupController.php index b8e5f6478a34e..b06032a72e2fd 100644 --- a/administrator/components/com_fields/Controller/GroupController.php +++ b/administrator/components/com_fields/Controller/GroupController.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Controller; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Controller/GroupsController.php b/administrator/components/com_fields/Controller/GroupsController.php index c55b45ded3f85..bc07f2fbc32cb 100644 --- a/administrator/components/com_fields/Controller/GroupsController.php +++ b/administrator/components/com_fields/Controller/GroupsController.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Controller; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/dispatcher.php b/administrator/components/com_fields/Dispatcher/Dispatcher.php similarity index 51% rename from administrator/components/com_fields/dispatcher.php rename to administrator/components/com_fields/Dispatcher/Dispatcher.php index a0e947cb4b8ee..fd9eda03b641c 100644 --- a/administrator/components/com_fields/dispatcher.php +++ b/administrator/components/com_fields/Dispatcher/Dispatcher.php @@ -7,27 +7,20 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt */ +namespace Joomla\Component\Fields\Administrator\Dispatcher; + defined('_JEXEC') or die; -use Joomla\CMS\Dispatcher\Dispatcher; -use Joomla\CMS\Factory; +use Joomla\CMS\Access\Exception\NotAllowed; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; /** * Dispatcher class for com_content * * @since 4.0.0 */ -class FieldsDispatcher extends Dispatcher +class Dispatcher extends \Joomla\CMS\Dispatcher\Dispatcher { - /** - * The extension namespace - * - * @var string - * - * @since 4.0.0 - */ - protected $namespace = 'Joomla\\Component\\Fields'; - /** * Method to check component access permission * @@ -37,13 +30,10 @@ class FieldsDispatcher extends Dispatcher */ protected function checkAccess() { - JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); - - $app = Factory::getApplication(); - $context = $app->getUserStateFromRequest( + $context = $this->app->getUserStateFromRequest( 'com_fields.groups.context', 'context', - $app->getUserStateFromRequest('com_fields.fields.context', 'context', 'com_content.article', 'CMD'), + $this->app->getUserStateFromRequest('com_fields.fields.context', 'context', 'com_content.article', 'CMD'), 'CMD' ); @@ -51,7 +41,7 @@ protected function checkAccess() if (!$parts || !$this->app->getIdentity()->authorise('core.manage', $parts[0])) { - throw new \Joomla\CMS\Access\Exception\Notallowed($this->app->getLanguage()->_('JERROR_ALERTNOAUTHOR'), 403); + throw new Notallowed($this->app->getLanguage()->_('JERROR_ALERTNOAUTHOR'), 403); } } } diff --git a/administrator/components/com_fields/Field/FieldcontextsField.php b/administrator/components/com_fields/Field/FieldcontextsField.php index 492b1759a935a..23e1c9b58d29b 100644 --- a/administrator/components/com_fields/Field/FieldcontextsField.php +++ b/administrator/components/com_fields/Field/FieldcontextsField.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Field; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Field/FieldgroupsField.php b/administrator/components/com_fields/Field/FieldgroupsField.php index f123a19bbb2b4..a7c26547416af 100644 --- a/administrator/components/com_fields/Field/FieldgroupsField.php +++ b/administrator/components/com_fields/Field/FieldgroupsField.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Field; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Field/Modal/FieldField.php b/administrator/components/com_fields/Field/Modal/FieldField.php index 2e6a7f3d82d8b..c79202f8627fc 100644 --- a/administrator/components/com_fields/Field/Modal/FieldField.php +++ b/administrator/components/com_fields/Field/Modal/FieldField.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Field\Modal; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Field/SectionField.php b/administrator/components/com_fields/Field/SectionField.php index f5a05c5f131b5..1338aafcfbb87 100644 --- a/administrator/components/com_fields/Field/SectionField.php +++ b/administrator/components/com_fields/Field/SectionField.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Field; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Field/TypeField.php b/administrator/components/com_fields/Field/TypeField.php index a8edde01ec753..b60569584a2f1 100644 --- a/administrator/components/com_fields/Field/TypeField.php +++ b/administrator/components/com_fields/Field/TypeField.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Field; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Helper/FieldsHelper.php b/administrator/components/com_fields/Helper/FieldsHelper.php new file mode 100644 index 0000000000000..a9cfec6ee17c9 --- /dev/null +++ b/administrator/components/com_fields/Helper/FieldsHelper.php @@ -0,0 +1,752 @@ +bootComponent($parts[0]); + + if ($component instanceof FieldsServiceInterface) + { + $newSection = $component->validateSection($parts[1], $item); + } + + if ($newSection) + { + $parts[1] = $newSection; + } + + return $parts; + } + + /** + * Returns the fields for the given context. + * If the item is an object the returned fields do have an additional field + * "value" which represents the value for the given item. If the item has an + * assigned_cat_ids field, then additionally fields which belong to that + * category will be returned. + * Should the value being prepared to be shown in an HTML context then + * prepareValue must be set to true. No further escaping needs to be done. + * The values of the fields can be overridden by an associative array where the keys + * have to be a name and its corresponding value. + * + * @param string $context The context of the content passed to the helper + * @param stdClass $item item + * @param boolean $prepareValue prepareValue + * @param array $valuesToOverride The values to override + * + * @return array + * + * @since 3.7.0 + */ + public static function getFields($context, $item = null, $prepareValue = false, array $valuesToOverride = null) + { + if (self::$fieldsCache === null) + { + // Load the model + self::$fieldsCache = Factory::getApplication()->bootComponent('com_fields')->createMVCFactory(Factory::getApplication())->createModel('Fields', 'Administrator', array('ignore_request' => true)); + + self::$fieldsCache->setState('filter.state', 1); + self::$fieldsCache->setState('list.limit', 0); + } + + if (is_array($item)) + { + $item = (object) $item; + } + + if (Multilanguage::isEnabled() && isset($item->language) && $item->language != '*') + { + self::$fieldsCache->setState('filter.language', array('*', $item->language)); + } + + self::$fieldsCache->setState('filter.context', $context); + + /* + * If item has assigned_cat_ids parameter display only fields which + * belong to the category + */ + if ($item && (isset($item->catid) || isset($item->fieldscatid))) + { + $assignedCatIds = $item->catid ?? $item->fieldscatid; + + if (!is_array($assignedCatIds)) + { + $assignedCatIds = explode(',', $assignedCatIds); + } + + // Fields without any category assigned should show as well + $assignedCatIds[] = 0; + + self::$fieldsCache->setState('filter.assigned_cat_ids', $assignedCatIds); + } + + $fields = self::$fieldsCache->getItems(); + + if ($fields === false) + { + return array(); + } + + if ($item && isset($item->id)) + { + if (self::$fieldCache === null) + { + self::$fieldCache = Factory::getApplication()->bootComponent('com_fields')->createMVCFactory(Factory::getApplication())->createModel('Field', 'Administrator', array('ignore_request' => true)); + } + + $fieldIds = array_map( + function ($f) + { + return $f->id; + }, + $fields + ); + + $fieldValues = self::$fieldCache->getFieldValues($fieldIds, $item->id); + + $new = array(); + + foreach ($fields as $key => $original) + { + /* + * Doing a clone, otherwise fields for different items will + * always reference to the same object + */ + $field = clone $original; + + if ($valuesToOverride && array_key_exists($field->name, $valuesToOverride)) + { + $field->value = $valuesToOverride[$field->name]; + } + elseif ($valuesToOverride && array_key_exists($field->id, $valuesToOverride)) + { + $field->value = $valuesToOverride[$field->id]; + } + elseif (array_key_exists($field->id, $fieldValues)) + { + $field->value = $fieldValues[$field->id]; + } + + if (!isset($field->value) || $field->value === '') + { + $field->value = $field->default_value; + } + + $field->rawvalue = $field->value; + + if ($prepareValue) + { + PluginHelper::importPlugin('fields'); + + /* + * On before field prepare + * Event allow plugins to modfify the output of the field before it is prepared + */ + Factory::getApplication()->triggerEvent('onCustomFieldsBeforePrepareField', array($context, $item, &$field)); + + // Gathering the value for the field + $value = Factory::getApplication()->triggerEvent('onCustomFieldsPrepareField', array($context, $item, &$field)); + + if (is_array($value)) + { + $value = implode(' ', $value); + } + + /* + * On after field render + * Event allows plugins to modify the output of the prepared field + */ + Factory::getApplication()->triggerEvent('onCustomFieldsAfterPrepareField', array($context, $item, $field, &$value)); + + // Assign the value + $field->value = $value; + } + + $new[$key] = $field; + } + + $fields = $new; + } + + return $fields; + } + + /** + * Renders the layout file and data on the context and does a fall back to + * Fields afterwards. + * + * @param string $context The context of the content passed to the helper + * @param string $layoutFile layoutFile + * @param array $displayData displayData + * + * @return NULL|string + * + * @since 3.7.0 + */ + public static function render($context, $layoutFile, $displayData) + { + $value = ''; + + /* + * Because the layout refreshes the paths before the render function is + * called, so there is no way to load the layout overrides in the order + * template -> context -> fields. + * If there is no override in the context then we need to call the + * layout from Fields. + */ + if ($parts = self::extract($context)) + { + // Trying to render the layout on the component from the context + $value = LayoutHelper::render($layoutFile, $displayData, null, array('component' => $parts[0], 'client' => 0)); + } + + if ($value == '') + { + // Trying to render the layout on Fields itself + $value = LayoutHelper::render($layoutFile, $displayData, null, array('component' => 'com_fields','client' => 0)); + } + + return $value; + } + + /** + * PrepareForm + * + * @param string $context The context of the content passed to the helper + * @param Form $form form + * @param object $data data. + * + * @return boolean + * + * @since 3.7.0 + */ + public static function prepareForm($context, Form $form, $data) + { + // Extracting the component and section + $parts = self::extract($context); + + if (! $parts) + { + return true; + } + + $context = $parts[0] . '.' . $parts[1]; + + // When no fields available return here + $fields = self::getFields($parts[0] . '.' . $parts[1], new CMSObject); + + if (! $fields) + { + return true; + } + + $component = $parts[0]; + $section = $parts[1]; + + $assignedCatids = $data->catid ?? $data->fieldscatid ?? $form->getValue('catid'); + + // Account for case that a submitted form has a multi-value category id field (e.g. a filtering form), just use the first category + $assignedCatids = is_array($assignedCatids) + ? (int) reset($assignedCatids) + : (int) $assignedCatids; + + if (!$assignedCatids && $formField = $form->getField('catid')) + { + $assignedCatids = $formField->getAttribute('default', null); + + // Choose the first category available + $xml = new \DOMDocument; + $xml->loadHTML($formField->__get('input')); + $options = $xml->getElementsByTagName('option'); + + if (!$assignedCatids && $firstChoice = $options->item(0)) + { + $assignedCatids = $firstChoice->getAttribute('value'); + } + + $data->fieldscatid = $assignedCatids; + } + + /* + * If there is a catid field we need to reload the page when the catid + * is changed + */ + if ($form->getField('catid') && $parts[0] != 'com_fields') + { + /* + * Setting the onchange event to reload the page when the category + * has changed + */ + $form->setFieldAttribute('catid', 'onchange', 'categoryHasChanged(this);'); + + // Preload spindle-wheel when we need to submit form due to category selector changed + Factory::getDocument()->addScriptDeclaration(" + function categoryHasChanged(element) { + var cat = jQuery(element); + if (cat.val() == '" . $assignedCatids . "')return; + Joomla.loadingLayer('show'); + jQuery('input[name=task]').val('" . $section . ".reload'); + element.form.submit(); + } + jQuery( document ).ready(function() { + Joomla.loadingLayer('load'); + var formControl = '#" . $form->getFormControl() . "_catid'; + if (!jQuery(formControl).val() != '" . $assignedCatids . "'){jQuery(formControl).val('" . $assignedCatids . "');} + });" + ); + } + + // Getting the fields + $fields = self::getFields($parts[0] . '.' . $parts[1], $data); + + if (!$fields) + { + return true; + } + + $fieldTypes = self::getFieldTypes(); + + // Creating the dom + $xml = new \DOMDocument('1.0', 'UTF-8'); + $fieldsNode = $xml->appendChild(new \DOMElement('form'))->appendChild(new \DOMElement('fields')); + $fieldsNode->setAttribute('name', 'com_fields'); + + // Organizing the fields according to their group + $fieldsPerGroup = array(0 => array()); + + foreach ($fields as $field) + { + if (!array_key_exists($field->type, $fieldTypes)) + { + // Field type is not available + continue; + } + + if (!array_key_exists($field->group_id, $fieldsPerGroup)) + { + $fieldsPerGroup[$field->group_id] = array(); + } + + if ($path = $fieldTypes[$field->type]['path']) + { + // Add the lookup path for the field + FormHelper::addFieldPath($path); + } + + if ($path = $fieldTypes[$field->type]['rules']) + { + // Add the lookup path for the rule + FormHelper::addRulePath($path); + } + + $fieldsPerGroup[$field->group_id][] = $field; + } + + $model = Factory::getApplication()->bootComponent('com_fields')->createMVCFactory(Factory::getApplication())->createModel('Groups', 'Administrator', array('ignore_request' => true)); + $model->setState('filter.context', $context); + + /** + * $model->getItems() would only return existing groups, but we also + * have the 'default' group with id 0 which is not in the database, + * so we create it virtually here. + */ + $defaultGroup = new \stdClass; + $defaultGroup->id = 0; + $defaultGroup->title = ''; + $defaultGroup->description = ''; + $iterateGroups = array_merge(array($defaultGroup), $model->getItems()); + + // Looping through the groups + foreach ($iterateGroups as $group) + { + if (empty($fieldsPerGroup[$group->id])) + { + continue; + } + + // Defining the field set + /** @var DOMElement $fieldset */ + $fieldset = $fieldsNode->appendChild(new \DOMElement('fieldset')); + $fieldset->setAttribute('name', 'fields-' . $group->id); + $fieldset->setAttribute('addfieldpath', '/administrator/components/' . $component . '/models/fields'); + $fieldset->setAttribute('addrulepath', '/administrator/components/' . $component . '/models/rules'); + + $label = $group->title; + $description = $group->description; + + if (!$label) + { + $key = strtoupper($component . '_FIELDS_' . $section . '_LABEL'); + + if (!Factory::getLanguage()->hasKey($key)) + { + $key = 'JGLOBAL_FIELDS'; + } + + $label = $key; + } + + if (!$description) + { + $key = strtoupper($component . '_FIELDS_' . $section . '_DESC'); + + if (Factory::getLanguage()->hasKey($key)) + { + $description = $key; + } + } + + $fieldset->setAttribute('label', $label); + $fieldset->setAttribute('description', strip_tags($description)); + + // Looping through the fields for that context + foreach ($fieldsPerGroup[$group->id] as $field) + { + try + { + Factory::getApplication()->triggerEvent('onCustomFieldsPrepareDom', array($field, $fieldset, $form)); + + /* + * If the field belongs to an assigned_cat_id but the assigned_cat_ids in the data + * is not known, set the required flag to false on any circumstance. + */ + if (!$assignedCatids && !empty($field->assigned_cat_ids) && $form->getField($field->name)) + { + $form->setFieldAttribute($field->name, 'required', 'false'); + } + } + catch (\Exception $e) + { + Factory::getApplication()->enqueueMessage($e->getMessage(), 'error'); + } + } + + // When the field set is empty, then remove it + if (!$fieldset->hasChildNodes()) + { + $fieldsNode->removeChild($fieldset); + } + } + + // Loading the XML fields string into the form + $form->load($xml->saveXML()); + + $model = Factory::getApplication()->bootComponent('com_fields')->createMVCFactory(Factory::getApplication())->createModel('Field', 'Administrator', array('ignore_request' => true)); + + if ((!isset($data->id) || !$data->id) && Factory::getApplication()->input->getCmd('controller') == 'modules' + && Factory::getApplication()->isClient('site')) + { + // Modules on front end editing don't have data and an id set + $data->id = Factory::getApplication()->input->getInt('id'); + } + + // Looping through the fields again to set the value + if (!isset($data->id) || !$data->id) + { + return true; + } + + foreach ($fields as $field) + { + $value = $model->getFieldValue($field->id, $data->id); + + if ($value === null) + { + continue; + } + + if (!is_array($value) && $value !== '') + { + // Function getField doesn't cache the fields, so we try to do it only when necessary + $formField = $form->getField($field->name, 'com_fields'); + + if ($formField && $formField->forceMultiple) + { + $value = (array) $value; + } + } + + // Setting the value on the field + $form->setValue($field->name, 'com_fields', $value); + } + + return true; + } + + /** + * Return a boolean if the actual logged in user can edit the given field value. + * + * @param \stdClass $field The field + * + * @return boolean + * + * @since 3.7.0 + */ + public static function canEditFieldValue($field) + { + $parts = self::extract($field->context); + + return Factory::getUser()->authorise('core.edit.value', $parts[0] . '.field.' . (int) $field->id); + } + + /** + * Adds Count Items for Category Manager. + * + * @param \stdClass[] &$items The field category objects + * + * @return \stdClass[] + * + * @since 3.7.0 + */ + public static function countItems(&$items) + { + $db = Factory::getDbo(); + + foreach ($items as $item) + { + $item->count_trashed = 0; + $item->count_archived = 0; + $item->count_unpublished = 0; + $item->count_published = 0; + + $query = $db->getQuery(true); + $query->select('state, count(1) AS count') + ->from($db->quoteName('#__fields')) + ->where('group_id = ' . (int) $item->id) + ->group('state'); + $db->setQuery($query); + + $fields = $db->loadObjectList(); + + $states = array( + '-2' => 'count_trashed', + '0' => 'count_unpublished', + '1' => 'count_published', + '2' => 'count_archived', + ); + + foreach ($fields as $field) + { + $property = $states[$field->state]; + $item->$property = $field->count; + } + } + + return $items; + } + + /** + * Gets assigned categories titles for a field + * + * @param \stdClass[] $fieldId The field ID + * + * @return array Array with the assigned categories + * + * @since 3.7.0 + */ + public static function getAssignedCategoriesTitles($fieldId) + { + $fieldId = (int) $fieldId; + + if (!$fieldId) + { + return array(); + } + + $db = Factory::getDbo(); + $query = $db->getQuery(true); + + $query->select($db->quoteName('c.title')) + ->from($db->quoteName('#__fields_categories', 'a')) + ->join('LEFT', $db->quoteName('#__categories', 'c') . ' ON a.category_id = c.id') + ->where('field_id = ' . $fieldId); + + $db->setQuery($query); + + return $db->loadColumn(); + } + + /** + * Gets the fields system plugin extension id. + * + * @return int The fields system plugin extension id. + * + * @since 3.7.0 + */ + public static function getFieldsPluginId() + { + $db = Factory::getDbo(); + $query = $db->getQuery(true) + ->select($db->quoteName('extension_id')) + ->from($db->quoteName('#__extensions')) + ->where($db->quoteName('folder') . ' = ' . $db->quote('system')) + ->where($db->quoteName('element') . ' = ' . $db->quote('fields')); + $db->setQuery($query); + + try + { + $result = (int) $db->loadResult(); + } + catch (\RuntimeException $e) + { + Factory::getApplication()->enqueueMessage($e->getMessage(), 'error'); + $result = 0; + } + + return $result; + } + + /** + * Configure the Linkbar. + * + * @param string $context The context the fields are used for + * @param string $vName The view currently active + * + * @return void + * + * @since 3.7.0 + */ + public static function addSubmenu($context, $vName) + { + $parts = self::extract($context); + + if (!$parts) + { + return; + } + + $component = $parts[0]; + + // Avoid nonsense situation. + if ($component == 'com_fields') + { + return; + } + + // Try to find the component helper. + $eName = str_replace('com_', '', $component); + $file = Path::clean(JPATH_ADMINISTRATOR . '/components/' . $component . '/helpers/' . $eName . '.php'); + + if (!file_exists($file)) + { + return; + } + + require_once $file; + + $cName = ucfirst($eName) . 'Helper'; + + if (class_exists($cName) && is_callable(array($cName, 'addSubmenu'))) + { + $lang = Factory::getLanguage(); + $lang->load($component, JPATH_ADMINISTRATOR) + || $lang->load($component, JPATH_ADMINISTRATOR . '/components/' . $component); + + $cName::addSubmenu('fields.' . $vName); + } + } + + /** + * Loads the fields plugins and returns an array of field types from the plugins. + * + * The returned array contains arrays with the following keys: + * - label: The label of the field + * - type: The type of the field + * - path: The path of the folder where the field can be found + * + * @return array + * + * @since 3.7.0 + */ + public static function getFieldTypes() + { + PluginHelper::importPlugin('fields'); + $eventData = Factory::getApplication()->triggerEvent('onCustomFieldsGetTypes'); + + $data = array(); + + foreach ($eventData as $fields) + { + foreach ($fields as $fieldDescription) + { + if (!array_key_exists('path', $fieldDescription)) + { + $fieldDescription['path'] = null; + } + + if (!array_key_exists('rules', $fieldDescription)) + { + $fieldDescription['rules'] = null; + } + + $data[$fieldDescription['type']] = $fieldDescription; + } + } + + return $data; + } + + /** + * Clears the internal cache for the custom fields. + * + * @return void + * + * @since 3.8.0 + */ + public static function clearFieldsCache() + { + self::$fieldCache = null; + self::$fieldsCache = null; + } +} diff --git a/administrator/components/com_fields/Model/FieldModel.php b/administrator/components/com_fields/Model/FieldModel.php index efaec5c376774..bcd0aeed6f406 100644 --- a/administrator/components/com_fields/Model/FieldModel.php +++ b/administrator/components/com_fields/Model/FieldModel.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Model; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Model/FieldsModel.php b/administrator/components/com_fields/Model/FieldsModel.php index 1e4e66300e31e..99137437109e9 100644 --- a/administrator/components/com_fields/Model/FieldsModel.php +++ b/administrator/components/com_fields/Model/FieldsModel.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Model; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Model/GroupModel.php b/administrator/components/com_fields/Model/GroupModel.php index ac7a59ce432f2..bfb0adaea6f96 100644 --- a/administrator/components/com_fields/Model/GroupModel.php +++ b/administrator/components/com_fields/Model/GroupModel.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Model; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Model/GroupsModel.php b/administrator/components/com_fields/Model/GroupsModel.php index 4c7caac6e3691..44eb54a9fc422 100644 --- a/administrator/components/com_fields/Model/GroupsModel.php +++ b/administrator/components/com_fields/Model/GroupsModel.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Model; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Plugin/FieldsListPlugin.php b/administrator/components/com_fields/Plugin/FieldsListPlugin.php index 82b11632c719b..ee007e14cca32 100644 --- a/administrator/components/com_fields/Plugin/FieldsListPlugin.php +++ b/administrator/components/com_fields/Plugin/FieldsListPlugin.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Plugin; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Plugin/FieldsPlugin.php b/administrator/components/com_fields/Plugin/FieldsPlugin.php index e666653010ee5..74258b8dfa9f9 100644 --- a/administrator/components/com_fields/Plugin/FieldsPlugin.php +++ b/administrator/components/com_fields/Plugin/FieldsPlugin.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Plugin; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Table/FieldTable.php b/administrator/components/com_fields/Table/FieldTable.php index f733cb8fdbab8..d14fd798cd397 100644 --- a/administrator/components/com_fields/Table/FieldTable.php +++ b/administrator/components/com_fields/Table/FieldTable.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Table; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/Table/GroupTable.php b/administrator/components/com_fields/Table/GroupTable.php index ff602189979e5..992ec385613ac 100644 --- a/administrator/components/com_fields/Table/GroupTable.php +++ b/administrator/components/com_fields/Table/GroupTable.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\Table; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/View/Field/HtmlView.php b/administrator/components/com_fields/View/Field/HtmlView.php index bb999d17e4d8e..873327e9b4cc3 100644 --- a/administrator/components/com_fields/View/Field/HtmlView.php +++ b/administrator/components/com_fields/View/Field/HtmlView.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\View\Field; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/View/Fields/HtmlView.php b/administrator/components/com_fields/View/Fields/HtmlView.php index c8a75656b6fcd..8c7098257f93a 100644 --- a/administrator/components/com_fields/View/Fields/HtmlView.php +++ b/administrator/components/com_fields/View/Fields/HtmlView.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\View\Fields; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/View/Group/HtmlView.php b/administrator/components/com_fields/View/Group/HtmlView.php index 1a0630466878b..ba66d8e45eff8 100644 --- a/administrator/components/com_fields/View/Group/HtmlView.php +++ b/administrator/components/com_fields/View/Group/HtmlView.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\View\Group; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/View/Groups/HtmlView.php b/administrator/components/com_fields/View/Groups/HtmlView.php index 3be8645316123..7fa3e34c0f875 100644 --- a/administrator/components/com_fields/View/Groups/HtmlView.php +++ b/administrator/components/com_fields/View/Groups/HtmlView.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Administrator\View\Groups; defined('_JEXEC') or die; diff --git a/administrator/components/com_fields/helpers/fields.php b/administrator/components/com_fields/helpers/fields.php index 69f48d9c6045e..6443a2bf50673 100644 --- a/administrator/components/com_fields/helpers/fields.php +++ b/administrator/components/com_fields/helpers/fields.php @@ -8,742 +8,13 @@ */ defined('_JEXEC') or die; -use Joomla\CMS\Factory; -use Joomla\CMS\Fields\FieldsServiceInterface; -use Joomla\CMS\Language\Multilanguage; -use Joomla\CMS\Layout\LayoutHelper; -use Joomla\CMS\Plugin\PluginHelper; -use Joomla\CMS\Filesystem\Path; -use Joomla\CMS\Filesystem\Folder; - -JLoader::register('Folder', JPATH_LIBRARIES . '/joomla/filesystem/folder.php'); - /** * FieldsHelper * * @since 3.7.0 + * + * @deprecated 5.0 Use \Joomla\Component\Fields\Administrator\Helper\FieldsHelper instead */ -class FieldsHelper +class FieldsHelper extends \Joomla\Component\Fields\Administrator\Helper\FieldsHelper { - private static $fieldsCache = null; - - private static $fieldCache = null; - - /** - * Extracts the component and section from the context string which has to - * be in the format component.context. - * - * @param string $contextString contextString - * @param object $item optional item object - * - * @return array|null - * - * @since 3.7.0 - */ - public static function extract($contextString, $item = null) - { - $parts = explode('.', $contextString, 2); - - if (count($parts) < 2) - { - return null; - } - - $newSection = ''; - - $component = Factory::getApplication()->bootComponent($parts[0]); - - if ($component instanceof FieldsServiceInterface) - { - $newSection = $component->validateSection($parts[1], $item); - } - - if ($newSection) - { - $parts[1] = $newSection; - } - - return $parts; - } - - /** - * Returns the fields for the given context. - * If the item is an object the returned fields do have an additional field - * "value" which represents the value for the given item. If the item has an - * assigned_cat_ids field, then additionally fields which belong to that - * category will be returned. - * Should the value being prepared to be shown in an HTML context then - * prepareValue must be set to true. No further escaping needs to be done. - * The values of the fields can be overridden by an associative array where the keys - * have to be a name and its corresponding value. - * - * @param string $context The context of the content passed to the helper - * @param stdClass $item item - * @param boolean $prepareValue prepareValue - * @param array $valuesToOverride The values to override - * - * @return array - * - * @since 3.7.0 - */ - public static function getFields($context, $item = null, $prepareValue = false, array $valuesToOverride = null) - { - if (self::$fieldsCache === null) - { - // Load the model - self::$fieldsCache = new \Joomla\Component\Fields\Administrator\Model\FieldsModel(array('ignore_request' => true)); - - self::$fieldsCache->setState('filter.state', 1); - self::$fieldsCache->setState('list.limit', 0); - } - - if (is_array($item)) - { - $item = (object) $item; - } - - if (Multilanguage::isEnabled() && isset($item->language) && $item->language != '*') - { - self::$fieldsCache->setState('filter.language', array('*', $item->language)); - } - - self::$fieldsCache->setState('filter.context', $context); - - /* - * If item has assigned_cat_ids parameter display only fields which - * belong to the category - */ - if ($item && (isset($item->catid) || isset($item->fieldscatid))) - { - $assignedCatIds = $item->catid ?? $item->fieldscatid; - - if (!is_array($assignedCatIds)) - { - $assignedCatIds = explode(',', $assignedCatIds); - } - - // Fields without any category assigned should show as well - $assignedCatIds[] = 0; - - self::$fieldsCache->setState('filter.assigned_cat_ids', $assignedCatIds); - } - - $fields = self::$fieldsCache->getItems(); - - if ($fields === false) - { - return array(); - } - - if ($item && isset($item->id)) - { - if (self::$fieldCache === null) - { - self::$fieldCache = new \Joomla\Component\Fields\Administrator\Model\FieldModel(array('ignore_request' => true)); - } - - $fieldIds = array_map( - function ($f) - { - return $f->id; - }, - $fields - ); - - $fieldValues = self::$fieldCache->getFieldValues($fieldIds, $item->id); - - $new = array(); - - foreach ($fields as $key => $original) - { - /* - * Doing a clone, otherwise fields for different items will - * always reference to the same object - */ - $field = clone $original; - - if ($valuesToOverride && array_key_exists($field->name, $valuesToOverride)) - { - $field->value = $valuesToOverride[$field->name]; - } - elseif ($valuesToOverride && array_key_exists($field->id, $valuesToOverride)) - { - $field->value = $valuesToOverride[$field->id]; - } - elseif (array_key_exists($field->id, $fieldValues)) - { - $field->value = $fieldValues[$field->id]; - } - - if (!isset($field->value) || $field->value === '') - { - $field->value = $field->default_value; - } - - $field->rawvalue = $field->value; - - if ($prepareValue) - { - PluginHelper::importPlugin('fields'); - - /* - * On before field prepare - * Event allow plugins to modfify the output of the field before it is prepared - */ - Factory::getApplication()->triggerEvent('onCustomFieldsBeforePrepareField', array($context, $item, &$field)); - - // Gathering the value for the field - $value = Factory::getApplication()->triggerEvent('onCustomFieldsPrepareField', array($context, $item, &$field)); - - if (is_array($value)) - { - $value = implode(' ', $value); - } - - /* - * On after field render - * Event allows plugins to modify the output of the prepared field - */ - Factory::getApplication()->triggerEvent('onCustomFieldsAfterPrepareField', array($context, $item, $field, &$value)); - - // Assign the value - $field->value = $value; - } - - $new[$key] = $field; - } - - $fields = $new; - } - - return $fields; - } - - /** - * Renders the layout file and data on the context and does a fall back to - * Fields afterwards. - * - * @param string $context The context of the content passed to the helper - * @param string $layoutFile layoutFile - * @param array $displayData displayData - * - * @return NULL|string - * - * @since 3.7.0 - */ - public static function render($context, $layoutFile, $displayData) - { - $value = ''; - - /* - * Because the layout refreshes the paths before the render function is - * called, so there is no way to load the layout overrides in the order - * template -> context -> fields. - * If there is no override in the context then we need to call the - * layout from Fields. - */ - if ($parts = self::extract($context)) - { - // Trying to render the layout on the component from the context - $value = LayoutHelper::render($layoutFile, $displayData, null, array('component' => $parts[0], 'client' => 0)); - } - - if ($value == '') - { - // Trying to render the layout on Fields itself - $value = LayoutHelper::render($layoutFile, $displayData, null, array('component' => 'com_fields','client' => 0)); - } - - return $value; - } - - /** - * PrepareForm - * - * @param string $context The context of the content passed to the helper - * @param JForm $form form - * @param object $data data. - * - * @return boolean - * - * @since 3.7.0 - */ - public static function prepareForm($context, JForm $form, $data) - { - // Extracting the component and section - $parts = self::extract($context); - - if (! $parts) - { - return true; - } - - $context = $parts[0] . '.' . $parts[1]; - - // When no fields available return here - $fields = self::getFields($parts[0] . '.' . $parts[1], new JObject); - - if (! $fields) - { - return true; - } - - $component = $parts[0]; - $section = $parts[1]; - - $assignedCatids = $data->catid ?? $data->fieldscatid ?? $form->getValue('catid'); - - // Account for case that a submitted form has a multi-value category id field (e.g. a filtering form), just use the first category - $assignedCatids = is_array($assignedCatids) - ? (int) reset($assignedCatids) - : (int) $assignedCatids; - - if (!$assignedCatids && $formField = $form->getField('catid')) - { - $assignedCatids = $formField->getAttribute('default', null); - - // Choose the first category available - $xml = new DOMDocument; - $xml->loadHTML($formField->__get('input')); - $options = $xml->getElementsByTagName('option'); - - if (!$assignedCatids && $firstChoice = $options->item(0)) - { - $assignedCatids = $firstChoice->getAttribute('value'); - } - - $data->fieldscatid = $assignedCatids; - } - - /* - * If there is a catid field we need to reload the page when the catid - * is changed - */ - if ($form->getField('catid') && $parts[0] != 'com_fields') - { - /* - * Setting the onchange event to reload the page when the category - * has changed - */ - $form->setFieldAttribute('catid', 'onchange', 'categoryHasChanged(this);'); - - // Preload spindle-wheel when we need to submit form due to category selector changed - Factory::getDocument()->addScriptDeclaration(" - function categoryHasChanged(element) { - var cat = jQuery(element); - if (cat.val() == '" . $assignedCatids . "')return; - Joomla.loadingLayer('show'); - jQuery('input[name=task]').val('" . $section . ".reload'); - element.form.submit(); - } - jQuery( document ).ready(function() { - Joomla.loadingLayer('load'); - var formControl = '#" . $form->getFormControl() . "_catid'; - if (!jQuery(formControl).val() != '" . $assignedCatids . "'){jQuery(formControl).val('" . $assignedCatids . "');} - });" - ); - } - - // Getting the fields - $fields = self::getFields($parts[0] . '.' . $parts[1], $data); - - if (!$fields) - { - return true; - } - - $fieldTypes = self::getFieldTypes(); - - // Creating the dom - $xml = new DOMDocument('1.0', 'UTF-8'); - $fieldsNode = $xml->appendChild(new DOMElement('form'))->appendChild(new DOMElement('fields')); - $fieldsNode->setAttribute('name', 'com_fields'); - - // Organizing the fields according to their group - $fieldsPerGroup = array(0 => array()); - - foreach ($fields as $field) - { - if (!array_key_exists($field->type, $fieldTypes)) - { - // Field type is not available - continue; - } - - if (!array_key_exists($field->group_id, $fieldsPerGroup)) - { - $fieldsPerGroup[$field->group_id] = array(); - } - - if ($path = $fieldTypes[$field->type]['path']) - { - // Add the lookup path for the field - JFormHelper::addFieldPath($path); - } - - if ($path = $fieldTypes[$field->type]['rules']) - { - // Add the lookup path for the rule - JFormHelper::addRulePath($path); - } - - $fieldsPerGroup[$field->group_id][] = $field; - } - - $model = new Joomla\Component\Fields\Administrator\Model\GroupsModel(array('ignore_request' => true)); - $model->setState('filter.context', $context); - - /** - * $model->getItems() would only return existing groups, but we also - * have the 'default' group with id 0 which is not in the database, - * so we create it virtually here. - */ - $defaultGroup = new \stdClass; - $defaultGroup->id = 0; - $defaultGroup->title = ''; - $defaultGroup->description = ''; - $iterateGroups = array_merge(array($defaultGroup), $model->getItems()); - - // Looping through the groups - foreach ($iterateGroups as $group) - { - if (empty($fieldsPerGroup[$group->id])) - { - continue; - } - - // Defining the field set - /** @var DOMElement $fieldset */ - $fieldset = $fieldsNode->appendChild(new DOMElement('fieldset')); - $fieldset->setAttribute('name', 'fields-' . $group->id); - $fieldset->setAttribute('addfieldpath', '/administrator/components/' . $component . '/models/fields'); - $fieldset->setAttribute('addrulepath', '/administrator/components/' . $component . '/models/rules'); - - $label = $group->title; - $description = $group->description; - - if (!$label) - { - $key = strtoupper($component . '_FIELDS_' . $section . '_LABEL'); - - if (!Factory::getLanguage()->hasKey($key)) - { - $key = 'JGLOBAL_FIELDS'; - } - - $label = $key; - } - - if (!$description) - { - $key = strtoupper($component . '_FIELDS_' . $section . '_DESC'); - - if (Factory::getLanguage()->hasKey($key)) - { - $description = $key; - } - } - - $fieldset->setAttribute('label', $label); - $fieldset->setAttribute('description', strip_tags($description)); - - // Looping through the fields for that context - foreach ($fieldsPerGroup[$group->id] as $field) - { - try - { - Factory::getApplication()->triggerEvent('onCustomFieldsPrepareDom', array($field, $fieldset, $form)); - - /* - * If the field belongs to an assigned_cat_id but the assigned_cat_ids in the data - * is not known, set the required flag to false on any circumstance. - */ - if (!$assignedCatids && !empty($field->assigned_cat_ids) && $form->getField($field->name)) - { - $form->setFieldAttribute($field->name, 'required', 'false'); - } - } - catch (Exception $e) - { - Factory::getApplication()->enqueueMessage($e->getMessage(), 'error'); - } - } - - // When the field set is empty, then remove it - if (!$fieldset->hasChildNodes()) - { - $fieldsNode->removeChild($fieldset); - } - } - - // Loading the XML fields string into the form - $form->load($xml->saveXML()); - - $model = new \Joomla\Component\Fields\Administrator\Model\FieldModel(array('ignore_request' => true)); - - if ((!isset($data->id) || !$data->id) && Factory::getApplication()->input->getCmd('controller') == 'modules' - && Factory::getApplication()->isClient('site')) - { - // Modules on front end editing don't have data and an id set - $data->id = Factory::getApplication()->input->getInt('id'); - } - - // Looping through the fields again to set the value - if (!isset($data->id) || !$data->id) - { - return true; - } - - foreach ($fields as $field) - { - $value = $model->getFieldValue($field->id, $data->id); - - if ($value === null) - { - continue; - } - - if (!is_array($value) && $value !== '') - { - // Function getField doesn't cache the fields, so we try to do it only when necessary - $formField = $form->getField($field->name, 'com_fields'); - - if ($formField && $formField->forceMultiple) - { - $value = (array) $value; - } - } - - // Setting the value on the field - $form->setValue($field->name, 'com_fields', $value); - } - - return true; - } - - /** - * Return a boolean if the actual logged in user can edit the given field value. - * - * @param stdClass $field The field - * - * @return boolean - * - * @since 3.7.0 - */ - public static function canEditFieldValue($field) - { - $parts = self::extract($field->context); - - return Factory::getUser()->authorise('core.edit.value', $parts[0] . '.field.' . (int) $field->id); - } - - /** - * Adds Count Items for Category Manager. - * - * @param stdClass[] &$items The field category objects - * - * @return stdClass[] - * - * @since 3.7.0 - */ - public static function countItems(&$items) - { - $db = Factory::getDbo(); - - foreach ($items as $item) - { - $item->count_trashed = 0; - $item->count_archived = 0; - $item->count_unpublished = 0; - $item->count_published = 0; - - $query = $db->getQuery(true); - $query->select('state, count(1) AS count') - ->from($db->quoteName('#__fields')) - ->where('group_id = ' . (int) $item->id) - ->group('state'); - $db->setQuery($query); - - $fields = $db->loadObjectList(); - - $states = array( - '-2' => 'count_trashed', - '0' => 'count_unpublished', - '1' => 'count_published', - '2' => 'count_archived', - ); - - foreach ($fields as $field) - { - $property = $states[$field->state]; - $item->$property = $field->count; - } - } - - return $items; - } - - /** - * Gets assigned categories titles for a field - * - * @param stdClass[] $fieldId The field ID - * - * @return array Array with the assigned categories - * - * @since 3.7.0 - */ - public static function getAssignedCategoriesTitles($fieldId) - { - $fieldId = (int) $fieldId; - - if (!$fieldId) - { - return array(); - } - - $db = Factory::getDbo(); - $query = $db->getQuery(true); - - $query->select($db->quoteName('c.title')) - ->from($db->quoteName('#__fields_categories', 'a')) - ->join('LEFT', $db->quoteName('#__categories', 'c') . ' ON a.category_id = c.id') - ->where('field_id = ' . $fieldId); - - $db->setQuery($query); - - return $db->loadColumn(); - } - - /** - * Gets the fields system plugin extension id. - * - * @return int The fields system plugin extension id. - * - * @since 3.7.0 - */ - public static function getFieldsPluginId() - { - $db = Factory::getDbo(); - $query = $db->getQuery(true) - ->select($db->quoteName('extension_id')) - ->from($db->quoteName('#__extensions')) - ->where($db->quoteName('folder') . ' = ' . $db->quote('system')) - ->where($db->quoteName('element') . ' = ' . $db->quote('fields')); - $db->setQuery($query); - - try - { - $result = (int) $db->loadResult(); - } - catch (RuntimeException $e) - { - Factory::getApplication()->enqueueMessage($e->getMessage(), 'error'); - $result = 0; - } - - return $result; - } - - /** - * Configure the Linkbar. - * - * @param string $context The context the fields are used for - * @param string $vName The view currently active - * - * @return void - * - * @since 3.7.0 - */ - public static function addSubmenu($context, $vName) - { - $parts = self::extract($context); - - if (!$parts) - { - return; - } - - $component = $parts[0]; - - // Avoid nonsense situation. - if ($component == 'com_fields') - { - return; - } - - // Try to find the component helper. - $eName = str_replace('com_', '', $component); - $file = Path::clean(JPATH_ADMINISTRATOR . '/components/' . $component . '/helpers/' . $eName . '.php'); - - if (!file_exists($file)) - { - return; - } - - require_once $file; - - $cName = ucfirst($eName) . 'Helper'; - - if (class_exists($cName) && is_callable(array($cName, 'addSubmenu'))) - { - $lang = Factory::getLanguage(); - $lang->load($component, JPATH_ADMINISTRATOR) - || $lang->load($component, JPATH_ADMINISTRATOR . '/components/' . $component); - - $cName::addSubmenu('fields.' . $vName); - } - } - - /** - * Loads the fields plugins and returns an array of field types from the plugins. - * - * The returned array contains arrays with the following keys: - * - label: The label of the field - * - type: The type of the field - * - path: The path of the folder where the field can be found - * - * @return array - * - * @since 3.7.0 - */ - public static function getFieldTypes() - { - PluginHelper::importPlugin('fields'); - $eventData = Factory::getApplication()->triggerEvent('onCustomFieldsGetTypes'); - - $data = array(); - - foreach ($eventData as $fields) - { - foreach ($fields as $fieldDescription) - { - if (!array_key_exists('path', $fieldDescription)) - { - $fieldDescription['path'] = null; - } - - if (!array_key_exists('rules', $fieldDescription)) - { - $fieldDescription['rules'] = null; - } - - $data[$fieldDescription['type']] = $fieldDescription; - } - } - - return $data; - } - - /** - * Clears the internal cache for the custom fields. - * - * @return void - * - * @since 3.8.0 - */ - public static function clearFieldsCache() - { - self::$fieldCache = null; - self::$fieldsCache = null; - } } diff --git a/administrator/components/com_fields/services/provider.php b/administrator/components/com_fields/services/provider.php new file mode 100644 index 0000000000000..f5bac66933c18 --- /dev/null +++ b/administrator/components/com_fields/services/provider.php @@ -0,0 +1,54 @@ +registerServiceProvider(new MVCFactoryFactory('\\Joomla\\Component\\Fields')); + $container->registerServiceProvider(new DispatcherFactory('\\Joomla\\Component\\Fields')); + + $container->set( + ComponentInterface::class, + function (Container $container) + { + $component = new MVCComponent($container->get(DispatcherFactoryInterface::class)); + + $component->setMvcFactoryFactory($container->get(MVCFactoryFactoryInterface::class)); + + return $component; + } + ); + } +}; diff --git a/components/com_contact/layouts/fields/render.php b/components/com_contact/layouts/fields/render.php index 49f00b067ac21..13dd621d3c9fc 100644 --- a/components/com_contact/layouts/fields/render.php +++ b/components/com_contact/layouts/fields/render.php @@ -8,6 +8,8 @@ */ defined('_JEXEC') or die; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; + // Check if we have all the data if (!array_key_exists('item', $displayData) || !array_key_exists('context', $displayData)) { @@ -29,8 +31,6 @@ return; } -JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); - $parts = explode('.', $context); $component = $parts[0]; $fields = null; diff --git a/components/com_fields/Controller/DisplayController.php b/components/com_fields/Controller/DisplayController.php index 846a02e45148b..d62cdeba99b3e 100644 --- a/components/com_fields/Controller/DisplayController.php +++ b/components/com_fields/Controller/DisplayController.php @@ -6,6 +6,7 @@ * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ + namespace Joomla\Component\Fields\Site\Controller; defined('_JEXEC') or die; diff --git a/components/com_fields/Dispatcher/Dispatcher.php b/components/com_fields/Dispatcher/Dispatcher.php new file mode 100644 index 0000000000000..773018cd2d72f --- /dev/null +++ b/components/com_fields/Dispatcher/Dispatcher.php @@ -0,0 +1,49 @@ +input->get('view') !== 'fields' || $this->input->get('layout') !== 'modal') + { + return; + } + + $context = $this->app->getUserStateFromRequest('com_fields.fields.context', 'context', 'com_content.article', 'CMD'); + $parts = FieldsHelper::extract($context); + + if (!$this->app->getIdentity()->authorise('core.create', $parts[0]) + || !$this->app->getIdentity()->authorise('core.edit', $parts[0])) + { + throw new NotAllowed($this->app->getLanguage()->_('JERROR_ALERTNOAUTHOR')); + } + } +} diff --git a/components/com_fields/dispatcher.php b/components/com_fields/dispatcher.php deleted file mode 100644 index ed2dbc0f2d5d8..0000000000000 --- a/components/com_fields/dispatcher.php +++ /dev/null @@ -1,43 +0,0 @@ -input; -$context = Factory::getApplication()->getUserStateFromRequest('com_fields.fields.context', 'context', 'com_content.article', 'CMD'); -$parts = FieldsHelper::extract($context); - -if ($input->get('view') === 'fields' && $input->get('layout') === 'modal') -{ - if (!Factory::getUser()->authorise('core.create', $parts[0]) - || !Factory::getUser()->authorise('core.edit', $parts[0])) - { - Factory::getApplication()->enqueueMessage(JText::_('JERROR_ALERTNOAUTHOR'), 'error'); - - return; - } -} - -$controller = BaseController::getInstance('Fields'); -$controller->execute($input->get('task')); -$controller->redirect(); diff --git a/components/com_fields/layouts/field/render.php b/components/com_fields/layouts/field/render.php index 9c8d91170ed9e..8706720520e76 100644 --- a/components/com_fields/layouts/field/render.php +++ b/components/com_fields/layouts/field/render.php @@ -8,13 +8,15 @@ */ defined('_JEXEC') or die; +use Joomla\CMS\Language\Text; + if (!key_exists('field', $displayData)) { return; } $field = $displayData['field']; -$label = JText::_($field->label); +$label = Text::_($field->label); $value = $field->value; $showLabel = $field->params->get('showlabel'); diff --git a/components/com_fields/layouts/fields/render.php b/components/com_fields/layouts/fields/render.php index 37c4153ed19c0..a0663b4789a8c 100644 --- a/components/com_fields/layouts/fields/render.php +++ b/components/com_fields/layouts/fields/render.php @@ -8,6 +8,8 @@ */ defined('_JEXEC') or die; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; + // Check if we have all the data if (!key_exists('item', $displayData) || !key_exists('context', $displayData)) { @@ -29,8 +31,6 @@ return; } -JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); - $parts = explode('.', $context); $component = $parts[0]; $fields = null; @@ -48,7 +48,6 @@ { return; } - ?>
diff --git a/plugins/content/fields/fields.php b/plugins/content/fields/fields.php index 6e6648ec51c19..26b6a78d1bbc1 100644 --- a/plugins/content/fields/fields.php +++ b/plugins/content/fields/fields.php @@ -10,6 +10,7 @@ defined('_JEXEC') or die(); use Joomla\CMS\Plugin\CMSPlugin; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; /** * Plug-in to show a custom field in eg an article @@ -56,9 +57,6 @@ public function onContentPrepare($context, &$item, &$params, $page = 0) return; } - // Register FieldsHelper - JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); - // Prepare the text if (isset($item->text)) { diff --git a/plugins/editors-xtd/fields/fields.php b/plugins/editors-xtd/fields/fields.php index e88cbcd6f5ba6..b22c7981ce5a9 100644 --- a/plugins/editors-xtd/fields/fields.php +++ b/plugins/editors-xtd/fields/fields.php @@ -15,6 +15,7 @@ use Joomla\CMS\Session\Session; use Joomla\CMS\Object\CMSObject; use Joomla\CMS\Component\ComponentHelper; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; /** * Editor Fields button @@ -48,9 +49,6 @@ public function onDisplay($name) return; } - // Register FieldsHelper - JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); - // Guess the field context based on view. $jinput = Factory::getApplication()->input; $context = $jinput->get('option') . '.' . $jinput->get('view'); diff --git a/plugins/system/fields/fields.php b/plugins/system/fields/fields.php index 2e0a8e2b1f94e..6b9c370b226ed 100644 --- a/plugins/system/fields/fields.php +++ b/plugins/system/fields/fields.php @@ -14,8 +14,7 @@ use Joomla\Registry\Registry; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Language\Multilanguage; - -JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; /** * Fields Plugin From c7a91bd3c9100aeb2a276d46f1bb03e5fdd26117 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 7 Aug 2018 08:54:21 +0200 Subject: [PATCH 2/4] cs --- .../com_fields/Helper/FieldsHelper.php | 16 ++++++++++++---- .../components/com_fields/helpers/fields.php | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/administrator/components/com_fields/Helper/FieldsHelper.php b/administrator/components/com_fields/Helper/FieldsHelper.php index a9cfec6ee17c9..2c74f30c11fc5 100644 --- a/administrator/components/com_fields/Helper/FieldsHelper.php +++ b/administrator/components/com_fields/Helper/FieldsHelper.php @@ -94,7 +94,9 @@ public static function getFields($context, $item = null, $prepareValue = false, if (self::$fieldsCache === null) { // Load the model - self::$fieldsCache = Factory::getApplication()->bootComponent('com_fields')->createMVCFactory(Factory::getApplication())->createModel('Fields', 'Administrator', array('ignore_request' => true)); + self::$fieldsCache = Factory::getApplication()->bootComponent('com_fields') + ->createMVCFactory(Factory::getApplication()) + ->createModel('Fields', 'Administrator', ['ignore_request' => true]); self::$fieldsCache->setState('filter.state', 1); self::$fieldsCache->setState('list.limit', 0); @@ -142,7 +144,9 @@ public static function getFields($context, $item = null, $prepareValue = false, { if (self::$fieldCache === null) { - self::$fieldCache = Factory::getApplication()->bootComponent('com_fields')->createMVCFactory(Factory::getApplication())->createModel('Field', 'Administrator', array('ignore_request' => true)); + self::$fieldCache = Factory::getApplication()->bootComponent('com_fields') + ->createMVCFactory(Factory::getApplication()) + ->createModel('Field', 'Administrator', ['ignore_request' => true]); } $fieldIds = array_map( @@ -393,7 +397,9 @@ function categoryHasChanged(element) { $fieldsPerGroup[$field->group_id][] = $field; } - $model = Factory::getApplication()->bootComponent('com_fields')->createMVCFactory(Factory::getApplication())->createModel('Groups', 'Administrator', array('ignore_request' => true)); + $model = Factory::getApplication()->bootComponent('com_fields') + ->createMVCFactory(Factory::getApplication()) + ->createModel('Groups', 'Administrator', ['ignore_request' => true]); $model->setState('filter.context', $context); /** @@ -482,7 +488,9 @@ function categoryHasChanged(element) { // Loading the XML fields string into the form $form->load($xml->saveXML()); - $model = Factory::getApplication()->bootComponent('com_fields')->createMVCFactory(Factory::getApplication())->createModel('Field', 'Administrator', array('ignore_request' => true)); + $model = Factory::getApplication()->bootComponent('com_fields') + ->createMVCFactory(Factory::getApplication()) + ->createModel('Field', 'Administrator', ['ignore_request' => true]); if ((!isset($data->id) || !$data->id) && Factory::getApplication()->input->getCmd('controller') == 'modules' && Factory::getApplication()->isClient('site')) diff --git a/administrator/components/com_fields/helpers/fields.php b/administrator/components/com_fields/helpers/fields.php index 6443a2bf50673..13d8d76526870 100644 --- a/administrator/components/com_fields/helpers/fields.php +++ b/administrator/components/com_fields/helpers/fields.php @@ -11,7 +11,7 @@ /** * FieldsHelper * - * @since 3.7.0 + * @since 3.7.0 * * @deprecated 5.0 Use \Joomla\Component\Fields\Administrator\Helper\FieldsHelper instead */ From 618d37a4c1648889e17ee29bd3c95df1046d4b60 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 21 Aug 2018 07:57:29 +0200 Subject: [PATCH 3/4] Fix conflicts --- .../com_fields/Helper/FieldsHelper.php | 46 ------------------- 1 file changed, 46 deletions(-) diff --git a/administrator/components/com_fields/Helper/FieldsHelper.php b/administrator/components/com_fields/Helper/FieldsHelper.php index 2c74f30c11fc5..9f321d954c074 100644 --- a/administrator/components/com_fields/Helper/FieldsHelper.php +++ b/administrator/components/com_fields/Helper/FieldsHelper.php @@ -548,52 +548,6 @@ public static function canEditFieldValue($field) return Factory::getUser()->authorise('core.edit.value', $parts[0] . '.field.' . (int) $field->id); } - /** - * Adds Count Items for Category Manager. - * - * @param \stdClass[] &$items The field category objects - * - * @return \stdClass[] - * - * @since 3.7.0 - */ - public static function countItems(&$items) - { - $db = Factory::getDbo(); - - foreach ($items as $item) - { - $item->count_trashed = 0; - $item->count_archived = 0; - $item->count_unpublished = 0; - $item->count_published = 0; - - $query = $db->getQuery(true); - $query->select('state, count(1) AS count') - ->from($db->quoteName('#__fields')) - ->where('group_id = ' . (int) $item->id) - ->group('state'); - $db->setQuery($query); - - $fields = $db->loadObjectList(); - - $states = array( - '-2' => 'count_trashed', - '0' => 'count_unpublished', - '1' => 'count_published', - '2' => 'count_archived', - ); - - foreach ($fields as $field) - { - $property = $states[$field->state]; - $item->$property = $field->count; - } - } - - return $items; - } - /** * Gets assigned categories titles for a field * From 1d52f6992465f28265bd9ecb4bc144b783ebd86f Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 21 Aug 2018 08:03:49 +0200 Subject: [PATCH 4/4] Use the namespaced class --- .../com_fields/Controller/FieldController.php | 3 ++- .../com_fields/Controller/GroupController.php | 3 ++- .../components/com_fields/Field/TypeField.php | 3 ++- .../components/com_fields/Model/FieldModel.php | 17 +++++++++-------- .../components/com_fields/Model/FieldsModel.php | 5 +++-- .../components/com_fields/Model/GroupModel.php | 3 ++- .../com_fields/Plugin/FieldsPlugin.php | 3 ++- .../com_fields/View/Fields/HtmlView.php | 5 +++-- .../com_fields/View/Group/HtmlView.php | 5 +++-- .../com_fields/View/Groups/HtmlView.php | 7 ++++--- .../com_fields/tmpl/fields/default.php | 1 + .../com_fields/tmpl/groups/default.php | 1 + .../Controller/ContactController.php | 5 +++-- 13 files changed, 37 insertions(+), 24 deletions(-) diff --git a/administrator/components/com_fields/Controller/FieldController.php b/administrator/components/com_fields/Controller/FieldController.php index 7fbde0e9ade3c..28d1c4ff30cf1 100644 --- a/administrator/components/com_fields/Controller/FieldController.php +++ b/administrator/components/com_fields/Controller/FieldController.php @@ -15,6 +15,7 @@ use Joomla\CMS\MVC\Controller\FormController; use Joomla\CMS\MVC\Model\BaseDatabaseModel; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; use Joomla\Registry\Registry; use Joomla\CMS\Language\Text; use Joomla\CMS\Session\Session; @@ -57,7 +58,7 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu parent::__construct($config, $factory, $app, $input); $this->internalContext = Factory::getApplication()->getUserStateFromRequest('com_fields.fields.context', 'context', 'com_content.article', 'CMD'); - $parts = \FieldsHelper::extract($this->internalContext); + $parts = FieldsHelper::extract($this->internalContext); $this->component = $parts ? $parts[0] : null; } diff --git a/administrator/components/com_fields/Controller/GroupController.php b/administrator/components/com_fields/Controller/GroupController.php index b06032a72e2fd..c63c0c4565695 100644 --- a/administrator/components/com_fields/Controller/GroupController.php +++ b/administrator/components/com_fields/Controller/GroupController.php @@ -14,6 +14,7 @@ use Joomla\CMS\MVC\Controller\FormController; use Joomla\CMS\MVC\Model\BaseDatabaseModel; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; use Joomla\Registry\Registry; use Joomla\CMS\Language\Text; use Joomla\CMS\Session\Session; @@ -59,7 +60,7 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu { parent::__construct($config, $factory, $app, $input); - $parts = \FieldsHelper::extract($this->input->getCmd('context')); + $parts = FieldsHelper::extract($this->input->getCmd('context')); if ($parts) { diff --git a/administrator/components/com_fields/Field/TypeField.php b/administrator/components/com_fields/Field/TypeField.php index 1492b1de4f25d..4ae2fe91e0215 100644 --- a/administrator/components/com_fields/Field/TypeField.php +++ b/administrator/components/com_fields/Field/TypeField.php @@ -15,6 +15,7 @@ use Joomla\CMS\Form\FormHelper; use Joomla\CMS\Uri\Uri; use Joomla\CMS\HTML\HTMLHelper; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; FormHelper::loadFieldClass('list'); @@ -60,7 +61,7 @@ protected function getOptions() { $options = parent::getOptions(); - $fieldTypes = \FieldsHelper::getFieldTypes(); + $fieldTypes = FieldsHelper::getFieldTypes(); foreach ($fieldTypes as $fieldType) { diff --git a/administrator/components/com_fields/Model/FieldModel.php b/administrator/components/com_fields/Model/FieldModel.php index bcd0aeed6f406..bfa22242dae8b 100644 --- a/administrator/components/com_fields/Model/FieldModel.php +++ b/administrator/components/com_fields/Model/FieldModel.php @@ -18,6 +18,7 @@ use Joomla\CMS\MVC\Model\AdminModel; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\Plugin\PluginHelper; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; use Joomla\Registry\Registry; use Joomla\String\StringHelper; use Joomla\Utilities\ArrayHelper; @@ -212,7 +213,7 @@ public function save($data) } } - \FieldsHelper::clearFieldsCache(); + FieldsHelper::clearFieldsCache(); return true; } @@ -236,7 +237,7 @@ private function checkDefaultValue($data) return true; } - $types = \FieldsHelper::getFieldTypes(); + $types = FieldsHelper::getFieldTypes(); // Check if type exists if (!array_key_exists($data['type'], $types)) @@ -505,7 +506,7 @@ public function getForm($data = array(), $loadData = true) if (empty($context) && isset($data['context'])) { $context = $data['context']; - $parts = \FieldsHelper::extract($context); + $parts = FieldsHelper::extract($context); $this->setState('field.context', $context); @@ -584,7 +585,7 @@ public function setFieldValue($fieldId, $itemId, $value) } // Don't save the value when the user is not authorized to change it - if (!$field || !\FieldsHelper::canEditFieldValue($field)) + if (!$field || !FieldsHelper::canEditFieldValue($field)) { return false; } @@ -653,7 +654,7 @@ public function setFieldValue($fieldId, $itemId, $value) } $this->valueCache = array(); - \FieldsHelper::clearFieldsCache(); + FieldsHelper::clearFieldsCache(); return true; } @@ -792,7 +793,7 @@ protected function canDelete($record) return false; } - $parts = \FieldsHelper::extract($record->context); + $parts = FieldsHelper::extract($record->context); return Factory::getUser()->authorise('core.delete', $parts[0] . '.field.' . (int) $record->id); } @@ -813,7 +814,7 @@ protected function canDelete($record) protected function canEditState($record) { $user = Factory::getUser(); - $parts = \FieldsHelper::extract($record->context); + $parts = FieldsHelper::extract($record->context); // Check for existing field. if (!empty($record->id)) @@ -841,7 +842,7 @@ protected function populateState() $context = $app->input->get('context', 'com_content.article'); $this->setState('field.context', $context); - $parts = \FieldsHelper::extract($context); + $parts = FieldsHelper::extract($context); // Extract the component name $this->setState('field.component', $parts[0]); diff --git a/administrator/components/com_fields/Model/FieldsModel.php b/administrator/components/com_fields/Model/FieldsModel.php index 99137437109e9..a164d1e938b32 100644 --- a/administrator/components/com_fields/Model/FieldsModel.php +++ b/administrator/components/com_fields/Model/FieldsModel.php @@ -13,6 +13,7 @@ use Joomla\CMS\MVC\Model\ListModel; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; use Joomla\Registry\Registry; use Joomla\Utilities\ArrayHelper; use Joomla\CMS\Factory; @@ -87,7 +88,7 @@ protected function populateState($ordering = null, $direction = null) $this->setState('filter.context', $context); // Split context into component and optional section - $parts = \FieldsHelper::extract($context); + $parts = FieldsHelper::extract($context); if ($parts) { @@ -190,7 +191,7 @@ protected function getListQuery() { $categories = (array) $categories; $categories = ArrayHelper::toInteger($categories); - $parts = \FieldsHelper::extract($context); + $parts = FieldsHelper::extract($context); if ($parts) { diff --git a/administrator/components/com_fields/Model/GroupModel.php b/administrator/components/com_fields/Model/GroupModel.php index bfb0adaea6f96..d694160c01ae0 100644 --- a/administrator/components/com_fields/Model/GroupModel.php +++ b/administrator/components/com_fields/Model/GroupModel.php @@ -17,6 +17,7 @@ use Joomla\CMS\Date\Date; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Factory; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; /** * Group Model @@ -211,7 +212,7 @@ protected function preprocessForm(\JForm $form, $data, $group = 'content') { parent::preprocessForm($form, $data, $group); - $parts = \FieldsHelper::extract($this->state->get('filter.context')); + $parts = FieldsHelper::extract($this->state->get('filter.context')); // Extract the component name $component = $parts[0]; diff --git a/administrator/components/com_fields/Plugin/FieldsPlugin.php b/administrator/components/com_fields/Plugin/FieldsPlugin.php index 74258b8dfa9f9..36f80d1a7d318 100644 --- a/administrator/components/com_fields/Plugin/FieldsPlugin.php +++ b/administrator/components/com_fields/Plugin/FieldsPlugin.php @@ -16,6 +16,7 @@ use Joomla\CMS\Language\Text; use Joomla\CMS\Filesystem\Folder; use Joomla\CMS\Factory; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; /** * Abstract Fields Plugin @@ -213,7 +214,7 @@ public function onCustomFieldsPrepareDom($field, \DOMElement $parent, \JForm $fo } // Check if it is allowed to edit the field - if (!\FieldsHelper::canEditFieldValue($field)) + if (!FieldsHelper::canEditFieldValue($field)) { $node->setAttribute('disabled', 'true'); } diff --git a/administrator/components/com_fields/View/Fields/HtmlView.php b/administrator/components/com_fields/View/Fields/HtmlView.php index 8c7098257f93a..b18dd46570b78 100644 --- a/administrator/components/com_fields/View/Fields/HtmlView.php +++ b/administrator/components/com_fields/View/Fields/HtmlView.php @@ -22,6 +22,7 @@ use Joomla\CMS\Toolbar\Toolbar; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Factory; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; /** * Fields View @@ -99,7 +100,7 @@ public function display($tpl = null) // Display a warning if the fields system plugin is disabled if (!PluginHelper::isEnabled('system', 'fields')) { - $link = Route::_('index.php?option=com_plugins&task=plugin.edit&extension_id=' . \FieldsHelper::getFieldsPluginId()); + $link = Route::_('index.php?option=com_plugins&task=plugin.edit&extension_id=' . FieldsHelper::getFieldsPluginId()); Factory::getApplication()->enqueueMessage(Text::sprintf('COM_FIELDS_SYSTEM_PLUGIN_NOT_ENABLED', $link), 'warning'); } @@ -116,7 +117,7 @@ public function display($tpl = null) } } - \FieldsHelper::addSubmenu($this->state->get('filter.context'), 'fields'); + FieldsHelper::addSubmenu($this->state->get('filter.context'), 'fields'); $this->sidebar = \JHtmlSidebar::render(); return parent::display($tpl); diff --git a/administrator/components/com_fields/View/Group/HtmlView.php b/administrator/components/com_fields/View/Group/HtmlView.php index ba66d8e45eff8..cbc1d223b29c2 100644 --- a/administrator/components/com_fields/View/Group/HtmlView.php +++ b/administrator/components/com_fields/View/Group/HtmlView.php @@ -17,6 +17,7 @@ use Joomla\CMS\Helper\ContentHelper; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Factory; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; /** * Group View @@ -73,7 +74,7 @@ public function display($tpl = null) $this->state = $this->get('State'); $component = ''; - $parts = \FieldsHelper::extract($this->state->get('filter.context')); + $parts = FieldsHelper::extract($this->state->get('filter.context')); if ($parts) { @@ -105,7 +106,7 @@ public function display($tpl = null) protected function addToolbar() { $component = ''; - $parts = \FieldsHelper::extract($this->state->get('filter.context')); + $parts = FieldsHelper::extract($this->state->get('filter.context')); if ($parts) { diff --git a/administrator/components/com_fields/View/Groups/HtmlView.php b/administrator/components/com_fields/View/Groups/HtmlView.php index 7fa3e34c0f875..d236a93892f98 100644 --- a/administrator/components/com_fields/View/Groups/HtmlView.php +++ b/administrator/components/com_fields/View/Groups/HtmlView.php @@ -22,6 +22,7 @@ use Joomla\CMS\Helper\ContentHelper; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Factory; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; /** * Groups View @@ -99,7 +100,7 @@ public function display($tpl = null) // Display a warning if the fields system plugin is disabled if (!PluginHelper::isEnabled('system', 'fields')) { - $link = Route::_('index.php?option=com_plugins&task=plugin.edit&extension_id=' . \FieldsHelper::getFieldsPluginId()); + $link = Route::_('index.php?option=com_plugins&task=plugin.edit&extension_id=' . FieldsHelper::getFieldsPluginId()); Factory::getApplication()->enqueueMessage(Text::sprintf('COM_FIELDS_SYSTEM_PLUGIN_NOT_ENABLED', $link), 'warning'); } @@ -112,7 +113,7 @@ public function display($tpl = null) $this->filterForm->removeField('language', 'filter'); } - \FieldsHelper::addSubmenu($this->state->get('filter.context'), 'groups'); + FieldsHelper::addSubmenu($this->state->get('filter.context'), 'groups'); $this->sidebar = \JHtmlSidebar::render(); return parent::display($tpl); @@ -129,7 +130,7 @@ protected function addToolbar() { $groupId = $this->state->get('filter.group_id'); $component = ''; - $parts = \FieldsHelper::extract($this->state->get('filter.context')); + $parts = FieldsHelper::extract($this->state->get('filter.context')); if ($parts) { diff --git a/administrator/components/com_fields/tmpl/fields/default.php b/administrator/components/com_fields/tmpl/fields/default.php index 6cbc8955d4e17..fc3b79fd65534 100644 --- a/administrator/components/com_fields/tmpl/fields/default.php +++ b/administrator/components/com_fields/tmpl/fields/default.php @@ -15,6 +15,7 @@ use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Categories\Categories; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; // Include the component HTML helpers. HTMLHelper::addIncludePath(JPATH_COMPONENT . '/helpers/html'); diff --git a/administrator/components/com_fields/tmpl/groups/default.php b/administrator/components/com_fields/tmpl/groups/default.php index e4b1dbc64e381..a425fa1211740 100644 --- a/administrator/components/com_fields/tmpl/groups/default.php +++ b/administrator/components/com_fields/tmpl/groups/default.php @@ -14,6 +14,7 @@ use Joomla\CMS\Layout\LayoutHelper; use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; // Include the component HTML helpers. HTMLHelper::addIncludePath(JPATH_COMPONENT . '/helpers/html'); diff --git a/components/com_contact/Controller/ContactController.php b/components/com_contact/Controller/ContactController.php index 40e4518181c7e..56906227fe1e1 100644 --- a/components/com_contact/Controller/ContactController.php +++ b/components/com_contact/Controller/ContactController.php @@ -21,6 +21,7 @@ use Joomla\CMS\Language\Text; use Joomla\CMS\Factory; use Joomla\CMS\Log\Log; +use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; /** * Controller for single contact view @@ -205,9 +206,9 @@ private function _sendEmail($data, $contact, $copy_email_activated) $body = $prefix . "\n" . $name . ' <' . $email . '>' . "\r\n\r\n" . stripslashes($body); // Load the custom fields - if (!empty($data['com_fields']) && $fields = \FieldsHelper::getFields('com_contact.mail', $contact, true, $data['com_fields'])) + if (!empty($data['com_fields']) && $fields = FieldsHelper::getFields('com_contact.mail', $contact, true, $data['com_fields'])) { - $output = \FieldsHelper::render( + $output = FieldsHelper::render( 'com_contact.mail', 'fields.render', array(