NEW Making widgets dependency optional

This commit is contained in:
Ingo Schommer 2013-04-11 12:35:22 +02:00
parent 966d168119
commit ee532292d2
6 changed files with 265 additions and 268 deletions

View File

@ -14,7 +14,7 @@ The blog module allows you to post blogs on your SilverStripe. It includes the a
## Requirements ## Requirements
* Silverstripe 3.0 * Silverstripe 3.0
* silverstripe-widgets module * (Optional) silverstripe-widgets module
## Feature Overview ## Feature Overview
@ -76,10 +76,6 @@ or: mysite/blog/2007 would show blog entries for 2007
See [:pagecomment](/pagecomment) for creating Askimet-protected comments for every page. See [:pagecomment](/pagecomment) for creating Askimet-protected comments for every page.
## Widgets
See [widgets](/widgets)
## Working with the theme ## Working with the theme

View File

@ -1,111 +1,112 @@
<?php <?php
/** if(class_exists('Widget')) {
* Shows a widget with viewing blog entries /**
* by months or years. * Shows a widget with viewing blog entries
* * by months or years.
* @package blog *
*/ * @package blog
class ArchiveWidget extends Widget { */
static $db = array( class ArchiveWidget extends Widget {
'DisplayMode' => 'Varchar' static $db = array(
); 'DisplayMode' => 'Varchar'
static $has_one = array();
static $has_many = array();
static $many_many = array();
static $belongs_many_many = array();
static $defaults = array(
'DisplayMode' => 'month'
);
static $title = 'Browse by Date';
static $cmsTitle = 'Blog Archive';
static $description = 'Show a list of months or years in which there are blog posts, and provide links to them.';
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->merge(
new FieldList(
new OptionsetField(
'DisplayMode',
_t('ArchiveWidget.DispBY', 'Display by'),
array(
'month' => _t('ArchiveWidget.MONTH', 'month'),
'year' => _t('ArchiveWidget.YEAR', 'year')
)
)
)
); );
$this->extend('updateCMSFields', $fields); static $has_one = array();
return $fields; static $has_many = array();
}
function getDates() { static $many_many = array();
Requirements::themedCSS('archivewidget');
$results = new ArrayList(); static $belongs_many_many = array();
$container = BlogTree::current();
$ids = $container->BlogHolderIDs();
$stage = Versioned::current_stage(); static $defaults = array(
$suffix = (!$stage || $stage == 'Stage') ? "" : "_$stage"; 'DisplayMode' => 'month'
);
$monthclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%m') : 'MONTH("Date")'; static $title = 'Browse by Date';
$yearclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%Y') : 'YEAR("Date")';
if($this->DisplayMode == 'month') { static $cmsTitle = 'Blog Archive';
$sqlResults = DB::query("
SELECT DISTINCT CAST($monthclause AS " . DB::getConn()->dbDataType('unsigned integer') . ") AS \"Month\", $yearclause AS \"Year\" static $description = 'Show a list of months or years in which there are blog posts, and provide links to them.';
FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\" ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\"
WHERE \"ParentID\" IN (" . implode(', ', $ids) . ") function getCMSFields() {
ORDER BY \"Year\" DESC, \"Month\" DESC;" $fields = parent::getCMSFields();
);
} else { $fields->merge(
$sqlResults = DB::query("
SELECT DISTINCT $yearclause AS \"Year\" new FieldList(
FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\" ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\" new OptionsetField(
WHERE \"ParentID\" IN (" . implode(', ', $ids) . ") 'DisplayMode',
ORDER BY \"Year\" DESC" _t('ArchiveWidget.DispBY', 'Display by'),
array(
'month' => _t('ArchiveWidget.MONTH', 'month'),
'year' => _t('ArchiveWidget.YEAR', 'year')
)
)
)
); );
$this->extend('updateCMSFields', $fields);
return $fields;
} }
if($sqlResults) foreach($sqlResults as $sqlResult) { function getDates() {
$isMonthDisplay = $this->DisplayMode == 'month'; Requirements::themedCSS('archivewidget');
$monthVal = (isset($sqlResult['Month'])) ? (int) $sqlResult['Month'] : 1; $results = new ArrayList();
$month = ($isMonthDisplay) ? $monthVal : 1; $container = BlogTree::current();
$year = ($sqlResult['Year']) ? (int) $sqlResult['Year'] : date('Y'); $ids = $container->BlogHolderIDs();
$date = DBField::create_field('Date', array( $stage = Versioned::current_stage();
'Day' => 1, $suffix = (!$stage || $stage == 'Stage') ? "" : "_$stage";
'Month' => $month,
'Year' => $year
));
if($isMonthDisplay) { $monthclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%m') : 'MONTH("Date")';
$link = $container->Link('date') . '/' . $sqlResult['Year'] . '/' . sprintf("%'02d", $monthVal); $yearclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%Y') : 'YEAR("Date")';
if($this->DisplayMode == 'month') {
$sqlResults = DB::query("
SELECT DISTINCT CAST($monthclause AS " . DB::getConn()->dbDataType('unsigned integer') . ") AS \"Month\", $yearclause AS \"Year\"
FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\" ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\"
WHERE \"ParentID\" IN (" . implode(', ', $ids) . ")
ORDER BY \"Year\" DESC, \"Month\" DESC;"
);
} else { } else {
$link = $container->Link('date') . '/' . $sqlResult['Year']; $sqlResults = DB::query("
SELECT DISTINCT $yearclause AS \"Year\"
FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\" ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\"
WHERE \"ParentID\" IN (" . implode(', ', $ids) . ")
ORDER BY \"Year\" DESC"
);
} }
$results->push(new ArrayData(array( if($sqlResults) foreach($sqlResults as $sqlResult) {
'Date' => $date, $isMonthDisplay = $this->DisplayMode == 'month';
'Link' => $link
))); $monthVal = (isset($sqlResult['Month'])) ? (int) $sqlResult['Month'] : 1;
$month = ($isMonthDisplay) ? $monthVal : 1;
$year = ($sqlResult['Year']) ? (int) $sqlResult['Year'] : date('Y');
$date = DBField::create_field('Date', array(
'Day' => 1,
'Month' => $month,
'Year' => $year
));
if($isMonthDisplay) {
$link = $container->Link('date') . '/' . $sqlResult['Year'] . '/' . sprintf("%'02d", $monthVal);
} else {
$link = $container->Link('date') . '/' . $sqlResult['Year'];
}
$results->push(new ArrayData(array(
'Date' => $date,
'Link' => $link
)));
}
return $results;
} }
return $results;
} }
}
?> }

