Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-05-23-28-33
[civicrm-core.git] / CRM / Dashlet / Page / Blog.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Main page for blog dashlet
38 */
39class CRM_Dashlet_Page_Blog extends CRM_Core_Page {
40
14026aa8
TO
41 const CHECK_TIMEOUT = 5;
42 const CACHE_DAYS = 1;
43 const BLOG_URL = 'https://civicrm.org/blog/feed';
6a488035 44
4b030206
TO
45 /**
46 * Get the final, usable URL string (after interpolating any variables)
47 *
48 * @return FALSE|string
49 */
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;
56 }
57 return CRM_Utils_System::evalUrl($url);
58 }
59
6a488035
TO
60 /**
61 * List blog articles as dashlet
62 *
6a488035 63 */
00be9182 64 public function run() {
6a488035
TO
65 $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'dashlet');
66 $this->assign('context', $context);
67
68 $this->assign('blog', $this->_getBlog());
69
70 return parent::run();
71 }
72
73 /**
74 * Load blog articles from cache
75 * Refresh cache if expired
76 *
77 * @return array
78 *
6a488035
TO
79 */
80 private function _getBlog() {
81 // Fetch data from cache
82 $cache = CRM_Core_DAO::executeQuery("SELECT data, created_date FROM civicrm_cache
83 WHERE group_name = 'dashboard' AND path = 'blog'");
84 if ($cache->fetch()) {
85 $expire = time() - (60 * 60 * 24 * self::CACHE_DAYS);
86 // Refresh data after CACHE_DAYS
87 if (strtotime($cache->created_date) < $expire) {
4b030206 88 $new_data = $this->_getFeed($this->getBlogUrl());
6a488035
TO
89 // If fetching the new rss feed was successful, return it
90 // Otherwise use the old cached data - it's better than nothing
91 if ($new_data) {
92 return $new_data;
93 }
94 }
95 return unserialize($cache->data);
96 }
4b030206 97 return $this->_getFeed($this->getBlogUrl());
6a488035
TO
98 }
99
100 /**
101 * Parse rss feed and cache results
102 *
77b97be7
EM
103 * @param $url
104 *
14026aa8 105 * @return array|NULL array of blog items; or NULL if not available
6a488035 106 *
6a488035 107 */
4b030206 108 public function _getFeed($url) {
14026aa8 109 $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT);
8c2d418b 110 list ($status, $rawFeed) = $httpClient->get($url);
14026aa8
TO
111 if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
112 return NULL;
113 }
114 $feed = @simplexml_load_string($rawFeed);
115
6a488035
TO
116 $blog = array();
117 if ($feed && !empty($feed->channel->item)) {
118 foreach ($feed->channel->item as $item) {
119 $item = (array) $item;
120 // Clean up description - remove tags that would break dashboard layout
121 $description = preg_replace('#<h[1-3][^>]*>(.+?)</h[1-3][^>]*>#s', '<h4>$1</h4>', $item['description']);
122 $item['description'] = strip_tags($description, "<a><p><h4><h5><h6><b><i><em><strong><ol><ul><li><dd><dt><code><pre><br>");
123 $blog[] = $item;
124 }
125 if ($blog) {
126 CRM_Core_BAO_Cache::setItem($blog, 'dashboard', 'blog');
127 }
128 }
129 return $blog;
130 }
131}