From 64d9bb2ff51aba0b6d7bd40a688de3301a5e865b Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 13 Jun 2017 13:26:05 +0200 Subject: [PATCH 1/6] Reload task belongs to the component controller --- administrator/components/com_admin/script.php | 2 + .../com_fields/controllers/field.php | 38 -------- .../components/com_fields/helpers/fields.php | 22 +---- components/com_fields/controllers/field.php | 49 ----------- libraries/legacy/controller/form.php | 87 +++++++++++++++++++ 5 files changed, 90 insertions(+), 108 deletions(-) delete mode 100644 components/com_fields/controllers/field.php diff --git a/administrator/components/com_admin/script.php b/administrator/components/com_admin/script.php index d245cccf84871..d7bba2ed14fc7 100644 --- a/administrator/components/com_admin/script.php +++ b/administrator/components/com_admin/script.php @@ -1899,6 +1899,8 @@ public function deleteUnexistingFiles() '/administrator/templates/hathor/html/mod_menu', '/administrator/components/com_messages/layouts/toolbar', '/administrator/components/com_messages/layouts', + // Joomla! __DEPLOY_VERSION__ + '/components/com_fields/controllers', ); jimport('joomla.filesystem.file'); diff --git a/administrator/components/com_fields/controllers/field.php b/administrator/components/com_fields/controllers/field.php index d2205e87d837a..4d75638dda8aa 100644 --- a/administrator/components/com_fields/controllers/field.php +++ b/administrator/components/com_fields/controllers/field.php @@ -46,44 +46,6 @@ public function __construct($config = array()) $this->component = $parts ? $parts[0] : null; } - /** - * Stores the form data into the user state. - * - * @return void - * - * @since 3.7.0 - */ - public function storeform() - { - JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); - - $app = JFactory::getApplication(); - $data = $this->input->get($this->input->get('formcontrol', 'jform'), array(), 'array'); - - $parts = FieldsHelper::extract($this->input->getCmd('context')); - - if ($parts) - { - $app->setUserState($parts[0] . '.edit.' . $parts[1] . '.data', $data); - } - - if ($this->input->get('userstatevariable')) - { - $app->setUserState($this->input->get('userstatevariable'), $data); - } - - $redirectUrl = base64_decode($this->input->get->getBase64('return')); - - // Don't redirect to an external URL. - If (!JUri::isInternal($redirectUrl)) - { - $redirectUrl = 'index.php'; - } - - $app->redirect($redirectUrl); - $app->close(); - } - /** * Method override to check if you can add a new record. * diff --git a/administrator/components/com_fields/helpers/fields.php b/administrator/components/com_fields/helpers/fields.php index aedb4666112b0..eb3f4aa99c418 100644 --- a/administrator/components/com_fields/helpers/fields.php +++ b/administrator/components/com_fields/helpers/fields.php @@ -312,25 +312,6 @@ public static function prepareForm($context, JForm $form, $data) */ if ($form->getField('catid') && $parts[0] != 'com_fields') { - // The uri to submit to - $uri = clone JUri::getInstance('index.php'); - - /* - * Removing the catid parameter from the actual URL and set it as - * return - */ - $returnUri = clone JUri::getInstance(); - $returnUri->setVar('catid', null); - $uri->setVar('return', base64_encode($returnUri->toString())); - - // Setting the options - $uri->setVar('option', 'com_fields'); - $uri->setVar('task', 'field.storeform'); - $uri->setVar('context', $parts[0] . '.' . $parts[1]); - $uri->setVar('formcontrol', $form->getFormControl()); - $uri->setVar('view', null); - $uri->setVar('layout', null); - /* * Setting the onchange event to reload the page when the category * has changed @@ -343,8 +324,7 @@ function categoryHasChanged(element) { Joomla.loadingLayer('show'); var cat = jQuery(element); if (cat.val() == '" . $assignedCatids . "')return; - jQuery('input[name=task]').val('field.storeform'); - element.form.action='" . $uri . "'; + jQuery('input[name=task]').val('" . $section . ".reload'); element.form.submit(); } jQuery( document ).ready(function() { diff --git a/components/com_fields/controllers/field.php b/components/com_fields/controllers/field.php deleted file mode 100644 index b37183e29fdcf..0000000000000 --- a/components/com_fields/controllers/field.php +++ /dev/null @@ -1,49 +0,0 @@ -input->get($this->input->get('formcontrol', 'jform'), array(), 'array'); - $parts = FieldsHelper::extract($this->input->getCmd('context')); - - if ($parts) - { - $app->setUserState($parts[0] . '.edit.' . $parts[1] . '.data', $data); - } - - $redirectUrl = base64_decode($this->input->get->getBase64('return')); - - // Don't redirect to an external URL. - If (!JUri::isInternal($redirectUrl)) - { - $redirectUrl = 'index.php'; - } - - $app->redirect($redirectUrl); - $app->close(); - } -} diff --git a/libraries/legacy/controller/form.php b/libraries/legacy/controller/form.php index 67f10dfb46596..681afbfd4a867 100644 --- a/libraries/legacy/controller/form.php +++ b/libraries/legacy/controller/form.php @@ -831,4 +831,91 @@ public function save($key = null, $urlVar = null) return true; } + + /** + * Method to reload a record. + * + * @param string $key The name of the primary key of the URL variable. + * @param string $urlVar The name of the URL variable if different from the primary key (sometimes required to avoid router collisions). + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function reload($key = null, $urlVar = null) + { + // Check for request forgeries. + JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); + + $app = JFactory::getApplication(); + $model = $this->getModel(); + $data = $this->input->post->get('jform', array(), 'array'); + + // Determine the name of the primary key for the data. + if (empty($key)) + { + $key = $model->getTable()->getKeyName(); + } + + // To avoid data collisions the urlVar may be different from the primary key. + if (empty($urlVar)) + { + $urlVar = $key; + } + + $recordId = $this->input->getInt($urlVar); + + // Populate the row id from the session. + $data[$key] = $recordId; + + // The redirect url + $redirectUrl = JRoute::_( + 'index.php?option=' . $this->option . '&view=' . $this->view_item . + $this->getRedirectToItemAppend($recordId, $urlVar), + false + ); + + // Validate the posted data. + // Sometimes the form needs some posted data, such as for plugins and modules. + $form = $model->getForm($data, false); + + if (!$form) + { + $app->enqueueMessage($model->getError(), 'error'); + + $app->redirect($redirectUrl); + $app->close(); + } + + // Test whether the data is valid. + $validData = $model->validate($form, $data); + + // Check for validation errors. + if ($validData === false) + { + // Get the validation messages. + $errors = $model->getErrors(); + + // Push up to three validation messages out to the user. + for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) + { + if ($errors[$i] instanceof Exception) + { + $app->enqueueMessage($errors[$i]->getMessage(), 'warning'); + } + else + { + $app->enqueueMessage($errors[$i], 'warning'); + } + } + } + else + { + // Save the data in the session. + $app->setUserState($this->option . '.edit.' . $this->context . '.data', $validData); + } + + $app->redirect($redirectUrl); + $app->close(); + } } From d10e8fc78bada54bdd6b72feffbb0c6b4c4420d5 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Wed, 14 Jun 2017 15:11:53 +0200 Subject: [PATCH 2/6] Add an extra check if the record can be edited. --- libraries/legacy/controller/form.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/libraries/legacy/controller/form.php b/libraries/legacy/controller/form.php index 681afbfd4a867..377d88d3b75f2 100644 --- a/libraries/legacy/controller/form.php +++ b/libraries/legacy/controller/form.php @@ -865,6 +865,17 @@ public function reload($key = null, $urlVar = null) $recordId = $this->input->getInt($urlVar); + if (!$this->allowEdit($data, $key)) + { + $app->redirect( + JRoute::_( + 'index.php?option=' . $this->option . '&view=' . $this->view_list + . $this->getRedirectToListAppend(), false + ) + ); + $app->close(); + } + // Populate the row id from the session. $data[$key] = $recordId; From aecca87b6d4ef2baee314db8cf436db533c29dc1 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Thu, 15 Jun 2017 08:58:20 +0200 Subject: [PATCH 3/6] Use the controller redirect functions --- libraries/legacy/controller/form.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/legacy/controller/form.php b/libraries/legacy/controller/form.php index 377d88d3b75f2..7b995c62364a7 100644 --- a/libraries/legacy/controller/form.php +++ b/libraries/legacy/controller/form.php @@ -867,13 +867,13 @@ public function reload($key = null, $urlVar = null) if (!$this->allowEdit($data, $key)) { - $app->redirect( + $this->setRedirect( JRoute::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false ) ); - $app->close(); + $this->redirect(); } // Populate the row id from the session. @@ -894,8 +894,8 @@ public function reload($key = null, $urlVar = null) { $app->enqueueMessage($model->getError(), 'error'); - $app->redirect($redirectUrl); - $app->close(); + $this->setRedirect($redirectUrl); + $this->redirect(); } // Test whether the data is valid. @@ -926,7 +926,7 @@ public function reload($key = null, $urlVar = null) $app->setUserState($this->option . '.edit.' . $this->context . '.data', $validData); } - $app->redirect($redirectUrl); - $app->close(); + $this->setRedirect($redirectUrl); + $this->redirect(); } } From edb3801b6fb81e8e6a28c1e4c84e92c332c77073 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Fri, 7 Jul 2017 13:27:29 +0200 Subject: [PATCH 4/6] Move type field to correct reload function --- .../com_fields/models/fields/type.php | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/administrator/components/com_fields/models/fields/type.php b/administrator/components/com_fields/models/fields/type.php index aa24240595cdb..d7f0ed7dbc494 100644 --- a/administrator/components/com_fields/models/fields/type.php +++ b/administrator/components/com_fields/models/fields/type.php @@ -68,25 +68,6 @@ function ($a, $b) } ); - // Reload the page when the type changes - $uri = clone JUri::getInstance('index.php'); - - // Removing the catid parameter from the actual URL and set it as - // return - $returnUri = clone JUri::getInstance(); - $returnUri->setVar('catid', null); - $uri->setVar('return', base64_encode($returnUri->toString())); - - // Setting the options - $uri->setVar('option', 'com_fields'); - $uri->setVar('task', 'field.storeform'); - $uri->setVar('context', 'com_fields.field'); - $uri->setVar('formcontrol', $this->form->getFormControl()); - $uri->setVar('userstatevariable', 'com_fields.edit.field.data'); - $uri->setVar('view', null); - $uri->setVar('layout', null); - - JFactory::getDocument()->addScriptDeclaration(" jQuery( document ).ready(function() { Joomla.loadingLayer('load'); @@ -94,8 +75,7 @@ function ($a, $b) function typeHasChanged(element){ Joomla.loadingLayer('show'); var cat = jQuery(element); - jQuery('input[name=task]').val('field.storeform'); - element.form.action='" . $uri . "'; + jQuery('input[name=task]').val('field.reload'); element.form.submit(); } "); From feb148b97dbab493f21a02a289ddf8d06df03692 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Fri, 7 Jul 2017 13:29:30 +0200 Subject: [PATCH 5/6] Only use filter function as we are not validating the data on reload If a new article is created and the title is not set and the category has changed, then the validated data is false, will not be set and the admin is loosing all its entered data. So we need to filter here only. --- libraries/legacy/controller/form.php | 29 ++-------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/libraries/legacy/controller/form.php b/libraries/legacy/controller/form.php index a2f5b686f86a0..26fe3071a6d4f 100644 --- a/libraries/legacy/controller/form.php +++ b/libraries/legacy/controller/form.php @@ -911,33 +911,8 @@ public function reload($key = null, $urlVar = null) $this->redirect(); } - // Test whether the data is valid. - $validData = $model->validate($form, $data); - - // Check for validation errors. - if ($validData === false) - { - // Get the validation messages. - $errors = $model->getErrors(); - - // Push up to three validation messages out to the user. - for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) - { - if ($errors[$i] instanceof Exception) - { - $app->enqueueMessage($errors[$i]->getMessage(), 'warning'); - } - else - { - $app->enqueueMessage($errors[$i], 'warning'); - } - } - } - else - { - // Save the data in the session. - $app->setUserState($this->option . '.edit.' . $this->context . '.data', $validData); - } + // Save the data in the session. + $app->setUserState($this->option . '.edit.' . $this->context . '.data', $form->filter($data)); $this->setRedirect($redirectUrl); $this->redirect(); From fb925e7cd3b0304cb778297cf2d08918670c9994 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Sat, 8 Jul 2017 17:19:21 +0200 Subject: [PATCH 6/6] Add the file to the remove script --- administrator/components/com_admin/script.php | 1 + 1 file changed, 1 insertion(+) diff --git a/administrator/components/com_admin/script.php b/administrator/components/com_admin/script.php index 927f116c8c4b0..8247bae5fe141 100644 --- a/administrator/components/com_admin/script.php +++ b/administrator/components/com_admin/script.php @@ -497,6 +497,7 @@ public function deleteUnexistingFiles() '/components/com_media/controller.php', '/components/com_media/helpers/index.html', '/components/com_media/helpers/media.php', + '/components/com_fields/controllers/field.php', // Joomla 3.0 '/administrator/components/com_admin/sql/updates/mysql/1.7.0-2011-06-06-2.sql', '/administrator/components/com_admin/sql/updates/mysql/1.7.0-2011-06-06.sql',