View File

@ -1,67 +1,66 @@
<?php <?php
/** if(class_exists('Widget')) {
* Blog Management Widget
* @package blog
*/
class BlogManagementWidget extends Widget implements PermissionProvider {
static $db = array();
static $has_one = array(); /**
* Blog Management Widget
* @package blog
*/
class BlogManagementWidget extends Widget {
static $db = array();
static $has_many = array(); static $has_one = array();
static $many_many = array(); static $has_many = array();
static $belongs_many_many = array(); static $many_many = array();
static $defaults = array(); static $belongs_many_many = array();
static $title = "Blog Management"; static $defaults = array();
static $cmsTitle = "Blog Management";
static $description = "Provide a number of links useful for administering a blog. Only shown if the user is an admin.";
function CommentText() { static $title = "Blog Management";
static $cmsTitle = "Blog Management";
static $description = "Provide a number of links useful for administering a blog. Only shown if the user is an admin.";
if(!class_exists('Comment')) return false; function CommentText() {
$unmoderatedcount = DB::query("SELECT COUNT(*) FROM \"Comment\" WHERE \"Moderated\"=1")->value();
if($unmoderatedcount == 1) { if(!class_exists('Comment')) return false;
return _t("BlogManagementWidget.UNM1", "You have 1 unmoderated comment"); $unmoderatedcount = DB::query("SELECT COUNT(*) FROM \"Comment\" WHERE \"Moderated\"=1")->value();
} else if($unmoderatedcount > 1) { if($unmoderatedcount == 1) {
return sprintf(_t("BlogManagementWidget.UNMM", "You have %i unmoderated comments"), $unmoderatedcount); return _t("BlogManagementWidget.UNM1", "You have 1 unmoderated comment");
} else { } else if($unmoderatedcount > 1) {
return _t("BlogManagementWidget.COMADM", "Comment administration"); return sprintf(_t("BlogManagementWidget.UNMM", "You have %i unmoderated comments"), $unmoderatedcount);
} else {
return _t("BlogManagementWidget.COMADM", "Comment administration");
}
} }
function CommentLink() {
if(!Permission::check('BLOGMANAGEMENT') || !class_exists('Comment')) return false;
$unmoderatedcount = DB::query("SELECT COUNT(*) FROM \"Comment\" WHERE \"Moderated\"=1")->value();
if($unmoderatedcount > 0) {
return "admin/comments/unmoderated";
} else {
return "admin/comments";
}
}
} }
function CommentLink() { class BlogManagementWidget_Controller extends Widget_Controller {
if(!Permission::check('BLOGMANAGEMENT') || !class_exists('Comment')) return false; function WidgetHolder() {
$unmoderatedcount = DB::query("SELECT COUNT(*) FROM \"Comment\" WHERE \"Moderated\"=1")->value(); if(Permission::check("BLOGMANAGEMENT")) {
return $this->renderWith("WidgetHolder");
if($unmoderatedcount > 0) { }
return "admin/comments/unmoderated";
} else {
return "admin/comments";
} }
}
function providePermissions() { function PostLink() {
return array("BLOGMANAGEMENT" => "Blog management"); $container = BlogTree::current();
return ($container && $container->ClassName != "BlogTree") ? $container->Link('post') : false;
}
} }
} }
class BlogManagementWidget_Controller extends Widget_Controller {
function WidgetHolder() {
if(Permission::check("BLOGMANAGEMENT")) {
return $this->renderWith("WidgetHolder");
}
}
function PostLink() {
$container = BlogTree::current();
return ($container && $container->ClassName != "BlogTree") ? $container->Link('post') : false;
}
}
?>

View File

@ -1,96 +1,96 @@
<?php <?php
if(class_exists('Widget')) {
class RSSWidget extends Widget { class RSSWidget extends Widget {
static $db = array( static $db = array(
"RSSTitle" => "Text", "RSSTitle" => "Text",
"RssUrl" => "Text", "RssUrl" => "Text",
"NumberToShow" => "Int" "NumberToShow" => "Int"
);
static $has_one = array();
static $has_many = array();
static $many_many = array();
static $belongs_many_many = array();
static $defaults = array(
"NumberToShow" => 10,
"RSSTitle" => 'RSS Feed'
);
static $cmsTitle = "RSS Feed";
static $description = "Downloads another page's RSS feed and displays items in a list.";
/**
* If the RssUrl is relative, convert it to absolute with the
* current baseURL to avoid confusing simplepie.
* Passing relative URLs to simplepie will result
* in strange DNS lookups and request timeouts.
*
* @return string
*/
function getAbsoluteRssUrl() {
$urlParts = parse_url($this->RssUrl);
if(!isset($urlParts['host']) || !$urlParts['host']) {
return Director::absoluteBaseURL() . $this->RssUrl;
} else {
return $this->RssUrl;
}
}
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->merge(
new FieldList(
new TextField("RSSTitle", _t('RSSWidget.CT', "Custom title for the feed")),
new TextField("RssUrl", _t('RSSWidget.URL', "URL of the other page's RSS feed. Please make sure this URL points to an RSS feed.")),
new NumericField("NumberToShow", _t('RSSWidget.NTS', "Number of Items to show"))
)
); );
$this->extend('updateCMSFields', $fields); static $has_one = array();
return $fields; static $has_many = array();
}
function Title() {
return ($this->RSSTitle) ? $this->RSSTitle : 'RSS Feed';
}
function getFeedItems() { static $many_many = array();
$output = new ArrayList();
// Protection against infinite loops when an RSS widget pointing to this page is added to this page static $belongs_many_many = array();
if(stristr($_SERVER['HTTP_USER_AGENT'], 'SimplePie')) {
return $output;
}
include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/simplepie.inc')); static $defaults = array(
"NumberToShow" => 10,
"RSSTitle" => 'RSS Feed'
);
static $cmsTitle = "RSS Feed";
static $description = "Downloads another page's RSS feed and displays items in a list.";
$t1 = microtime(true); /**
$feed = new SimplePie($this->AbsoluteRssUrl, TEMP_FOLDER); * If the RssUrl is relative, convert it to absolute with the
$feed->init(); * current baseURL to avoid confusing simplepie.
if($items = $feed->get_items(0, $this->NumberToShow)) { * Passing relative URLs to simplepie will result
foreach($items as $item) { * in strange DNS lookups and request timeouts.
*
// Cast the Date * @return string
$date = new Date('Date'); */
$date->setValue($item->get_date()); function getAbsoluteRssUrl() {
$urlParts = parse_url($this->RssUrl);
// Cast the Title if(!isset($urlParts['host']) || !$urlParts['host']) {
$title = new Text('Title'); return Director::absoluteBaseURL() . $this->RssUrl;
$title->setValue($item->get_title()); } else {
return $this->RssUrl;
$output->push(new ArrayData(array( }
'Title' => $title, }
'Date' => $date,
'Link' => $item->get_link() function getCMSFields() {
))); $fields = parent::getCMSFields();
$fields->merge(
new FieldList(
new TextField("RSSTitle", _t('RSSWidget.CT', "Custom title for the feed")),
new TextField("RssUrl", _t('RSSWidget.URL', "URL of the other page's RSS feed. Please make sure this URL points to an RSS feed.")),
new NumericField("NumberToShow", _t('RSSWidget.NTS', "Number of Items to show"))
)
);
$this->extend('updateCMSFields', $fields);
return $fields;
}
function Title() {
return ($this->RSSTitle) ? $this->RSSTitle : 'RSS Feed';
}
function getFeedItems() {
$output = new ArrayList();
// 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 $output;
}
include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/simplepie.inc'));
$t1 = microtime(true);
$feed = new SimplePie($this->AbsoluteRssUrl, TEMP_FOLDER);
$feed->init();
if($items = $feed->get_items(0, $this->NumberToShow)) {
foreach($items as $item) {
// Cast the Date
$date = new Date('Date');
$date->setValue($item->get_date());
// Cast the Title
$title = new Text('Title');
$title->setValue($item->get_title());
$output->push(new ArrayData(array(
'Title' => $title,
'Date' => $date,
'Link' => $item->get_link()
)));
}
return $output;
} }
return $output;
} }
} }
}
?> }

View File

@ -1,31 +1,31 @@
<?php <?php
if(class_exists('Widget')) {
/**
* A simple widget that just shows a link
* to this website's blog RSS, with an RSS
* icon.
*
* @package blog
*/
class SubscribeRSSWidget extends Widget {
static $title = 'Subscribe via RSS';
static $cmsTitle = 'Subscribe via RSS widget';
static $description = 'Shows a link allowing a user to subscribe to this blog via RSS.';
/** /**
* Return an absolute URL based on the BlogHolder * A simple widget that just shows a link
* that this widget is located on. * to this website's blog RSS, with an RSS
* icon.
* *
* @return string * @package blog
*/ */
function getRSSLink() { class SubscribeRSSWidget extends Widget {
Requirements::themedCSS('subscribersswidget');
$container = BlogTree::current();
if ($container) return $container->Link('rss');
}
}
?> static $title = 'Subscribe via RSS';
static $cmsTitle = 'Subscribe via RSS widget';
static $description = 'Shows a link allowing a user to subscribe to this blog via RSS.';
/**
* Return an absolute URL based on the BlogHolder
* that this widget is located on.
*
* @return string
*/
function getRSSLink() {
Requirements::themedCSS('subscribersswidget');
$container = BlogTree::current();
if ($container) return $container->Link('rss');
}
}
}

View File

@ -14,9 +14,10 @@
} }
], ],
"require": "require": {
{ "silverstripe/cms": "3.0.*"
"silverstripe/cms": "3.0.*", },
"suggests": {
"silverstripe/widgets": "0.1.*" "silverstripe/widgets": "0.1.*"
} }
} }