From: totten@civicrm.org Date: Fri, 27 Jun 2014 07:34:19 +0000 (-0700) Subject: CRM_Core_Page_Redirect - Generic page-redirect handler X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=5c2885f84f169ea79f802849b412f17cec5c955e;hp=3b4339fd5fc6645a60f1d8e6235c4fb8ecbac011;p=civicrm-core.git CRM_Core_Page_Redirect - Generic page-redirect handler --- 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); + } +}