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, $this->urlParams['Action'], $this->urlParams['ID'], $this->urlParams['OtherID'] ); $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']}"); $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); } }
/** * Test methods that get DataObjects * - DataObject::get() * - All records of a DataObject * - Filtering * - Sorting * - Joins * - Limit * - Container class * - DataObject::get_by_id() * - DataObject::get_by_url() * - DataObject::get_one() * - With and without caching * - With and without ordering */ function testGet() { // Test getting all records of a DataObject $comments = DataObject::get('PageComment'); $this->assertEquals(8, $comments->Count()); // Test WHERE clause $comments = DataObject::get('PageComment', 'Name="Bob"'); $this->assertEquals(2, $comments->Count()); foreach($comments as $comment) { $this->assertEquals('Bob', $comment->Name); } // Test sorting $comments = DataObject::get('PageComment', '', 'Name ASC'); $this->assertEquals(8, $comments->Count()); $this->assertEquals('Bob', $comments->First()->Name); $comments = DataObject::get('PageComment', '', 'Name DESC'); $this->assertEquals(8, $comments->Count()); $this->assertEquals('Joe', $comments->First()->Name); // Test join $comments = DataObject::get('PageComment', '`SiteTree`.Title="First Page"', '', 'INNER JOIN SiteTree ON PageComment.ParentID = SiteTree.ID'); $this->assertEquals(2, $comments->Count()); $this->assertEquals('Bob', $comments->First()->Name); $this->assertEquals('Bob', $comments->Last()->Name); // Test limit $comments = DataObject::get('PageComment', '', 'Name ASC', '', '1,2'); $this->assertEquals(2, $comments->Count()); $this->assertEquals('Bob', $comments->First()->Name); $this->assertEquals('Dean', $comments->Last()->Name); // Test container class $comments = DataObject::get('PageComment', '', '', '', '', 'DataObjectSet'); $this->assertEquals('DataObjectSet', get_class($comments)); $comments = DataObject::get('PageComment', '', '', '', '', 'ComponentSet'); $this->assertEquals('ComponentSet', get_class($comments)); // Test get_by_id() $homepageID = $this->idFromFixture('Page', 'home'); $page = DataObject::get_by_id('Page', $homepageID); $this->assertEquals('Home', $page->Title); // Test get_by_url() $page = SiteTree::get_by_url('home'); $this->assertEquals($homepageID, $page->ID); // Test get_one() without caching $comment1 = DataObject::get_one('PageComment', 'Name="Joe"', false); $comment1->Comment = "Something Else"; $comment2 = DataObject::get_one('PageComment', 'Name="Joe"', false); $this->assertNotEquals($comment1->Comment, $comment2->Comment); // Test get_one() with caching $comment1 = DataObject::get_one('PageComment', 'Name="Jane"', true); $comment1->Comment = "Something Else"; $comment2 = DataObject::get_one('PageComment', 'Name="Jane"', true); $this->assertEquals((string)$comment1->Comment, (string)$comment2->Comment); // Test get_one() with order by without caching $comment = DataObject::get_one('PageComment', '', false, 'Name ASC'); $this->assertEquals('Bob', $comment->Name); $comment = DataObject::get_one('PageComment', '', false, 'Name DESC'); $this->assertEquals('Joe', $comment->Name); // Test get_one() with order by with caching $comment = DataObject::get_one('PageComment', '', true, 'Name ASC'); $this->assertEquals('Bob', $comment->Name); $comment = DataObject::get_one('PageComment', '', true, 'Name DESC'); $this->assertEquals('Joe', $comment->Name); }
/** * Test SiteTree::get_by_url() */ function testGetByURL() { // Test basic get by url $this->assertEquals($this->idFromFixture('Page', 'home'), SiteTree::get_by_url("home")->ID); // Test the extraFilter argument // Note: One day, it would be more appropriate to return null instead of false for queries such as these $this->assertFalse(SiteTree::get_by_url("home", "1 = 2")); }
public function addpage() { $className = isset($_REQUEST['PageType']) ? $_REQUEST['PageType'] : "Page"; $parent = isset($_REQUEST['ParentID']) ? $_REQUEST['ParentID'] : 0; $suffix = isset($_REQUEST['Suffix']) ? "-" . $_REQUEST['Suffix'] : null; if(!$parent && isset($_REQUEST['Parent'])) { $page = SiteTree::get_by_url($_REQUEST['Parent']); if($page) $parent = $page->ID; } if(is_numeric($parent)) $parentObj = DataObject::get_by_id("SiteTree", $parent); if(!$parentObj || !$parentObj->ID) $parent = 0; if($parentObj && !$parentObj->canAddChildren()) return Security::permissionFailure($this); if(!singleton($className)->canCreate()) return Security::permissionFailure($this); $p = $this->getNewItem("new-$className-$parent".$suffix, false); $p->write(); return $this->returnItemToUser($p); }
/** * Returns the dataobject of the current page. * This will only return a value if you are looking at a SiteTree page */ static function currentPage() { if (isset(Director::$urlParams['URLSegment'])) { $SQL_urlSegment = Convert::raw2sql(Director::$urlParams['URLSegment']); return SiteTree::get_by_url($SQL_urlSegment); } else { return Controller::curr(); } }
protected function onBeforeWrite() { 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); $segment = ereg_replace('-+','-',$segment); if(!$segment) { $segment = "page-$this->ID"; } $this->URLSegment = $segment; } DataObject::set_context_obj($this); $idFilter = ($this->ID) ? "`SiteTree`.ID <> '$this->ID'" : ''; $count = 1; while ( (class_exists($this->URLSegment) && is_subclass_of($this->URLSegment, 'RequestHandler')) || SiteTree::get_by_url($this->URLSegment, $idFilter) ) { $count++; $this->URLSegment = ereg_replace('-[0-9]+$','', $this->URLSegment) . "-$count"; } DataObject::set_context_obj(null); // If the URLSegment has been changed, rewrite links if(isset($this->changed['URLSegment']) && $this->changed['URLSegment']) { if($this->hasMethod('BackLinkTracking')) { $links = $this->BackLinkTracking(); if($links) { foreach($links as $link) { $link->rewriteLink($this->original['URLSegment'] . '/', $this->URLSegment . '/'); $link->write(); } } } } parent::onBeforeWrite(); }
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; }