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