2009-05-01 00:05:54 +02:00
< ? php
/**
* @ package blog
*/
/**
* Blog tree allows a tree of Blog Holders . Viewing branch nodes shows all blog entries from all blog holder children
*/
class BlogTree extends Page {
2009-07-20 08:40:44 +02:00
// Default number of blog entries to show
static $default_entries_limit = 10 ;
2009-05-01 00:05:54 +02:00
static $db = array (
2009-05-20 04:18:48 +02:00
'Name' => 'Varchar' ,
2009-05-01 00:05:54 +02:00
'InheritSideBar' => 'Boolean' ,
'LandingPageFreshness' => 'Varchar' ,
);
static $defaults = array (
'InheritSideBar' => True
);
static $has_one = array (
" SideBar " => " WidgetArea " ,
);
static $allowed_children = array (
'BlogTree' , 'BlogHolder'
);
/*
* Finds the BlogTree object most related to the current page .
* - If this page is a BlogTree , use that
* - If this page is a BlogEntry , use the parent Holder
* - Otherwise , try and find a 'top-level' BlogTree
2010-03-19 01:15:41 +01:00
*
* @ param $page allows you to force a specific page , otherwise ,
* uses current
2009-05-01 00:05:54 +02:00
*/
2010-03-19 01:15:41 +01:00
static function current ( $page = null ) {
2009-11-18 04:00:40 +01:00
2010-03-19 01:15:41 +01:00
if ( ! $page ) {
$controller = Controller :: curr ();
if ( $controller ) $page = $controller -> data ();
}
2009-05-01 00:05:54 +02:00
// If we _are_ a BlogTree, use us
if ( $page instanceof BlogTree ) return $page ;
// Or, if we a a BlogEntry underneath a BlogTree, use our parent
2009-11-30 09:18:56 +01:00
if ( $page -> is_a ( " BlogEntry " )) {
2009-05-01 00:05:54 +02:00
$parent = $page -> getParent ();
2009-11-30 09:18:56 +01:00
if ( $parent instanceof BlogTree ) return $parent ;
2009-05-01 00:05:54 +02:00
}
// Try to find a top-level BlogTree
2009-11-30 09:18:56 +01:00
$top = DataObject :: get_one ( 'BlogTree' , " \" ParentID \" = '0' " );
if ( $top ) return $top ;
2009-05-01 00:05:54 +02:00
// Try to find any BlogTree that is not inside another BlogTree
foreach ( DataObject :: get ( 'BlogTree' ) as $tree ) {
2009-11-30 09:18:56 +01:00
if ( ! ( $tree -> getParent () instanceof BlogTree )) return $tree ;
2009-05-01 00:05:54 +02:00
}
// This shouldn't be possible, but assuming the above fails, just return anything you can get
return DataObject :: get_one ( 'BlogTree' );
}
/* ----------- ACCESSOR OVERRIDES -------------- */
public function getLandingPageFreshness () {
$freshness = $this -> getField ( 'LandingPageFreshness' );
// If we want to inherit freshness, try that first
2009-07-19 23:24:45 +02:00
if ( $freshness == " INHERIT " && $this -> getParent ()) $freshness = $this -> getParent () -> LandingPageFreshness ;
2009-05-01 00:05:54 +02:00
// If we don't have a parent, or the inherited result was still inherit, use default
2009-07-19 23:24:45 +02:00
if ( $freshness == " INHERIT " ) $freshness = '' ;
2009-05-01 00:05:54 +02:00
return $freshness ;
}
function SideBar () {
2009-11-30 09:18:56 +01:00
if ( $this -> InheritSideBar && $this -> getParent ()) {
2009-05-26 00:30:10 +02:00
if ( method_exists ( $this -> getParent (), 'SideBar' )) return $this -> getParent () -> SideBar ();
}
2009-08-05 04:38:32 +02:00
if ( $this -> SideBarID ){
return DataObject :: get_by_id ( 'WidgetArea' , $this -> SideBarID );
// @todo: This segfaults - investigate why then fix: return $this->getComponent('SideBar');
}
2009-05-01 00:05:54 +02:00
}
/* ----------- CMS CONTROL -------------- */
function getCMSFields () {
$fields = parent :: getCMSFields ();
2009-05-20 04:18:48 +02:00
$fields -> addFieldToTab ( " Root.Content.Main " , new TextField ( " Name " , " Name of blog " ));
2009-05-01 00:05:54 +02:00
$fields -> addFieldToTab ( 'Root.Content.Main' , new DropdownField ( 'LandingPageFreshness' , 'When you first open the blog, how many entries should I show' , array (
" " => " All entries " ,
" 1 MONTH " => " Last month's entries " ,
" 2 MONTH " => " Last 2 months' entries " ,
" 3 MONTH " => " Last 3 months' entries " ,
" 4 MONTH " => " Last 4 months' entries " ,
" 5 MONTH " => " Last 5 months' entries " ,
" 6 MONTH " => " Last 6 months' entries " ,
" 7 MONTH " => " Last 7 months' entries " ,
" 8 MONTH " => " Last 8 months' entries " ,
" 9 MONTH " => " Last 9 months' entries " ,
" 10 MONTH " => " Last 10 months' entries " ,
" 11 MONTH " => " Last 11 months' entries " ,
" 12 MONTH " => " Last year's entries " ,
" INHERIT " => " Take value from parent Blog Tree "
)));
$fields -> addFieldToTab ( " Root.Content.Widgets " , new CheckboxField ( " InheritSideBar " , 'Inherit Sidebar From Parent' ));
$fields -> addFieldToTab ( " Root.Content.Widgets " , new WidgetAreaEditor ( " SideBar " ));
return $fields ;
}
/* ----------- New accessors -------------- */
public function loadDescendantBlogHolderIDListInto ( & $idList ) {
if ( $children = $this -> AllChildren ()) {
foreach ( $children as $child ) {
2009-11-30 09:18:56 +01:00
if ( in_array ( $child -> ID , $idList )) continue ;
2009-05-01 00:05:54 +02:00
2009-11-30 09:18:56 +01:00
if ( $child instanceof BlogHolder ) {
2009-08-04 01:37:03 +02:00
$idList [] = $child -> ID ;
2009-11-30 09:18:56 +01:00
} elseif ( $child instanceof BlogTree ) {
2009-08-04 01:37:03 +02:00
$child -> loadDescendantBlogHolderIDListInto ( $idList );
}
2009-05-01 00:05:54 +02:00
}
}
}
// Build a list of all IDs for BlogHolders that are children of us
public function BlogHolderIDs () {
$holderIDs = array ();
$this -> loadDescendantBlogHolderIDListInto ( $holderIDs );
return $holderIDs ;
}
/**
* Get entries in this blog .
* @ param string limit A clause to insert into the limit clause .
* @ param string tag Only get blog entries with this tag
* @ param string date Only get blog entries on this date - either a year , or a year - month eg '2008' or '2008-02'
* @ param callback retrieveCallback A function to call with pagetype , filter and limit for custom blog sorting or filtering
2009-12-22 21:55:23 +01:00
* @ param string $where
2009-05-01 00:05:54 +02:00
* @ return DataObjectSet
*/
2009-12-22 21:55:23 +01:00
public function Entries ( $limit = '' , $tag = '' , $date = '' , $retrieveCallback = null , $filter = '' ) {
2010-05-28 01:30:14 +02:00
2009-05-01 00:05:54 +02:00
$tagCheck = '' ;
$dateCheck = '' ;
2009-11-30 09:18:56 +01:00
if ( $tag ) {
2009-05-01 00:05:54 +02:00
$SQL_tag = Convert :: raw2sql ( $tag );
2009-11-30 09:18:56 +01:00
$tagCheck = " AND \" BlogEntry \" . \" Tags \" LIKE '% $SQL_tag %' " ;
2009-05-01 00:05:54 +02:00
}
2009-12-22 21:55:23 +01:00
2009-11-30 09:18:56 +01:00
if ( $date ) {
2009-05-01 00:05:54 +02:00
if ( strpos ( $date , '-' )) {
$year = ( int ) substr ( $date , 0 , strpos ( $date , '-' ));
$month = ( int ) substr ( $date , strpos ( $date , '-' ) + 1 );
2010-02-01 06:27:18 +01:00
2009-05-01 00:05:54 +02:00
if ( $year && $month ) {
2010-02-01 06:27:18 +01:00
if ( method_exists ( DB :: getConn (), 'formattedDatetimeClause' )) {
2010-04-30 00:39:40 +02:00
$db_date = DB :: getConn () -> formattedDatetimeClause ( '"BlogEntry"."Date"' , '%m' );
$dateCheck = " AND CAST( $db_date AS " . DB :: getConn () -> dbDataType ( 'unsigned integer' ) . " ) = $month AND " . DB :: getConn () -> formattedDatetimeClause ( '"BlogEntry"."Date"' , '%Y' ) . " = ' $year ' " ;
2010-02-01 06:27:18 +01:00
} else {
$dateCheck = " AND MONTH( \" BlogEntry \" . \" Date \" ) = ' $month ' AND YEAR( \" BlogEntry \" . \" Date \" ) = ' $year ' " ;
}
2009-05-01 00:05:54 +02:00
}
} else {
$year = ( int ) $date ;
if ( $year ) {
2010-02-01 06:27:18 +01:00
if ( method_exists ( DB :: getConn (), 'formattedDatetimeClause' )) {
$dateCheck = " AND " . DB :: getConn () -> formattedDatetimeClause ( '"BlogEntry"."Date"' , '%Y' ) . " = ' $year ' " ;
} else {
$dateCheck = " AND YEAR( \" BlogEntry \" . \" Date \" ) = ' $year ' " ;
}
2009-05-01 00:05:54 +02:00
}
}
}
2009-12-22 21:55:23 +01:00
2009-05-01 00:05:54 +02:00
// Build a list of all IDs for BlogHolders that are children of us
$holderIDs = $this -> BlogHolderIDs ();
// If no BlogHolders, no BlogEntries. So return false
2009-11-30 09:18:56 +01:00
if ( empty ( $holderIDs )) return false ;
2009-05-01 00:05:54 +02:00
// Otherwise, do the actual query
2009-12-22 21:55:23 +01:00
if ( $filter ) $filter .= ' AND ' ;
$filter .= '"ParentID" IN (' . implode ( ',' , $holderIDs ) . " ) $tagCheck $dateCheck " ;
2009-05-11 04:48:24 +02:00
2009-11-30 09:18:56 +01:00
$order = '"BlogEntry"."Date" DESC' ;
2009-05-01 00:05:54 +02:00
// By specifying a callback, you can alter the SQL, or sort on something other than date.
2009-12-22 21:55:23 +01:00
if ( $retrieveCallback ) return call_user_func ( $retrieveCallback , 'BlogEntry' , $filter , $limit , $order );
2010-05-28 01:30:14 +02:00
2010-05-18 05:21:08 +02:00
return DataObject :: get ( 'BlogEntry' , $filter , $order , '' , $limit );
2009-05-01 00:05:54 +02:00
}
}
class BlogTree_Controller extends Page_Controller {
2010-05-16 04:38:19 +02:00
2009-07-08 01:04:02 +02:00
static $allowed_actions = array (
2009-09-10 08:08:22 +02:00
'index' ,
2009-07-08 01:15:12 +02:00
'rss' ,
2010-07-02 01:30:13 +02:00
'tag' ,
'date'
2009-07-08 01:04:02 +02:00
);
2009-05-01 00:05:54 +02:00
function init () {
parent :: init ();
2009-10-21 01:17:35 +02:00
$this -> IncludeBlogRSS ();
2009-05-01 00:05:54 +02:00
Requirements :: themedCSS ( " blog " );
}
2009-07-20 08:40:44 +02:00
function BlogEntries ( $limit = null ) {
2010-05-28 05:46:17 +02:00
require_once ( 'Zend/Date.php' );
2009-11-30 09:18:56 +01:00
if ( $limit === null ) $limit = BlogTree :: $default_entries_limit ;
2009-12-22 21:55:23 +01:00
// only use freshness if no action is present (might be displaying tags or rss)
if ( $this -> LandingPageFreshness && ! $this -> request -> param ( 'Action' )) {
2010-05-28 05:46:17 +02:00
$d = new Zend_Date ( SS_Datetime :: now () -> getValue ());
$d -> sub ( $this -> LandingPageFreshness );
$date = $d -> toString ( 'YYYY-MM-dd' );
$filter = " \" BlogEntry \" . \" Date \" > ' $date ' " ;
2009-12-22 21:55:23 +01:00
} else {
$filter = '' ;
}
2010-05-18 05:21:08 +02:00
// allow filtering by author field and some blogs have an authorID field which
// may allow filtering by id
if ( isset ( $_GET [ 'author' ]) && isset ( $_GET [ 'authorID' ])) {
$author = Convert :: raw2sql ( $_GET [ 'author' ]);
$id = Convert :: raw2sql ( $_GET [ 'authorID' ]);
$filter .= " \" BlogEntry \" . \" Author \" LIKE ' " . $author . " ' OR \" BlogEntry \" . \" AuthorID \" = ' " . $id . " ' " ;
}
else if ( isset ( $_GET [ 'author' ])) {
$filter .= " \" BlogEntry \" . \" Author \" LIKE ' " . Convert :: raw2sql ( $_GET [ 'author' ]) . " ' " ;
}
else if ( isset ( $_GET [ 'authorID' ])) {
$filter .= " \" BlogEntry \" . \" AuthorID \" = ' " . Convert :: raw2sql ( $_GET [ 'authorID' ]) . " ' " ;
}
2009-05-01 00:05:54 +02:00
$start = isset ( $_GET [ 'start' ]) ? ( int ) $_GET [ 'start' ] : 0 ;
2010-05-16 04:38:19 +02:00
$date = $this -> SelectedDate ();
2010-05-28 04:33:58 +02:00
2010-07-02 01:30:13 +02:00
return $this -> Entries ( " $start , $limit " , $this -> SelectedTag (), ( $date ) ? $date : '' , null , $filter );
2009-05-01 00:05:54 +02:00
}
2010-05-16 04:38:19 +02:00
/**
* This will create a < link > tag point to the RSS feed
*/
2009-10-21 01:17:35 +02:00
function IncludeBlogRSS () {
RSSFeed :: linkToFeed ( $this -> Link () . " rss " , _t ( 'BlogHolder.RSSFEED' , " RSS feed of these blogs " ));
}
2009-05-01 00:05:54 +02:00
/**
* Get the rss feed for this blog holder ' s entries
*/
function rss () {
2009-05-20 04:34:57 +02:00
global $project_name ;
2009-05-01 00:05:54 +02:00
$blogName = $this -> Name ;
2009-05-20 04:34:57 +02:00
$altBlogName = $project_name . ' blog' ;
2009-05-01 00:05:54 +02:00
$entries = $this -> Entries ( 20 );
if ( $entries ) {
2010-06-18 01:19:56 +02:00
$rss = new RSSFeed ( $entries , $this -> Link (), ( $blogName ? $blogName : $altBlogName ), " " , " Title " , " RSSContent " );
2009-05-01 00:05:54 +02:00
$rss -> outputToBrowser ();
}
}
2010-05-16 04:38:19 +02:00
/**
* Protection against infinite loops when an RSS widget pointing to this page is added to this page
*/
2009-05-01 00:05:54 +02:00
function defaultAction ( $action ) {
if ( stristr ( $_SERVER [ 'HTTP_USER_AGENT' ], 'SimplePie' )) return $this -> rss ();
return parent :: defaultAction ( $action );
2010-05-16 04:38:19 +02:00
}
/**
* Return the currently viewing tag used in the template as $Tag
*
* @ return String
*/
function SelectedTag () {
2010-10-03 22:02:43 +02:00
return ( $this -> request -> latestParam ( 'Action' ) == 'tag' ) ? Convert :: raw2xml ( $this -> request -> latestParam ( 'ID' )) : '' ;
2010-05-16 04:38:19 +02:00
}
/**
* Return the selected date from the blog tree
*
* @ return Date
*/
function SelectedDate () {
2010-05-28 01:30:14 +02:00
if ( $this -> request -> latestParam ( 'Action' ) == 'date' ) {
$year = $this -> request -> latestParam ( 'ID' );
$month = $this -> request -> latestParam ( 'OtherID' );
2010-05-16 04:38:19 +02:00
if ( is_numeric ( $year ) && is_numeric ( $month ) && $month < 13 ) {
2010-07-02 01:30:13 +02:00
$date = $year . '-' . $month ;
2010-05-16 04:38:19 +02:00
return $date ;
2010-07-02 01:30:13 +02:00
} else {
if ( is_numeric ( $year )) return $year ;
2010-05-16 04:38:19 +02:00
}
}
return false ;
}
2010-07-02 01:30:13 +02:00
function SelectedNiceDate (){
$date = $this -> SelectedDate ();
if ( strpos ( $date , '-' )) {
$date = explode ( " - " , $date );
return date ( " F " , mktime ( 0 , 0 , 0 , $date [ 1 ], 1 , date ( 'Y' ))) . " " . date ( " Y " , mktime ( 0 , 0 , 0 , date ( 'm' ), 1 , $date [ 0 ]));
} else {
return date ( " Y " , mktime ( 0 , 0 , 0 , date ( 'm' ), 1 , $date ));
}
}
2010-05-16 04:38:19 +02:00
}