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-12-18 02:41:51 +01:00
2007-11-14 02:15:40 +01:00
/**
* Blog holder to display summarised blog entries
*/
2007-12-18 02:41:51 +01:00
2007-09-07 00:33:58 +02:00
class BlogHolder extends Page {
2007-11-14 02:16:13 +01:00
static $icon = " blog/images/blogholder " ;
2007-09-07 00:33:58 +02:00
static $db = array (
);
static $has_one = array (
2007-12-15 23:47:20 +01:00
" SideBar " => " WidgetArea " ,
'Newsletter' => 'NewsletterType'
2007-09-07 00:33:58 +02:00
);
static $allowed_children = array (
'BlogEntry'
);
function getCMSFields () {
$fields = parent :: getCMSFields ();
$fields -> removeFieldFromTab ( " Root.Content.Main " , " Content " );
$fields -> addFieldToTab ( " Root.Content.Widgets " , new WidgetAreaEditor ( " SideBar " ));
2007-12-15 23:47:20 +01:00
// Add a dropdown to display all newsletter types.
if ( $groups = $this -> getNewsletters ()) {
$groupsMap = $groups -> toDropdownMap ( 'ID' , 'Title' );
$fields -> addFieldToTab ( 'Root.Content.Main' , new DropdownField ( 'NewsletterID' , 'Subscription newsletter type' , $groupsMap , '' , '' , '(Select one)' ));
}
2007-09-07 00:33:58 +02:00
return $fields ;
}
2007-12-15 23:47:20 +01:00
/**
* Get all newsletter type instances .
*/
function getNewsletters () {
return DataObject :: get ( 'NewsletterType' );
2007-12-18 02:41:51 +01:00
}
2007-12-15 23:47:20 +01:00
2007-11-14 02:15:40 +01:00
/**
* The DataObject of blog entries
*/
2007-09-07 00:33:58 +02:00
public function BlogEntries ( $limit = 10 ) {
$start = isset ( $_GET [ 'start' ]) ? ( int ) $_GET [ 'start' ] : 0 ;
$tagCheck = '' ;
$dateCheck = " " ;
if ( isset ( $_GET [ 'tag' ])) {
$tag = addslashes ( $_GET [ 'tag' ]);
$tag = str_replace ( array ( " \\ " , '_' , '%' , " ' " ), array ( " \\ \\ " , " \\ _ " , " \\ % " , " \\ ' " ), $tag );
$tagCheck = " AND `BlogEntry`.Tags LIKE '% $tag %' " ;
}
if ( Director :: urlParams ()){
2007-11-23 09:35:49 +01:00
if ( Director :: urlParam ( 'Action' ) == 'tag' ) {
$tag = addslashes ( Director :: urlParam ( 'ID' ));
$tag = str_replace ( array ( " \\ " , '_' , '%' , " ' " ), array ( " \\ \\ " , " \\ _ " , " \\ % " , " \\ ' " ), $tag );
$tagCheck = " AND `BlogEntry`.Tags LIKE '% $tag %' " ;
} else {
$year = Director :: urlParam ( 'Action' );
$month = Director :: urlParam ( 'ID' );
2007-12-18 02:41:51 +01:00
2008-01-14 04:34:47 +01:00
if ( is_numeric ( $month ) && is_numeric ( $month )){
$nextyear = ( $month == 12 ) ? $year + 1 : $year ;
$nextmonth = $month % 12 + 1 ;
$dateCheck = " AND `BlogEntry`.Date BETWEEN ' $year - $month -1' AND ' $nextyear - $nextmonth -1' " ;
2007-12-18 02:41:51 +01:00
} else if ( isset ( $year )){
2008-01-14 04:34:47 +01:00
$nextyear = $year + 1 ;
$dateCheck = " AND `BlogEntry`.Date BETWEEN ' $year -1-1' AND ' " . $nextyear . " -1-1' " ;
2007-11-23 09:35:49 +01:00
}
2007-09-07 00:33:58 +02:00
}
}
return DataObject :: get ( " Page " , " `ParentID` = $this->ID AND ShowInMenus = 1 $tagCheck $dateCheck " , " `BlogEntry`.Date DESC " , '' , " $start , $limit " );
}
2007-11-14 02:15:40 +01:00
/**
* Only display the blog entries that have the specified tag
*/
2007-09-07 00:33:58 +02:00
function Tag () {
2007-12-18 02:41:51 +01:00
if ( Director :: urlParam ( 'Action' ) == 'tag' ) {
return Director :: urlParam ( 'ID' );
}
2007-09-07 00:33:58 +02:00
return isset ( $_GET [ 'tag' ]) ? $_GET [ 'tag' ] : false ;
}
2007-12-20 03:23:20 +01:00
/**
* Return a new instance of NewsletterSignupForm .
2007-12-20 04:31:38 +01:00
* If there is no related Newsletter , then don ' t show it .
2007-12-20 03:23:20 +01:00
*/
function NewsletterSignupForm () {
2007-12-20 04:31:38 +01:00
if ( $this -> Newsletter () && $this -> Newsletter () -> ID ) {
return new NewsletterSignupForm ( $this , 'NewsletterSignupForm' );
}
2007-12-20 03:23:20 +01:00
}
2007-11-14 02:15:40 +01:00
/**
* A simple form for creating blog entries
*/
2007-09-07 00:33:58 +02:00
function BlogEntryForm (){
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 = Director :: urlParam ( 'ID' );
}
$codeparser = new BBCodeParser ();
$membername = Member :: currentMember () ? Member :: currentMember () -> getName () : " " ;
$fields = new FieldSet (
new HiddenField ( " ParentID " , " ParentID " , $this -> ID ),
new HiddenField ( " ID " , " ID " ),
new HiddenField ( " Date " , " Date " ),
2007-12-18 02:41:51 +01:00
new TextField ( " Title " , _t ( 'BlogHolder.SJ' , " Subject " )),
new TextField ( " Author " , _t ( 'BlogEntry.AU' ), $membername ),
2007-09-07 00:33:58 +02:00
new CompositeField (
2007-12-18 02:41:51 +01:00
new LiteralField ( " BBCodeHelper " , " <a id= \" BBCodeHint \" target='new'> " . _t ( " BlogEntry.BBH " ) . " </a><div class='clear'><!-- --></div> " ),
new TextareaField ( " Content " , _t ( " BlogEntry.CN " ), 20 ),
2007-09-07 00:33:58 +02:00
new LiteralField ( " BBCodeTags " , " <div id='BBTagsHolder' style='display:none;'> " . $codeparser -> useable_tagsHTML () . " </div> " )
),
new TextField ( " Tags " , " Tags " ),
2007-12-18 02:41:51 +01:00
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> " )
2007-09-07 00:33:58 +02:00
);
2007-12-18 02:41:51 +01:00
$submitAction = new FormAction ( 'postblog' , _t ( 'BlogHolder.POST' , 'Post blog entry' ));
2007-09-07 00:33:58 +02:00
$actions = new FieldSet ( $submitAction );
$validator = new RequiredFields ( 'Title' , 'Content' );
$form = new BlogEntry_Form ( $this , 'BlogEntryForm' , $fields , $actions , $validator );
if ( $id != 0 ){
$form -> loadNonBlankDataFrom ( DataObject :: get_by_id ( 'BlogEntry' , $id ));
} else {
$form -> loadNonBlankDataFrom ( array ( " Author " => Cookie :: get ( " BlogHolder_Name " )));
}
return $form ;
}
2007-12-18 02:41:51 +01:00
2007-11-14 02:15:40 +01:00
/**
* Check if url has " /post "
*/
2007-09-07 00:33:58 +02:00
function isPost (){
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-12-18 02:41:51 +01:00
2007-11-14 02:15:40 +01:00
/**
* Create default blog setup
*/
2007-09-07 00:33:58 +02:00
function requireDefaultRecords () {
parent :: requireDefaultRecords ();
if ( ! DataObject :: get_one ( 'BlogHolder' )) {
$blogholder = new BlogHolder ();
$blogholder -> Title = " Blog " ;
$blogholder -> URLSegment = " blog " ;
$blogholder -> Status = " Published " ;
$widgetarea = new WidgetArea ();
$widgetarea -> write ();
$blogholder -> SideBarID = $widgetarea -> ID ;
$blogholder -> write ();
$blogholder -> publish ( " Stage " , " Live " );
$managementwidget = new BlogManagementWidget ();
$managementwidget -> ParentID = $widgetarea -> ID ;
$managementwidget -> write ();
$tagcloudwidget = new TagCloudWidget ();
$tagcloudwidget -> ParentID = $widgetarea -> ID ;
$tagcloudwidget -> write ();
$archivewidget = new ArchiveWidget ();
$archivewidget -> ParentID = $widgetarea -> ID ;
$archivewidget -> write ();
$widgetarea -> write ();
$blog = new BlogEntry ();
2007-12-18 02:41:51 +01:00
$blog -> Title = _t ( 'BlogHolder.SUCTITLE' , " SilverStripe blog module successfully installed " );
2007-09-07 00:33:58 +02:00
$blog -> URLSegment = 'sample-blog-entry' ;
$blog -> setDate ( date ( " Y-m-d H:i:s " , time ()));
2007-12-18 02:41:51 +01:00
$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 " );
Database :: alteration_message ( " Blog page created " , " created " );
}
}
}
class BlogHolder_Controller extends Page_Controller {
function init () {
2007-12-18 02:41:51 +01:00
parent :: init ();
2007-09-07 00:33:58 +02:00
// This will create a <link> tag point to the RSS feed
2007-12-18 02:41:51 +01:00
RSSFeed :: linkToFeed ( $this -> Link () . " rss " , _t ( 'BlogHolder.RSSFEED' , " RSS feed of this blog " ));
2007-09-07 00:33:58 +02:00
Requirements :: themedCSS ( " blog " );
Requirements :: themedCSS ( " bbcodehelp " );
}
2007-12-18 02:41:51 +01:00
2007-11-14 02:15:40 +01:00
/**
2007-12-18 02:41:51 +01:00
* Gets the archived blogs for a particular month or year , in the format / year / month / eg : / 2008 / 10 /
2007-11-14 02:15:40 +01:00
*/
2007-09-07 00:33:58 +02:00
function showarchive () {
$month = addslashes ( $this -> urlParams [ 'ID' ]);
return array (
" Children " => DataObject :: get ( 'SiteTree' , " ParentID = $this->ID AND DATE_FORMAT(`BlogEntry`.`Date`, '%Y-%M') = ' $month ' " ),
);
}
function ArchiveMonths () {
$months = DB :: query ( " SELECT DISTINCT DATE_FORMAT(`BlogEntry`.`Date`, '%M') AS `Month`, DATE_FORMAT(`BlogEntry`.`Date`, '%Y') AS `Year` FROM `BlogEntry` ORDER BY `BlogEntry`.`Date` DESC " );
$output = new DataObjectSet ();
foreach ( $months as $month ) {
$month [ 'Link' ] = $this -> Link () . " showarchive/ $month[Year] - $month[Month] " ;
$output -> push ( new ArrayData ( $month ));
}
2008-01-07 02:49:29 +01:00
2007-09-07 00:33:58 +02:00
return $output ;
}
2007-11-14 02:15:40 +01:00
/**
2007-12-18 02:41:51 +01:00
* Get the rss feed for this blog holder ' s entries
2007-11-14 02:15:40 +01:00
*/
2007-09-07 00:33:58 +02:00
function rss () {
global $project ;
2008-01-13 21:16:27 +01:00
$children = $this -> Children ();
$children -> sort ( 'Date' , 'DESC' );
$rss = new RSSFeed ( $children , $this -> Link (), $project . " blog " , " " , " Title " , " ParsedContent " );
2007-09-07 00:33:58 +02:00
$rss -> outputToBrowser ();
}
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 ();
}
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 ( 'ADMIN' )){
Security :: permissionFailure ( $this ,
2007-12-18 02:41:51 +01:00
_t ( 'BlogHolder.HAVENTPERM' , " Posting blogs is an administrator task. Please log in. " ));
2007-09-07 00:33:58 +02:00
}
return array ();
}
2007-11-01 22:56:44 +01:00
function defaultAction ( $action ) {
// Protection against infinite loops when an RSS widget pointing to this page is added to this page
if ( stristr ( $_SERVER [ 'HTTP_USER_AGENT' ], 'SimplePie' )) {
return $this -> rss ();
}
return parent :: defaultAction ( $action );
}
2007-09-07 00:33:58 +02:00
}
2007-11-14 02:15:40 +01:00
/**
* Blog entry form
*/
2007-09-07 00:33:58 +02:00
class BlogEntry_Form extends Form {
function postblog ( $data ) {
Cookie :: set ( " BlogHolder_Name " , $data [ 'Author' ]);
$blogentry = new BlogEntry ();
$this -> saveInto ( $blogentry );
if ( $data [ 'ID' ] != 0 ){ //new post
$blogentry = DataObject :: get_by_id ( " BlogEntry " , $data [ 'ID' ]);
$this -> saveInto ( $blogentry );
$blogentry -> setDate ( $data [ 'Date' ]);
} else {
$blogentry -> setDate ( date ( " Y-m-d H:i:s " , time ()));
}
$blogentry -> Status = " Published " ;
$blogentry -> writeToStage ( " Stage " );
$blogentry -> publish ( " Stage " , " Live " );
Director :: redirect ( Director :: currentURLSegment ());
}
}
2007-11-01 22:56:44 +01:00
?>