diff --git a/README.md b/README.md index 2688cda..54510a7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/code/widgets/ArchiveWidget.php b/code/widgets/ArchiveWidget.php index ca75c68..9de826d 100644 --- a/code/widgets/ArchiveWidget.php +++ b/code/widgets/ArchiveWidget.php @@ -1,111 +1,112 @@ '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; + } + } -?> +} \ No newline at end of file diff --git a/code/widgets/BlogManagementWidget.php b/code/widgets/BlogManagementWidget.php index c5b2022..391f00e 100644 --- a/code/widgets/BlogManagementWidget.php +++ b/code/widgets/BlogManagementWidget.php @@ -1,67 +1,66 @@ 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; - } -} -?> +} \ No newline at end of file diff --git a/code/widgets/RSSWidget.php b/code/widgets/RSSWidget.php index 19f9966..1d474dd 100644 --- a/code/widgets/RSSWidget.php +++ b/code/widgets/RSSWidget.php @@ -1,96 +1,96 @@ "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; } } -} -?> +} \ No newline at end of file diff --git a/code/widgets/SubscribeRSSWidget.php b/code/widgets/SubscribeRSSWidget.php index e75bdec..d2a2b2a 100644 --- a/code/widgets/SubscribeRSSWidget.php +++ b/code/widgets/SubscribeRSSWidget.php @@ -1,31 +1,31 @@ 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'); + } + } + +} \ No newline at end of file diff --git a/composer.json b/composer.json index 2304c26..0a81f81 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,10 @@ } ], - "require": - { - "silverstripe/cms": "3.0.*", + "require": { + "silverstripe/cms": "3.0.*" + }, + "suggests": { "silverstripe/widgets": "0.1.*" } }