silverstripe-blog/code/BlogEntry.php

263 lines
6.0 KiB
PHP
Raw Normal View History

2007-09-17 12:14:28 +02:00
<?php
2007-11-14 02:15:40 +01:00
/**
* An individual blog entry page type.
*
2007-11-14 02:15:40 +01:00
* @package blog
*/
2007-09-07 00:33:58 +02:00
class BlogEntry extends Page {
static $db = array(
"Date" => "Datetime",
2007-09-07 00:33:58 +02:00
"Author" => "Text",
2008-04-09 01:21:13 +02:00
"Tags" => "Text"
2007-09-07 00:33:58 +02:00
);
static $default_parent = 'BlogHolder';
static $can_be_root = false;
static $icon = "blog/images/blogpage";
2009-01-20 05:01:45 +01:00
static $has_one = array();
2009-01-20 05:01:45 +01:00
static $has_many = array();
2009-01-20 05:01:45 +01:00
static $many_many = array();
static $belongs_many_many = array();
2007-09-07 00:33:58 +02:00
static $defaults = array(
"ProvideComments" => true,
'ShowInMenus' => false
2007-09-07 00:33:58 +02:00
);
static $extensions = array(
'Hierarchy',
'TrackBackDecorator',
"Versioned('Stage', 'Live')"
);
2007-09-07 00:33:58 +02:00
/**
* Is WYSIWYG editing allowed?
* @var boolean
*/
2009-11-30 09:20:46 +01:00
static $allow_wysiwyg_editing = true;
/**
* Is WYSIWYG editing enabled?
* Used in templates.
*
* @return boolean
*/
public function IsWYSIWYGEnabled() {
return self::$allow_wysiwyg_editing;
}
2007-09-07 00:33:58 +02:00
/**
* Overload so that the default date is today.
2007-09-07 00:33:58 +02:00
*/
public function populateDefaults(){
parent::populateDefaults();
$this->setField('Date', date('Y-m-d H:i:s', strtotime('now')));
2007-09-07 00:33:58 +02:00
}
function getCMSFields() {
Requirements::javascript('blog/javascript/bbcodehelp.js');
Requirements::themedCSS('bbcodehelp');
$firstName = Member::currentUser() ? Member::currentUser()->FirstName : '';
$codeparser = new BBCodeParser();
SiteTree::disableCMSFieldsExtensions();
2007-09-07 00:33:58 +02:00
$fields = parent::getCMSFields();
SiteTree::enableCMSFieldsExtensions();
if(!self::$allow_wysiwyg_editing) {
$fields->removeFieldFromTab("Root.Content.Main","Content");
$fields->addFieldToTab("Root.Content.Main", new TextareaField("Content", _t("BlogEntry.CN", "Content"), 20));
}
$fields->addFieldToTab("Root.Content.Main", $dateField = new DatetimeField("Date", _t("BlogEntry.DT", "Date")),"Content");
$dateField->getDateField()->setConfig('showcalendar', true);
$dateField->getTimeField()->setConfig('showdropdown', true);
$fields->addFieldToTab("Root.Content.Main", new TextField("Author", _t("BlogEntry.AU", "Author"), $firstName),"Content");
2007-09-07 00:33:58 +02:00
if(!self::$allow_wysiwyg_editing) {
$fields->addFieldToTab("Root.Content.Main", new LiteralField("BBCodeHelper", "<div id='BBCode' class='field'>" .
2008-01-06 23:48:49 +01:00
"<a id=\"BBCodeHint\" target='new'>" . _t("BlogEntry.BBH", "BBCode help") . "</a>" .
"<div id='BBTagsHolder' style='display:none;'>".$codeparser->useable_tagsHTML()."</div></div>"));
}
2007-09-07 00:33:58 +02:00
$fields->addFieldToTab("Root.Content.Main", new TextField("Tags", _t("BlogEntry.TS", "Tags (comma sep.)")),"Content");
$this->extend('updateCMSFields', $fields);
2007-09-07 00:33:58 +02:00
return $fields;
}
2007-11-14 02:15:40 +01:00
/**
* Returns the tags added to this blog entry
*/
function TagsCollection() {
$tags = split(" *, *", trim($this->Tags));
2007-09-07 00:33:58 +02:00
$output = new DataObjectSet();
$link = $this->getParent() ? $this->getParent()->Link('tag') : '';
foreach($tags as $tag) {
2007-09-07 00:33:58 +02:00
$output->push(new ArrayData(array(
'Tag' => $tag,
'Link' => $link . '/' . urlencode($tag),
'URLTag' => urlencode($tag)
2007-09-07 00:33:58 +02:00
)));
}
if($this->Tags) {
2007-09-07 00:33:58 +02:00
return $output;
}
}
2007-11-14 02:15:40 +01:00
/**
* Get the sidebar from the BlogHolder.
2007-11-14 02:15:40 +01:00
*/
2007-09-07 00:33:58 +02:00
function SideBar() {
return $this->getParent()->SideBar();
}
2007-11-14 02:15:40 +01:00
/**
* Get a bbcode parsed summary of the blog entry
*/
2007-09-07 00:33:58 +02:00
function ParagraphSummary(){
if(self::$allow_wysiwyg_editing) {
2008-01-22 04:44:44 +01:00
return $this->obj('Content')->FirstParagraph('html');
} else {
2008-01-22 04:44:44 +01:00
$parser = new BBCodeParser($this->Content);
$html = new HTMLText('Content');
$html->setValue($parser->parse());
return $html->FirstParagraph('html');
}
2007-09-07 00:33:58 +02:00
}
2007-11-14 02:15:40 +01:00
/**
* Get the bbcode parsed content
*/
2007-09-07 00:33:58 +02:00
function ParsedContent() {
if(self::$allow_wysiwyg_editing) {
return $this->obj('Content');
} else {
$parser = new BBCodeParser($this->Content);
$content = new Text('Content');
2008-10-24 02:05:01 +02:00
$content->value = $parser->parse();
return $content;
}
2007-09-07 00:33:58 +02:00
}
2007-11-14 02:15:40 +01:00
/**
* Link for editing this blog entry
2007-11-14 02:15:40 +01:00
*/
function EditURL() {
return $this->getParent()->Link('post') . '/' . $this->ID . '/';
2007-09-07 00:33:58 +02:00
}
/**
* Check to see if trackbacks are enabled.
*/
function TrackBacksEnabled() {
return $this->getParent()->TrackBacksEnabled;
}
function trackbackping() {
if($this->TrackBacksEnabled() && $this->hasExtension('TrackBackDecorator')) {
return $this->decoratedTrackbackping();
} else {
Director::redirect($this->Link());
}
}
2007-09-07 00:33:58 +02:00
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'
);
}
2007-09-07 00:33:58 +02:00
}
class BlogEntry_Controller extends Page_Controller {
2008-12-16 05:29:34 +01:00
static $allowed_actions = array(
'index',
2008-12-16 05:29:34 +01:00
'trackbackping',
2008-12-16 21:37:14 +01:00
'unpublishPost',
'PageComments',
'SearchForm'
2008-12-16 05:29:34 +01:00
);
2007-09-07 00:33:58 +02:00
function init() {
parent::init();
Requirements::themedCSS('blog');
2007-09-07 00:33:58 +02:00
}
2007-11-14 02:15:40 +01: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;
2007-09-07 00:33:58 +02:00
$page = DataObject::get_by_id('SiteTree', $SQL_id);
2007-09-07 00:33:58 +02:00
$page->deleteFromStage('Live');
$page->flushCache();
$page = DataObject::get_by_id('SiteTree', $SQL_id);
$page->Status = 'Unpublished';
2007-09-07 00:33:58 +02:00
Director::redirect($this->getParent()->Link());
}
}
2007-09-07 00:33:58 +02:00
}
?>