From b6f31f2a4910679f5e7930e126f1927fdef3894f Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 25 Aug 2016 22:44:22 -0400 Subject: [PATCH] CRM-18953 - Add events tab to news dashlet --- CRM/Dashlet/Page/Blog.php | 83 +++++++++++++++++------- templates/CRM/Dashlet/Page/Blog.tpl | 98 ++++++++++++++++++++++++----- 2 files changed, 144 insertions(+), 37 deletions(-) diff --git a/CRM/Dashlet/Page/Blog.php b/CRM/Dashlet/Page/Blog.php index acc144ba9e..c18554401e 100644 --- a/CRM/Dashlet/Page/Blog.php +++ b/CRM/Dashlet/Page/Blog.php @@ -41,11 +41,12 @@ class CRM_Dashlet_Page_Blog extends CRM_Core_Page { const CHECK_TIMEOUT = 5; const CACHE_DAYS = 1; const BLOG_URL = 'https://civicrm.org/blog/feed'; + const EVENT_URL = 'https://civicrm.org/civicrm/event/ical?reset=1&list=1&rss=1'; /** - * Get the final, usable URL string (after interpolating any variables) + * Gets url for blog feed. * - * @return FALSE|string + * @return string */ public function getBlogUrl() { // Note: We use "*default*" as the default (rather than self::BLOG_URL) so that future @@ -58,24 +59,38 @@ class CRM_Dashlet_Page_Blog extends CRM_Core_Page { } /** - * List blog articles as dashlet. + * Gets url for the events feed. + * + * @return string */ - public function run() { - $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'dashlet'); - $this->assign('context', $context); + public function getEventUrl() { + $url = self::EVENT_URL + . '&start=' . date("Ymd") + . '&end=' . date("Ymd", strtotime('now +6 month')); + return $url; + } - $this->assign('blog', $this->_getBlog()); + /** + * Output data to template. + */ + public function run() { + $this->assign('tabs', array( + 'blog' => ts('Blog'), + 'events' => ts('Events'), + )); + $this->assign('feeds', $this->getData()); return parent::run(); } /** - * Load blog articles from cache. - * Refresh cache if expired + * Load feeds from cache. + * + * Refresh cache if expired. * * @return array */ - private function _getBlog() { + protected function getData() { // Fetch data from cache $cache = CRM_Core_DAO::executeQuery("SELECT data, created_date FROM civicrm_cache WHERE group_name = 'dashboard' AND path = 'blog'"); @@ -83,38 +98,65 @@ class CRM_Dashlet_Page_Blog extends CRM_Core_Page { $expire = time() - (60 * 60 * 24 * self::CACHE_DAYS); // Refresh data after CACHE_DAYS if (strtotime($cache->created_date) < $expire) { - $new_data = $this->_getFeed($this->getBlogUrl()); + $new_data = $this->getFeeds(); // If fetching the new rss feed was successful, return it // Otherwise use the old cached data - it's better than nothing - if ($new_data) { + if ($new_data['blog']) { return $new_data; } } return unserialize($cache->data); } - return $this->_getFeed($this->getBlogUrl()); + return $this->getFeeds(); } /** - * Parse rss feed and cache results. + * Fetch all feeds & cache results. + * + * @return array + */ + protected function getFeeds() { + $blogFeed = $this->getFeed($this->getBlogUrl()); + // If unable to fetch the first feed, give up and return empty results. + if (!$blogFeed) { + return array_fill_keys(array_keys($this->get_template_vars('tabs')), array()); + } + $eventFeed = $this->getFeed($this->getEventUrl()); + $feeds = array( + 'blog' => $this->formatItems($blogFeed), + 'events' => $this->formatItems($eventFeed), + ); + CRM_Core_BAO_Cache::setItem($feeds, 'dashboard', 'blog'); + return $feeds; + } + + /** + * Parse a single rss feed. * * @param $url * * @return array|NULL * array of blog items; or NULL if not available */ - public function _getFeed($url) { + protected function getFeed($url) { $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT); list ($status, $rawFeed) = $httpClient->get($url); if ($status !== CRM_Utils_HttpClient::STATUS_OK) { return NULL; } - $feed = @simplexml_load_string($rawFeed); + return @simplexml_load_string($rawFeed); + } - $blog = array(); + /** + * @param string $feed + * @return array + */ + protected function formatItems($feed) { + $items = array(); if ($feed && !empty($feed->channel->item)) { foreach ($feed->channel->item as $item) { $item = (array) $item; + $item['title'] = strip_tags($item['title']); // Clean up description - remove tags that would break dashboard layout $description = preg_replace('#]*>(.+?)]*>#s', '

$1

', $item['description']); $description = strip_tags($description, "