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
|
2009-03-22 23:59:14 +01:00
|
|
|
* @subpackage control
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ModelAsController extends Controller implements NestedController {
|
|
|
|
|
2009-10-11 02:07:02 +02:00
|
|
|
/**
|
|
|
|
* Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and
|
|
|
|
* return it.
|
|
|
|
*
|
|
|
|
* @param SiteTree $siteTree The SiteTree object to find a controller for.
|
|
|
|
* @param string $action The optional action that was requested, so that action-specific controllers work.
|
|
|
|
* @return ContentController
|
|
|
|
*/
|
|
|
|
public static function controller_for(SiteTree $siteTree, $action = null) {
|
|
|
|
$controller = "{$siteTree->class}_Controller";
|
|
|
|
|
|
|
|
if($action && class_exists($controller . '_' . ucfirst($action))) {
|
|
|
|
$controller = $controller . '_' . ucfirst($action);
|
|
|
|
}
|
|
|
|
|
|
|
|
return class_exists($controller) ? new $controller($siteTree) : $siteTree;
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
public function handleRequest($request) {
|
2007-08-18 01:11:43 +02:00
|
|
|
$this->pushCurrent();
|
2009-10-11 02:07:03 +02:00
|
|
|
|
|
|
|
$this->request = $request;
|
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();
|
2009-03-10 23:08:52 +01:00
|
|
|
|
|
|
|
// If the basic database hasn't been created, then build it.
|
|
|
|
if(!DB::isActive() || !ClassInfo::hasTable('SiteTree')) {
|
|
|
|
$this->response = new HTTPResponse();
|
2009-04-29 01:40:35 +02:00
|
|
|
$this->redirect("dev/build?returnURL=" . (isset($_GET['url']) ? urlencode($_GET['url']) : ''));
|
|
|
|
$this->popCurrent();
|
2009-03-10 23:08:52 +01:00
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
2008-11-13 03:10:15 +01:00
|
|
|
$result = $this->getNestedController();
|
|
|
|
|
|
|
|
if(is_object($result) && $result instanceOf RequestHandler) {
|
|
|
|
$result = $result->handleRequest($request);
|
2008-05-15 10:44:37 +02:00
|
|
|
}
|
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);
|
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']);
|
2009-10-11 02:07:08 +02:00
|
|
|
$child = SiteTree::get_by_link($SQL_URLSegment);
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$child) {
|
2008-11-13 03:10:15 +01:00
|
|
|
if($child = $this->findOldPage($SQL_URLSegment)) {
|
|
|
|
$url = Controller::join_links(
|
|
|
|
Director::baseURL(),
|
|
|
|
$child->URLSegment,
|
2009-03-04 04:44:11 +01:00
|
|
|
isset($this->urlParams['Action']) ? $this->urlParams['Action'] : null,
|
|
|
|
isset($this->urlParams['ID']) ? $this->urlParams['ID'] : null,
|
|
|
|
isset($this->urlParams['OtherID']) ? $this->urlParams['OtherID'] : null
|
2008-11-13 03:10:15 +01:00
|
|
|
);
|
2009-03-04 04:44:11 +01:00
|
|
|
|
2008-11-13 03:10:15 +01:00
|
|
|
$response = new HTTPResponse();
|
|
|
|
$response->redirect($url, 301);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:03 +02:00
|
|
|
return ErrorPage::response_for(404, $this->request);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($child) {
|
|
|
|
if(isset($_REQUEST['debug'])) Debug::message("Using record #$child->ID of type $child->class with URL {$this->urlParams['URLSegment']}");
|
|
|
|
|
2009-01-16 05:07:17 +01:00
|
|
|
// set language
|
2009-05-17 07:36:01 +02:00
|
|
|
if($child->Locale) Translatable::set_current_locale($child->Locale);
|
2009-01-16 05:07:17 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$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-11-22 04:33:00 +01:00
|
|
|
return new HTTPResponse("The requested page couldn't be found.",404);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-13 03:10:15 +01:00
|
|
|
protected function findOldPage($urlSegment) {
|
2009-09-14 00:26:23 +02:00
|
|
|
// Build the query by replacing "SiteTree" with "SiteTree_versions" in a regular query.
|
2009-03-04 04:44:11 +01:00
|
|
|
// Note that this should *really* be handled by a more full-featured data mapper; as it stands
|
|
|
|
// this is a bit of a hack.
|
|
|
|
$origStage = Versioned::current_stage();
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
$versionedQuery = singleton('SiteTree')->extendedSQL('');
|
|
|
|
Versioned::reading_stage($origStage);
|
2008-11-13 03:10:15 +01:00
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
foreach($versionedQuery->from as $k => $v) {
|
|
|
|
$versionedQuery->renameTable($k, $k . '_versions');
|
|
|
|
}
|
2009-03-23 05:18:41 +01:00
|
|
|
$versionedQuery->select = array("\"SiteTree_versions\".\"RecordID\"");
|
|
|
|
$versionedQuery->where[] = "\"SiteTree_versions\".\"WasPublished\" = 1 AND \"URLSegment\" = '$urlSegment'";
|
|
|
|
$versionedQuery->orderby = "\"LastEdited\" DESC, \"SiteTree_versions\".\"WasPublished\"";
|
2009-03-04 04:44:11 +01:00
|
|
|
$versionedQuery->limit = 1;
|
|
|
|
|
2008-11-13 03:10:15 +01:00
|
|
|
$result = $versionedQuery->execute();
|
|
|
|
|
|
|
|
if($result->numRecords() == 1 && $redirectPage = $result->nextRecord()) {
|
2009-03-04 04:44:11 +01:00
|
|
|
$redirectObj = DataObject::get_by_id('SiteTree', $redirectPage['RecordID']);
|
|
|
|
if($redirectObj) {
|
|
|
|
// Double-check by querying this page in the same way that getNestedController() does. This
|
|
|
|
// will prevent query muck-ups from modules such as subsites
|
2009-10-11 02:07:08 +02:00
|
|
|
$doubleCheck = SiteTree::get_by_link($redirectObj->URLSegment);
|
2009-03-04 04:44:11 +01:00
|
|
|
if($doubleCheck) return $redirectObj;
|
|
|
|
}
|
2008-11-13 03:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
?>
|