2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ModelAsController will hand over all control to the appopriate model object
|
|
|
|
* It uses URLSegment to determine the right object. Also, if (ModelClass)_Controller exists,
|
|
|
|
* that controller will be used instead. It should be a subclass of ContentController.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* @package sapphire
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ModelAsController extends Controller implements NestedController {
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
public function handleRequest($request) {
|
2007-08-18 01:11:43 +02:00
|
|
|
$this->pushCurrent();
|
2008-08-09 05:19:54 +02:00
|
|
|
$this->urlParams = $request->allParams();
|
2007-08-18 01:11:43 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->init();
|
2007-08-17 05:09:46 +02:00
|
|
|
$nested = $this->getNestedController();
|
2008-05-15 10:44:37 +02:00
|
|
|
if(is_object($nested)) {
|
2008-08-09 07:35:08 +02:00
|
|
|
$result = $nested->handleRequest($request);
|
2008-05-15 10:44:37 +02:00
|
|
|
} else {
|
|
|
|
$result = $nested;
|
|
|
|
}
|
2007-08-18 01:11:43 +02:00
|
|
|
|
|
|
|
$this->popCurrent();
|
|
|
|
return $result;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function init() {
|
2007-08-16 08:27:32 +02:00
|
|
|
singleton('SiteTree')->extend('modelascontrollerInit', $this);
|
2008-02-26 02:22:29 +01:00
|
|
|
|
|
|
|
Director::set_site_mode('site');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getNestedController() {
|
|
|
|
if($this->urlParams['URLSegment']) {
|
2007-09-16 18:14:01 +02:00
|
|
|
$SQL_URLSegment = Convert::raw2sql($this->urlParams['URLSegment']);
|
|
|
|
if (Translatable::is_enabled()) {
|
2008-11-08 01:02:35 +01:00
|
|
|
$child = Translatable::get_one("SiteTree", "URLSegment = '$SQL_URLSegment'", false);
|
2007-09-16 18:14:01 +02:00
|
|
|
} else {
|
2008-11-08 01:02:35 +01:00
|
|
|
$child = DataObject::get_one("SiteTree", "URLSegment = '$SQL_URLSegment'", false);
|
2007-09-16 18:14:01 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$child) {
|
|
|
|
$child = $this->get404Page();
|
|
|
|
}
|
|
|
|
|
|
|
|
if($child) {
|
|
|
|
if(isset($_REQUEST['debug'])) Debug::message("Using record #$child->ID of type $child->class with URL {$this->urlParams['URLSegment']}");
|
|
|
|
|
|
|
|
$controllerClass = "{$child->class}_Controller";
|
|
|
|
|
|
|
|
if($this->urlParams['Action'] && ClassInfo::exists($controllerClass.'_'.$this->urlParams['Action'])) {
|
|
|
|
$controllerClass = $controllerClass.'_'.$this->urlParams['Action'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ClassInfo::exists($controllerClass)) {
|
|
|
|
$controller = new $controllerClass($child);
|
|
|
|
} else {
|
|
|
|
$controller = $child;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $controller;
|
|
|
|
} else {
|
2008-05-15 10:44:37 +02:00
|
|
|
return "The requested page couldn't be found.";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
user_error("ModelAsController not geting a URLSegment. It looks like the site isn't redirecting to home", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function get404Page() {
|
|
|
|
if($page = DataObject::get_one("ErrorPage", "ErrorCode = '404'")) return $page;
|
|
|
|
else return DataObject::get_one("SiteTree", "URLSegment = '404'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|