Commit | Line | Data |
---|---|---|
04eee3f6 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
04eee3f6 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
04eee3f6 | 9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
04eee3f6 | 16 | */ |
17 | ||
18 | /** | |
19 | * Main page for getting started dashlet | |
20 | */ | |
21 | class CRM_Dashlet_Page_GettingStarted extends CRM_Core_Page { | |
22 | ||
23 | const CHECK_TIMEOUT = 5; | |
e1674273 | 24 | const CACHE_DAYS = 5; |
04eee3f6 | 25 | const GETTING_STARTED_URL = 'https://alert.civicrm.org/welcome?prot=1&ver={ver}&uf={uf}&sid={sid}&lang={lang}&co={co}'; |
26 | ||
27 | /** | |
28 | * Define tokens available for getting started | |
7b966967 | 29 | * @var array |
04eee3f6 | 30 | */ |
7b966967 | 31 | public static $_tokens = [ |
be2fb01f | 32 | 'crmurl' => [ |
04eee3f6 | 33 | 'configbackend' => 'civicrm/admin/configtask', |
be2fb01f CW |
34 | ], |
35 | ]; | |
04eee3f6 | 36 | |
37 | /** | |
38 | * Get the final, usable URL string (after interpolating any variables) | |
39 | * | |
40 | * @return FALSE|string | |
41 | */ | |
42 | public function gettingStartedUrl() { | |
43 | // Note: We use "*default*" as the default (rather than self::GETTING_STARTED_URL) so that future | |
44 | // developers can change GETTING_STARTED_URL without needing to update {civicrm_setting}. | |
1f957d1e | 45 | $url = Civi::settings()->get('gettingStartedUrl'); |
04eee3f6 | 46 | if ($url === '*default*') { |
47 | $url = self::GETTING_STARTED_URL; | |
48 | } | |
49 | return CRM_Utils_System::evalUrl($url); | |
50 | } | |
51 | ||
52 | /** | |
53 | * List gettingStarted page as dashlet. | |
54 | */ | |
55 | public function run() { | |
edc80cda | 56 | $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'dashlet'); |
04eee3f6 | 57 | |
7e90e573 JL |
58 | // Assign smarty variables. |
59 | $this->assign('context', $context); | |
04eee3f6 | 60 | $this->assign('gettingStarted', $this->_gettingStarted()); |
61 | ||
7e90e573 | 62 | // Use smarty to generate page. |
04eee3f6 | 63 | return parent::run(); |
64 | } | |
65 | ||
66 | /** | |
67 | * Load gettingStarted page from cache. | |
68 | * Refresh cache if expired | |
69 | * | |
70 | * @return array | |
71 | */ | |
72 | private function _gettingStarted() { | |
d59ab677 ML |
73 | $tsLocale = CRM_Core_I18n::getLocale(); |
74 | $key = 'dashboard_gettingStarted_' . $tsLocale; | |
75 | $value = Civi::cache('community_messages')->get($key); | |
a03490f5 ML |
76 | |
77 | if (!$value) { | |
78 | $value = $this->_getHtml($this->gettingStartedUrl()); | |
79 | ||
80 | if ($value) { | |
d59ab677 | 81 | Civi::cache('community_messages')->set($key, $value, (60 * 60 * 24 * self::CACHE_DAYS)); |
04eee3f6 | 82 | } |
04eee3f6 | 83 | } |
a03490f5 ML |
84 | |
85 | return $value; | |
04eee3f6 | 86 | } |
87 | ||
88 | /** | |
a03490f5 | 89 | * Get html. |
04eee3f6 | 90 | * |
fa3fdebc | 91 | * @param string $url |
04eee3f6 | 92 | * |
93 | * @return array|NULL | |
94 | * array of gettingStarted items; or NULL if not available | |
95 | */ | |
96 | public function _getHtml($url) { | |
04eee3f6 | 97 | $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT); |
98 | list ($status, $html) = $httpClient->get($url); | |
a03490f5 | 99 | |
04eee3f6 | 100 | if ($status !== CRM_Utils_HttpClient::STATUS_OK) { |
101 | return NULL; | |
102 | } | |
4175ee03 | 103 | |
04eee3f6 | 104 | $tokensList = CRM_Utils_Token::getTokens($html); |
105 | $this->replaceLinkToken($tokensList, $html); | |
04eee3f6 | 106 | return $html; |
107 | } | |
108 | ||
04eee3f6 | 109 | /** |
110 | * @param array $tokensList | |
4175ee03 | 111 | * @param string $str |
04eee3f6 | 112 | * |
4175ee03 | 113 | */ |
04eee3f6 | 114 | public function replaceLinkToken($tokensList, &$str) { |
4175ee03 | 115 | foreach ($tokensList as $categories => $tokens) { |
116 | foreach ($tokens as $token) { | |
04eee3f6 | 117 | $value = ''; |
118 | if (!empty(self::$_tokens[$categories][$token])) { | |
119 | $value = self::$_tokens[$categories][$token]; | |
120 | if ($categories == 'crmurl') { | |
5c4a9e59 | 121 | $value = CRM_Utils_System::url($value, "reset=1"); |
04eee3f6 | 122 | } |
123 | } | |
124 | CRM_Utils_Token::token_replace($categories, $token, $value, $str); | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
129 | } |