Skip to content

Instantly share code, notes, and snippets.

@cjsewell
Last active October 4, 2015 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjsewell/9d41c5c278bd36fac877 to your computer and use it in GitHub Desktop.
Save cjsewell/9d41c5c278bd36fac877 to your computer and use it in GitHub Desktop.
SS Container Page Example
<?php
class ContainterPage extends Page {
static $db = array(
);
public function getCMSFields() {
$fields = new FieldList(
$rootTab = new TabSet("Root", $tabMain = new Tab('Main', LabelField::create("ContainerWarning", "Container page does not have any editable properties")
)
)
);
return $fields;
}
public function getCMSActions() {
$actions = parent::getCMSActions();
$actions->removeByName("MajorActions");
return $actions;
}
public static function FindOrCreate($title, $parent) {
$result = $parent->Children()->find("Title", $title);
if (!$result) {
$result = ContainterPage::create();
$result->Title = $title;
$result->setParent($parent);
$result->write();
$result->publish("Live", "Stage");
$result->flushCache();
}
return $result;
}
}
class ContainterPage_Controller extends Page_Controller {
public function index() {
$goTo = $goTo = $this->Parent()->Link();
if ($this->Children()->Count()) {
$goTo = $this->Children()->First()->Link();
}
$this->redirect($goTo);
}
}
<?php
/**
* @property Date $PublishDate
*/
class ContainterPageTestPage extends Page {
private static $can_be_root = false;
private static $db = array(
"PublishDate" => "Date"
);
private $landingPage;
private $yearPage;
private $monthPage;
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Main", DateField::create("PublishDate", "Publish Date")->setConfig('showcalendar', true), "Content");
return $fields;
}
/**
*
* @return Page
*/
private function getLandingPage() {
if (!$this->landingPage) {
$parent = $this->Parent();
while ((int) $parent->ParentID !== 0 && $parent->exists()) {
$parent = $parent->Parent();
}
$this->landingPage = $parent;
}
return $this->landingPage;
}
/**
*
* @return ContainterPage
*/
private function getYearPage() {
if (!$this->yearPage && $this->getLandingPage()) {
$this->yearPage = ContainterPage::FindOrCreate($this->dbObject("PublishDate")->Format("Y"), $this->getLandingPage());
}
return $this->yearPage;
}
/**
*
* @return ContainterPage
*/
private function getMonthPage() {
if (!$this->monthPage && $this->getYearPage()) {
$this->monthPage = ContainterPage::FindOrCreate($this->dbObject("PublishDate")->Format("F"), $this->getYearPage());
}
return $this->monthPage;
}
public function validate() {
$result = parent::validate();
if (!$this->PublishDate) {
$this->PublishDate = Date::create();
$this->PublishDate->setValue(time());
}
if (!$this->getLandingPage() || !$this->getLandingPage()->exists()) {
$result->error("Unable to find parent landing page");
}
if (!$this->getYearPage() || !$this->getYearPage()->exists()) {
$result->error("Unable to find year page");
}
if (!$this->getMonthPage() || !$this->getMonthPage()->exists()) {
$result->error("Unable to find month page");
}
return $result;
}
protected function onBeforeWrite() {
if (!$this->PublishDate) {
$this->PublishDate = Date::create();
$this->PublishDate->setValue(time());
}
$oldParent = $this->getParent();
if ($oldParent->ID !== $this->getMonthPage()->ID) {
$this->setParent($this->getMonthPage());
$this->logChange(__FUNCTION__);
}
return parent::onBeforeWrite();
}
public function onAfterWrite() {
$this->logChange(__FUNCTION__);
return parent::onAfterWrite();
}
private function logChange($Caller) {
$changedFields = $this->getChangedFields();
if (isset($changedFields["ParentID"])) {
Debug::log(__CLASS__ . "::" . $Caller . ": Changed ParentID " . $changedFields["ParentID"]["before"] . "=>" . $changedFields["ParentID"]["after"]);
}
}
}
class ContainterPageTestPage_Controller extends Page_Controller {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment