2012-05-19 04:36:48 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataModel;
|
|
|
|
|
2013-10-15 00:26:23 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage admin
|
|
|
|
*/
|
2012-05-19 04:36:48 +02:00
|
|
|
class AdminRootController extends Controller {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
* @config
|
|
|
|
* The url base that all LeftAndMain derived panels will live under
|
|
|
|
* Won't automatically update the base route if you change this - that has to be done seperately
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_base = 'admin';
|
2012-05-19 04:36:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
* @config
|
2012-09-26 23:34:00 +02:00
|
|
|
* The LeftAndMain child that will be used as the initial panel to display if none is selected (i.e. if you
|
|
|
|
* visit /admin)
|
2012-05-19 04:36:48 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_panel = 'SecurityAdmin';
|
2012-05-19 04:36:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
* Holds an array of url_pattern => controller k/v pairs, the same as Director::rules. However this is built
|
|
|
|
* dynamically from introspecting on all the classes that derive from LeftAndMain.
|
|
|
|
*
|
|
|
|
* Don't access this directly - always access via the rules() accessor below, which will build this array
|
|
|
|
* the first time it's accessed
|
|
|
|
*/
|
|
|
|
private static $_rules = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list of url_pattern => controller k/v pairs for each LeftAndMain derived controller
|
|
|
|
*/
|
|
|
|
public static function rules() {
|
|
|
|
if (self::$_rules === null) {
|
|
|
|
self::$_rules = array();
|
|
|
|
|
|
|
|
// Map over the array calling add_rule_for_controller on each
|
2016-03-30 03:50:22 +02:00
|
|
|
$classes = CMSMenu::get_cms_classes(null, true, CMSMenu::URL_PRIORITY);
|
|
|
|
array_map(array(__CLASS__, 'add_rule_for_controller'), $classes);
|
2012-05-19 04:36:48 +02:00
|
|
|
}
|
|
|
|
return self::$_rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the appropriate k/v pair to self::$rules for the given controller.
|
2016-03-30 03:50:22 +02:00
|
|
|
*
|
|
|
|
* @param string $controllerClass Name of class
|
2012-05-19 04:36:48 +02:00
|
|
|
*/
|
|
|
|
protected static function add_rule_for_controller($controllerClass) {
|
|
|
|
$urlSegment = Config::inst()->get($controllerClass, 'url_segment', Config::FIRST_SET);
|
|
|
|
$urlRule = Config::inst()->get($controllerClass, 'url_rule', Config::FIRST_SET);
|
|
|
|
|
2012-05-22 06:57:04 +02:00
|
|
|
if($urlSegment) {
|
2012-05-19 04:36:48 +02:00
|
|
|
// Make director rule
|
|
|
|
if($urlRule[0] == '/') $urlRule = substr($urlRule,1);
|
|
|
|
$rule = $urlSegment . '//' . $urlRule;
|
|
|
|
|
2012-05-22 06:57:04 +02:00
|
|
|
// ensure that the first call to add_rule_for_controller for a rule takes precedence
|
|
|
|
if(!isset(self::$_rules[$rule])) self::$_rules[$rule] = $controllerClass;
|
2012-05-19 04:36:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleRequest(SS_HTTPRequest $request, DataModel $model) {
|
2012-05-19 04:36:48 +02:00
|
|
|
// If this is the final portion of the request (i.e. the URL is just /admin), direct to the default panel
|
|
|
|
if ($request->allParsed()) {
|
|
|
|
$base = $this->config()->url_base;
|
|
|
|
$segment = Config::inst()->get($this->config()->default_panel, 'url_segment');
|
|
|
|
|
2016-04-19 01:55:45 +02:00
|
|
|
$this->redirect(Controller::join_links($base, $segment, '/'));
|
2015-08-14 02:39:33 +02:00
|
|
|
return $this->getResponse();
|
2012-05-19 04:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise
|
|
|
|
else {
|
|
|
|
$rules = self::rules();
|
|
|
|
foreach($rules as $pattern => $controller) {
|
|
|
|
if(($arguments = $request->match($pattern, true)) !== false) {
|
|
|
|
$controllerObj = Injector::inst()->create($controller);
|
|
|
|
$controllerObj->setSession($this->session);
|
|
|
|
|
|
|
|
return $controllerObj->handleRequest($request, $model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-03 22:46:16 +02:00
|
|
|
|
|
|
|
return $this->httpError(404, 'Not found');
|
2012-05-19 04:36:48 +02:00
|
|
|
}
|
2012-05-22 06:57:04 +02:00
|
|
|
}
|