From de528824f93341c8909e42592f51f4fc6ed60c45 Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Wed, 3 Dec 2025 15:32:46 +0700 Subject: [PATCH 1/4] Add redirect URLs utility methods for FormController --- .../src/MVC/Controller/FormController.php | 148 +++++++----------- 1 file changed, 57 insertions(+), 91 deletions(-) diff --git a/libraries/src/MVC/Controller/FormController.php b/libraries/src/MVC/Controller/FormController.php index e6f252316054b..7ddee8bdc1e7a 100644 --- a/libraries/src/MVC/Controller/FormController.php +++ b/libraries/src/MVC/Controller/FormController.php @@ -173,13 +173,7 @@ public function add() // Set the internal error and also the redirect error. $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED'), 'error'); - $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) - ); + $this->setRedirect($this->getRedirectToListUrl()); return false; } @@ -189,11 +183,7 @@ public function add() // Redirect to the edit screen. $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend(), - false - ) + $this->getRedirectToItemUrl() ); return true; @@ -325,11 +315,7 @@ public function cancel($key = null) $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error'); $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $key), - false - ) + $this->getRedirectToItemUrl($recordId, $key) ); return false; @@ -339,19 +325,17 @@ public function cancel($key = null) $this->releaseEditId($context, $recordId); $this->app->setUserState($context . '.data', null); - $url = 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(); - // Check if there is a return value $return = $this->input->get('return', null, 'base64'); if (!\is_null($return) && Uri::isInternal(base64_decode($return))) { - $url = base64_decode($return); + // If a return param exists and is internal, redirect to it. + $this->setRedirect(Route::_(base64_decode($return), false)); + } else { + // Otherwise use the standard list URL helper. + $this->setRedirect($this->getRedirectToListUrl()); } - // Redirect to the list screen. - $this->setRedirect(Route::_($url, false)); - return true; } @@ -395,11 +379,7 @@ public function edit($key = null, $urlVar = null) $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'), 'error'); $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) + $this->getRedirectToListUrl() ); return false; @@ -411,11 +391,7 @@ public function edit($key = null, $urlVar = null) $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKOUT_FAILED', $model->getError()), 'error'); $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) + $this->getRedirectToItemUrl($recordId, $urlVar) ); return false; @@ -426,11 +402,7 @@ public function edit($key = null, $urlVar = null) $this->app->setUserState($context . '.data', null); $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) + $this->getRedirectToItemUrl($recordId, $urlVar) ); return true; @@ -456,6 +428,22 @@ public function getModel($name = '', $prefix = '', $config = ['ignore_request' = return parent::getModel($name, $prefix, $config); } + /** + * Gets the redirect URL to an item. + * + * @param integer $recordId The primary key id for the item. + * @param string $urlVar The name of the URL variable for the id. + * + * @return string The redirect URL to the item. + * + * @since __DEPLOY_VERSION__ + */ + protected function getRedirectToItemUrl($recordId = null, $urlVar = 'id'):string + { + return Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item + . $this->getRedirectToItemAppend($recordId, $urlVar), false); + } + /** * Gets the URL arguments to append to an item redirect. * @@ -496,6 +484,22 @@ protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id') return $append; } + /** + * Gets the redirect URL to a list. + * + * @return string The redirect URL to the list. + * + * @since __DEPLOY_VERSION__ + */ + protected function getRedirectToListUrl(): string + { + return Route::_( + 'index.php?option=' . $this->option . '&view=' . $this->view_list + . $this->getRedirectToListAppend(), + false + ); + } + /** * Gets the URL arguments to append to a list redirect. * @@ -579,11 +583,7 @@ public function save($key = null, $urlVar = null) $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error'); $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) + $this->getRedirectToItemUrl($recordId, $urlVar) ); return false; @@ -600,11 +600,7 @@ public function save($key = null, $urlVar = null) $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error'); $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) + $this->getRedirectToListUrl() ); return false; @@ -671,11 +667,7 @@ public function save($key = null, $urlVar = null) // Redirect back to the edit screen. $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) + $this->getRedirectToItemUrl($recordId, $urlVar) ); return false; @@ -694,11 +686,7 @@ public function save($key = null, $urlVar = null) $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error'); $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) + $this->getRedirectToItemUrl($recordId, $urlVar) ); return false; @@ -713,11 +701,7 @@ public function save($key = null, $urlVar = null) $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error'); $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) + $this->getRedirectToItemUrl($recordId, $urlVar) ); return false; @@ -739,11 +723,7 @@ public function save($key = null, $urlVar = null) // Redirect back to the edit screen. $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) + $this->getRedirectToItemUrl($recordId, $urlVar) ); break; @@ -754,11 +734,7 @@ public function save($key = null, $urlVar = null) // Redirect back to the edit screen. $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend(null, $urlVar), - false - ) + $this->getRedirectToItemUrl(null, $urlVar) ); break; @@ -767,18 +743,16 @@ public function save($key = null, $urlVar = null) $this->releaseEditId($context, $recordId); $this->app->setUserState($context . '.data', null); - $url = 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(); - // Check if there is a return value $return = $this->input->get('return', null, 'base64'); if (!\is_null($return) && Uri::isInternal(base64_decode($return))) { - $url = base64_decode($return); + // Route the provided return URL if internal + $this->setRedirect(Route::_(base64_decode($return), false)); + } else { + // Otherwise use the standard list URL helper. + $this->setRedirect($this->getRedirectToListUrl()); } - - // Redirect to the list screen. - $this->setRedirect(Route::_($url, false)); break; } @@ -824,21 +798,13 @@ public function reload($key = null, $urlVar = null) // Check if it is allowed to edit or create the data if (($recordId && !$this->allowEdit($data, $key)) || (!$recordId && !$this->allowAdd($data))) { $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) + $this->getRedirectToListUrl() ); $this->redirect(); } // The redirect url - $redirectUrl = Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item . - $this->getRedirectToItemAppend($recordId, $urlVar), - false - ); + $redirectUrl = $this->getRedirectToItemUrl($recordId, $urlVar); /** @var \Joomla\CMS\Form\Form $form */ $form = $model->getForm($data, false); From d0ab493b1d32e9fcace0fc1671081cc78ded05c5 Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Wed, 3 Dec 2025 15:44:48 +0700 Subject: [PATCH 2/4] CS --- libraries/src/MVC/Controller/FormController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/src/MVC/Controller/FormController.php b/libraries/src/MVC/Controller/FormController.php index 7ddee8bdc1e7a..6ccfb4f622b61 100644 --- a/libraries/src/MVC/Controller/FormController.php +++ b/libraries/src/MVC/Controller/FormController.php @@ -438,7 +438,7 @@ public function getModel($name = '', $prefix = '', $config = ['ignore_request' = * * @since __DEPLOY_VERSION__ */ - protected function getRedirectToItemUrl($recordId = null, $urlVar = 'id'):string + protected function getRedirectToItemUrl($recordId = null, $urlVar = 'id'): string { return Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false); From ec32d760f6e31a2cd6e8f10815dff787e8c7302d Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Wed, 3 Dec 2025 19:50:48 +0700 Subject: [PATCH 3/4] Add getRedirectToListUrl method to AdminController --- .../src/MVC/Controller/AdminController.php | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/libraries/src/MVC/Controller/AdminController.php b/libraries/src/MVC/Controller/AdminController.php index a831e46600fad..74774d61c6e59 100644 --- a/libraries/src/MVC/Controller/AdminController.php +++ b/libraries/src/MVC/Controller/AdminController.php @@ -160,11 +160,7 @@ public function delete() } $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) + $this->getRedirectToListUrl() ); } @@ -241,13 +237,7 @@ public function publish() } } - $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) - ); + $this->setRedirect($this->getRedirectToListUrl()); } /** @@ -271,7 +261,7 @@ public function reorder() $model = $this->getModel(); $return = $model->reorder($ids, $inc); - $redirect = Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false); + $redirect = $this->getRedirectToListUrl(); if ($return === false) { // Reorder failed. @@ -317,7 +307,7 @@ public function saveorder() // Save the ordering $return = $model->saveorder($pks, $order); - $redirect = Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false); + $redirect = $this->getRedirectToListUrl(); if ($return === false) { // Reorder failed @@ -358,10 +348,7 @@ public function checkin() // Checkin failed. $message = Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()); $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), - false - ), + $this->getRedirectToListUrl(), $message, 'error' ); @@ -371,13 +358,7 @@ public function checkin() // Checkin succeeded. $message = Text::plural($this->text_prefix . '_N_ITEMS_CHECKED_IN', \count($ids)); - $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), - false - ), - $message - ); + $this->setRedirect($this->getRedirectToListUrl(), $message); return true; } @@ -452,7 +433,7 @@ public function runTransition() $return = $model->executeTransition($pks, $transitionId); - $redirect = Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false); + $redirect = $this->getRedirectToListUrl(); if ($return === false) { // Transition change failed. @@ -469,6 +450,21 @@ public function runTransition() return true; } + /** + * Gets the URL to redirect to the list view. + * + * @return string The redirect URL. + * + * @since __DEPLOY_VERSION__ + */ + protected function getRedirectToListUrl(): string + { + return Route::_( + 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), + false + ); + } + /** * Gets the URL arguments to append to a list redirect. * From 0b1c0693cfcf48f583536a4c453073efe57a4e7d Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Fri, 5 Dec 2025 10:42:57 +0700 Subject: [PATCH 4/4] Rename methods +CS --- .../src/MVC/Controller/AdminController.php | 18 +++--- .../src/MVC/Controller/FormController.php | 64 ++++++------------- 2 files changed, 27 insertions(+), 55 deletions(-) diff --git a/libraries/src/MVC/Controller/AdminController.php b/libraries/src/MVC/Controller/AdminController.php index 74774d61c6e59..826502d466b8b 100644 --- a/libraries/src/MVC/Controller/AdminController.php +++ b/libraries/src/MVC/Controller/AdminController.php @@ -159,9 +159,7 @@ public function delete() $this->postDeleteHook($model, $cid); } - $this->setRedirect( - $this->getRedirectToListUrl() - ); + $this->setRedirect($this->getRedirectUrlToList()); } /** @@ -237,7 +235,7 @@ public function publish() } } - $this->setRedirect($this->getRedirectToListUrl()); + $this->setRedirect($this->getRedirectUrlToList()); } /** @@ -261,7 +259,7 @@ public function reorder() $model = $this->getModel(); $return = $model->reorder($ids, $inc); - $redirect = $this->getRedirectToListUrl(); + $redirect = $this->getRedirectUrlToList(); if ($return === false) { // Reorder failed. @@ -307,7 +305,7 @@ public function saveorder() // Save the ordering $return = $model->saveorder($pks, $order); - $redirect = $this->getRedirectToListUrl(); + $redirect = $this->getRedirectUrlToList(); if ($return === false) { // Reorder failed @@ -348,7 +346,7 @@ public function checkin() // Checkin failed. $message = Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()); $this->setRedirect( - $this->getRedirectToListUrl(), + $this->getRedirectUrlToList(), $message, 'error' ); @@ -358,7 +356,7 @@ public function checkin() // Checkin succeeded. $message = Text::plural($this->text_prefix . '_N_ITEMS_CHECKED_IN', \count($ids)); - $this->setRedirect($this->getRedirectToListUrl(), $message); + $this->setRedirect($this->getRedirectUrlToList(), $message); return true; } @@ -433,7 +431,7 @@ public function runTransition() $return = $model->executeTransition($pks, $transitionId); - $redirect = $this->getRedirectToListUrl(); + $redirect = $this->getRedirectUrlToList(); if ($return === false) { // Transition change failed. @@ -457,7 +455,7 @@ public function runTransition() * * @since __DEPLOY_VERSION__ */ - protected function getRedirectToListUrl(): string + protected function getRedirectUrlToList(): string { return Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), diff --git a/libraries/src/MVC/Controller/FormController.php b/libraries/src/MVC/Controller/FormController.php index 6ccfb4f622b61..1f463193e9745 100644 --- a/libraries/src/MVC/Controller/FormController.php +++ b/libraries/src/MVC/Controller/FormController.php @@ -173,7 +173,7 @@ public function add() // Set the internal error and also the redirect error. $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED'), 'error'); - $this->setRedirect($this->getRedirectToListUrl()); + $this->setRedirect($this->getRedirectUrlToList()); return false; } @@ -182,9 +182,7 @@ public function add() $this->app->setUserState($context . '.data', null); // Redirect to the edit screen. - $this->setRedirect( - $this->getRedirectToItemUrl() - ); + $this->setRedirect($this->getRedirectUrlToItem()); return true; } @@ -314,9 +312,7 @@ public function cancel($key = null) // Check-in failed, go back to the record and display a notice. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error'); - $this->setRedirect( - $this->getRedirectToItemUrl($recordId, $key) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $key)); return false; } @@ -333,7 +329,7 @@ public function cancel($key = null) $this->setRedirect(Route::_(base64_decode($return), false)); } else { // Otherwise use the standard list URL helper. - $this->setRedirect($this->getRedirectToListUrl()); + $this->setRedirect($this->getRedirectUrlToList()); } return true; @@ -378,9 +374,7 @@ public function edit($key = null, $urlVar = null) if (!$this->allowEdit([$key => $recordId], $key)) { $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'), 'error'); - $this->setRedirect( - $this->getRedirectToListUrl() - ); + $this->setRedirect($this->getRedirectUrlToList()); return false; } @@ -390,9 +384,7 @@ public function edit($key = null, $urlVar = null) // Check-out failed, display a notice but allow the user to see the record. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKOUT_FAILED', $model->getError()), 'error'); - $this->setRedirect( - $this->getRedirectToItemUrl($recordId, $urlVar) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return false; } @@ -401,9 +393,7 @@ public function edit($key = null, $urlVar = null) $this->holdEditId($context, $recordId); $this->app->setUserState($context . '.data', null); - $this->setRedirect( - $this->getRedirectToItemUrl($recordId, $urlVar) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return true; } @@ -438,7 +428,7 @@ public function getModel($name = '', $prefix = '', $config = ['ignore_request' = * * @since __DEPLOY_VERSION__ */ - protected function getRedirectToItemUrl($recordId = null, $urlVar = 'id'): string + protected function getRedirectUrlToItem($recordId = null, $urlVar = 'id'): string { return Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false); @@ -491,7 +481,7 @@ protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id') * * @since __DEPLOY_VERSION__ */ - protected function getRedirectToListUrl(): string + protected function getRedirectUrlToList(): string { return Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list @@ -582,9 +572,7 @@ public function save($key = null, $urlVar = null) // Check-in failed. Go back to the item and display a notice. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error'); - $this->setRedirect( - $this->getRedirectToItemUrl($recordId, $urlVar) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return false; } @@ -599,9 +587,7 @@ public function save($key = null, $urlVar = null) if (!$this->allowSave($data, $key)) { $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error'); - $this->setRedirect( - $this->getRedirectToListUrl() - ); + $this->setRedirect($this->getRedirectUrlToList()); return false; } @@ -666,9 +652,7 @@ public function save($key = null, $urlVar = null) $this->app->setUserState($context . '.data', $data); // Redirect back to the edit screen. - $this->setRedirect( - $this->getRedirectToItemUrl($recordId, $urlVar) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return false; } @@ -685,9 +669,7 @@ public function save($key = null, $urlVar = null) // Redirect back to the edit screen. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error'); - $this->setRedirect( - $this->getRedirectToItemUrl($recordId, $urlVar) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return false; } @@ -700,9 +682,7 @@ public function save($key = null, $urlVar = null) // Check-in failed, so go back to the record and display a notice. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error'); - $this->setRedirect( - $this->getRedirectToItemUrl($recordId, $urlVar) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return false; } @@ -722,9 +702,7 @@ public function save($key = null, $urlVar = null) $model->checkout($recordId); // Redirect back to the edit screen. - $this->setRedirect( - $this->getRedirectToItemUrl($recordId, $urlVar) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); break; case 'save2new': @@ -733,9 +711,7 @@ public function save($key = null, $urlVar = null) $this->app->setUserState($context . '.data', null); // Redirect back to the edit screen. - $this->setRedirect( - $this->getRedirectToItemUrl(null, $urlVar) - ); + $this->setRedirect($this->getRedirectUrlToItem(null, $urlVar)); break; default: @@ -751,7 +727,7 @@ public function save($key = null, $urlVar = null) $this->setRedirect(Route::_(base64_decode($return), false)); } else { // Otherwise use the standard list URL helper. - $this->setRedirect($this->getRedirectToListUrl()); + $this->setRedirect($this->getRedirectUrlToList()); } break; } @@ -797,14 +773,12 @@ public function reload($key = null, $urlVar = null) // Check if it is allowed to edit or create the data if (($recordId && !$this->allowEdit($data, $key)) || (!$recordId && !$this->allowAdd($data))) { - $this->setRedirect( - $this->getRedirectToListUrl() - ); + $this->setRedirect($this->getRedirectUrlToList()); $this->redirect(); } // The redirect url - $redirectUrl = $this->getRedirectToItemUrl($recordId, $urlVar); + $redirectUrl = $this->getRedirectUrlToItem($recordId, $urlVar); /** @var \Joomla\CMS\Form\Form $form */ $form = $model->getForm($data, false);