adds nested URL functionality to SilverStripe and Sapphire.
Note: Theres a newer, changing version available here
static $test_servers = array();
static protected $environment_type;
+
+ protected static $fullUrl;
/**
* Sets the site mode (if it is the public site or the cms),
*/
function direct($url) { if(isset($_GET['debug_profile'])) Profiler::mark("Director","direct");+ self::$fullUrl = $url;
$controllerObj = Director::getControllerForURL($url);
if(is_string($controllerObj) && substr($controllerObj,0,9) == 'redirect:') {return false;
}
+
+ /**
+ * Returns the full URL as requested
+ *
+ * @return string
+ */
+ public static function getUrl(){+ return self::$fullUrl == '/' ? RootURLController::get_homepage_urlsegment() : self::$fullUrl;
+ }
/**
* @deprecated use isDev() instead
public function __construct($dataRecord) {$this->dataRecord = $dataRecord;
$this->failover = $this->dataRecord;
+
+ //So we have the correct Page ID
+ $this->dataRecord->setCurrentPageID($this->dataRecord->ID);
+
+ //And the correct Section IDs
+ $currentSections = array();
+ $nextID = $this->dataRecord->ID;
+ while($nextID) {+ $currentSections[] = $nextID;
+ $nextID = DB::query("SELECT ParentID FROM SiteTree WHERE ID = $nextID")->value();+ }
+
+ $this->dataRecord->setCurrentPageIDs($currentSections);
parent::__construct();
}
}
public function getNestedController() {- if($this->urlParams['URLSegment']) {- $SQL_URLSegment = Convert::raw2sql($this->urlParams['URLSegment']);
- if (Translatable::is_enabled()) {- $child = Translatable::get_one("SiteTree", "URLSegment = '$SQL_URLSegment'");- } else {- $child = DataObject::get_one("SiteTree", "URLSegment = '$SQL_URLSegment'");- }
- if(!$child) {- $child = $this->get404Page();
- }
+ //strip lead and end slashes
+ $url = preg_replace(array('/^\//', '/\/$/'), '', Director::getUrl());+ $segments = explode('/', $url);- 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'];
+ $SQL_URLSegment = Convert::raw2sql($segments[0]);
+ if(Translatable::is_enabled()){+ $model = Translatable::get_one('SiteTree', 'URLSegment = \'' . $SQL_URLSegment . '\'');+ } else {+ $model = DataObject::get_one('SiteTree', 'URLSegment = \'' . $SQL_URLSegment . '\'');+ }
+
+ if($model){+
+ $segment = 1;
+ if(Translatable::is_enabled()){+ while(isset($segments[$segment]) && $obj = Translatable::get_one('SiteTree', 'URLSegment = \'' . Convert::raw2sql($segments[$segment]) . + '\' AND ParentID = ' . $model->ID)){+ $model = $obj;
+ $segment++;
}
-
- if(ClassInfo::exists($controllerClass)) {- $controller = new $controllerClass($child);
- } else {- $controller = $child;
+ } else {+ while(isset($segments[$segment]) && $obj = DataObject::get_one('SiteTree', 'URLSegment = \'' . Convert::raw2sql($segments[$segment]) . + '\' AND ParentID = ' . $model->ID)){+ $model = $obj;
+ $segment++;
}
- $controller->setURLParams($this->urlParams);
+ }
- return $controller;
- } else {- return "The requested page couldn't be found.";
+ if(isset($_REQUEST['debug'])) Debug::message("Using record #$model->ID of type $model->class - routed from $url");+
+ $controllerClass = "{$model->class}_Controller";+
+ $segment--;
+ foreach($this->urlParams as $key=>$param){+ $this->urlParams[$key] = isset($segments[$segment]) ? $segments[$segment] : NULL;
+ $segment++;
}
+ if($this->urlParams['Action'] && ClassInfo::exists($controllerClass.'_'.$this->urlParams['Action'])) {+ $controllerClass = $controllerClass.'_'.$this->urlParams['Action'];
+ }
+
+ $controller = ClassInfo::exists($controllerClass) ? new $controllerClass($model) : $model;
+ $controller->setURLParams($this->urlParams);
+
+ return $controller;
} else {- user_error("ModelAsController not geting a URLSegment. It looks like the site isn't redirecting to home", E_USER_ERROR);+ $model = $this->get404Page();
}
}
}
}
}
+
+ /**
+ * Set the curent page ID
+ *
+ * @param int ID
+ * @return void
+ */
+ public function setCurrentPageID($id){+ self::$currentPageID = (int) $id;
+ }
+
+ /**
+ * Sets the current Section IDs
+ *
+ * @param array $sections
+ * @return void
+ */
+ public function setCurrentPageIDs($sections){+ self::$currentSectionIDs = $sections;
+ }
/**
),
$tabMeta = new Tab('Meta-data', new FieldGroup(_t('SiteTree.URL', "URL"),- new LabelField("http://www.yoursite.com/"),+ new LabelField("http://www.yoursite.com/" . $this->getURLBase()), //new TextField("URLSegment",""), new UniqueRestrictedTextField("URLSegment","URLSegment",
return $classes;
}
+ /**
+ * Overrides URLSegment, returns the URL Segment along with all nested
+ * parent information
+ *
+ * @return string
+ */
+ public function getURLSegment(){+ //if in CMS mode we need only the editable part
+ if(Director::get_site_mode() == 'cms') return $this->getField('URLSegment');+
+ return $this->getURLBase() . $this->getField('URLSegment');+ }
+
+ /**
+ * Returns the nested URL for this SiteTree, without the actual URLSegment
+ *
+ * @return string
+ */
+ public function getURLBase(){+ $model = $this;
+ $urlChain = '';
+ while($model = $model->getParent()) $urlChain = $model->getField('URLSegment') . '/' . $urlChain;+
+ return $urlChain;
+ }
}
?>