Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.6 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2015 | | |
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-2015 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Main page for getting started dashlet | |
38 | */ | |
39 | class CRM_Dashlet_Page_GettingStarted extends CRM_Core_Page { | |
40 | ||
41 | const CHECK_TIMEOUT = 5; | |
42 | const CACHE_DAYS = 5; | |
43 | const GETTING_STARTED_URL = 'https://alert.civicrm.org/welcome?prot=1&ver={ver}&uf={uf}&sid={sid}&lang={lang}&co={co}'; | |
44 | ||
45 | /** | |
46 | * Define tokens available for getting started | |
47 | */ | |
48 | static $_tokens = array( | |
49 | 'crmurl' => array( | |
50 | 'configbackend' => 'civicrm/admin/configtask', | |
51 | ), | |
52 | ); | |
53 | ||
54 | /** | |
55 | * Get the final, usable URL string (after interpolating any variables) | |
56 | * | |
57 | * @return FALSE|string | |
58 | */ | |
59 | public function gettingStartedUrl() { | |
60 | // Note: We use "*default*" as the default (rather than self::GETTING_STARTED_URL) so that future | |
61 | // developers can change GETTING_STARTED_URL without needing to update {civicrm_setting}. | |
62 | $url = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'gettingStartedUrl', NULL, '*default*'); | |
63 | if ($url === '*default*') { | |
64 | $url = self::GETTING_STARTED_URL; | |
65 | } | |
66 | return CRM_Utils_System::evalUrl($url); | |
67 | } | |
68 | ||
69 | /** | |
70 | * List gettingStarted page as dashlet. | |
71 | */ | |
72 | public function run() { | |
73 | $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'dashlet'); | |
74 | $this->assign('context', $context); | |
75 | ||
76 | $this->assign('gettingStarted', $this->_gettingStarted()); | |
77 | ||
78 | return parent::run(); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Load gettingStarted page from cache. | |
83 | * Refresh cache if expired | |
84 | * | |
85 | * @return array | |
86 | */ | |
87 | private function _gettingStarted() { | |
88 | // Fetch data from cache | |
89 | $cache = CRM_Core_DAO::executeQuery("SELECT data, created_date FROM civicrm_cache | |
90 | WHERE group_name = 'dashboard' AND path = 'gettingStarted'"); | |
91 | if ($cache->fetch()) { | |
92 | $expire = time() - (60 * 60 * 24 * self::CACHE_DAYS); | |
93 | // Refresh data after CACHE_DAYS | |
94 | if (strtotime($cache->created_date) < $expire) { | |
95 | $new_data = $this->_getHtml($this->gettingStartedUrl()); | |
96 | // If fetching the new html was successful, return it | |
97 | // Otherwise use the old cached data - it's better than nothing | |
98 | if ($new_data) { | |
99 | return $new_data; | |
100 | } | |
101 | } | |
102 | return unserialize($cache->data); | |
103 | } | |
104 | return $this->_getHtml($this->gettingStartedUrl()); | |
105 | } | |
106 | ||
107 | /** | |
108 | * Get html and cache results. | |
109 | * | |
110 | * @param $url | |
111 | * | |
112 | * @return array|NULL | |
113 | * array of gettingStarted items; or NULL if not available | |
114 | */ | |
115 | public function _getHtml($url) { | |
116 | ||
117 | $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT); | |
118 | list ($status, $html) = $httpClient->get($url); | |
119 | if ($status !== CRM_Utils_HttpClient::STATUS_OK) { | |
120 | return NULL; | |
121 | } | |
122 | ||
123 | $tokensList = CRM_Utils_Token::getTokens($html); | |
124 | $this->replaceLinkToken($tokensList, $html); | |
125 | if ($html) { | |
126 | CRM_Core_BAO_Cache::setItem($html, 'dashboard', 'gettingStarted'); | |
127 | } | |
128 | return $html; | |
129 | } | |
130 | ||
131 | ||
132 | /** | |
133 | * @param array $tokensList | |
134 | * @param string $str | |
135 | * | |
136 | */ | |
137 | public function replaceLinkToken($tokensList, &$str) { | |
138 | foreach ($tokensList as $categories => $tokens) { | |
139 | foreach ($tokens as $token) { | |
140 | $value = ''; | |
141 | if (!empty(self::$_tokens[$categories][$token])) { | |
142 | $value = self::$_tokens[$categories][$token]; | |
143 | if ($categories == 'crmurl') { | |
144 | $value = CRM_Utils_System::url($value, "reset=1", FALSE, NULL, TRUE, TRUE); | |
145 | } | |
146 | } | |
147 | CRM_Utils_Token::token_replace($categories, $token, $value, $str); | |
148 | } | |
149 | } | |
150 | } | |
151 | ||
152 | } |