mlanthaler: Bugfix: Fixed error "Trying to get property of non-object" when the accessed page doesn't exists (passed wrong URL).

(merged from branches/gsoc)


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41789 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2007-09-14 19:17:37 +00:00
parent f96879bb13
commit 17202b859e

View File

@ -7,8 +7,8 @@
/**
* Basic data-object representing all pages within the site tree.
* This data-object takes care of the heirachy. All page types that live within the heirachy
* should inherit from this.
*
* should inherit from this.
*
* In addition, it contains a number of static methods for querying the site tree.
*/
class SiteTree extends DataObject {
@ -20,7 +20,7 @@ class SiteTree extends DataObject {
public function Link($action = null) {
if($action == "index") {
$action = "";
}
}
return Director::baseURL() . $this->URLSegment . "/$action";
}
@ -41,7 +41,7 @@ class SiteTree extends DataObject {
public function LinkOrCurrent() {
return $this->isCurrent() ? "current" : "link";
}
/**
* Returns link/section, depending on whether you're on the current section.
* This is useful for css styling of menus.
@ -50,7 +50,7 @@ class SiteTree extends DataObject {
public function LinkOrSection() {
return $this->isSection() ? "section" : "link";
}
/**
* Returns link/current/section, depending if you're not in the current section,
* you're on the current page, or you're in the current section
@ -59,7 +59,7 @@ class SiteTree extends DataObject {
*/
public function LinkingMode() {
$this->prepareCurrentAndSection();
if($this->ID == self::$currentPageID) {
return "current";
} else if(in_array($this->ID, self::$currentSectionIDs)) {
@ -68,7 +68,7 @@ class SiteTree extends DataObject {
return "link";
}
}
/**
* Get the URL segment for this page, eg 'home'
* @return string The URL segment
@ -90,7 +90,7 @@ class SiteTree extends DataObject {
}
return false;
}
/**
* Returns comments on this page. This will only show comments that
* have been marked as spam if "?showspam=1" is appended to the URL.
@ -103,7 +103,7 @@ class SiteTree extends DataObject {
return $comments ? $comments : new DataObjectSet();
}
/**
* Create a duplicate of this node. Doesn't affect joined data - create a custom overloading of this
* if you need such behaviour.
@ -114,7 +114,7 @@ class SiteTree extends DataObject {
$page->CheckedPublicationDifferences = $page->AddedToStage = true;
return $page;
}
/**
* Duplicates each child of this node recursively and returns the duplicate node.
* @return SiteTree The duplicated object.
@ -130,10 +130,10 @@ class SiteTree extends DataObject {
$childClone->write();
}
}
return $clone;
}
/**
* Duplicate this node and its children as a child of the node with the given id
* @param int $id ID of the new node's new parent
@ -143,7 +143,7 @@ class SiteTree extends DataObject {
$newSiteTree->ParentID = $id;
$newSiteTree->write();
}
/**
* An array of this pages URL segment and it's parents.
* This is generated by prepareCurrentAndSection for use by
@ -158,7 +158,7 @@ class SiteTree extends DataObject {
* @var int
*/
protected static $currentPageID;
/**
* This function is used for isCurrent() and isSection() to prepare
* the cached answers.
@ -172,7 +172,7 @@ class SiteTree extends DataObject {
} else {
$nextID = SiteTree::$currentPageID;
}
$table = (Versioned::current_stage() == "Live") ? "SiteTree_Live" : "SiteTree";
SiteTree::$currentSectionIDs = array();
@ -182,7 +182,7 @@ class SiteTree extends DataObject {
}
}
}
/**
* Check if this is the currently viewed page.
* @return boolean True if this is the current page.
@ -191,7 +191,7 @@ class SiteTree extends DataObject {
$this->prepareCurrentAndSection();
return $this->ID == SiteTree::$currentPageID;
}
/**
* Check if the currently viewed page is in this section.
* @return boolean True if the currently viewed page is in this section.
@ -200,7 +200,7 @@ class SiteTree extends DataObject {
$this->prepareCurrentAndSection();
return in_array($this->ID, self::$currentSectionIDs);
}
/**
* Return a breadcrumb trail to this page.
* @param int $maxDepth The maximum depth to traverse.
@ -221,10 +221,10 @@ class SiteTree extends DataObject {
}
$page = $page->Parent;
}
return implode(" » ", array_reverse($parts));
}
/**
* Get the parent of this page.
* @return SiteTree Parent of this page.
@ -233,7 +233,7 @@ class SiteTree extends DataObject {
if($this->getField("ParentID"))
return DataObject::get_one("SiteTree", "`SiteTree`.ID = " . $this->getField("ParentID"));
}
/**
* Make this page a child of another page.
* @param SiteTree|int $item Either the parent object, or the parent id
@ -245,7 +245,7 @@ class SiteTree extends DataObject {
$this->setField("ParentID", $item);
}
}
/**
* Return a string of the form "parent - page" or "grandparent - parent - page".
* @param int $level The maximum amount of levels to traverse.
@ -313,25 +313,25 @@ class SiteTree extends DataObject {
public function canAddChildren() {
return $this->canEdit() && $this->stat('allowed_children') != 'none';
}
/**
* This function should return true if the current user can delete this page.
* It can be overloaded to customise the security model for an application.
* @return boolean True if the current user can delete this page.
*/
public function canDelete() {
return $this->stat('can_create') != false;
return $this->stat('can_create') != false;
}
/**
* This function should return true if the current user can create new pages of this class.
* It can be overloaded to customise the security model for an application.
* @return boolean True if the current user can create pages on this class.
*/
public function canCreate() {
return $this->stat('can_create') != false || Director::isDev();
return $this->stat('can_create') != false || Director::isDev();
}
/**
* This function should return true if the current user can edit this page.
* It can be overloaded to customise the security model for an application.
@ -349,9 +349,9 @@ class SiteTree extends DataObject {
public function canPublish() {
return $this->canEdit();
}
/**
* Collate selected descendants of this page.
* Collate selected descendants of this page.
* $condition will be evaluated on each descendant, and if it is succeeds, that item will be added
* to the $collator array.
* @param string $condition The PHP condition to be evaluated. The page will be called $item
@ -366,7 +366,7 @@ class SiteTree extends DataObject {
return true;
}
}
/**
* Return the title, description and keywords metatags.
* @param boolean|string $includeTitle Show default <title>-tag, set to false for custom templating
@ -378,7 +378,7 @@ class SiteTree extends DataObject {
$tags .= "<title>" . Convert::raw2xml($this->MetaTitle ? $this->MetaTitle : $this->Title) . "</title>\n";
}
$tags .= "<meta name=\"generator\" http-equiv=\"generator\" content=\"SilverStripe 2.0 - http://www.silverstripe.com\" />\n";
$charset = ContentNegotiator::get_encoding();
$tags .= "<meta http-equiv=\"Content-type\" content=\"text/html; charset=$charset\" />\n";
if($this->MetaKeywords) {
@ -393,7 +393,7 @@ class SiteTree extends DataObject {
return $tags;
}
/**
* Returns the object that contains the content that a user would associate with this page.
* Ordinarily, this is just the page itself, but for example on RedirectorPages or VirtualPages
@ -403,7 +403,7 @@ class SiteTree extends DataObject {
public function ContentSource() {
return $this;
}
function requireDefaultRecords() {
parent::requireDefaultRecords();
@ -450,11 +450,11 @@ class SiteTree extends DataObject {
if(!$this->Sort && $this->ParentID) {
$this->Sort = DB::query("SELECT MAX(Sort) + 1 FROM SiteTree WHERE ParentID = $this->ParentID")->value();
}
// Auto-set URLSegment
if((!$this->URLSegment || $this->URLSegment == 'new-page') && $this->Title) {
$this->URLSegment = $this->generateURLSegment($this->Title);
// Keep it clean
} else if(isset($this->changed['URLSegment']) && $this->changed['URLSegment']) {
$segment = ereg_replace('[^A-Za-z0-9]+','-',$this->URLSegment);
@ -489,7 +489,7 @@ class SiteTree extends DataObject {
}
}
}
parent::onBeforeWrite();
}
@ -510,11 +510,11 @@ class SiteTree extends DataObject {
}
return $t;
}
function makelinksunique() {
$badURLs = "'" . implode("', '", DB::query("SELECT URLSegment, count(*) FROM SiteTree GROUP BY URLSegment HAVING count(*) > 1")->column()) . "'";
$pages = DataObject::get("SiteTree", "URLSegment IN ($badURLs)");
foreach($pages as $page) {
echo "<li>$page->Title: ";
$urlSegment = $page->URLSegment;
@ -522,13 +522,13 @@ class SiteTree extends DataObject {
if($urlSegment != $page->URLSegment) echo " changed $urlSegment -> $page->URLSegment";
else echo " $urlSegment is already unique";
die();
}
}
}
function makelinksuniquequick() {
$badURLs = "'" . implode("', '", DB::query("SELECT URLSegment, count(*) FROM SiteTree GROUP BY URLSegment HAVING count(*) > 1")->column()) . "'";
$pages = DB::query("SELECT *, SiteTree.ID FROM SiteTree LEFT JOIN Page ON Page.ID = SiteTree.ID WHERE URLSegment IN ($badURLs)");
foreach($pages as $page) {
echo "<li>$page[Title]: ";
$urlSegment = $page['URLSegment'];
@ -536,10 +536,10 @@ class SiteTree extends DataObject {
DB::query("UPDATE SiteTree SET URLSegment = '$newURLSegment' WHERE ID = $page[ID]");
if($urlSegment != $newURLSegment) echo " changed $urlSegment -> $newURLSegment";
else echo " $urlSegment is already unique";
}
}
echo "<p>done";
}
/**
* Replace a URL in html content with a new URL.
* @param string $old The old URL
@ -556,7 +556,7 @@ class SiteTree extends DataObject {
}
}
}
//------------------------------------------------------------------------------------//
/**
@ -603,7 +603,7 @@ class SiteTree extends DataObject {
if(!isset($backlinks)) {
$backlinks = "<p>This page hasn't been linked to from any pages.</p>";
}
// Status / message
// Create a status message for multiple parents
if($this->ID && is_numeric($this->ID)) {
@ -614,34 +614,34 @@ class SiteTree extends DataObject {
foreach($linkedPages as $linkedPage) {
$parentPage = $linkedPage->Parent;
$parentPageTitle = $parentPage->Title;
if($parentPage->ID) {
$parentPageLinks[] = "<a class=\"cmsEditlink\" href=\"admin/show/$linkedPage->ID\">{$parentPage->Title}</a>";
} else {
$parentPageLinks[] = "<a class=\"cmsEditlink\" href=\"admin/show/$linkedPage->ID\">Site Content (Top Level)</a>";
}
}
$lastParent = array_pop($parentPageLinks);
$parentList = "'$lastParent'";
if(count( $parentPageLinks ) > 0) {
$parentList = "'" . implode("', '", $parentPageLinks) . "' and " . $parentList;
}
$statusMessage[] = "This content also appears on the virtual pages in the $parentList sections.";
}
if($this->HasBrokenLink || $this->HasBrokenFile) {
$statusMessage[] = "This page has broken links.";
}
$message = "STATUS: $this->Status<br />";
if(isset($statusMessage)) {
$message .= "NOTE: " . implode("<br />", $statusMessage);
}
// Lay out the fields
// Lay out the fields
$fields = new FieldSet(
new TabSet("Root",
new TabSet("Content",
@ -713,7 +713,7 @@ class SiteTree extends DataObject {
),
new NamedLabelField("Status", $message, "pageStatusMessage", true)
);
foreach(self::$cms_additions as $extension)
{
$fields = call_user_func($extension,$fields);
@ -744,7 +744,7 @@ class SiteTree extends DataObject {
return new DataObjectSet($actions);
}
/**
* Check if this page is new - that is, if it has yet to have been written
* to the database.
@ -757,13 +757,13 @@ class SiteTree extends DataObject {
*/
if(empty($this->ID))
return true;
if(is_numeric($this->ID))
return false;
return stripos($this->ID, 'new') === 0;
}
/**
* Check if this page has been published.
* @return boolean True if this page has been published.
@ -772,7 +772,7 @@ class SiteTree extends DataObject {
if($this->isNew()) return false;
return DB::query("SELECT ID FROM `SiteTree_Live` WHERE ID = $this->ID")->value() ? true : false;
}
/**
* Look for ghost parents
*/
@ -787,7 +787,7 @@ class SiteTree extends DataObject {
}
return $parents;
}
/**
* Get the class dropdown used in the CMS to change the class of a page.
* This returns the list of options in the drop as a Map from class name
@ -809,7 +809,7 @@ class SiteTree extends DataObject {
return $result;
}
/**
* Returns an array of the class names of classes that are allowed
* to be children of this class.
@ -831,7 +831,7 @@ class SiteTree extends DataObject {
return $allowedChildren;
}
}
/**
* Returns the class name of the default class for children
* of this page.
@ -846,7 +846,7 @@ class SiteTree extends DataObject {
return $default;
}
}
/**
* Returns the class name of the default class for the parent
* of this page.
@ -855,7 +855,7 @@ class SiteTree extends DataObject {
function defaultParent() {
return $this->stat('default_parent');
}
/**
* Function to clean up the currently loaded page after a reorganise has been called.
* It should return a piece of JavaScript to be executed on the client side, to clean
@ -863,9 +863,9 @@ class SiteTree extends DataObject {
*/
function cmsCleanup_parentChanged() {
}
/**
/**
* Get the title for use in menus for this page. If the MenuTitle
* field is set it returns that, else it returns the Title field.
* @return string
@ -877,7 +877,7 @@ class SiteTree extends DataObject {
return $this->getField("Title");
}
}
/**
* Set the menu title for this page.
* @param string $value
@ -889,7 +889,7 @@ class SiteTree extends DataObject {
$this->setField("MenuTitle", $value);
}
}
/**
* TitleWithStatus will return the title in an <ins>, <del> or <span class=\"modified\"> tag
* depending on its publication status.
@ -900,24 +900,24 @@ class SiteTree extends DataObject {
if(!$this->CheckedPublicationDifferences && $this->ID) {
$stageVersion = DB::query("SELECT Version FROM SiteTree WHERE ID = $this->ID")->value();
$liveVersion = DB::query("SELECT Version FROM SiteTree_Live WHERE ID = $this->ID")->value();
if($stageVersion && !$liveVersion) $this->AddedToStage = true;
else if(!$stageVersion && $liveVersion) $this->DeletedFromStage = true;
else if($stageVersion != $liveVersion) $this->ModifiedOnStage = true;
}
$tag =
($this->DeletedFromStage ? "del title=\"Removed from stage site\"" :
($this->AddedToStage ? "ins title=\"Added to stage site\"" :
$tag =
($this->DeletedFromStage ? "del title=\"Removed from stage site\"" :
($this->AddedToStage ? "ins title=\"Added to stage site\"" :
($this->ModifiedOnStage ? "span title=\"Modified on stage site\" class=\"modified\"" : "")));
if($tag) {
return "<$tag>" . $this->Title . "</" . strtok($tag,' ') . ">";
} else {
return $this->Title;
}
}
/**
* Return the CSS classes to apply to this node in the CMS tree
* @param Controller $controller The controller object that the tree appears on
@ -929,12 +929,12 @@ class SiteTree extends DataObject {
if(!$this->canAddChildren()) $classes .= " nochildren";
if(!$this->canDelete()) $classes .= " nodelete";
if($controller->isCurrentPage($this)) $classes .= " current";
$classes .= $this->markingClasses();
return $classes;
}
/**
* Indicates what kind of children this page type can have.
* This can be an array of allowed child classes, or the string "none" - indicating that
@ -944,13 +944,13 @@ class SiteTree extends DataObject {
* @var array
*/
static $allowed_children = array("SiteTree");
/**
* The default child class for this page.
* @var string
*/
static $default_child = "Page";
/**
* The default parent class for this page.
* @var string
@ -962,14 +962,14 @@ class SiteTree extends DataObject {
* @var bool
*/
static $can_be_root = true;
/**
* List of permission codes a user can have to allow a user to create a page
* of this type.
* @var array
*/
static $need_permission = null;
/**
* If you extend a class, and don't want to be able to select the old class
* in the cms, set this to the old class name. Eg, if you extended Product to
@ -977,7 +977,7 @@ class SiteTree extends DataObject {
* @var string
*/
static $hide_ancestor = null;
static $db = array(
"URLSegment" => "Varchar(255)",
"Title" => "Varchar(255)",
@ -1003,25 +1003,25 @@ class SiteTree extends DataObject {
"EditorsGroup" => "Int",
"Priority" => "Float"
);
static $indexes = array(
"SearchFields" => "fulltext (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords)",
"TitleSearchFields" => "fulltext (Title)"
);
static $has_many = array(
"Comments" => "PageComment"
);
static $many_many = array(
"LinkTracking" => "SiteTree",
"ImageTracking" => "File"
);
static $belongs_many_many = array(
"BackLinkTracking" => "SiteTree"
);
static $many_many_extraFields = array(
"LinkTracking" => array("FieldName" => "Varchar"),
"ImageTracking" => array("FieldName" => "Varchar")
@ -1032,7 +1032,7 @@ class SiteTree extends DataObject {
"LastEdited" => "Datetime",
"Created" => "Datetime",
);
static $defaults = array(
"ShowInMenus" => 1,
"ShowInSearch" => 1,
@ -1046,26 +1046,26 @@ class SiteTree extends DataObject {
static $has_one = array(
"Parent" => "SiteTree"
);
static $versioning = array(
"Stage", "Live"
);
static $default_sort = "Sort";
/**
* The text shown in the create page dropdown. If
* this is not set, default to "Create a ClassName".
* @var string
*/
static $add_action = null;
/**
* If this is false, the class cannot be created in the CMS.
* @var boolean
*/
static $can_create = true;
/**
* Icon to use in the CMS
* This should be the base filename. The suffixes -file.gif, -openfolder.gif and -closedfolder.gif will