-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
helpfulrobot
committed
Dec 17, 2015
1 parent
c82908b
commit cab8cac
Showing
2 changed files
with
159 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,110 @@ | ||
<?php | ||
|
||
class NewsArticle extends Page { | ||
|
||
private static $db = array( | ||
'Date' => 'Date' | ||
); | ||
|
||
private static $has_one = array( | ||
'AttachedImage' => 'Image' | ||
); | ||
|
||
static $defaults = array( | ||
'ShowInMenus' => false | ||
); | ||
|
||
static $summary_fields = array( | ||
"Date" => "Date", | ||
"Title" => "Title" | ||
); | ||
|
||
static $default_sort = "Date DESC, Created DESC"; | ||
static $default_parent = "news"; | ||
static $description = 'An individual news item displayed on a News holder page'; | ||
static $singular_name = 'News Article'; | ||
static $plural_name = 'News Articles'; | ||
static $icon = "basic-news/images/newspaper"; | ||
static $allowed_children = array(); | ||
static $can_be_root = false; | ||
|
||
public function populateDefaults() { | ||
$this->Date = date('Y-m-d'); | ||
parent::populateDefaults(); | ||
} | ||
|
||
public function getCMSFields() { | ||
class NewsArticle extends Page | ||
{ | ||
|
||
private static $db = array( | ||
'Date' => 'Date' | ||
); | ||
|
||
private static $has_one = array( | ||
'AttachedImage' => 'Image' | ||
); | ||
|
||
public static $defaults = array( | ||
'ShowInMenus' => false | ||
); | ||
|
||
public static $summary_fields = array( | ||
"Date" => "Date", | ||
"Title" => "Title" | ||
); | ||
|
||
public static $default_sort = "Date DESC, Created DESC"; | ||
public static $default_parent = "news"; | ||
public static $description = 'An individual news item displayed on a News holder page'; | ||
public static $singular_name = 'News Article'; | ||
public static $plural_name = 'News Articles'; | ||
public static $icon = "basic-news/images/newspaper"; | ||
public static $allowed_children = array(); | ||
public static $can_be_root = false; | ||
|
||
public function populateDefaults() | ||
{ | ||
$this->Date = date('Y-m-d'); | ||
parent::populateDefaults(); | ||
} | ||
|
||
public function getCMSFields() | ||
{ | ||
$this->beforeUpdateCMSFields(function ($fields) { | ||
|
||
$this->beforeUpdateCMSFields(function($fields) { | ||
$datefield = new DateField('Date', 'Date (DD/MM/YYYY)'); | ||
$datefield->setConfig('showcalendar', true); | ||
$datefield->setConfig('showdropdown', true); | ||
$datefield->setConfig('dateformat', 'dd/MM/YYYY'); | ||
|
||
$fields->addFieldToTab('Root.Main', $datefield, 'Content'); | ||
|
||
$image = new UploadField('AttachedImage', 'Main Image'); | ||
$image->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif')); | ||
$image->setConfig('allowedMaxFileNumber', 1); | ||
$image->setFolderName('Managed/NewsImages'); | ||
$image->setCanPreviewFolder(false); | ||
$image->setRightTitle("Displayed to the right of the content in the main article, where it can be clicked to enlarge. <br />A thumbnail also appears next to the article summary on the main News page."); | ||
$fields->addFieldToTab('Root.Main', $image, "Content"); | ||
|
||
$datefield = new DateField('Date','Date (DD/MM/YYYY)'); | ||
$datefield->setConfig('showcalendar', true); | ||
$datefield->setConfig('showdropdown', true); | ||
$datefield->setConfig('dateformat', 'dd/MM/YYYY'); | ||
|
||
$fields->addFieldToTab('Root.Main', $datefield, 'Content'); | ||
|
||
$image = new UploadField('AttachedImage', 'Main Image'); | ||
$image->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif')); | ||
$image->setConfig('allowedMaxFileNumber', 1); | ||
$image->setFolderName('Managed/NewsImages'); | ||
$image->setCanPreviewFolder(false); | ||
$image->setRightTitle("Displayed to the right of the content in the main article, where it can be clicked to enlarge. <br />A thumbnail also appears next to the article summary on the main News page."); | ||
$fields->addFieldToTab('Root.Main', $image,"Content"); | ||
}); | ||
|
||
}); | ||
$fields = parent::getCMSFields(); | ||
|
||
$fields->renameField("Title", "Headline"); | ||
$fields->removeFieldFromTab("Root.Main", "MenuTitle"); | ||
|
||
$fields = parent::getCMSFields(); | ||
|
||
$fields->renameField("Title", "Headline"); | ||
$fields->removeFieldFromTab("Root.Main","MenuTitle"); | ||
return $fields; | ||
} | ||
|
||
public function onBeforeWrite() | ||
{ | ||
|
||
// Change MenuTitle, so date appears in CMS SiteTree | ||
$this->MenuTitle = $this->Date.": ".$this->Title; | ||
|
||
// Move to News holder if created under something else | ||
if ($this->Parent()->ClassName != "NewsHolder") { | ||
$this->ParentID = NewsHolder::get()->first()->ID; | ||
} | ||
|
||
return $fields; | ||
} | ||
|
||
function onBeforeWrite() { | ||
|
||
// Change MenuTitle, so date appears in CMS SiteTree | ||
$this->MenuTitle = $this->Date.": ".$this->Title; | ||
|
||
// Move to News holder if created under something else | ||
if ($this->Parent()->ClassName != "NewsHolder") { | ||
$this->ParentID = NewsHolder::get()->first()->ID; | ||
} | ||
|
||
// Add Today's Date if None | ||
if (!$this->Date) { | ||
$this->Date = date('Y-m-d'); | ||
} | ||
|
||
parent::onBeforeWrite(); | ||
} | ||
} | ||
|
||
|
||
class NewsArticle_Controller extends Page_Controller { | ||
|
||
public function init() { | ||
if(Director::fileExists(project() . "/css/news.css")) { | ||
Requirements::css(project() . "/css/news.css"); | ||
}else{ | ||
Requirements::css("basic-news/css/news.css"); | ||
} | ||
parent::init(); | ||
} | ||
|
||
// Provides a resized image with the max width provided | ||
public function ArticleImageSized($maxwidth = 250) { | ||
if($this->AttachedImage()->getWidth() < $maxwidth) { | ||
return $this->AttachedImage(); | ||
} else { | ||
return $this->AttachedImage()->setWidth($maxwidth); | ||
} | ||
} | ||
|
||
} | ||
|
||
?> | ||
// Add Today's Date if None | ||
if (!$this->Date) { | ||
$this->Date = date('Y-m-d'); | ||
} | ||
|
||
parent::onBeforeWrite(); | ||
} | ||
} | ||
|
||
|
||
class NewsArticle_Controller extends Page_Controller | ||
{ | ||
|
||
public function init() | ||
{ | ||
if (Director::fileExists(project() . "/css/news.css")) { | ||
Requirements::css(project() . "/css/news.css"); | ||
} else { | ||
Requirements::css("basic-news/css/news.css"); | ||
} | ||
parent::init(); | ||
} | ||
|
||
// Provides a resized image with the max width provided | ||
public function ArticleImageSized($maxwidth = 250) | ||
{ | ||
if ($this->AttachedImage()->getWidth() < $maxwidth) { | ||
return $this->AttachedImage(); | ||
} else { | ||
return $this->AttachedImage()->setWidth($maxwidth); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters