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
* Silverstripe 3.0
* silverstripe-widgets module
* (Optional) silverstripe-widgets module
## 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.
## Widgets
See [widgets](/widgets)
## Working with the theme

View File

@ -1,111 +1,112 @@
<?php
/**
* Shows a widget with viewing blog entries
* by months or years.
*
* @package blog
*/
class ArchiveWidget extends Widget {
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')
)
)
)
if(class_exists('Widget')) {
/**
* Shows a widget with viewing blog entries
* by months or years.
*
* @package blog
*/
class ArchiveWidget extends Widget {
static $db = array(
'DisplayMode' => 'Varchar'
);
$this->extend('updateCMSFields', $fields);
static $has_one = array();
return $fields;
}
function getDates() {
Requirements::themedCSS('archivewidget');
static $has_many = array();
$results = new ArrayList();
$container = BlogTree::current();
$ids = $container->BlogHolderIDs();
static $many_many = array();
$stage = Versioned::current_stage();
$suffix = (!$stage || $stage == 'Stage') ? "" : "_$stage";
static $belongs_many_many = array();
static $defaults = array(
'DisplayMode' => 'month'
);
static $title = 'Browse by Date';
$monthclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%m') : 'MONTH("Date")';
$yearclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%Y') : 'YEAR("Date")';
static $cmsTitle = 'Blog Archive';
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 {
$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"
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);
return $fields;
}
if($sqlResults) foreach($sqlResults as $sqlResult) {
$isMonthDisplay = $this->DisplayMode == 'month';
function getDates() {
Requirements::themedCSS('archivewidget');
$monthVal = (isset($sqlResult['Month'])) ? (int) $sqlResult['Month'] : 1;
$month = ($isMonthDisplay) ? $monthVal : 1;
$year = ($sqlResult['Year']) ? (int) $sqlResult['Year'] : date('Y');
$results = new ArrayList();
$container = BlogTree::current();
$ids = $container->BlogHolderIDs();
$date = DBField::create_field('Date', array(
'Day' => 1,
'Month' => $month,
'Year' => $year
));
$stage = Versioned::current_stage();
$suffix = (!$stage || $stage == 'Stage') ? "" : "_$stage";
$monthclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%m') : 'MONTH("Date")';
$yearclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%Y') : 'YEAR("Date")';
if($isMonthDisplay) {
$link = $container->Link('date') . '/' . $sqlResult['Year'] . '/' . sprintf("%'02d", $monthVal);
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 {
$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(
'Date' => $date,
'Link' => $link
)));
}
return $results;
}
}
if($sqlResults) foreach($sqlResults as $sqlResult) {
$isMonthDisplay = $this->DisplayMode == 'month';
$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;
}
}
?>
}

View File

@ -1,67 +1,66 @@
<?php
/**
* Blog Management Widget
* @package blog
*/
class BlogManagementWidget extends Widget implements PermissionProvider {
static $db = array();
if(class_exists('Widget')) {
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 $cmsTitle = "Blog Management";
static $description = "Provide a number of links useful for administering a blog. Only shown if the user is an admin.";
static $defaults = array();
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;
$unmoderatedcount = DB::query("SELECT COUNT(*) FROM \"Comment\" WHERE \"Moderated\"=1")->value();
if($unmoderatedcount == 1) {
return _t("BlogManagementWidget.UNM1", "You have 1 unmoderated comment");
} else if($unmoderatedcount > 1) {
return sprintf(_t("BlogManagementWidget.UNMM", "You have %i unmoderated comments"), $unmoderatedcount);
} else {
return _t("BlogManagementWidget.COMADM", "Comment administration");
function CommentText() {
if(!class_exists('Comment')) return false;
$unmoderatedcount = DB::query("SELECT COUNT(*) FROM \"Comment\" WHERE \"Moderated\"=1")->value();
if($unmoderatedcount == 1) {
return _t("BlogManagementWidget.UNM1", "You have 1 unmoderated comment");
} else if($unmoderatedcount > 1) {
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";
}
}
}
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;
}
}
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 providePermissions() {
return array("BLOGMANAGEMENT" => "Blog management");
}
}
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
class RSSWidget extends Widget {
static $db = array(
"RSSTitle" => "Text",
"RssUrl" => "Text",
"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"))
)
if(class_exists('Widget')) {
class RSSWidget extends Widget {
static $db = array(
"RSSTitle" => "Text",
"RssUrl" => "Text",
"NumberToShow" => "Int"
);
$this->extend('updateCMSFields', $fields);
static $has_one = array();
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;
}
static $has_many = array();
include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/simplepie.inc'));
static $many_many = array();
$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()
)));
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);
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
/**
* 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.';
if(class_exists('Widget')) {
/**
* Return an absolute URL based on the BlogHolder
* that this widget is located on.
* A simple widget that just shows a link
* to this website's blog RSS, with an RSS
* icon.
*
* @return string
* @package blog
*/
function getRSSLink() {
Requirements::themedCSS('subscribersswidget');
$container = BlogTree::current();
if ($container) return $container->Link('rss');
}
}
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
* 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":
{
"silverstripe/cms": "3.0.*",
"require": {
"silverstripe/cms": "3.0.*"
},
"suggests": {
"silverstripe/widgets": "0.1.*"
}
}