silverstripe-framework/core/control/ModelAsController.php
Ingo Schommer 7ad7f8dcf2 ENHANCEMENT Adjusted SearchForm, Debug, ErrorPage, SiteTree to using locales instead of lang codes
API CHANGE Changed Translatable datamodel to use locales ("en_US") instead of lang values ("en).
API CHANGE Changed Translatable::$default_lang to $default_locale, Translatable::$reading_lang to $reading_locale
API CHANGE Using "locale" instead of "lang" in Translatable::choose_site_lang() to auto-detect language from cookies or GET parameters
API CHANGE Deprecated Translatable::is_default_lang(), set_default_lang(), get_default_lang(), current_lang(), set_reading_lang(), get_reading_lang(), get_by_lang(), get_one_by_lang()
API CHANGE Removed Translatable::get_original() - with the new "translation groups" concept there no longer is an original for a translation
BUGFIX Updated MigrateTranslatableTask to new Locale based datamodel

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@73468 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-03-20 08:47:06 +00:00

129 lines
4.2 KiB
PHP

<?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.
*
* @package sapphire
*/
class ModelAsController extends Controller implements NestedController {
public function handleRequest($request) {
$this->pushCurrent();
$this->urlParams = $request->allParams();
$this->init();
// If the basic database hasn't been created, then build it.
if(!DB::isActive() || !ClassInfo::hasTable('SiteTree')) {
$this->response = new HTTPResponse();
$this->redirect("dev/build?returnURL=" . urlencode($_GET['url']));
return $this->response;
}
$result = $this->getNestedController();
if(is_object($result) && $result instanceOf RequestHandler) {
$result = $result->handleRequest($request);
}
$this->popCurrent();
return $result;
}
public function init() {
singleton('SiteTree')->extend('modelascontrollerInit', $this);
Director::set_site_mode('site');
}
public function getNestedController() {
if($this->urlParams['URLSegment']) {
$SQL_URLSegment = Convert::raw2sql($this->urlParams['URLSegment']);
$child = SiteTree::get_by_url($SQL_URLSegment);
if(!$child) {
if($child = $this->findOldPage($SQL_URLSegment)) {
$url = Controller::join_links(
Director::baseURL(),
$child->URLSegment,
isset($this->urlParams['Action']) ? $this->urlParams['Action'] : null,
isset($this->urlParams['ID']) ? $this->urlParams['ID'] : null,
isset($this->urlParams['OtherID']) ? $this->urlParams['OtherID'] : null
);
$response = new HTTPResponse();
$response->redirect($url, 301);
return $response;
}
$child = $this->get404Page();
}
if($child) {
if(isset($_REQUEST['debug'])) Debug::message("Using record #$child->ID of type $child->class with URL {$this->urlParams['URLSegment']}");
// set language
if($child->Locale) Translatable::set_reading_locale($child->Locale);
$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 {
return new HTTPResponse("The requested page couldn't be found.",404);
}
} else {
user_error("ModelAsController not geting a URLSegment. It looks like the site isn't redirecting to home", E_USER_ERROR);
}
}
protected function findOldPage($urlSegment) {
// Build the query by replacing `SiteTree` with `SiteTree_versions` in a regular query.
// 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);
foreach($versionedQuery->from as $k => $v) {
$versionedQuery->renameTable($k, $k . '_versions');
}
$versionedQuery->select = array("`SiteTree_versions`.RecordID");
$versionedQuery->where[] = "`SiteTree_versions`.`WasPublished` = 1 AND `URLSegment` = '$urlSegment'";
$versionedQuery->orderby = '`LastEdited` DESC, `SiteTree_versions`.`WasPublished`';
$versionedQuery->limit = 1;
$result = $versionedQuery->execute();
if($result->numRecords() == 1 && $redirectPage = $result->nextRecord()) {
$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
$doubleCheck = SiteTree::get_by_url($redirectObj->URLSegment);
if($doubleCheck) return $redirectObj;
}
}
return false;
}
protected function get404Page() {
if($page = DataObject::get_one("ErrorPage", "\"ErrorCode\" = '404'")) return $page;
else return DataObject::get_one("SiteTree", "\"URLSegment\" = '404'");
}
}
?>