phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / CRM / Core / Page / Redirect.php
CommitLineData
5c2885f8 1<?php
2
3/**
4 * Placeholder page which generates a redirect
5 *
6 * @code
7 * <item>
8 * <path>civicrm/admin/options/case_type</path>
9 * <page_callback>CRM_Core_Page_Redirect</page_callback>
10 * <page_arguments>url=civicrm/foo/bar?whiz=bang&amp;passthru=%%passthru%%</page_arguments>
11 * </item>
12 * @endcoe
13 */
14class CRM_Core_Page_Redirect extends CRM_Core_Page {
15 function run($path = NULL, $pageArgs = array()) {
16 $url = self::createUrl($path, $_REQUEST, $pageArgs, TRUE);
17 // return $url;
18 CRM_Utils_System::redirect($url);
19 }
20
21 /**
22 * @param array $requestPath the parts of the path in the current page request
23 * @param array $requestArgs any GET arguments
24 * @param array $pageArgs the page_arguments registered in the router
25 * @param array $requestArgs any parameters passed as part of this page request
26 * @param bool $absolute whether to return an absolute URL
27 * @return string URL
28 */
29 public static function createUrl($requestPath, $requestArgs, $pageArgs, $absolute) {
30 if (empty($pageArgs['url'])) {
31 CRM_Core_Error::fatal('This page is configured as a redirect, but it does not have a target.');
32 }
33
34 $vars = array();
35 // note: %% isn't legal in a well-formed URL, so it's not a bad variable-delimiter
36 foreach ($requestPath as $pathPos => $pathPart) {
37 $vars["%%{$pathPos}%%"] = urlencode($pathPart);
38 }
39 foreach ($requestArgs as $var => $value) {
40 $vars["%%{$var}%%"] = urlencode($value);
41 }
42 $urlString = strtr($pageArgs['url'], $vars);
43 $urlString = preg_replace('/%%[a-zA-Z0-9]+%%/', '', $urlString);
44
45 $urlParts = parse_url($urlString);
46 $url = CRM_Utils_System::url(
47 $urlParts['path'],
48 CRM_Utils_Array::value('query', $urlParts, NULL),
49 $absolute,
50 CRM_Utils_Array::value('fragment', $urlParts, NULL)
51 );
52
53 return $url;
54 }
55}