<?php
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.3 |
+ | CiviCRM version 4.4 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
*/
class CRM_Core_CommunityMessages {
- const DEFAULT_MESSAGES_URL = 'http://alert.civicrm.org/alert?prot=1&ver={ver}&uf={uf}&sid={sid}';
+ const DEFAULT_MESSAGES_URL = 'https://alert.civicrm.org/alert?prot=1&ver={ver}&uf={uf}&sid={sid}&lang={lang}&co={co}';
const DEFAULT_PERMISSION = 'administer CiviCRM';
/**
*/
protected $messagesUrl;
+ /**
+ * Create default instance
+ *
+ * @return CRM_Core_CommunityMessages
+ */
+ public static function create() {
+ return new CRM_Core_CommunityMessages(
+ new CRM_Utils_Cache_SqlGroup(array(
+ 'group' => 'community-messages',
+ 'prefetch' => FALSE,
+ )),
+ CRM_Utils_HttpClient::singleton()
+ );
+ }
+
/**
* @param CRM_Utils_Cache_Interface $cache
* @param CRM_Utils_HttpClient $client
$this->cache = $cache;
$this->client = $client;
if ($messagesUrl === NULL) {
- $this->messagesUrl = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'community_messages_url', NULL, self::DEFAULT_MESSAGES_URL);
+ $this->messagesUrl = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'communityMessagesUrl', NULL, '*default*');
}
else {
$this->messagesUrl = $messagesUrl;
}
+ if ($this->messagesUrl === '*default*') {
+ $this->messagesUrl = self::DEFAULT_MESSAGES_URL;
+ }
}
/**
* @return NULL|array
*/
public function getDocument() {
- if ($this->messagesUrl === FALSE) {
- return NULL;
- }
-
$isChanged = FALSE;
$document = $this->cache->get('communityMessages');
}
if ($document['expires'] <= CRM_Utils_Time::getTimeRaw()) {
- $newDocument = $this->fetchDocument($this->messagesUrl);
+ $newDocument = $this->fetchDocument();
if ($newDocument && $this->validateDocument($newDocument)) {
$document = $newDocument;
$document['expires'] = CRM_Utils_Time::getTimeRaw() + $document['ttl'];
/**
* Download document from URL and parse as JSON
*
- * @param string $url
* @return NULL|array parsed JSON
*/
- public function fetchDocument($url) {
- list($status, $json) = $this->client->get(CRM_Utils_System::evalUrl($url));
+ public function fetchDocument() {
+ list($status, $json) = $this->client->get($this->getRenderedUrl());
if ($status != CRM_Utils_HttpClient::STATUS_OK || empty($json)) {
return NULL;
}
return $doc;
}
+ /**
+ * Get the final, usable URL string (after interpolating any variables)
+ *
+ * @return FALSE|string
+ */
+ public function getRenderedUrl() {
+ return CRM_Utils_System::evalUrl($this->messagesUrl);
+ }
+
+ /**
+ * @return bool
+ */
+ public function isEnabled() {
+ return $this->messagesUrl !== FALSE && $this->messagesUrl !== 'FALSE';
+ }
+
/**
* Pick a message to display
*
);
$vars = array();
foreach ($vals as $k => $v) {
- $vars['%%'.$k.'%%'] = $v;
- $vars['{{'.$k.'}}'] = urlencode($v);
+ $vars['%%' . $k . '%%'] = $v;
+ $vars['{{' . $k . '}}'] = urlencode($v);
}
return strtr($markup, $vars);
}