Skip to content

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
helpfulrobot committed Dec 17, 2015
1 parent c82908b commit cab8cac
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 151 deletions.
203 changes: 103 additions & 100 deletions code/NewsArticle.php
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);
}
}
}
107 changes: 56 additions & 51 deletions code/NewsHolder.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,71 @@
<?php

class NewsHolder extends Page {
class NewsHolder extends Page
{

public function getCMSFields() {
public function getCMSFields()
{
$intro = null;

$intro = null;

$this->beforeUpdateCMSFields(function($fields) {
$fields->renameField("Content", "Intro Content");
$this->beforeUpdateCMSFields(function ($fields) {
$fields->renameField("Content", "Intro Content");
$fields->insertBefore(new Tab($this->getLumberjackTitle()), 'Main');
});
});

$fields = parent::getCMSFields();
$fields = parent::getCMSFields();

if ($intro) {
$intro->setRightTitle("Appears at the top of the main ".$this->Title." page, above the list of articles.");
}
if ($intro) {
$intro->setRightTitle("Appears at the top of the main ".$this->Title." page, above the list of articles.");
}

return $fields;
}
return $fields;
}

public function getLumberjackTitle() {
public function getLumberjackTitle()
{
return "News Articles";
}

// Only allows certain children to be created
private static $allowed_children = array('NewsArticle');
private static $description = 'Holds News Article pages';
private static $icon = "basic-news/images/newspaper-page";

public function stageChildren($showAll = false) {
return $this->__call('stageChildren', array($showAll))->sort(array('MenuTitle'=>'DESC',"Created"=>'DESC'));
}
// Only allows certain children to be created
private static $allowed_children = array('NewsArticle');
private static $description = 'Holds News Article pages';
private static $icon = "basic-news/images/newspaper-page";

public function stageChildren($showAll = false)
{
return $this->__call('stageChildren', array($showAll))->sort(array('MenuTitle'=>'DESC', "Created"=>'DESC'));
}

public function init() {
RSSFeed::linkToFeed($this->Link("rss"), "News RSS Feed");
public function init()
{
RSSFeed::linkToFeed($this->Link("rss"), "News RSS Feed");
parent::init();
}

}
}


class NewsHolder_Controller extends Page_Controller {
class NewsHolder_Controller extends Page_Controller
{

private static $allowed_actions = array(
"rss"
);
private static $allowed_actions = array(
"rss"
);

function init() {
RSSFeed::linkToFeed($this->Link() . "rss");
if(Director::fileExists(project() . "/css/news.css")) {
Requirements::css(project() . "/css/news.css");
}else{
Requirements::css("basic-news/css/news.css");
}
public function init()
{
RSSFeed::linkToFeed($this->Link() . "rss");
if (Director::fileExists(project() . "/css/news.css")) {
Requirements::css(project() . "/css/news.css");
} else {
Requirements::css("basic-news/css/news.css");
}

parent::init();
}

public function rss() {
$config = SiteConfig::current_site_config();
parent::init();
}

public function rss()
{
$config = SiteConfig::current_site_config();
// Creates a new RSS Feed list
$rss = new RSSFeed(
$list = NewsArticle::get(), // an SS_List containing your feed items
Expand All @@ -70,13 +76,12 @@ public function rss() {
// Outputs the RSS feed to the user.
return $rss->outputToBrowser();
}

// Provides Paginated List of NewsArticles
function PaginatedPages() {
$list = new PaginatedList(NewsArticle::get()->filter("ParentID",$this->ID), $this->request);
$list->setPageLength(10);
return $list;
}

// Provides Paginated List of NewsArticles
public function PaginatedPages()
{
$list = new PaginatedList(NewsArticle::get()->filter("ParentID", $this->ID), $this->request);
$list->setPageLength(10);
return $list;
}
}

?>

0 comments on commit cab8cac

Please sign in to comment.