assign('context', $context);
$this->assign('blog', $this->_getBlog());
return parent::run();
}
/**
* Load blog articles from cache
* Refresh cache if expired
*
* @return array
*
* @access private
*/
private function _getBlog() {
// Fetch data from cache
$cache = CRM_Core_DAO::executeQuery("SELECT data, created_date FROM civicrm_cache
WHERE group_name = 'dashboard' AND path = 'blog'");
if ($cache->fetch()) {
$expire = time() - (60 * 60 * 24 * self::CACHE_DAYS);
// Refresh data after CACHE_DAYS
if (strtotime($cache->created_date) < $expire) {
$new_data = $this->_getFeed();
// If fetching the new rss feed was successful, return it
// Otherwise use the old cached data - it's better than nothing
if ($new_data) {
return $new_data;
}
}
return unserialize($cache->data);
}
return $this->_getFeed();
}
/**
* Parse rss feed and cache results
*
* @return array|NULL array of blog items; or NULL if not available
*
* @access public
*/
public function _getFeed( $url = self::BLOG_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);
$blog = array();
if ($feed && !empty($feed->channel->item)) {
foreach ($feed->channel->item as $item) {
$item = (array) $item;
// Clean up description - remove tags that would break dashboard layout
$description = preg_replace('#
");
$blog[] = $item;
}
if ($blog) {
CRM_Core_BAO_Cache::setItem($blog, 'dashboard', 'blog');
}
}
return $blog;
}
}