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();
|
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);
|
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']);
|
2009-01-10 12:35:50 +01:00
|
|
|
$child = SiteTree::get_by_url($SQL_URLSegment);
|
|
|
|
|
Merged, debugged and enhanced Translatable patches from branches/translatable at r64523, r64523, 64523, thanks wakeless!
API CHANGE Changed Translatable schema from auxilliary tables (SiteTree_lang, SiteTree_lang_Live) to automatically filtered records on the original table (SiteTree, SiteTree_Live), using $Lang and $OriginalID properties. Incompatible update to old schema, migration script is in the works.
API CHANGE Removed Translatable::get_one(), Translatable::write()
ENHANCEMENT Simplified Translatable tree generation by using getSiteTreeFor() in CMSMain->createtranslation()
ENHANCEMENT Added AllChildrenIncludingDeleted(), augmentNumChildrenCountQuery(), augmentAllChildrenIncludingDeleted(), augmentStageChildren() to Translatable class to allow for more stable tree generation.
ENHANCEMENT Moved definition of Translatable schema from augmentDatabase() to Translatable->extraStatics()
ENHANCEMENT Changes to the CMS language selection refresh the whole admin interface instead of the tree only. This way we can add a URL parameter ?lang=<lang> to /admin, which makes the specific language bookmarkable and reloadable. Changes to LangSelector.js
ENHANCEMENT Added fallback to ModelAsController->getNestedController() to fetch page with matching URLSegment but different language in case no page is found in the current language.
ENHANCEMENT Added helper methods to Translatable: getTranslation(), hasTranslation(), isTranslation(), findOriginalIDs()
ENHANCEMENT Getters and setters for Translatable->getOriginalPage() etc.
ENHANCEMENT Hooking Translatable into ModelAsController and ContentController initialization in order to call choose_site_lang()
ENHANCEMENT Simplified Translatable->augmentSQL(), augmentWrite() by not using auxilliary tables
ENHANCEMENT Showing clickable links for Translations in Translatable->updateCMSFields()
BUGFIX Modifying Hierarchy/SiteTree Children getters to accept optional "context" which can be used to set a language explicitly through the $Lang property, rather than implicitly reyling on the static Translatable::current_lang()
BUGFIX Fixed TranslatableTest to work with new datamodel
BUGFIX Temporarily disabled cookie/session selection in Translatable::choose_site_lang() until we have a good test suite for the side effects.
MINOR Added "untranslated" CSS styles to tree nodes and marking them as inactive/grey
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@69959 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-01-10 13:15:30 +01:00
|
|
|
// fallback to default language
|
|
|
|
// @todo Migrate into extension point and module
|
|
|
|
if(!$child && Translatable::is_enabled()) {
|
|
|
|
$child = Translatable::get_one_by_lang('SiteTree', Translatable::default_lang(), "URLSegment = '{$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,
|
|
|
|
$this->urlParams['Action'],
|
|
|
|
$this->urlParams['ID'],
|
|
|
|
$this->urlParams['OtherID']
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = new HTTPResponse();
|
|
|
|
$response->redirect($url, 301);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$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-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) {
|
|
|
|
$versionedQuery = new SQLQuery (
|
2008-11-24 00:28:16 +01:00
|
|
|
'"RecordID"', '"SiteTree_versions"',
|
|
|
|
"\"WasPublished\" AND \"URLSegment\" = '$urlSegment'",
|
2008-11-23 01:31:06 +01:00
|
|
|
'"LastEdited" DESC, "WasPublished"',
|
2008-11-13 03:10:15 +01:00
|
|
|
null, null, 1
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $versionedQuery->execute();
|
|
|
|
|
|
|
|
if($result->numRecords() == 1 && $redirectPage = $result->nextRecord()) {
|
|
|
|
if($redirectObj = DataObject::get_by_id('SiteTree', $redirectPage['RecordID'])) return $redirectObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected function get404Page() {
|
2008-11-24 00:28:16 +01:00
|
|
|
if($page = DataObject::get_one("ErrorPage", "\"ErrorCode\" = '404'")) return $page;
|
|
|
|
else return DataObject::get_one("SiteTree", "\"URLSegment\" = '404'");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|