4498ecd0cd1a17f22da9ae2b69ea3e57c5d07c26
[civicrm-core.git] / CRM / Core / Page / Redirect.php
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 */
14 class CRM_Core_Page_Redirect extends CRM_Core_Page {
15
16 /**
17 * Run page
18 *
19 * @param string $path
20 * @param array $pageArgs
21 *
22 * @return string|void
23 */
24 public function run($path = NULL, $pageArgs = array()) {
25 $url = self::createUrl($path, $_REQUEST, $pageArgs, TRUE);
26 // return $url;
27 CRM_Utils_System::redirect($url);
28 }
29
30 /**
31 * @param array $requestPath
32 * The parts of the path in the current page request.
33 * @param array $requestArgs
34 * Any GET arguments.
35 * @param array $pageArgs
36 * The page_arguments registered in the router.
37 * @param bool $absolute
38 * Whether to return an absolute URL.
39 * @return string
40 * URL
41 */
42 public static function createUrl($requestPath, $requestArgs, $pageArgs, $absolute) {
43 if (empty($pageArgs['url'])) {
44 CRM_Core_Error::fatal('This page is configured as a redirect, but it does not have a target.');
45 }
46
47 $vars = array();
48 // note: %% isn't legal in a well-formed URL, so it's not a bad variable-delimiter
49 foreach ($requestPath as $pathPos => $pathPart) {
50 $vars["%%{$pathPos}%%"] = urlencode($pathPart);
51 }
52 foreach ($requestArgs as $var => $value) {
53 $vars["%%{$var}%%"] = urlencode($value);
54 }
55 $urlString = strtr($pageArgs['url'], $vars);
56 $urlString = preg_replace('/%%[a-zA-Z0-9]+%%/', '', $urlString);
57
58 $urlParts = parse_url($urlString);
59 $url = CRM_Utils_System::url(
60 $urlParts['path'],
61 CRM_Utils_Array::value('query', $urlParts, NULL),
62 $absolute,
63 CRM_Utils_Array::value('fragment', $urlParts, NULL)
64 );
65
66 return $url;
67 }
68
69 }