diff --git a/libraries/src/MVC/Controller/AdminController.php b/libraries/src/MVC/Controller/AdminController.php index a831e46600fa..826502d466b8 100644 --- a/libraries/src/MVC/Controller/AdminController.php +++ b/libraries/src/MVC/Controller/AdminController.php @@ -159,13 +159,7 @@ public function delete() $this->postDeleteHook($model, $cid); } - $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToList()); } /** @@ -241,13 +235,7 @@ public function publish() } } - $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToList()); } /** @@ -271,7 +259,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->getRedirectUrlToList(); if ($return === false) { // Reorder failed. @@ -317,7 +305,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->getRedirectUrlToList(); if ($return === false) { // Reorder failed @@ -358,10 +346,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->getRedirectUrlToList(), $message, 'error' ); @@ -371,13 +356,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->getRedirectUrlToList(), $message); return true; } @@ -452,7 +431,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->getRedirectUrlToList(); if ($return === false) { // Transition change failed. @@ -469,6 +448,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 getRedirectUrlToList(): 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. * diff --git a/libraries/src/MVC/Controller/FormController.php b/libraries/src/MVC/Controller/FormController.php index e6f252316054..1f463193e974 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->getRedirectUrlToList()); return false; } @@ -188,13 +182,7 @@ public function add() $this->app->setUserState($context . '.data', null); // Redirect to the edit screen. - $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend(), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToItem()); return true; } @@ -324,13 +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( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $key), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $key)); return false; } @@ -339,19 +321,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->getRedirectUrlToList()); } - // Redirect to the list screen. - $this->setRedirect(Route::_($url, false)); - return true; } @@ -394,13 +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( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToList()); return false; } @@ -410,13 +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( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return false; } @@ -425,13 +393,7 @@ public function edit($key = null, $urlVar = null) $this->holdEditId($context, $recordId); $this->app->setUserState($context . '.data', null); - $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return true; } @@ -456,6 +418,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 getRedirectUrlToItem($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 +474,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 getRedirectUrlToList(): 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. * @@ -578,13 +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( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return false; } @@ -599,13 +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( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToList()); return false; } @@ -670,13 +652,7 @@ public function save($key = null, $urlVar = null) $this->app->setUserState($context . '.data', $data); // Redirect back to the edit screen. - $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return false; } @@ -693,13 +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( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return false; } @@ -712,13 +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( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); return false; } @@ -738,13 +702,7 @@ public function save($key = null, $urlVar = null) $model->checkout($recordId); // Redirect back to the edit screen. - $this->setRedirect( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item - . $this->getRedirectToItemAppend($recordId, $urlVar), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToItem($recordId, $urlVar)); break; case 'save2new': @@ -753,13 +711,7 @@ public function save($key = null, $urlVar = null) $this->app->setUserState($context . '.data', 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->setRedirect($this->getRedirectUrlToItem(null, $urlVar)); break; default: @@ -767,18 +719,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->getRedirectUrlToList()); } - - // Redirect to the list screen. - $this->setRedirect(Route::_($url, false)); break; } @@ -823,22 +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( - Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_list - . $this->getRedirectToListAppend(), - false - ) - ); + $this->setRedirect($this->getRedirectUrlToList()); $this->redirect(); } // The redirect url - $redirectUrl = Route::_( - 'index.php?option=' . $this->option . '&view=' . $this->view_item . - $this->getRedirectToItemAppend($recordId, $urlVar), - false - ); + $redirectUrl = $this->getRedirectUrlToItem($recordId, $urlVar); /** @var \Joomla\CMS\Form\Form $form */ $form = $model->getForm($data, false);