3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * Main page for blog dashlet
39 class CRM_Dashlet_Page_Blog
extends CRM_Core_Page
{
41 const CHECK_TIMEOUT
= 5;
43 const BLOG_URL
= 'https://civicrm.org/blog/feed';
46 * Get the final, usable URL string (after interpolating any variables)
48 * @return FALSE|string
50 public function getBlogUrl() {
51 // Note: We use "*default*" as the default (rather than self::BLOG_URL) so that future
52 // developers can change BLOG_URL without needing to update {civicrm_setting}.
53 $url = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'blogUrl', NULL, '*default*');
54 if ($url === '*default*') {
55 $url = self
::BLOG_URL
;
57 return CRM_Utils_System
::evalUrl($url);
61 * List blog articles as dashlet.
63 public function run() {
64 $context = CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'dashlet');
65 $this->assign('context', $context);
67 $this->assign('blog', $this->_getBlog());
73 * Load blog articles from cache.
74 * Refresh cache if expired
78 private function _getBlog() {
79 // Fetch data from cache
80 $cache = CRM_Core_DAO
::executeQuery("SELECT data, created_date FROM civicrm_cache
81 WHERE group_name = 'dashboard' AND path = 'blog'");
82 if ($cache->fetch()) {
83 $expire = time() - (60 * 60 * 24 * self
::CACHE_DAYS
);
84 // Refresh data after CACHE_DAYS
85 if (strtotime($cache->created_date
) < $expire) {
86 $new_data = $this->_getFeed($this->getBlogUrl());
87 // If fetching the new rss feed was successful, return it
88 // Otherwise use the old cached data - it's better than nothing
93 return unserialize($cache->data
);
95 return $this->_getFeed($this->getBlogUrl());
99 * Parse rss feed and cache results.
104 * array of blog items; or NULL if not available
106 public function _getFeed($url) {
107 $httpClient = new CRM_Utils_HttpClient(self
::CHECK_TIMEOUT
);
108 list ($status, $rawFeed) = $httpClient->get($url);
109 if ($status !== CRM_Utils_HttpClient
::STATUS_OK
) {
112 $feed = @simplexml_load_string
($rawFeed);
115 if ($feed && !empty($feed->channel
->item
)) {
116 foreach ($feed->channel
->item
as $item) {
117 $item = (array) $item;
118 // Clean up description - remove tags that would break dashboard layout
119 $description = preg_replace('#<h[1-3][^>]*>(.+?)</h[1-3][^>]*>#s', '<h4>$1</h4>', $item['description']);
120 $item['description'] = strip_tags($description, "<a><p><h4><h5><h6><b><i><em><strong><ol><ul><li><dd><dt><code><pre><br/>");
124 CRM_Core_BAO_Cache
::setItem($blog, 'dashboard', 'blog');