2012-05-21 04:58:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package blog
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-08-17 04:08:43 +02:00
|
|
|
* Blog tree is a way to group Blogs. It allows a tree of "Blog Holders".
|
|
|
|
* Viewing branch nodes shows all blog entries from all blog holder children
|
2012-05-21 04:58:26 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
class BlogTree extends Page {
|
2012-08-17 04:08:43 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $icon = "blog/images/blogtree-file.png";
|
2012-09-21 17:39:31 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $description = "A grouping of blogs";
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $singular_name = 'Blog Tree Page';
|
2012-10-09 02:14:31 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $plural_name = 'Blog Tree Pages';
|
2012-10-09 02:14:31 +02:00
|
|
|
|
2012-05-21 04:58:26 +02:00
|
|
|
// Default number of blog entries to show
|
|
|
|
static $default_entries_limit = 10;
|
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $db = array(
|
2012-11-13 20:50:25 +01:00
|
|
|
'Name' => 'Varchar(255)',
|
2012-05-21 04:58:26 +02:00
|
|
|
'InheritSideBar' => 'Boolean',
|
|
|
|
'LandingPageFreshness' => 'Varchar',
|
|
|
|
);
|
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $defaults = array(
|
2012-05-21 04:58:26 +02:00
|
|
|
'InheritSideBar' => True
|
|
|
|
);
|
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $has_one = array();
|
2012-05-28 08:12:53 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $has_many = array();
|
2012-05-21 04:58:26 +02:00
|
|
|
|
2013-04-04 04:30:33 +02:00
|
|
|
private static $allowed_children = array(
|
2012-05-21 04:58:26 +02:00
|
|
|
'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
|
|
|
|
*
|
|
|
|
* @param $page allows you to force a specific page, otherwise,
|
|
|
|
* uses current
|
|
|
|
*/
|
|
|
|
static function current($page = null) {
|
|
|
|
|
|
|
|
if (!$page) {
|
|
|
|
$controller = Controller::curr();
|
|
|
|
if($controller) $page = $controller->data();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if($page->is_a("BlogEntry")) {
|
|
|
|
$parent = $page->getParent();
|
|
|
|
if($parent instanceof BlogTree) return $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a top-level BlogTree
|
|
|
|
$top = DataObject::get_one('BlogTree', "\"ParentID\" = '0'");
|
|
|
|
if($top) return $top;
|
|
|
|
|
|
|
|
// Try to find any BlogTree that is not inside another BlogTree
|
2012-07-30 23:46:58 +02:00
|
|
|
if($blogTrees=DataObject::get('BlogTree')) foreach($blogTrees as $tree) {
|
2012-05-21 04:58:26 +02:00
|
|
|
if(!($tree->getParent() instanceof BlogTree)) return $tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This shouldn't be possible, but assuming the above fails, just return anything you can get
|
2012-07-31 00:27:45 +02:00
|
|
|
return $blogTrees->first();
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------- ACCESSOR OVERRIDES -------------- */
|
|
|
|
|
|
|
|
public function getLandingPageFreshness() {
|
|
|
|
$freshness = $this->getField('LandingPageFreshness');
|
|
|
|
// If we want to inherit freshness, try that first
|
|
|
|
if ($freshness == "INHERIT" && $this->getParent()) $freshness = $this->getParent()->LandingPageFreshness;
|
|
|
|
// If we don't have a parent, or the inherited result was still inherit, use default
|
|
|
|
if ($freshness == "INHERIT") $freshness = '';
|
|
|
|
return $freshness;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------- CMS CONTROL -------------- */
|
|
|
|
|
2013-04-11 00:42:23 +02:00
|
|
|
function getSettingsFields() {
|
|
|
|
$fields = parent::getSettingsFields();
|
|
|
|
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Settings',
|
|
|
|
new DropdownField(
|
|
|
|
'LandingPageFreshness',
|
|
|
|
'When you first open the blog, how many entries should I show',
|
|
|
|
array(
|
|
|
|
"" => "All entries",
|
|
|
|
"1" => "Last month's entries",
|
|
|
|
"2" => "Last 2 months' entries",
|
|
|
|
"3" => "Last 3 months' entries",
|
|
|
|
"4" => "Last 4 months' entries",
|
|
|
|
"5" => "Last 5 months' entries",
|
|
|
|
"6" => "Last 6 months' entries",
|
|
|
|
"7" => "Last 7 months' entries",
|
|
|
|
"8" => "Last 8 months' entries",
|
|
|
|
"9" => "Last 9 months' entries",
|
|
|
|
"10" => "Last 10 months' entries",
|
|
|
|
"11" => "Last 11 months' entries",
|
|
|
|
"12" => "Last year's entries",
|
|
|
|
"INHERIT" => "Take value from parent Blog Tree"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2012-05-21 05:58:40 +02:00
|
|
|
|
2012-05-21 04:58:26 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------- New accessors -------------- */
|
|
|
|
|
|
|
|
public function loadDescendantBlogHolderIDListInto(&$idList) {
|
|
|
|
if ($children = $this->AllChildren()) {
|
|
|
|
foreach($children as $child) {
|
|
|
|
if(in_array($child->ID, $idList)) continue;
|
|
|
|
|
|
|
|
if($child instanceof BlogHolder) {
|
|
|
|
$idList[] = $child->ID;
|
|
|
|
} elseif($child instanceof BlogTree) {
|
|
|
|
$child->loadDescendantBlogHolderIDListInto($idList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
* @param string $where
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
public function Entries($limit = '', $tag = '', $date = '', $retrieveCallback = null, $filter = '') {
|
|
|
|
|
|
|
|
$tagCheck = '';
|
|
|
|
$dateCheck = '';
|
|
|
|
|
|
|
|
if($tag) {
|
|
|
|
$SQL_tag = Convert::raw2sql($tag);
|
|
|
|
$tagCheck = "AND \"BlogEntry\".\"Tags\" LIKE '%$SQL_tag%'";
|
|
|
|
}
|
|
|
|
|
|
|
|
if($date) {
|
|
|
|
// Some systems still use the / seperator for date presentation
|
|
|
|
if( strpos($date, '-') ) $seperator = '-';
|
|
|
|
elseif( strpos($date, '/') ) $seperator = '/';
|
|
|
|
|
|
|
|
if(isset($seperator) && !empty($seperator)) {
|
|
|
|
// The 2 in the explode argument will tell it to only create 2 elements
|
|
|
|
// i.e. in this instance the $year and $month fields respectively
|
|
|
|
list($year,$month) = explode( $seperator, $date, 2);
|
|
|
|
|
|
|
|
$year = (int)$year;
|
|
|
|
$month = (int)$month;
|
|
|
|
|
|
|
|
if($year && $month) {
|
|
|
|
if(method_exists(DB::getConn(), 'formattedDatetimeClause')) {
|
|
|
|
$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'";
|
|
|
|
} else {
|
|
|
|
$dateCheck = "AND MONTH(\"BlogEntry\".\"Date\") = '$month' AND YEAR(\"BlogEntry\".\"Date\") = '$year'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$year = (int) $date;
|
|
|
|
if($year) {
|
|
|
|
if(method_exists(DB::getConn(), 'formattedDatetimeClause')) {
|
|
|
|
$dateCheck = "AND " . DB::getConn()->formattedDatetimeClause('"BlogEntry"."Date"', '%Y') . " = '$year'";
|
|
|
|
} else {
|
|
|
|
$dateCheck = "AND YEAR(\"BlogEntry\".\"Date\") = '$year'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a list of all IDs for BlogHolders that are children of us
|
|
|
|
$holderIDs = $this->BlogHolderIDs();
|
|
|
|
|
|
|
|
// If no BlogHolders, no BlogEntries. So return false
|
|
|
|
if(empty($holderIDs)) return false;
|
|
|
|
|
|
|
|
// Otherwise, do the actual query
|
|
|
|
if($filter) $filter .= ' AND ';
|
2012-07-11 20:13:25 +02:00
|
|
|
$filter .= '"SiteTree"."ParentID" IN (' . implode(',', $holderIDs) . ") $tagCheck $dateCheck";
|
2012-05-21 04:58:26 +02:00
|
|
|
|
|
|
|
$order = '"BlogEntry"."Date" DESC';
|
|
|
|
|
|
|
|
// By specifying a callback, you can alter the SQL, or sort on something other than date.
|
|
|
|
if($retrieveCallback) return call_user_func($retrieveCallback, 'BlogEntry', $filter, $limit, $order);
|
2012-07-03 03:16:18 +02:00
|
|
|
|
2012-07-08 01:25:53 +02:00
|
|
|
$entries = BlogEntry::get()->where($filter)->sort($order);
|
2012-07-03 03:16:18 +02:00
|
|
|
|
2012-07-08 01:25:53 +02:00
|
|
|
$list = new PaginatedList($entries, Controller::curr()->request);
|
|
|
|
$list->setPageLength($limit);
|
|
|
|
return $list;
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BlogTree_Controller extends Page_Controller {
|
|
|
|
|
2013-04-04 04:24:53 +02:00
|
|
|
private static $allowed_actions = array(
|
2012-05-21 04:58:26 +02:00
|
|
|
'index',
|
|
|
|
'rss',
|
|
|
|
'tag',
|
|
|
|
'date'
|
|
|
|
);
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
parent::init();
|
|
|
|
|
|
|
|
$this->IncludeBlogRSS();
|
|
|
|
|
2012-07-09 02:05:53 +02:00
|
|
|
Requirements::themedCSS("blog","blog");
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function BlogEntries($limit = null) {
|
|
|
|
require_once('Zend/Date.php');
|
|
|
|
|
|
|
|
if($limit === null) $limit = BlogTree::$default_entries_limit;
|
|
|
|
|
|
|
|
// only use freshness if no action is present (might be displaying tags or rss)
|
|
|
|
if ($this->LandingPageFreshness && !$this->request->param('Action')) {
|
|
|
|
$d = new Zend_Date(SS_Datetime::now()->getValue());
|
2012-07-14 15:19:09 +02:00
|
|
|
$d->sub($this->LandingPageFreshness, Zend_Date::MONTH);
|
2012-05-21 04:58:26 +02:00
|
|
|
$date = $d->toString('YYYY-MM-dd');
|
|
|
|
|
|
|
|
$filter = "\"BlogEntry\".\"Date\" > '$date'";
|
|
|
|
} else {
|
|
|
|
$filter = '';
|
|
|
|
}
|
|
|
|
// 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']). "'";
|
|
|
|
}
|
2012-07-03 10:16:23 +02:00
|
|
|
|
2012-05-21 04:58:26 +02:00
|
|
|
$date = $this->SelectedDate();
|
|
|
|
|
2012-07-03 10:16:23 +02:00
|
|
|
return $this->Entries($limit, $this->SelectedTag(), ($date) ? $date : '', null, $filter);
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will create a <link> tag point to the RSS feed
|
|
|
|
*/
|
|
|
|
function IncludeBlogRSS() {
|
|
|
|
RSSFeed::linkToFeed($this->Link('rss'), _t('BlogHolder.RSSFEED',"RSS feed of these blogs"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the rss feed for this blog holder's entries
|
|
|
|
*/
|
|
|
|
function rss() {
|
|
|
|
global $project_name;
|
|
|
|
|
2013-04-11 00:41:36 +02:00
|
|
|
$blogName = $this->Title;
|
2012-05-21 04:58:26 +02:00
|
|
|
$altBlogName = $project_name . ' blog';
|
|
|
|
|
|
|
|
$entries = $this->Entries(20);
|
|
|
|
|
|
|
|
if($entries) {
|
|
|
|
$rss = new RSSFeed($entries, $this->Link('rss'), ($blogName ? $blogName : $altBlogName), "", "Title", "RSSContent");
|
2012-10-28 04:21:46 +01:00
|
|
|
return $rss->outputToBrowser();
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Protection against infinite loops when an RSS widget pointing to this page is added to this page
|
|
|
|
*/
|
|
|
|
function defaultAction($action) {
|
|
|
|
if(stristr($_SERVER['HTTP_USER_AGENT'], 'SimplePie')) return $this->rss();
|
|
|
|
|
|
|
|
return parent::defaultAction($action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the currently viewing tag used in the template as $Tag
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function SelectedTag() {
|
2013-03-17 02:39:16 +01:00
|
|
|
if ($this->request->latestParam('Action') == 'tag') {
|
|
|
|
$tag = $this->request->latestParam('ID');
|
|
|
|
$tag = urldecode($tag);
|
|
|
|
return Convert::raw2xml($tag);
|
2013-04-29 09:14:30 +02:00
|
|
|
}
|
2013-03-17 02:39:16 +01:00
|
|
|
return '';
|
2012-05-21 04:58:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the selected date from the blog tree
|
|
|
|
*
|
|
|
|
* @return Date
|
|
|
|
*/
|
|
|
|
function SelectedDate() {
|
|
|
|
if($this->request->latestParam('Action') == 'date') {
|
|
|
|
$year = $this->request->latestParam('ID');
|
|
|
|
$month = $this->request->latestParam('OtherID');
|
|
|
|
|
|
|
|
if(is_numeric($year) && is_numeric($month) && $month < 13) {
|
|
|
|
|
|
|
|
$date = $year .'-'. $month;
|
|
|
|
return $date;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if(is_numeric($year)) return $year;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-28 17:33:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function SelectedAuthor() {
|
|
|
|
if($this->request->getVar('author')) {
|
|
|
|
$hasAuthor = BlogEntry::get()->filter('Author', $this->request->getVar('author'))->Count();
|
|
|
|
return $hasAuthor ? Convert::raw2xml($this->request->getVar('author')) : null;
|
|
|
|
} elseif($this->request->getVar('authorID')) {
|
|
|
|
$hasAuthor = BlogEntry::get()->filter('AuthorID', $this->request->getVar('authorID'))->Count();
|
|
|
|
if($hasAuthor) {
|
|
|
|
$member = Member::get()->byId($this->request->getVar('authorID'));
|
|
|
|
if($member) {
|
|
|
|
if($member->hasMethod('BlogAuthorTitle')) {
|
|
|
|
return Convert::raw2xml($member->BlogAuthorTitle);
|
|
|
|
} else {
|
|
|
|
return Convert::raw2xml($member->Title);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-21 04:58:26 +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));
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 23:46:58 +02:00
|
|
|
}
|