silverstripe-blog/code/BlogEntry.php

210 lines
4.8 KiB
PHP
Raw Normal View History

2007-09-17 12:14:28 +02:00
<?php
2007-09-07 00:33:58 +02:00
2007-11-14 02:15:40 +01:00
/**
* @package blog
*/
2007-11-14 02:15:40 +01:00
/**
* An individual blog entry page to show a blog entry in full
*/
2007-09-07 00:33:58 +02:00
class BlogEntry extends Page {
static $default_parent = 'BlogHolder';
2007-09-07 00:33:58 +02:00
static $can_be_root = false;
2008-08-21 03:30:04 +02:00
static $icon = "blog/images/blogpage";
2007-09-07 00:33:58 +02:00
static $db = array(
2008-10-16 15:46:59 +02:00
"Date" => "SSDatetime",
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 $has_one = array(
);
static $has_many = array(
);
static $many_many = array(
);
2007-09-07 00:33:58 +02:00
static $casting = array(
"Date" => "Date"
);
static $defaults = array(
2008-04-09 01:21:13 +02:00
"ProvideComments" => true
2007-09-07 00:33:58 +02:00
);
static $extensions = array(
'Hierarchy',
'TrackBackDecorator',
"Versioned('Stage', 'Live')"
);
2007-09-07 00:33:58 +02:00
static $allowed_children = "none";
/**
* Is WYSIWYG editing allowed?
* @var boolean
*/
static $allow_wysiwyg_editing = false;
/**
* 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.
*/
public function populateDefaults(){
parent::populateDefaults();
2008-01-14 03:19:53 +01:00
$this->Date = date("Y-m-d H:i:s",time());
2007-09-07 00:33:58 +02:00
}
/**
* Ensures the most recent article edited on the same day is shown first.
*/
public function setDate($val){
$datepart = date("Y-m-d",strtotime($val));
$minutepart = date("H:i:s",time());
$date = $datepart . " " . $minutepart;
return $this->setField("Date",$date);
}
function getCMSFields() {
Requirements::javascript('blog/javascript/bbcodehelp.js');
Requirements::css('blog/css/bbcodehelp.css');
$firstName = Member::currentUser() ? Member::currentUser()->FirstName : '';
$codeparser = new BBCodeParser();
2007-09-07 00:33:58 +02:00
$fields = parent::getCMSFields();
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", new CalendarDateField("Date", _t("BlogEntry.DT", "Date")),"Content");
$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");
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() {
2007-09-07 00:33:58 +02:00
$theseTags = split(" *, *", trim($this->Tags));
$output = new DataObjectSet();
foreach($theseTags as $tag) {
$output->push(new ArrayData(array(
"Tag" => $tag,
2007-11-23 09:35:49 +01:00
"Link" => $this->getParent()->Link() . 'tag/' . urlencode($tag)
2007-09-07 00:33:58 +02:00
)));
}
if($this->Tags){
return $output;
}
}
2007-11-14 02:15:40 +01:00
/**
* Get the sidebar
*/
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() {
2007-09-07 00:33:58 +02:00
return $this->getParent()->Link('post')."/".$this->ID."/";
}
/**
* 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;
}
2007-09-07 00:33:58 +02:00
}
class BlogEntry_Controller extends Page_Controller {
function init() {
parent::init();
Requirements::themedCSS("blog");
}
2007-11-14 02:15:40 +01:00
/**
* Gets a link to unpublish the blog entry
*/
2007-09-07 00:33:58 +02:00
function unpublishPost(){
if(!Permission::check('ADMIN')){
Security::permissionFailure($this,
"Unpublishing blogs is an administrator task. Please log in.");
}
else{
$SQL_id = Convert::raw2sql($this->ID);
$page = DataObject::get_by_id("SiteTree", $SQL_id);
$page->deleteFromStage('Live');
$page->flushCache();
$page = DataObject::get_by_id("SiteTree", $SQL_id);
$page->Status = "Unpublished";
Director::redirect($this->getParent()->Link());
}
}
2007-09-07 00:33:58 +02:00
}
2007-11-23 09:35:49 +01:00
?>