Merge pull request #15821 from seamuslee001/dev_core_183_custom_group
[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 * $Id$
17 *
18 */
19
20 /**
21 * Main page for getting started dashlet
22 */
23 class CRM_Dashlet_Page_GettingStarted extends CRM_Core_Page {
24
25 const CHECK_TIMEOUT = 5;
26 const CACHE_DAYS = 5;
27 const GETTING_STARTED_URL = 'https://alert.civicrm.org/welcome?prot=1&ver={ver}&uf={uf}&sid={sid}&lang={lang}&co={co}';
28
29 /**
30 * Define tokens available for getting started
31 * @var array
32 */
33 public static $_tokens = [
34 'crmurl' => [
35 'configbackend' => 'civicrm/admin/configtask',
36 ],
37 ];
38
39 /**
40 * Get the final, usable URL string (after interpolating any variables)
41 *
42 * @return FALSE|string
43 */
44 public function gettingStartedUrl() {
45 // Note: We use "*default*" as the default (rather than self::GETTING_STARTED_URL) so that future
46 // developers can change GETTING_STARTED_URL without needing to update {civicrm_setting}.
47 $url = Civi::settings()->get('gettingStartedUrl');
48 if ($url === '*default*') {
49 $url = self::GETTING_STARTED_URL;
50 }
51 return CRM_Utils_System::evalUrl($url);
52 }
53
54 /**
55 * List gettingStarted page as dashlet.
56 */
57 public function run() {
58 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'dashlet');
59
60 // Assign smarty variables.
61 $this->assign('context', $context);
62 $this->assign('gettingStarted', $this->_gettingStarted());
63
64 // Use smarty to generate page.
65 return parent::run();
66 }
67
68 /**
69 * Load gettingStarted page from cache.
70 * Refresh cache if expired
71 *
72 * @return array
73 */
74 private function _gettingStarted() {
75 $value = Civi::cache('community_messages')->get('dashboard_gettingStarted');
76
77 if (!$value) {
78 $value = $this->_getHtml($this->gettingStartedUrl());
79
80 if ($value) {
81 Civi::cache('community_messages')->set('dashboard_gettingStarted', $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 }