silverstripe-blog/code/BlogHolder.php

284 lines
8.0 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
/**
* Blog holder to display summarised blog entries.
*
* A blog holder is the leaf end of a BlogTree, but can also be used standalone in simpler circumstances.
* BlogHolders can only hold BlogEntries, BlogTrees can only hold BlogTrees and BlogHolders
* BlogHolders have a form on them for easy posting, and an owner that can post to them, BlogTrees don't
2007-11-14 02:15:40 +01:00
*/
class BlogHolder extends BlogTree implements PermissionProvider {
2007-11-14 02:16:13 +01:00
static $icon = "blog/images/blogholder";
2007-09-07 00:33:58 +02:00
static $db = array(
'TrackBacksEnabled' => 'Boolean',
'AllowCustomAuthors' => 'Boolean',
2007-09-07 00:33:58 +02:00
);
2007-09-07 00:33:58 +02:00
static $has_one = array(
'Owner' => 'Member',
2007-09-07 00:33:58 +02:00
);
2007-09-07 00:33:58 +02:00
static $allowed_children = array(
'BlogEntry'
);
2007-09-07 00:33:58 +02:00
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new CheckboxField('TrackBacksEnabled', 'Enable TrackBacks'));
$fields->addFieldToTab('Root.Content.Main', new DropdownField('OwnerID', 'Blog owner', DataObject::get('Member')->toDropDownMap('ID', 'Name', 'None')));
$fields->addFieldToTab('Root.Content.Main', new CheckboxField('AllowCustomAuthors', 'Allow non-admins to have a custom author field'));
2007-09-07 00:33:58 +02:00
return $fields;
}
public function BlogHolderIDs() {
return array( $this->ID );
2007-09-07 00:33:58 +02:00
}
/*
* @todo: These next few functions don't really belong in the model. Can we remove them?
*/
2007-11-14 02:15:40 +01:00
/**
* Only display the blog entries that have the specified tag
*/
2008-04-22 03:20:59 +02:00
function ShowTag() {
if(Director::urlParam('Action') == 'tag') {
return Convert::raw2xml(Director::urlParam('ID'));
}
2007-09-07 00:33:58 +02:00
}
2007-11-14 02:15:40 +01:00
/**
* Check if url has "/post"
*/
function isPost() {
2007-09-07 00:33:58 +02:00
return Director::urlParam('Action') == 'post';
}
2007-11-14 02:15:40 +01:00
/**
* Link for creating a new blog entry
*/
2007-09-07 00:33:58 +02:00
function postURL(){
return $this->Link('post');
2007-09-07 00:33:58 +02:00
}
/**
* Returns true if the current user is an admin, or is the owner of this blog
*
* @return Boolean
*/
function IsOwner() {
return (Permission::check('BLOGMANAGEMENT') || Permission::check('ADMIN'));
//return Permission::check('ADMIN') || (Member::currentUserID() == $this->OwnerID);
}
2007-11-14 02:15:40 +01:00
/**
* Create default blog setup
*/
2007-09-07 00:33:58 +02:00
function requireDefaultRecords() {
parent::requireDefaultRecords();
$blogHolder = DataObject::get_one('BlogHolder');
//TODO: This does not check for whether this blogholder is an orphan or not
if(!$blogHolder) {
2007-09-07 00:33:58 +02:00
$blogholder = new BlogHolder();
$blogholder->Title = "Blog";
$blogholder->URLSegment = "blog";
$blogholder->Status = "Published";
2007-09-07 00:33:58 +02:00
$widgetarea = new WidgetArea();
$widgetarea->write();
2007-09-07 00:33:58 +02:00
$blogholder->SideBarID = $widgetarea->ID;
$blogholder->write();
$blogholder->publish("Stage", "Live");
2007-09-07 00:33:58 +02:00
$managementwidget = new BlogManagementWidget();
$managementwidget->ParentID = $widgetarea->ID;
$managementwidget->write();
2007-09-07 00:33:58 +02:00
$tagcloudwidget = new TagCloudWidget();
$tagcloudwidget->ParentID = $widgetarea->ID;
$tagcloudwidget->write();
2007-09-07 00:33:58 +02:00
$archivewidget = new ArchiveWidget();
$archivewidget->ParentID = $widgetarea->ID;
$archivewidget->write();
2007-09-07 00:33:58 +02:00
$widgetarea->write();
2007-09-07 00:33:58 +02:00
$blog = new BlogEntry();
$blog->Title = _t('BlogHolder.SUCTITLE', "SilverStripe blog module successfully installed");
2007-09-07 00:33:58 +02:00
$blog->URLSegment = 'sample-blog-entry';
$blog->Tags = _t('BlogHolder.SUCTAGS',"silverstripe, blog");
$blog->Content = _t('BlogHolder.SUCCONTENT',"Congratulations, the SilverStripe blog module has been successfully installed. This blog entry can be safely deleted. You can configure aspects of your blog (such as the widgets displayed in the sidebar) in [url=admin]the CMS[/url].");
2007-09-07 00:33:58 +02:00
$blog->Status = "Published";
$blog->ParentID = $blogholder->ID;
$blog->write();
$blog->publish("Stage", "Live");
// 2.3/2.4 dual compatibility
if(method_exists('DB', 'alteration_message')) DB::alteration_message("Blog page created","created");
2007-09-07 00:33:58 +02:00
}
}
}
class BlogHolder_Controller extends BlogTree_Controller {
static $allowed_actions = array(
'index',
'tag',
'postblog' => 'BLOGMANAGEMENT',
'post' => 'BLOGMANAGEMENT',
'BlogEntryForm' => 'BLOGMANAGEMENT',
);
2007-09-07 00:33:58 +02:00
function init() {
parent::init();
2007-09-07 00:33:58 +02:00
Requirements::themedCSS("bbcodehelp");
}
/**
* TODO: this is an urgent fix to work with archive link (e.g. page_url/2009/04). Replace this with something better.
*/
function checkAccessAction($action) {
if (preg_match('/[0-9]{4}/', $action))
{
return true;
}
return parent::checkAccessAction($action);
}
2007-11-14 02:15:40 +01:00
/**
* Return list of usable tags for help
*/
2007-09-07 00:33:58 +02:00
function BBTags() {
return BBCodeParser::usable_tags();
}
function providePermissions() {
return array("BLOGMANAGEMENT" => "Blog management");
}
2007-11-14 02:15:40 +01:00
/**
* Post a new blog entry
*/
2007-09-07 00:33:58 +02:00
function post(){
if(!Permission::check('BLOGMANAGEMENT')) return Security::permissionFailure();
$page = $this->customise(array(
'Content' => false,
'Form' => $this->BlogEntryForm()
));
return $page->renderWith('Page');
2007-09-07 00:33:58 +02:00
}
/**
* A simple form for creating blog entries
*/
function BlogEntryForm() {
if(!Permission::check('BLOGMANAGEMENT')) return Security::permissionFailure();
Requirements::javascript('jsparty/behaviour.js');
Requirements::javascript('jsparty/prototype.js');
Requirements::javascript('jsparty/scriptaculous/effects.js');
Requirements::javascript('cms/javascript/PageCommentInterface.js');
Requirements::javascript('blog/javascript/bbcodehelp.js');
$id = 0;
if(Director::urlParam('ID')) {
$id = (int) Director::urlParam('ID');
}
$codeparser = new BBCodeParser();
$membername = Member::currentMember() ? Member::currentMember()->getName() : "";
if(BlogEntry::$allow_wysiwyg_editing) {
$contentfield = new HtmlEditorField("BlogPost", _t("BlogEntry.CN"));
} else {
$contentfield = new CompositeField(
new LiteralField("BBCodeHelper","<a id=\"BBCodeHint\" target='new'>"._t("BlogEntry.BBH")."</a><div class='clear'><!-- --></div>" ),
new TextareaField("BlogPost", _t("BlogEntry.CN"),20), // This is called BlogPost as the id #Content is generally used already
new LiteralField("BBCodeTags","<div id=\"BBTagsHolder\">".$codeparser->useable_tagsHTML()."</div>")
);
}
2008-12-17 04:59:02 +01:00
if(class_exists('TagField')) {
$tagfield = new TagField('Tags', null, null, 'BlogEntry');
$tagfield->setSeparator(', ');
} else {
$tagfield = new TextField('Tags');
}
$field = 'TextField';
if(!$this->AllowCustomAuthors && !Permission::check('ADMIN')) {
$field = 'ReadonlyField';
}
$fields = new FieldSet(
new HiddenField("ID", "ID"),
new TextField("Title",_t('BlogHolder.SJ', "Subject")),
new $field("Author",_t('BlogEntry.AU'),$membername),
$contentfield,
2008-12-17 04:59:02 +01:00
$tagfield,
new LiteralField("Tagsnote"," <label id='tagsnote'>"._t('BlogHolder.TE', "For example: sport, personal, science fiction")."<br/>" .
_t('BlogHolder.SPUC', "Please separate tags using commas.")."</label>")
);
$submitAction = new FormAction('postblog', _t('BlogHolder.POST', 'Post blog entry'));
$actions = new FieldSet($submitAction);
$validator = new RequiredFields('Title','Content');
$form = new Form($this, 'BlogEntryForm',$fields, $actions,$validator);
if($id != 0) {
$entry = DataObject::get_by_id('BlogEntry', $id);
if($entry->IsOwner()) {
$form->loadDataFrom($entry);
$form->datafieldByName('BlogPost')->setValue($entry->Content);
}
} else {
$form->loadDataFrom(array("Author" => Cookie::get("BlogHolder_Name")));
}
return $form;
}
function postblog($data, $form) {
if(!Permission::check('BLOGMANAGEMENT')) return Security::permissionFailure();
2007-09-07 00:33:58 +02:00
Cookie::set("BlogHolder_Name", $data['Author']);
$blogentry = false;
if(isset($data['ID']) && $data['ID']) {
$blogentry = DataObject::get_by_id("BlogEntry", $data['ID']);
if(!$blogentry->IsOwner()) {
unset($blogentry);
}
}
if(!$blogentry) {
$blogentry = new BlogEntry();
2007-09-07 00:33:58 +02:00
}
$form->saveInto($blogentry);
$blogentry->ParentID = $this->ID;
$blogentry->Content = $form->datafieldByName('BlogPost')->dataValue();
2007-09-07 00:33:58 +02:00
$blogentry->Status = "Published";
$blogentry->writeToStage("Stage");
$blogentry->publish("Stage", "Live");
Director::redirect($this->Link());
2007-09-07 00:33:58 +02:00
}
}
?>