2012-05-21 04:58:26 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* An individual blog entry page type.
|
|
|
|
*
|
|
|
|
* @package blog
|
|
|
|
*/
|
|
|
|
class BlogEntry extends Page {
|
2013-04-04 04:30:33 +02:00
|
|
|
|
|
|
|
private static $db = array(
|
2012-05-21 04:58:26 +02:00
|
|
|
"Date" => "SS_Datetime",
|
|
|
|
"Author" => "Text",
|
|
|
|
"Tags" => "Text"
|
|
|
|
);
|
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $default_parent = 'BlogHolder';
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $can_be_root = false;
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $icon = "blog/images/blogpage-file.png";
|
2012-09-21 17:39:31 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $description = "An individual blog entry";
|
2012-10-09 02:14:31 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $singular_name = 'Blog Entry Page';
|
2012-10-09 02:14:31 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $plural_name = 'Blog Entry Pages';
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $has_one = array();
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $has_many = array();
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $many_many = array();
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $belongs_many_many = array();
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $defaults = array(
|
2012-05-21 04:58:26 +02:00
|
|
|
"ProvideComments" => true,
|
|
|
|
'ShowInMenus' => false
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is WYSIWYG editing allowed?
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
static $allow_wysiwyg_editing = true;
|
|
|
|
|
|
|
|
/**
|
2014-06-02 00:36:32 +02:00
|
|
|
* Overload so that the default date is today and the default author is the logged in Member.
|
2012-05-21 04:58:26 +02:00
|
|
|
*/
|
|
|
|
public function populateDefaults(){
|
|
|
|
parent::populateDefaults();
|
|
|
|
|
|
|
|
$this->setField('Date', date('Y-m-d H:i:s', strtotime('now')));
|
2014-12-08 06:43:50 +01:00
|
|
|
|
|
|
|
if(Security::database_is_ready() && ($member = Member::currentUser())) {
|
|
|
|
$this->setField('Author', $member->getName());
|
|
|
|
}
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCMSFields() {
|
|
|
|
Requirements::javascript('blog/javascript/bbcodehelp.js');
|
|
|
|
Requirements::themedCSS('bbcodehelp');
|
|
|
|
|
|
|
|
$codeparser = new BBCodeParser();
|
|
|
|
|
|
|
|
SiteTree::disableCMSFieldsExtensions();
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
SiteTree::enableCMSFieldsExtensions();
|
|
|
|
|
|
|
|
if(!self::$allow_wysiwyg_editing) {
|
|
|
|
$fields->removeFieldFromTab("Root.Main","Content");
|
|
|
|
$fields->addFieldToTab("Root.Main", new TextareaField("Content", _t("BlogEntry.CN", "Content"), 20));
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields->addFieldToTab("Root.Main", $dateField = new DatetimeField("Date", _t("BlogEntry.DT", "Date")),"Content");
|
|
|
|
$dateField->getDateField()->setConfig('showcalendar', true);
|
2013-06-06 13:57:01 +02:00
|
|
|
$dateField->getTimeField()->setConfig('timeformat', 'H:m:s');
|
2014-06-02 00:36:32 +02:00
|
|
|
$fields->addFieldToTab("Root.Main", new TextField("Author", _t("BlogEntry.AU", "Author")),"Content");
|
2012-05-21 04:58:26 +02:00
|
|
|
|
|
|
|
if(!self::$allow_wysiwyg_editing) {
|
|
|
|
$fields->addFieldToTab("Root.Main", new LiteralField("BBCodeHelper", "<div id='BBCode' class='field'>" .
|
|
|
|
"<a id=\"BBCodeHint\" target='new'>" . _t("BlogEntry.BBH", "BBCode help") . "</a>" .
|
|
|
|
"<div id='BBTagsHolder' style='display:none;'>".$codeparser->useable_tagsHTML()."</div></div>"));
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields->addFieldToTab("Root.Main", new TextField("Tags", _t("BlogEntry.TS", "Tags (comma sep.)")),"Content");
|
|
|
|
|
|
|
|
$this->extend('updateCMSFields', $fields);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2014-02-28 05:00:54 +01:00
|
|
|
/**
|
|
|
|
* Safely split and parse all distinct tags assigned to this BlogEntry
|
|
|
|
*
|
|
|
|
* @return array Associative array of lowercase tag to native case tags
|
|
|
|
*/
|
|
|
|
public function TagNames() {
|
|
|
|
$tags = preg_split("/\s*,\s*/", trim($this->Tags));
|
|
|
|
$results = array();
|
|
|
|
foreach($tags as $tag) {
|
|
|
|
if($tag) $results[mb_strtolower($tag)] = $tag;
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2012-05-21 04:58:26 +02:00
|
|
|
/**
|
|
|
|
* Returns the tags added to this blog entry
|
2014-02-28 05:00:54 +01:00
|
|
|
*
|
|
|
|
* @return ArrayList List of ArrayData with Tag, Link, and URLTag keys
|
2012-05-21 04:58:26 +02:00
|
|
|
*/
|
2014-02-28 05:00:54 +01:00
|
|
|
public function TagsCollection() {
|
2012-05-21 05:58:40 +02:00
|
|
|
|
2014-02-28 05:00:54 +01:00
|
|
|
$tags = $this->TagNames();
|
2012-05-21 04:58:26 +02:00
|
|
|
$output = new ArrayList();
|
|
|
|
|
2014-02-28 05:00:54 +01:00
|
|
|
$link = ($parent = $this->getParent()) ? $parent->Link('tag') : '';
|
|
|
|
foreach($tags as $tag => $tagLabel) {
|
|
|
|
$urlKey = urlencode($tag);
|
2012-05-21 04:58:26 +02:00
|
|
|
$output->push(new ArrayData(array(
|
2014-02-28 05:00:54 +01:00
|
|
|
'Tag' => $tagLabel,
|
|
|
|
'Link' => Controller::join_links($link, $urlKey),
|
|
|
|
'URLTag' => $urlKey
|
2012-05-21 04:58:26 +02:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2014-02-28 05:00:54 +01:00
|
|
|
return $output;
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Content() {
|
|
|
|
if(self::$allow_wysiwyg_editing) {
|
|
|
|
return $this->getField('Content');
|
|
|
|
} else {
|
|
|
|
$parser = new BBCodeParser($this->Content);
|
|
|
|
$content = new HTMLText('Content');
|
|
|
|
$content->value = $parser->parse();
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To be used by RSSFeed. If RSSFeed uses Content field, it doesn't pull in correctly parsed content.
|
|
|
|
*/
|
|
|
|
function RSSContent() {
|
|
|
|
return $this->Content();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a bbcode parsed summary of the blog entry
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
function ParagraphSummary(){
|
|
|
|
user_error("BlogEntry::ParagraphSummary() is deprecated; use BlogEntry::Content()", E_USER_NOTICE);
|
|
|
|
|
|
|
|
$val = $this->Content();
|
|
|
|
$content = $val;
|
|
|
|
|
|
|
|
if(!($content instanceof HTMLText)) {
|
|
|
|
$content = new HTMLText('Content');
|
|
|
|
$content->value = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content->FirstParagraph('html');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the bbcode parsed content
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
function ParsedContent() {
|
|
|
|
user_error("BlogEntry::ParsedContent() is deprecated; use BlogEntry::Content()", E_USER_NOTICE);
|
|
|
|
return $this->Content();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Link for editing this blog entry
|
|
|
|
*/
|
|
|
|
function EditURL() {
|
|
|
|
return ($this->getParent()) ? $this->getParent()->Link('post') . '/' . $this->ID . '/' : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function IsOwner() {
|
|
|
|
if(method_exists($this->Parent(), 'IsOwner')) {
|
|
|
|
return $this->Parent()->IsOwner();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this to enable WYSIWYG editing on your blog entries.
|
|
|
|
* By default the blog uses BBCode
|
|
|
|
*/
|
|
|
|
static function allow_wysiwyg_editing() {
|
|
|
|
self::$allow_wysiwyg_editing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the previous blog entry from this section of blog pages.
|
|
|
|
*
|
|
|
|
* @return BlogEntry
|
|
|
|
*/
|
|
|
|
function PreviousBlogEntry() {
|
|
|
|
return DataObject::get_one(
|
|
|
|
'BlogEntry',
|
|
|
|
"\"SiteTree\".\"ParentID\" = '$this->ParentID' AND \"BlogEntry\".\"Date\" < '$this->Date'",
|
|
|
|
true,
|
|
|
|
'Date DESC'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next blog entry from this section of blog pages.
|
|
|
|
*
|
|
|
|
* @return BlogEntry
|
|
|
|
*/
|
|
|
|
function NextBlogEntry() {
|
|
|
|
return DataObject::get_one(
|
|
|
|
'BlogEntry',
|
|
|
|
"\"SiteTree\".\"ParentID\" = '$this->ParentID' AND \"BlogEntry\".\"Date\" > '$this->Date'",
|
|
|
|
true,
|
|
|
|
'Date ASC'
|
|
|
|
);
|
|
|
|
}
|
2012-10-17 23:10:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the blog holder of this entry
|
|
|
|
*
|
|
|
|
* @return BlogHolder
|
|
|
|
*/
|
|
|
|
function getBlogHolder() {
|
|
|
|
$holder = null;
|
|
|
|
if($this->ParentID && $this->Parent()->ClassName == 'BlogHolder') {
|
|
|
|
$holder = $this->Parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $holder;
|
|
|
|
}
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class BlogEntry_Controller extends Page_Controller {
|
|
|
|
|
2013-04-04 04:24:53 +02:00
|
|
|
private static $allowed_actions = array(
|
2012-05-21 04:58:26 +02:00
|
|
|
'index',
|
|
|
|
'unpublishPost',
|
|
|
|
'PageComments',
|
|
|
|
'SearchForm'
|
|
|
|
);
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
parent::init();
|
|
|
|
|
2012-07-09 02:05:53 +02:00
|
|
|
Requirements::themedCSS("blog","blog");
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a link to unpublish the blog entry
|
|
|
|
*/
|
|
|
|
function unpublishPost() {
|
|
|
|
if(!$this->IsOwner()) {
|
|
|
|
Security::permissionFailure(
|
|
|
|
$this,
|
|
|
|
'Unpublishing blogs is an administrator task. Please log in.'
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$SQL_id = (int) $this->ID;
|
|
|
|
|
|
|
|
$page = DataObject::get_by_id('SiteTree', $SQL_id);
|
|
|
|
$page->deleteFromStage('Live');
|
|
|
|
$page->flushCache();
|
|
|
|
|
2012-07-08 01:25:53 +02:00
|
|
|
$this->redirect($this->getParent()->Link());
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Temporary workaround for compatibility with 'comments' module
|
|
|
|
* (has been extracted from sapphire/trunk in 12/2010).
|
|
|
|
*
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
function PageComments() {
|
|
|
|
if($this->hasMethod('CommentsForm')) return $this->CommentsForm();
|
|
|
|
else if(method_exists('Page_Controller', 'PageComments')) return parent::PageComments();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|