2012-05-21 14:58:26 +12:00
< ? php
/**
* @ package blog
*/
/**
* 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
*/
class BlogHolder extends BlogTree implements PermissionProvider {
2013-04-04 15:30:33 +13:00
private static $icon = " blog/images/blogholder-file.png " ;
2013-04-11 01:01:09 +02:00
2013-04-04 15:30:33 +13:00
private static $description = " Displays listings of blog entries " ;
2012-10-09 10:14:31 +10:00
2013-04-04 15:30:33 +13:00
private static $singular_name = 'Blog Holder Page' ;
2013-04-11 01:01:09 +02:00
2013-04-04 15:30:33 +13:00
private static $plural_name = 'Blog Holder Pages' ;
2012-09-21 11:39:31 -04:00
2013-04-04 15:30:33 +13:00
private static $db = array (
2012-05-21 14:58:26 +12:00
'AllowCustomAuthors' => 'Boolean' ,
2012-10-18 10:10:16 +13:00
'ShowFullEntry' => 'Boolean' ,
2012-05-21 14:58:26 +12:00
);
2013-04-04 15:30:33 +13:00
private static $has_one = array (
2012-05-21 14:58:26 +12:00
'Owner' => 'Member' ,
);
2013-04-04 15:30:33 +13:00
private static $allowed_children = array (
2012-05-21 14:58:26 +12:00
'BlogEntry'
);
2014-08-22 16:36:42 +12:00
public function getCMSFields () {
2012-05-21 14:58:26 +12:00
$blogOwners = $this -> blogOwners ();
2014-08-22 16:36:42 +12:00
// Add holder fields prior to extensions being called
$this -> beforeUpdateCMSFields ( function ( $fields ) use ( $blogOwners ) {
$fields -> addFieldsToTab (
'Root.Main' ,
array (
DropdownField :: create ( 'OwnerID' , 'Blog owner' , $blogOwners -> map ( 'ID' , 'Name' ) -> toArray ())
-> setEmptyString ( '(None)' )
-> setHasEmptyDefault ( true ),
CheckboxField :: create ( 'AllowCustomAuthors' , 'Allow non-admins to have a custom author field' ),
CheckboxField :: create ( " ShowFullEntry " , " Show Full Entry " )
-> setDescription ( 'Show full content in overviews rather than summary' )
),
" Content "
);
});
2012-11-13 09:59:48 +13:00
2014-08-22 16:36:42 +12:00
return parent :: getCMSFields ();
2012-05-21 14:58:26 +12:00
}
/**
* Get members who have BLOGMANAGEMENT and ADMIN permission
2014-08-22 16:36:42 +12:00
*
* @ param array $sort
* @ param string $direction
* @ return SS_List
*/
public function blogOwners ( $sort = array ( 'FirstName' => 'ASC' , 'Surname' => 'ASC' ), $direction = null ) {
$members = Permission :: get_members_by_permission ( array ( 'ADMIN' , 'BLOGMANAGEMENT' ));
2012-05-21 14:58:26 +12:00
$members -> sort ( $sort );
$this -> extend ( 'extendBlogOwners' , $members );
return $members ;
}
public function BlogHolderIDs () {
return array ( $this -> ID );
}
/*
* @ todo : These next few functions don ' t really belong in the model . Can we remove them ?
*/
/**
* Only display the blog entries that have the specified tag
*/
2014-08-22 16:36:42 +12:00
public function ShowTag () {
2012-05-21 14:58:26 +12:00
if ( $this -> request -> latestParam ( 'Action' ) == 'tag' ) {
return Convert :: raw2xml ( Director :: urlParam ( 'ID' ));
}
}
/**
* Check if url has " /post "
*/
2014-08-22 16:36:42 +12:00
public function isPost () {
2012-05-21 14:58:26 +12:00
return $this -> request -> latestParam ( 'Action' ) == 'post' ;
}
/**
* Link for creating a new blog entry
*/
2014-08-22 16:36:42 +12:00
public function postURL () {
2012-05-21 14:58:26 +12:00
return $this -> Link ( 'post' );
}
/**
* Returns true if the current user is an admin , or is the owner of this blog
*
2014-08-22 16:36:42 +12:00
* @ return bool
2012-05-21 14:58:26 +12:00
*/
2014-08-22 16:36:42 +12:00
public function IsOwner () {
2012-05-21 14:58:26 +12:00
return ( Permission :: check ( 'BLOGMANAGEMENT' ) || Permission :: check ( 'ADMIN' ));
}
/**
* Create default blog setup
*/
2014-08-22 16:36:42 +12:00
public function requireDefaultRecords () {
2012-05-21 14:58:26 +12:00
parent :: requireDefaultRecords ();
2014-02-27 15:52:27 +13:00
// Skip creation of default records
if ( ! self :: config () -> create_default_pages ) return ;
2012-05-21 14:58:26 +12:00
$blogHolder = DataObject :: get_one ( 'BlogHolder' );
//TODO: This does not check for whether this blogholder is an orphan or not
if ( ! $blogHolder ) {
$blogholder = new BlogHolder ();
$blogholder -> Title = " Blog " ;
$blogholder -> URLSegment = " blog " ;
$blogholder -> Status = " Published " ;
2013-04-16 15:28:45 +02:00
$blogholder -> write ();
$blogholder -> publish ( " Stage " , " Live " );
2012-05-21 14:58:26 +12:00
2013-04-16 15:28:45 +02:00
// Add default widgets to first found WidgetArea relationship
2012-05-28 18:12:53 +12:00
if ( class_exists ( 'WidgetArea' )) {
2013-04-16 15:28:45 +02:00
foreach ( $this -> has_one () as $name => $class ) {
2013-10-21 10:19:41 +02:00
if ( $class == 'WidgetArea' || is_subclass_of ( $class , 'WidgetArea' )) {
2013-04-16 15:28:45 +02:00
$relationName = " { $name } ID " ;
$widgetarea = new WidgetArea ();
$widgetarea -> write ();
2012-05-28 18:12:53 +12:00
2013-04-16 15:28:45 +02:00
$blogholder -> $relationName = $widgetarea -> ID ;
$blogholder -> write ();
$blogholder -> publish ( " Stage " , " Live " );
2012-05-28 18:12:53 +12:00
2013-04-16 15:28:45 +02:00
$managementwidget = new BlogManagementWidget ();
$managementwidget -> ParentID = $widgetarea -> ID ;
$managementwidget -> write ();
2012-05-28 18:12:53 +12:00
2013-04-16 15:28:45 +02:00
$tagcloudwidget = new TagCloudWidget ();
$tagcloudwidget -> ParentID = $widgetarea -> ID ;
$tagcloudwidget -> write ();
2012-05-28 18:12:53 +12:00
2013-04-16 15:28:45 +02:00
$archivewidget = new ArchiveWidget ();
$archivewidget -> ParentID = $widgetarea -> ID ;
$archivewidget -> write ();
2012-05-28 18:12:53 +12:00
2013-04-16 15:28:45 +02:00
$widgetarea -> write ();
2012-05-28 18:12:53 +12:00
2013-04-16 15:28:45 +02:00
break ; // only apply to one
}
}
}
2012-05-21 14:58:26 +12:00
$blog = new BlogEntry ();
$blog -> Title = _t ( 'BlogHolder.SUCTITLE' , " SilverStripe blog module successfully installed " );
$blog -> URLSegment = 'sample-blog-entry' ;
$blog -> Tags = _t ( 'BlogHolder.SUCTAGS' , " silverstripe, blog " );
2013-04-16 15:28:45 +02:00
$blog -> Content = _t ( 'BlogHolder.SUCCONTENT' , " <p>Congratulations, the SilverStripe blog module has been successfully installed. This blog entry can be safely deleted. You can configure aspects of your blog in <a href= \" admin \" >the CMS</a>.</p> " );
2012-05-21 14:58:26 +12:00
$blog -> Status = " Published " ;
$blog -> ParentID = $blogholder -> ID ;
$blog -> write ();
$blog -> publish ( " Stage " , " Live " );
DB :: alteration_message ( " Blog page created " , " created " );
}
}
2013-04-14 23:18:00 +02:00
2014-08-22 16:36:42 +12:00
public function providePermissions () {
2013-04-14 23:18:00 +02:00
return array ( " BLOGMANAGEMENT " => " Blog management " );
}
2012-05-21 14:58:26 +12:00
}
class BlogHolder_Controller extends BlogTree_Controller {
2013-04-04 15:24:53 +13:00
private static $allowed_actions = array (
2012-05-21 14:58:26 +12:00
'index' ,
'tag' ,
'date' ,
'metaweblog' ,
'postblog' => 'BLOGMANAGEMENT' ,
2013-03-18 11:00:04 +13:00
'post' ,
2012-05-21 14:58:26 +12:00
'BlogEntryForm' => 'BLOGMANAGEMENT' ,
);
2014-08-22 16:36:42 +12:00
public function init () {
2012-05-21 14:58:26 +12:00
parent :: init ();
Requirements :: themedCSS ( " bbcodehelp " );
}
/**
* Return list of usable tags for help
*/
2014-08-22 16:36:42 +12:00
public function BBTags () {
2012-05-21 14:58:26 +12:00
return BBCodeParser :: usable_tags ();
}
/**
* Post a new blog entry
*/
2014-08-22 16:36:42 +12:00
public function post (){
2012-05-21 14:58:26 +12:00
if ( ! Permission :: check ( 'BLOGMANAGEMENT' )) return Security :: permissionFailure ();
$page = $this -> customise ( array (
'Content' => false ,
'Form' => $this -> BlogEntryForm ()
));
return $page -> renderWith ( 'Page' );
}
/**
* A simple form for creating blog entries
2014-08-22 16:36:42 +12:00
*
* @ return Form
2012-05-21 14:58:26 +12:00
*/
2014-08-22 16:36:42 +12:00
public function BlogEntryForm () {
2012-05-21 14:58:26 +12:00
if ( ! Permission :: check ( 'BLOGMANAGEMENT' )) return Security :: permissionFailure ();
2014-08-22 16:36:42 +12:00
$codeparser = BBCodeParser :: create ();
2012-05-21 14:58:26 +12:00
$membername = Member :: currentUser () ? Member :: currentUser () -> getName () : " " ;
if ( BlogEntry :: $allow_wysiwyg_editing ) {
2014-08-22 16:36:42 +12:00
$contentfield = HtmlEditorField :: create ( " BlogPost " , _t ( " BlogEntry.CN " ));
2012-05-21 14:58:26 +12:00
} else {
2014-08-22 16:36:42 +12:00
$contentfield = CompositeField :: create (
LiteralField :: create (
" BBCodeHelper " ,
" <a id= \" BBCodeHint \" target='new'> " . _t ( " BlogEntry.BBH " ) . " </a><div class='clear'><!-- --></div> "
),
TextareaField :: create (
" BlogPost " ,
_t ( " BlogEntry.CN " ),
20
), // This is called BlogPost as the id #Content is generally used already
LiteralField :: create (
" BBCodeTags " ,
" <div id= \" BBTagsHolder \" > " . $codeparser -> useable_tagsHTML () . " </div> "
)
2012-05-21 14:58:26 +12:00
);
}
2014-08-22 16:36:42 +12:00
// Support for https://github.com/chillu/silverstripe-tagfield
2012-05-21 14:58:26 +12:00
if ( class_exists ( 'TagField' )) {
2014-08-22 16:36:42 +12:00
$tagfield = TagField :: create ( 'Tags' , null , null , 'BlogEntry' );
2012-05-21 14:58:26 +12:00
$tagfield -> setSeparator ( ', ' );
} else {
2014-08-22 16:36:42 +12:00
$tagfield = TextField :: create ( 'Tags' );
2012-05-21 14:58:26 +12:00
}
$field = 'TextField' ;
if ( ! $this -> AllowCustomAuthors && ! Permission :: check ( 'ADMIN' )) {
$field = 'ReadonlyField' ;
}
2014-08-22 16:36:42 +12:00
$fields = FieldList :: create (
HiddenField :: create ( " ID " , " ID " ),
TextField :: create ( " Title " , _t ( 'BlogHolder.SJ' , " Subject " )),
$field :: create ( " Author " , _t ( 'BlogEntry.AU' ), $membername ),
2012-05-21 14:58:26 +12:00
$contentfield ,
$tagfield ,
2014-08-22 16:36:42 +12:00
LiteralField :: create (
" Tagsnote " ,
" <label id='tagsnote'> " . _t ( 'BlogHolder.TE' , " For example: sport, personal, science fiction " ) . " <br/> " .
_t ( 'BlogHolder.SPUC' , " Please separate tags using commas. " ) . " </label> "
)
2012-05-21 14:58:26 +12:00
);
2014-08-22 16:36:42 +12:00
$submitAction = FormAction :: create ( 'postblog' , _t ( 'BlogHolder.POST' , 'Post blog entry' ));
2012-05-21 15:58:40 +12:00
2014-08-22 16:36:42 +12:00
$actions = FieldList :: create ( $submitAction );
$validator = RequiredFields :: create ( 'Title' , 'BlogPost' );
2012-05-21 14:58:26 +12:00
2014-08-22 16:36:42 +12:00
$form = Form :: create ( $this , 'BlogEntryForm' , $fields , $actions , $validator );
2012-05-21 14:58:26 +12:00
2014-08-22 16:36:42 +12:00
$id = ( int ) $this -> request -> latestParam ( 'ID' );
if ( $id ) {
$entry = BlogEntry :: get () -> byID ( $id );
2012-05-21 14:58:26 +12:00
if ( $entry -> IsOwner ()) {
$form -> loadDataFrom ( $entry );
$form -> Fields () -> fieldByName ( 'BlogPost' ) -> setValue ( $entry -> Content );
}
} else {
$form -> loadDataFrom ( array ( " Author " => Cookie :: get ( " BlogHolder_Name " )));
}
return $form ;
}
2014-08-22 16:36:42 +12:00
public function postblog ( $data , $form ) {
2012-05-21 14:58:26 +12:00
if ( ! Permission :: check ( 'BLOGMANAGEMENT' )) return Security :: permissionFailure ();
Cookie :: set ( " BlogHolder_Name " , $data [ 'Author' ]);
$blogentry = false ;
2014-08-22 16:36:42 +12:00
if ( ! empty ( $data [ 'ID' ])) {
$candidate = BlogEntry :: get () -> byID ( $data [ 'ID' ]);
if ( $candidate -> IsOwner ()) $blogentry = $candidate ;
2012-05-21 14:58:26 +12:00
}
2014-08-22 16:36:42 +12:00
if ( ! $blogentry ) $blogentry = BlogEntry :: create ();
2012-05-21 14:58:26 +12:00
$form -> saveInto ( $blogentry );
$blogentry -> ParentID = $this -> ID ;
$blogentry -> Content = str_replace ( " \r \n " , " \n " , $form -> Fields () -> fieldByName ( 'BlogPost' ) -> dataValue ());
if ( Object :: has_extension ( $this -> ClassName , 'Translatable' )) {
$blogentry -> Locale = $this -> Locale ;
}
2014-08-22 16:36:42 +12:00
$oldMode = Versioned :: get_reading_mode ();
Versioned :: reading_stage ( 'Stage' );
$blogentry -> write ();
2012-05-21 14:58:26 +12:00
$blogentry -> publish ( " Stage " , " Live " );
2014-08-22 16:36:42 +12:00
Versioned :: set_reading_mode ( $oldMode );
2012-05-21 14:58:26 +12:00
2012-06-21 13:45:07 +12:00
$this -> redirect ( $this -> Link ());
2012-05-21 14:58:26 +12:00
}
}