Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-10-02-11-18-44
[civicrm-core.git] / CRM / Dashlet / Page / Blog.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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
TO
44
45 /**
46 * List blog articles as dashlet
47 *
48 * @access public
49 */
50 function run() {
51 $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'dashlet');
52 $this->assign('context', $context);
53
54 $this->assign('blog', $this->_getBlog());
55
56 return parent::run();
57 }
58
59 /**
60 * Load blog articles from cache
61 * Refresh cache if expired
62 *
63 * @return array
64 *
65 * @access private
66 */
67 private function _getBlog() {
68 // Fetch data from cache
69 $cache = CRM_Core_DAO::executeQuery("SELECT data, created_date FROM civicrm_cache
70 WHERE group_name = 'dashboard' AND path = 'blog'");
71 if ($cache->fetch()) {
72 $expire = time() - (60 * 60 * 24 * self::CACHE_DAYS);
73 // Refresh data after CACHE_DAYS
74 if (strtotime($cache->created_date) < $expire) {
75 $new_data = $this->_getFeed();
76 // If fetching the new rss feed was successful, return it
77 // Otherwise use the old cached data - it's better than nothing
78 if ($new_data) {
79 return $new_data;
80 }
81 }
82 return unserialize($cache->data);
83 }
84 return $this->_getFeed();
85 }
86
87 /**
88 * Parse rss feed and cache results
89 *
14026aa8 90 * @return array|NULL array of blog items; or NULL if not available
6a488035
TO
91 *
92 * @access private
93 */
94 private function _getFeed() {
14026aa8
TO
95 $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT);
96 list ($status, $rawFeed) = $httpClient->get(self::BLOG_URL);
97 if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
98 return NULL;
99 }
100 $feed = @simplexml_load_string($rawFeed);
101
6a488035
TO
102 $blog = array();
103 if ($feed && !empty($feed->channel->item)) {
104 foreach ($feed->channel->item as $item) {
105 $item = (array) $item;
106 // Clean up description - remove tags that would break dashboard layout
107 $description = preg_replace('#<h[1-3][^>]*>(.+?)</h[1-3][^>]*>#s', '<h4>$1</h4>', $item['description']);
108 $item['description'] = strip_tags($description, "<a><p><h4><h5><h6><b><i><em><strong><ol><ul><li><dd><dt><code><pre><br>");
109 $blog[] = $item;
110 }
111 if ($blog) {
112 CRM_Core_BAO_Cache::setItem($blog, 'dashboard', 'blog');
113 }
114 }
115 return $blog;
116 }
117}