2013-07-21 12:23:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An indivisual blog post.
|
|
|
|
*
|
|
|
|
* @package silverstripe
|
|
|
|
* @subpackage blog
|
|
|
|
*
|
2015-03-06 03:52:07 +01:00
|
|
|
* @method ManyManyList Categories()
|
|
|
|
* @method ManyManyList Tags()
|
|
|
|
* @method ManyManyList Authors()
|
|
|
|
*
|
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",
|
2015-03-06 03:52:07 +01:00
|
|
|
"AuthorNames" => "Varchar(1024)"
|
2013-07-21 12:23:35 +02:00
|
|
|
);
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
private static $has_one = array(
|
|
|
|
"FeaturedImage" => "Image",
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many = array(
|
|
|
|
"Categories" => "BlogCategory",
|
|
|
|
"Tags" => "BlogTag",
|
2015-03-06 03:52:07 +01:00
|
|
|
"Authors" => "Member",
|
2013-08-04 18:38:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-03-04 23:20:47 +01:00
|
|
|
private static $casting = array(
|
|
|
|
'Excerpt' => 'Text'
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2015-03-06 03:52:07 +01:00
|
|
|
/**
|
|
|
|
* Determine if the given member is an author of this post
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
protected function isAuthor($member) {
|
|
|
|
if(!$member || !$member->exists()) return false;
|
|
|
|
|
|
|
|
$list = $this->Authors();
|
|
|
|
if($list instanceof UnsavedRelationList) {
|
|
|
|
return in_array($member->ID, $list->getIDList());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list->byID($member->ID) !== null;
|
|
|
|
}
|
|
|
|
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
public function getCMSFields() {
|
2014-12-11 11:42:36 +01:00
|
|
|
Requirements::css(BLOGGER_DIR . '/css/cms.css');
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2014-07-27 07:34:43 +02:00
|
|
|
$self =& $this;
|
|
|
|
$this->beforeUpdateCMSFields(function($fields) use ($self) {
|
2015-02-01 10:24:37 +01:00
|
|
|
|
|
|
|
// Add featured image
|
2015-02-02 11:32:40 +01:00
|
|
|
$fields->insertAfter(
|
2015-02-01 10:24:37 +01:00
|
|
|
$uploadField = UploadField::create("FeaturedImage", _t("BlogPost.FeaturedImage", "Featured Image")),
|
|
|
|
"Content"
|
|
|
|
);
|
|
|
|
$uploadField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
|
|
|
|
|
2015-02-02 11:32:40 +01:00
|
|
|
// We're going to hide MenuTitle - Its not needed in blog posts.
|
|
|
|
$fields->push(HiddenField::create('MenuTitle'));
|
|
|
|
|
|
|
|
// We're going to add the url segment to sidebar so we're making it a little lighter
|
2015-02-01 10:24:37 +01:00
|
|
|
$urlSegment = $fields->dataFieldByName('URLSegment');
|
2015-03-04 23:20:47 +01:00
|
|
|
$urlSegment->setURLPrefix($self->Parent()->RelativeLink());
|
2015-02-02 11:32:40 +01:00
|
|
|
|
|
|
|
// Remove the MenuTitle and URLSegment from the main tab
|
2015-02-01 10:24:37 +01:00
|
|
|
$fields->removeFieldsFromTab('Root.Main', array(
|
|
|
|
'MenuTitle',
|
|
|
|
'URLSegment',
|
|
|
|
));
|
2015-02-02 11:32:40 +01:00
|
|
|
|
2015-03-06 03:52:07 +01:00
|
|
|
// Author field
|
|
|
|
$authorField = ListboxField::create(
|
|
|
|
"Authors",
|
|
|
|
_t("BlogPost.Authors", "Authors"),
|
|
|
|
Member::get()->map()->toArray()
|
|
|
|
)->setMultiple(true);
|
|
|
|
|
|
|
|
$authorNames = TextField::create(
|
|
|
|
"AuthorNames",
|
|
|
|
_t("BlogPost.AdditionalCredits", "Additional Credits"),
|
|
|
|
null,
|
|
|
|
1024
|
2015-04-02 01:52:44 +02:00
|
|
|
)->setDescription('If some authors of this post don\'t have CMS access, enter their name(s) here. You can separate multiple names with a comma.');
|
2015-03-16 00:26:04 +01:00
|
|
|
if(!$self->canEditAuthors()) {
|
2015-03-06 03:52:07 +01:00
|
|
|
$authorField = $authorField->performDisabledTransformation();
|
|
|
|
$authorNames = $authorNames->performDisabledTransformation();
|
|
|
|
}
|
|
|
|
|
2015-02-02 11:32:40 +01:00
|
|
|
// Build up our sidebar
|
2015-02-01 10:24:37 +01:00
|
|
|
$options = BlogAdminSidebar::create(
|
2014-12-11 11:42:36 +01:00
|
|
|
$publishDate = DatetimeField::create("PublishDate", _t("BlogPost.PublishDate", "Publish Date")),
|
2015-02-02 11:32:40 +01:00
|
|
|
$urlSegment,
|
2014-12-11 11:42:36 +01:00
|
|
|
ListboxField::create(
|
|
|
|
"Categories",
|
|
|
|
_t("BlogPost.Categories", "Categories"),
|
|
|
|
$self->Parent()->Categories()->map()->toArray()
|
|
|
|
)->setMultiple(true),
|
|
|
|
ListboxField::create(
|
|
|
|
"Tags",
|
|
|
|
_t("BlogPost.Tags", "Tags"),
|
|
|
|
$self->Parent()->Tags()->map()->toArray()
|
2015-03-06 03:52:07 +01:00
|
|
|
)->setMultiple(true),
|
|
|
|
$authorField,
|
|
|
|
$authorNames
|
2015-02-01 10:24:37 +01:00
|
|
|
)->setTitle('Post Options');
|
2013-09-08 05:46:51 +02:00
|
|
|
$publishDate->getDateField()->setConfig("showcalendar", true);
|
2015-02-02 11:32:40 +01:00
|
|
|
|
|
|
|
// Insert it before the TabSet
|
|
|
|
$fields->insertBefore($options, 'Root');
|
2014-07-27 07:34:43 +02:00
|
|
|
});
|
2013-08-04 18:38:26 +02:00
|
|
|
|
2014-07-27 07:34:43 +02:00
|
|
|
$fields = parent::getCMSFields();
|
2014-12-11 11:42:36 +01:00
|
|
|
|
2015-02-02 11:32:40 +01:00
|
|
|
// We need to render an outer template to deal with our custom layout
|
|
|
|
$fields->fieldByName('Root')->setTemplate('TabSet_holder');
|
|
|
|
|
2013-07-21 12:23:35 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2015-03-04 23:20:47 +01:00
|
|
|
protected function onBeforeWrite() {
|
2013-07-21 12:23:35 +02:00
|
|
|
parent::onBeforeWrite();
|
2015-03-06 03:52:07 +01:00
|
|
|
|
|
|
|
// If no publish date is set, set the date to now.
|
|
|
|
if(!$this->PublishDate) $this->PublishDate = SS_Datetime::now()->getValue();
|
|
|
|
|
|
|
|
// If creating a new entry, assign the current member as an author
|
|
|
|
// This allows writers and contributors to then edit their new post
|
|
|
|
if(!$this->exists() && ($member = Member::currentUser())) {
|
|
|
|
$this->Authors()->add($member);
|
|
|
|
}
|
2013-07-21 12:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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()) {
|
2015-03-06 03:52:07 +01:00
|
|
|
$this->PublishDate = SS_Datetime::now()->getValue();
|
2014-06-13 12:45:28 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2015-03-06 03:52:07 +01:00
|
|
|
public function canEdit($member = null) {
|
|
|
|
$member = $member ?: Member::currentUser();
|
|
|
|
if(is_numeric($member)) $member = Member::get()->byID($member);
|
|
|
|
|
|
|
|
// Inherit permission
|
|
|
|
if(parent::canEdit($member)) return true;
|
|
|
|
|
|
|
|
// Check if assigned to a blog
|
|
|
|
$parent = $this->Parent();
|
|
|
|
if(!$parent || !$parent->exists() || !($parent instanceof Blog)) return false;
|
|
|
|
|
|
|
|
// Editors have full control
|
|
|
|
if($parent->isEditor($member)) return true;
|
|
|
|
|
|
|
|
// Only writers or contributors can edit
|
|
|
|
if(!$parent->isWriter($member) && !$parent->isContributor($member)) return false;
|
|
|
|
|
|
|
|
// And only if they are also authors
|
|
|
|
return $this->isAuthor($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this user can edit the authors list
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canEditAuthors($member = null) {
|
|
|
|
$member = $member ?: Member::currentUser();
|
|
|
|
if(is_numeric($member)) $member = Member::get()->byID($member);
|
|
|
|
|
|
|
|
$extended = $this->extendedCan('canEditAuthors', $member);
|
|
|
|
if($extended !== null) return $extended;
|
|
|
|
|
|
|
|
// Check blog roles
|
|
|
|
$parent = $this->Parent();
|
|
|
|
if($parent instanceof Blog && $parent->exists()) {
|
|
|
|
// Editors can do anything
|
|
|
|
if($parent->isEditor($member)) return true;
|
|
|
|
|
|
|
|
// Writers who are also authors can edit authors
|
|
|
|
if($parent->isWriter($member) && $this->isAuthor($member)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check permission
|
|
|
|
return Permission::checkMember($member, Blog::MANAGE_USERS);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canPublish($member = null) {
|
|
|
|
$member = $member ?: Member::currentUser();
|
|
|
|
if(is_numeric($member)) $member = Member::get()->byID($member);
|
|
|
|
|
|
|
|
if(Permission::checkMember($member, "ADMIN")) return true;
|
|
|
|
|
|
|
|
// Standard mechanism for accepting permission changes from extensions
|
|
|
|
$extended = $this->extendedCan('canPublish', $member);
|
|
|
|
if($extended !== null) return $extended;
|
|
|
|
|
|
|
|
// Check blog roles
|
|
|
|
$parent = $this->Parent();
|
|
|
|
if($parent instanceof Blog && $parent->exists()) {
|
|
|
|
// Editors can do anything
|
|
|
|
if($parent->isEditor($member)) return true;
|
|
|
|
|
|
|
|
// Writers who are also authors can edit authors
|
|
|
|
if($parent->isWriter($member) && $this->isAuthor($member)) return true;
|
|
|
|
|
|
|
|
// Contributors can ONLY publish this page if they somehow have global publish permissions
|
|
|
|
// In this case defer to old canEdit implementation
|
|
|
|
if($parent->isContributor($member)) return parent::canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal case - fail over to canEdit()
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
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
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
2015-04-01 03:52:46 +02:00
|
|
|
/**
|
|
|
|
* Resolves static and dynamic authors linked to this post.
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function getCredits()
|
|
|
|
{
|
|
|
|
$list = new ArrayList();
|
|
|
|
|
|
|
|
$list->merge($this->getDynamicCredits());
|
|
|
|
$list->merge($this->getStaticCredits());
|
|
|
|
|
|
|
|
return $list->sort('Name');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves dynamic authors linked to this post.
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
protected function getDynamicCredits()
|
|
|
|
{
|
|
|
|
$items = new ArrayList();
|
|
|
|
|
|
|
|
foreach($this->Authors() as $author) {
|
|
|
|
$items->push(
|
|
|
|
$author->customise(array(
|
|
|
|
'URL' => $this->Parent->ProfileLink($author->URLSegment),
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2014-06-11 11:20:20 +02:00
|
|
|
|
2015-04-01 03:52:46 +02:00
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves static authors linked to this post.
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
protected function getStaticCredits()
|
|
|
|
{
|
|
|
|
$items = new ArrayList();
|
|
|
|
|
|
|
|
$authors = array_filter(preg_split('/\s*,\s*/', $this->AuthorNames));
|
|
|
|
|
|
|
|
foreach ($authors as $author) {
|
|
|
|
$item = new ArrayData(array(
|
|
|
|
'Name' => $author,
|
|
|
|
));
|
|
|
|
|
|
|
|
$items->push($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
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
|
|
|
}
|