2013-07-21 12:23:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An indivisual blog post.
|
|
|
|
*
|
|
|
|
* @package silverstripe
|
|
|
|
* @subpackage blog
|
|
|
|
*
|
2014-02-16 06:19:26 +01:00
|
|
|
* @author Michael Strong <github@michaelstrong.co.uk>
|
2013-07-21 12:23:35 +02:00
|
|
|
**/
|
|
|
|
class BlogPost extends Page {
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
"PublishDate" => "SS_Datetime",
|
|
|
|
);
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
private static $has_one = array(
|
|
|
|
"FeaturedImage" => "Image",
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many = array(
|
|
|
|
"Categories" => "BlogCategory",
|
|
|
|
"Tags" => "BlogTag",
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $defaults = array(
|
2013-08-11 00:34:46 +02:00
|
|
|
"ShowInMenus" => false,
|
|
|
|
"InheritSideBar" => true, // Support for widgets
|
|
|
|
"ProvideComments" => true, // Support for comments
|
2013-08-04 18:38:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $extensions = array(
|
|
|
|
"BlogPostFilter",
|
|
|
|
);
|
|
|
|
|
2013-08-11 00:34:46 +02:00
|
|
|
private static $searchable_fields = array(
|
|
|
|
"Title"
|
|
|
|
);
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2013-08-11 00:34:46 +02:00
|
|
|
private static $summary_fields = array(
|
|
|
|
"Title",
|
|
|
|
);
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2014-02-16 06:19:26 +01:00
|
|
|
private static $allowed_children = array();
|
|
|
|
|
2013-08-11 00:34:46 +02:00
|
|
|
private static $default_sort = "PublishDate DESC";
|
2013-07-21 12:23:35 +02:00
|
|
|
|
2014-02-16 06:19:26 +01:00
|
|
|
private static $can_be_root = false;
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
/**
|
|
|
|
* This will display or hide the current class from the SiteTree. This
|
|
|
|
* variable can be configured using YAML.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
**/
|
2013-08-11 00:34:46 +02:00
|
|
|
private static $show_in_sitetree = false;
|
2013-07-21 12:23:35 +02:00
|
|
|
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
public function getCMSFields() {
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2013-09-08 05:46:51 +02:00
|
|
|
// Assign to variable & pass for PHP <= 5.4 closure compatibility
|
2013-10-10 00:09:28 +02:00
|
|
|
$data = array(
|
|
|
|
"TagsMap" => $this->Parent()->Tags()->map()->toArray(),
|
|
|
|
"CategoryMap" => $this->Parent()->Categories()->map()->toArray()
|
|
|
|
);
|
2013-09-08 05:46:51 +02:00
|
|
|
|
|
|
|
$this->beforeUpdateCMSFields(function($fields) use ($data) {
|
|
|
|
// Add Publish date fields
|
|
|
|
$fields->insertAfter(
|
|
|
|
$publishDate = DatetimeField::create("PublishDate", _t("BlogPost.PublishDate", "Publish Date")),
|
|
|
|
"Content"
|
|
|
|
);
|
|
|
|
$publishDate->getDateField()->setConfig("showcalendar", true);
|
|
|
|
|
|
|
|
// Add Categories & Tags fields
|
|
|
|
$categoriesField = ListboxField::create("Categories",
|
|
|
|
_t("BlogPost.Categories", "Categories"), $data['CategoryMap'])->setMultiple(true);
|
|
|
|
$fields->insertAfter($categoriesField, "PublishDate");
|
|
|
|
|
|
|
|
$tagsField = ListboxField::create("Tags", _t("BlogPost.Tags", "Tags"), $data['TagsMap'])
|
|
|
|
->setMultiple(true);
|
|
|
|
$fields->insertAfter($tagsField, "Categories");
|
|
|
|
|
|
|
|
// Add featured image
|
|
|
|
$fields->insertBefore(
|
|
|
|
$uploadField = UploadField::create("FeaturedImage", _t("BlogPost.FeaturedImage", "Featured Image")),
|
|
|
|
"Content"
|
|
|
|
);
|
|
|
|
$uploadField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
|
2013-09-06 23:28:48 +02:00
|
|
|
});
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2013-09-08 05:46:51 +02:00
|
|
|
$fields = parent::getCMSFields();
|
2013-07-21 12:23:35 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If no publish date is set, set the date to now.
|
|
|
|
**/
|
|
|
|
public function onBeforeWrite() {
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
if(!$this->PublishDate) $this->setCastedField("PublishDate", time());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-12 19:55:40 +02:00
|
|
|
/**
|
|
|
|
* Update the PublishDate to now, if being published for the first time, and the date hasn't been set to the future.
|
|
|
|
**/
|
|
|
|
public function onBeforePublish() {
|
2014-06-13 12:45:28 +02:00
|
|
|
if ($this->dbObject('PublishDate')->InPast() && !$this->isPublished()) {
|
|
|
|
$this->setCastedField("PublishDate", time());
|
|
|
|
$this->write();
|
2014-06-12 19:55:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
/**
|
2013-10-10 00:09:28 +02:00
|
|
|
* Checks the publish date to see if the blog post has actually been published.
|
2013-07-21 12:23:35 +02:00
|
|
|
*
|
|
|
|
* @param $member Member|null
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
**/
|
|
|
|
public function canView($member = null) {
|
|
|
|
if(!parent::canView($member)) return false;
|
|
|
|
|
|
|
|
if($this->PublishDate) {
|
|
|
|
$publishDate = $this->dbObject("PublishDate");
|
|
|
|
if($publishDate->InFuture() && !Permission::checkMember($member, "VIEW_DRAFT_CONTENT")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the post excerpt.
|
|
|
|
*
|
|
|
|
* @param $wordCount int - number of words to display
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
**/
|
2013-08-12 16:51:25 +02:00
|
|
|
public function Excerpt($wordCount = 30) {
|
2013-08-04 18:38:26 +02:00
|
|
|
return $this->dbObject("Content")->LimitWordCount($wordCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a monthly archive link for the current blog post.
|
|
|
|
*
|
2013-08-11 00:34:46 +02:00
|
|
|
* @param $type string day|month|year
|
|
|
|
*
|
2013-08-04 18:38:26 +02:00
|
|
|
* @return string URL
|
|
|
|
**/
|
2013-08-11 00:34:46 +02:00
|
|
|
public function getMonthlyArchiveLink($type = "day") {
|
2013-08-04 18:38:26 +02:00
|
|
|
$date = $this->dbObject("PublishDate");
|
2013-08-11 00:34:46 +02:00
|
|
|
$year = $date->format("Y");
|
|
|
|
if($type != "year") {
|
|
|
|
if($type == "day") {
|
2014-02-16 06:19:26 +01:00
|
|
|
return Controller::join_links(
|
|
|
|
$this->Parent()->Link("archive"),
|
|
|
|
$date->format("Y"),
|
|
|
|
$date->format("m"),
|
|
|
|
$date->format("d")
|
|
|
|
);
|
2013-08-11 00:34:46 +02:00
|
|
|
}
|
|
|
|
return Controller::join_links($this->Parent()->Link("archive"), $date->format("Y"), $date->format("m"));
|
|
|
|
}
|
|
|
|
return Controller::join_links($this->Parent()->Link("archive"), $date->format("Y"));
|
2013-08-04 18:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a yearly archive link for the current blog post.
|
|
|
|
*
|
|
|
|
* @return string URL
|
|
|
|
**/
|
|
|
|
public function getYearlyArchiveLink() {
|
|
|
|
$date = $this->dbObject("PublishDate");
|
|
|
|
return Controller::join_links($this->Parent()->Link("archive"), $date->format("Y"));
|
|
|
|
}
|
|
|
|
|
2014-06-11 11:20:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name')
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
**/
|
|
|
|
public function fieldLabels($includerelations = true) {
|
|
|
|
$labels = parent::fieldLabels($includerelations);
|
|
|
|
$labels['Title'] = _t('BlogPost.PageTitleLabel', "Post Title");
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-08-04 18:38:26 +02:00
|
|
|
* Blog Post controller
|
2013-07-21 12:23:35 +02:00
|
|
|
*
|
|
|
|
* @package silverstripe
|
|
|
|
* @subpackage blog
|
|
|
|
*
|
2014-02-16 06:19:26 +01:00
|
|
|
* @author Michael Strong <github@michaelstrong.co.uk>
|
2013-07-21 12:23:35 +02:00
|
|
|
**/
|
|
|
|
class BlogPost_Controller extends Page_Controller {
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
}
|