Completed
Push — correct-classname-values ( f9b487 )
by Sam
08:29
created

get_template_global_variables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
1
<?php
2
3
use SilverStripe\ORM\DataModel;
4
5
/**
6
 * @package framework
7
 * @subpackage admin
8
 */
9
class AdminRootController extends Controller implements TemplateGlobalProvider {
10
11
	/**
12
 	 * Fallback admin URL in case this cannot be infered from Director.rules
13
	 *
14
	 * @var string
15
 	 * @config
16
	 */
17
	private static $url_base = 'admin';
18
19
	/**
20
	 * Convenience function to return the admin route config.
21
	 * Looks for the {@link Director::$rules} for the current admin Controller.
22
	 *
23
	 * @return string
24
	 */
25
	public static function get_admin_route() {
26
		$rules = Config::inst()->get('Director', 'rules');
27
		$adminRoute = array_search(__CLASS__, $rules);
28
		return $adminRoute ?: static::config()->url_base;
29
	}
30
31
	/**
32
	 * Returns the root admin URL for the site with trailing slash
33
	 *
34
	 * @return string
35
	 */
36
	public static function admin_url() {
37
		return self::get_admin_route() . '/';
38
	}
39
40
	/**
41
	 * @var string
42
	 * @config
43
	 * The LeftAndMain child that will be used as the initial panel to display if none is selected (i.e. if you
44
	 * visit /admin)
45
	 */
46
	private static $default_panel = 'SecurityAdmin';
47
48
	/**
49
	 * @var array
50
	 * Holds an array of url_pattern => controller k/v pairs, the same as Director::rules. However this is built
51
	 * dynamically from introspecting on all the classes that derive from LeftAndMain.
52
	 *
53
	 * Don't access this directly - always access via the rules() accessor below, which will build this array
54
	 * the first time it's accessed
55
	 */
56
	private static $_rules = null;
57
58
	/**
59
	 * Gets a list of url_pattern => controller k/v pairs for each LeftAndMain derived controller
60
	 */
61
	public static function rules() {
62
		if (self::$_rules === null) {
63
			self::$_rules = array();
64
65
			// Map over the array calling add_rule_for_controller on each
66
			$classes = CMSMenu::get_cms_classes(null, true, CMSMenu::URL_PRIORITY);
67
			array_map(array(__CLASS__, 'add_rule_for_controller'), $classes);
68
		}
69
		return self::$_rules;
70
	}
71
72
	/**
73
	 * Add the appropriate k/v pair to self::$rules for the given controller.
74
	 *
75
	 * @param string $controllerClass Name of class
76
	 */
77
	protected static function add_rule_for_controller($controllerClass) {
78
		$urlSegment = Config::inst()->get($controllerClass, 'url_segment', Config::FIRST_SET);
79
		$urlRule    = Config::inst()->get($controllerClass, 'url_rule', Config::FIRST_SET);
80
81
		if($urlSegment) {
82
			// Make director rule
83
			if($urlRule[0] == '/') $urlRule = substr($urlRule,1);
84
			$rule = $urlSegment . '//' . $urlRule;
85
86
			// ensure that the first call to add_rule_for_controller for a rule takes precedence
87
			if(!isset(self::$_rules[$rule])) self::$_rules[$rule] = $controllerClass;
88
		}
89
	}
90
91
	public function handleRequest(SS_HTTPRequest $request, DataModel $model) {
92
		// If this is the final portion of the request (i.e. the URL is just /admin), direct to the default panel
93
		if ($request->allParsed()) {
94
			$segment = Config::inst()->get($this->config()->default_panel, 'url_segment');
95
96
			$this->redirect(Controller::join_links(self::admin_url(), $segment, '/'));
97
			return $this->getResponse();
98
		}
99
100
		// Otherwise
101
		else {
102
			$rules = self::rules();
103
			foreach($rules as $pattern => $controller) {
104
				if(($arguments = $request->match($pattern, true)) !== false) {
105
					$controllerObj = Injector::inst()->create($controller);
106
					$controllerObj->setSession($this->session);
107
108
					return $controllerObj->handleRequest($request, $model);
109
				}
110
			}
111
		}
112
113
		return $this->httpError(404, 'Not found');
114
	}
115
116
	/**
117
	 * @return array Returns an array of strings of the method names of methods on the call that should be exposed
118
	 * as global variables in the templates.
119
	 */
120
	public static function get_template_global_variables() {
121
		return array(
122
			'adminURL' => 'admin_url'
123
		);
124
	}
125
}
126