Merge pull request #22646 from braders/feature/tasktrait-issingle-docblock
[civicrm-core.git] / CRM / Dashlet / Page / GettingStarted.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
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;
24 const CACHE_DAYS = 5;
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
29 * @var array
30 */
31 public static $_tokens = [
32 'crmurl' => [
33 'configbackend' => 'civicrm/admin/configtask',
34 ],
35 ];
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}.
45 $url = Civi::settings()->get('gettingStartedUrl');
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() {
56 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'dashlet');
57
58 // Assign smarty variables.
59 $this->assign('context', $context);
60 $this->assign('gettingStarted', $this->_gettingStarted());
61
62 // Use smarty to generate page.
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() {
73 $tsLocale = CRM_Core_I18n::getLocale();
74 $key = 'dashboard_gettingStarted_' . $tsLocale;
75 $value = Civi::cache('community_messages')->get($key);
76
77 if (!$value) {
78 $value = $this->_getHtml($this->gettingStartedUrl());
79
80 if ($value) {
81 Civi::cache('community_messages')->set($key, $value, (60 * 60 * 24 * self::CACHE_DAYS));
82 }
83 }
84
85 return $value;
86 }
87
88 /**
89 * Get html.
90 *
91 * @param $url
92 *
93 * @return array|NULL
94 * array of gettingStarted items; or NULL if not available
95 */
96 public function _getHtml($url) {
97 $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT);
98 list ($status, $html) = $httpClient->get($url);
99
100 if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
101 return NULL;
102 }
103
104 $tokensList = CRM_Utils_Token::getTokens($html);
105 $this->replaceLinkToken($tokensList, $html);
106 return $html;
107 }
108
109 /**
110 * @param array $tokensList
111 * @param string $str
112 *
113 */
114 public function replaceLinkToken($tokensList, &$str) {
115 foreach ($tokensList as $categories => $tokens) {
116 foreach ($tokens as $token) {
117 $value = '';
118 if (!empty(self::$_tokens[$categories][$token])) {
119 $value = self::$_tokens[$categories][$token];
120 if ($categories == 'crmurl') {
121 $value = CRM_Utils_System::url($value, "reset=1");
122 }
123 }
124 CRM_Utils_Token::token_replace($categories, $token, $value, $str);
125 }
126 }
127 }
128
129 }