Merge pull request #14018 from seamuslee001/financial_extension_export_cxn_dashlet
[civicrm-core.git] / CRM / Dashlet / Page / GettingStarted.php
CommitLineData
04eee3f6 1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
04eee3f6 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
04eee3f6 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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
04eee3f6 32 * $Id$
33 *
34 */
35
36/**
37 * Main page for getting started dashlet
38 */
39class CRM_Dashlet_Page_GettingStarted extends CRM_Core_Page {
40
41 const CHECK_TIMEOUT = 5;
e1674273 42 const CACHE_DAYS = 5;
04eee3f6 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
7b966967 47 * @var array
04eee3f6 48 */
7b966967 49 public static $_tokens = [
be2fb01f 50 'crmurl' => [
04eee3f6 51 'configbackend' => 'civicrm/admin/configtask',
be2fb01f
CW
52 ],
53 ];
04eee3f6 54
55 /**
56 * Get the final, usable URL string (after interpolating any variables)
57 *
58 * @return FALSE|string
59 */
60 public function gettingStartedUrl() {
61 // Note: We use "*default*" as the default (rather than self::GETTING_STARTED_URL) so that future
62 // developers can change GETTING_STARTED_URL without needing to update {civicrm_setting}.
1f957d1e 63 $url = Civi::settings()->get('gettingStartedUrl');
04eee3f6 64 if ($url === '*default*') {
65 $url = self::GETTING_STARTED_URL;
66 }
67 return CRM_Utils_System::evalUrl($url);
68 }
69
70 /**
71 * List gettingStarted page as dashlet.
72 */
73 public function run() {
edc80cda 74 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'dashlet');
04eee3f6 75
7e90e573
JL
76 // Assign smarty variables.
77 $this->assign('context', $context);
04eee3f6 78 $this->assign('gettingStarted', $this->_gettingStarted());
79
7e90e573 80 // Use smarty to generate page.
04eee3f6 81 return parent::run();
82 }
83
84 /**
85 * Load gettingStarted page from cache.
86 * Refresh cache if expired
87 *
88 * @return array
89 */
90 private function _gettingStarted() {
a03490f5
ML
91 $value = Civi::cache('community_messages')->get('dashboard_gettingStarted');
92
93 if (!$value) {
94 $value = $this->_getHtml($this->gettingStartedUrl());
95
96 if ($value) {
97 Civi::cache('community_messages')->set('dashboard_gettingStarted', $value, (60 * 60 * 24 * self::CACHE_DAYS));
04eee3f6 98 }
04eee3f6 99 }
a03490f5
ML
100
101 return $value;
04eee3f6 102 }
103
104 /**
a03490f5 105 * Get html.
04eee3f6 106 *
107 * @param $url
108 *
109 * @return array|NULL
110 * array of gettingStarted items; or NULL if not available
111 */
112 public function _getHtml($url) {
04eee3f6 113 $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT);
114 list ($status, $html) = $httpClient->get($url);
a03490f5 115
04eee3f6 116 if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
117 return NULL;
118 }
4175ee03 119
04eee3f6 120 $tokensList = CRM_Utils_Token::getTokens($html);
121 $this->replaceLinkToken($tokensList, $html);
04eee3f6 122 return $html;
123 }
124
04eee3f6 125 /**
126 * @param array $tokensList
4175ee03 127 * @param string $str
04eee3f6 128 *
4175ee03 129 */
04eee3f6 130 public function replaceLinkToken($tokensList, &$str) {
4175ee03 131 foreach ($tokensList as $categories => $tokens) {
132 foreach ($tokens as $token) {
04eee3f6 133 $value = '';
134 if (!empty(self::$_tokens[$categories][$token])) {
135 $value = self::$_tokens[$categories][$token];
136 if ($categories == 'crmurl') {
5c4a9e59 137 $value = CRM_Utils_System::url($value, "reset=1");
04eee3f6 138 }
139 }
140 CRM_Utils_Token::token_replace($categories, $token, $value, $str);
141 }
142 }
143 }
144
145}