From 5c2885f84f169ea79f802849b412f17cec5c955e Mon Sep 17 00:00:00 2001 From: "totten@civicrm.org" Date: Fri, 27 Jun 2014 00:34:19 -0700 Subject: [PATCH] CRM_Core_Page_Redirect - Generic page-redirect handler --- CRM/Core/Page/Redirect.php | 55 ++++++++++++++++++++ tests/phpunit/CRM/Core/Page/RedirectTest.php | 50 ++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 CRM/Core/Page/Redirect.php create mode 100644 tests/phpunit/CRM/Core/Page/RedirectTest.php diff --git a/CRM/Core/Page/Redirect.php b/CRM/Core/Page/Redirect.php new file mode 100644 index 0000000000..1441ada009 --- /dev/null +++ b/CRM/Core/Page/Redirect.php @@ -0,0 +1,55 @@ + + * civicrm/admin/options/case_type + * CRM_Core_Page_Redirect + * url=civicrm/foo/bar?whiz=bang&passthru=%%passthru%% + * + * @endcoe + */ +class CRM_Core_Page_Redirect extends CRM_Core_Page { + function run($path = NULL, $pageArgs = array()) { + $url = self::createUrl($path, $_REQUEST, $pageArgs, TRUE); + // return $url; + CRM_Utils_System::redirect($url); + } + + /** + * @param array $requestPath the parts of the path in the current page request + * @param array $requestArgs any GET arguments + * @param array $pageArgs the page_arguments registered in the router + * @param array $requestArgs any parameters passed as part of this page request + * @param bool $absolute whether to return an absolute URL + * @return string URL + */ + public static function createUrl($requestPath, $requestArgs, $pageArgs, $absolute) { + if (empty($pageArgs['url'])) { + CRM_Core_Error::fatal('This page is configured as a redirect, but it does not have a target.'); + } + + $vars = array(); + // note: %% isn't legal in a well-formed URL, so it's not a bad variable-delimiter + foreach ($requestPath as $pathPos => $pathPart) { + $vars["%%{$pathPos}%%"] = urlencode($pathPart); + } + foreach ($requestArgs as $var => $value) { + $vars["%%{$var}%%"] = urlencode($value); + } + $urlString = strtr($pageArgs['url'], $vars); + $urlString = preg_replace('/%%[a-zA-Z0-9]+%%/', '', $urlString); + + $urlParts = parse_url($urlString); + $url = CRM_Utils_System::url( + $urlParts['path'], + CRM_Utils_Array::value('query', $urlParts, NULL), + $absolute, + CRM_Utils_Array::value('fragment', $urlParts, NULL) + ); + + return $url; + } +} diff --git a/tests/phpunit/CRM/Core/Page/RedirectTest.php b/tests/phpunit/CRM/Core/Page/RedirectTest.php new file mode 100644 index 0000000000..30e55b42bf --- /dev/null +++ b/tests/phpunit/CRM/Core/Page/RedirectTest.php @@ -0,0 +1,50 @@ +assertEquals($expectedUrl, $actualUrl); + } +} -- 2.25.1