From 417ae8304e4204243205fbd9fbea21d3537e5867 Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Wed, 5 Mar 2025 22:33:07 +0700 Subject: [PATCH 1/2] Clean up com_templates StyleModel, fix warnings --- .../com_templates/src/Model/StyleModel.php | 89 ++++--------------- 1 file changed, 15 insertions(+), 74 deletions(-) diff --git a/administrator/components/com_templates/src/Model/StyleModel.php b/administrator/components/com_templates/src/Model/StyleModel.php index bb381388aea5e..12853b0a3d518 100644 --- a/administrator/components/com_templates/src/Model/StyleModel.php +++ b/administrator/components/com_templates/src/Model/StyleModel.php @@ -11,19 +11,16 @@ namespace Joomla\Component\Templates\Administrator\Model; use Joomla\CMS\Application\ApplicationHelper; -use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Factory; use Joomla\CMS\Form\Form; use Joomla\CMS\Language\Multilanguage; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\MVC\Model\AdminModel; -use Joomla\CMS\Object\CMSObject; use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Table\Table; use Joomla\Database\ParameterType; use Joomla\Filesystem\Path; -use Joomla\Registry\Registry; use Joomla\String\StringHelper; use Joomla\Utilities\ArrayHelper; @@ -54,14 +51,6 @@ class StyleModel extends AdminModel */ protected $helpURL; - /** - * Item cache. - * - * @var array - * @since 1.6 - */ - private $_cache = []; - /** * Constructor. * @@ -87,28 +76,6 @@ public function __construct($config = [], ?MVCFactoryInterface $factory = null) parent::__construct($config, $factory); } - /** - * Method to auto-populate the model state. - * - * @note Calling getState in this method will result in recursion. - * - * @return void - * - * @since 1.6 - */ - protected function populateState() - { - $app = Factory::getApplication(); - - // Load the User state. - $pk = $app->getInput()->getInt('id'); - $this->setState('style.id', $pk); - - // Load the parameters. - $params = ComponentHelper::getParams('com_templates'); - $this->setState('params', $params); - } - /** * Method to delete rows. * @@ -284,10 +251,6 @@ public function getForm($data = [], $loadData = true) // Get the form. $form = $this->loadForm('com_templates.style', 'style', ['control' => 'jform', 'load_data' => $loadData]); - if (empty($form)) { - return false; - } - // Modify the form based on access controls. if (!$this->canEditState((object) $data)) { // Disable fields for display. @@ -331,42 +294,18 @@ protected function loadFormData() */ public function getItem($pk = null) { - $pk = (!empty($pk)) ? $pk : (int) $this->getState('style.id'); - - if (!isset($this->_cache[$pk])) { - // Get a row instance. - $table = $this->getTable(); - - // Attempt to load the row. - $return = $table->load($pk); - - // Check for a table object error. - if ($return === false && $table->getError()) { - $this->setError($table->getError()); - - return false; - } - - // Convert to the \Joomla\CMS\Object\CMSObject before adding other data. - $properties = $table->getProperties(1); - $this->_cache[$pk] = ArrayHelper::toObject($properties, CMSObject::class); - - // Convert the params field to an array. - $registry = new Registry($table->params); - $this->_cache[$pk]->params = $registry->toArray(); - - // Get the template XML. - $client = ApplicationHelper::getClientInfo($table->client_id); - $path = Path::clean($client->path . '/templates/' . $table->template . '/templateDetails.xml'); + if ($item = parent::getItem($pk)) { + $client = ApplicationHelper::getClientInfo($item->client_id); + $path = Path::clean($client->path . '/templates/' . $item->template . '/templateDetails.xml'); if (file_exists($path)) { - $this->_cache[$pk]->xml = simplexml_load_file($path); + $item->xml = simplexml_load_file($path); } else { - $this->_cache[$pk]->xml = null; + $item->xml = null; } } - return $this->_cache[$pk]; + return $item; } /** @@ -394,10 +333,16 @@ protected function preprocessForm(Form $form, $data, $group = 'content') $formFile = Path::clean($client->path . '/templates/' . $template . '/templateDetails.xml'); + /** + * $data could be array or object, so we use object casting here to make sure $styleObj + * is an object and access to template style data via the object's properties + */ + $styleObj = (object) $data; + // Load the core and/or local language file(s). // Default to using parent template language constants - $lang->load('tpl_' . $data->parent, $client->path) - || $lang->load('tpl_' . $data->parent, $client->path . '/templates/' . $data->parent); + $lang->load('tpl_' . $styleObj->parent, $client->path) + || $lang->load('tpl_' . $styleObj->parent, $client->path . '/templates/' . $styleObj->parent); // Apply any, optional, overrides for child template language constants $lang->load('tpl_' . $template, $client->path) @@ -411,11 +356,7 @@ protected function preprocessForm(Form $form, $data, $group = 'content') } // Disable home field if it is default style - - if ( - (\is_array($data) && \array_key_exists('home', $data) && $data['home'] == '1') - || (\is_object($data) && isset($data->home) && $data->home == '1') - ) { + if (isset($styleObj->home) && $styleObj->home == '1') { $form->setFieldAttribute('home', 'readonly', 'true'); } From 24d923d6ffde65e79b0d0428a77c359674d202f6 Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Fri, 7 Mar 2025 18:25:03 +0700 Subject: [PATCH 2/2] Prevent warnings on save --- .../components/com_templates/src/Model/StyleModel.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/administrator/components/com_templates/src/Model/StyleModel.php b/administrator/components/com_templates/src/Model/StyleModel.php index 12853b0a3d518..ee5ed39d665aa 100644 --- a/administrator/components/com_templates/src/Model/StyleModel.php +++ b/administrator/components/com_templates/src/Model/StyleModel.php @@ -324,7 +324,7 @@ protected function preprocessForm(Form $form, $data, $group = 'content') { $clientId = $this->getState('item.client_id'); $template = $this->getState('item.template'); - $lang = Factory::getLanguage(); + $lang = Factory::getApplication()->getLanguage(); $client = ApplicationHelper::getClientInfo($clientId); if (!$form->loadFile('style_' . $client->name, true)) { @@ -341,8 +341,10 @@ protected function preprocessForm(Form $form, $data, $group = 'content') // Load the core and/or local language file(s). // Default to using parent template language constants - $lang->load('tpl_' . $styleObj->parent, $client->path) + if (!empty($styleObj->parent)) { + $lang->load('tpl_' . $styleObj->parent, $client->path) || $lang->load('tpl_' . $styleObj->parent, $client->path . '/templates/' . $styleObj->parent); + } // Apply any, optional, overrides for child template language constants $lang->load('tpl_' . $template, $client->path)