GettingStarted.php 4.59 KB
Newer Older
sunil's avatar
sunil committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
sunil's avatar
sunil committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
sunil's avatar
sunil committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
 */

/**
 *
 * @package CRM
Seamus Lee's avatar
Seamus Lee committed
31
 * @copyright CiviCRM LLC (c) 2004-2019
sunil's avatar
sunil committed
32 33 34 35 36 37 38 39 40 41
 * $Id$
 *
 */

/**
 * Main page for getting started dashlet
 */
class CRM_Dashlet_Page_GettingStarted extends CRM_Core_Page {

  const CHECK_TIMEOUT = 5;
sunil's avatar
sunil committed
42
  const CACHE_DAYS = 5;
sunil's avatar
sunil committed
43 44 45 46
  const GETTING_STARTED_URL = 'https://alert.civicrm.org/welcome?prot=1&ver={ver}&uf={uf}&sid={sid}&lang={lang}&co={co}';

  /**
   * Define tokens available for getting started
47
   * @var array
sunil's avatar
sunil committed
48
   */
49
  public static $_tokens = [
50
    'crmurl' => [
sunil's avatar
sunil committed
51
      'configbackend' => 'civicrm/admin/configtask',
52 53
    ],
  ];
sunil's avatar
sunil committed
54 55 56 57 58 59 60 61 62

  /**
   * Get the final, usable URL string (after interpolating any variables)
   *
   * @return FALSE|string
   */
  public function gettingStartedUrl() {
    // Note: We use "*default*" as the default (rather than self::GETTING_STARTED_URL) so that future
    // developers can change GETTING_STARTED_URL without needing to update {civicrm_setting}.
63
    $url = Civi::settings()->get('gettingStartedUrl');
sunil's avatar
sunil committed
64 65 66 67 68 69 70 71 72 73
    if ($url === '*default*') {
      $url = self::GETTING_STARTED_URL;
    }
    return CRM_Utils_System::evalUrl($url);
  }

  /**
   * List gettingStarted page as dashlet.
   */
  public function run() {
74
    $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'dashlet');
sunil's avatar
sunil committed
75

76 77
    // Assign smarty variables.
    $this->assign('context', $context);
sunil's avatar
sunil committed
78 79
    $this->assign('gettingStarted', $this->_gettingStarted());

80
    // Use smarty to generate page.
sunil's avatar
sunil committed
81 82 83 84 85 86 87 88 89 90
    return parent::run();
  }

  /**
   * Load gettingStarted page from cache.
   * Refresh cache if expired
   *
   * @return array
   */
  private function _gettingStarted() {
91 92 93 94 95 96 97
    $value = Civi::cache('community_messages')->get('dashboard_gettingStarted');

    if (!$value) {
      $value = $this->_getHtml($this->gettingStartedUrl());

      if ($value) {
        Civi::cache('community_messages')->set('dashboard_gettingStarted', $value, (60 * 60 * 24 * self::CACHE_DAYS));
sunil's avatar
sunil committed
98 99
      }
    }
100 101

    return $value;
sunil's avatar
sunil committed
102 103 104
  }

  /**
105
   * Get html.
sunil's avatar
sunil committed
106 107 108 109 110 111 112 113 114
   *
   * @param $url
   *
   * @return array|NULL
   *   array of gettingStarted items; or NULL if not available
   */
  public function _getHtml($url) {
    $httpClient = new CRM_Utils_HttpClient(self::CHECK_TIMEOUT);
    list ($status, $html) = $httpClient->get($url);
115

sunil's avatar
sunil committed
116 117 118
    if ($status !== CRM_Utils_HttpClient::STATUS_OK) {
      return NULL;
    }
sunil's avatar
sunil committed
119

sunil's avatar
sunil committed
120 121 122 123 124 125 126
    $tokensList = CRM_Utils_Token::getTokens($html);
    $this->replaceLinkToken($tokensList, $html);
    return $html;
  }

  /**
   * @param array $tokensList
sunil's avatar
sunil committed
127
   * @param string $str
sunil's avatar
sunil committed
128
   *
sunil's avatar
sunil committed
129
   */
sunil's avatar
sunil committed
130
  public function replaceLinkToken($tokensList, &$str) {
sunil's avatar
sunil committed
131 132
    foreach ($tokensList as $categories => $tokens) {
      foreach ($tokens as $token) {
sunil's avatar
sunil committed
133 134 135 136
        $value = '';
        if (!empty(self::$_tokens[$categories][$token])) {
          $value = self::$_tokens[$categories][$token];
          if ($categories == 'crmurl') {
137
            $value = CRM_Utils_System::url($value, "reset=1");
sunil's avatar
sunil committed
138 139 140 141 142 143 144 145
          }
        }
        CRM_Utils_Token::token_replace($categories, $token, $value, $str);
      }
    }
  }

}