CRM-12193 - Register setting "communityMessagesUrl"; extract methods "isEnabled"...
authorTim Otten <totten@civicrm.org>
Sat, 30 Mar 2013 03:06:44 +0000 (23:06 -0400)
committerTim Otten <totten@civicrm.org>
Sat, 30 Mar 2013 03:19:27 +0000 (23:19 -0400)
----------------------------------------
* CRM-12193: In-app fundraising for CiviCRM
  http://issues.civicrm.org/jira/browse/CRM-12193

CRM/Core/CommunityMessages.php
settings/Core.setting.php
tests/phpunit/CRM/Core/CommunityMessagesTest.php

index 8df5a0c41ce6a1aa5e36e2f5ac1c65cd498c8c0a..77b241d4019db1d74cd97cf8779e9d7179b925ea 100644 (file)
@@ -53,6 +53,21 @@ class CRM_Core_CommunityMessages {
    */
   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
@@ -61,11 +76,14 @@ class CRM_Core_CommunityMessages {
     $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;
+    }
   }
 
   /**
@@ -74,10 +92,6 @@ class CRM_Core_CommunityMessages {
    * @return NULL|array
    */
   public function getDocument() {
-    if ($this->messagesUrl === FALSE) {
-      return NULL;
-    }
-
     $isChanged = FALSE;
     $document = $this->cache->get('communityMessages');
 
@@ -92,7 +106,7 @@ class CRM_Core_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'];
@@ -114,11 +128,10 @@ class CRM_Core_CommunityMessages {
   /**
    * 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;
     }
@@ -129,6 +142,22 @@ class CRM_Core_CommunityMessages {
     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
    *
@@ -180,8 +209,8 @@ class CRM_Core_CommunityMessages {
     );
     $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);
   }
index af2b70d8696976ef95f90627339c2fd8b5c62921..8e7a982e7e2fff91c9c1089ceecbd9680a45cebe 100644 (file)
@@ -488,6 +488,28 @@ When enabled, statistics about your CiviCRM installation are reported anonymousl
     'description' => null,
     'help_text' => null,
   ),
+  'communityMessagesUrl' => array(
+    'group_name' => 'CiviCRM Preferences',
+    'group' => 'core',
+    'name' => 'communityMessagesUrl',
+    'prefetch' => 0,
+    'config_only'=> 1,
+    'type' => 'String',
+    'quick_form_type' => 'Element',
+    'html_type' => 'text',
+    'html_attributes' => array(
+      'size' => 64,
+      'maxlength' => 128,
+    ),
+    'html_type' => 'Text',
+    'default' => '*default*',
+    'add' => '4.3',
+    'title' => 'Community Messages URL',
+    'is_domain' => 1,
+    'is_contact' => 0,
+    'description' => 'Service providing CiviCRM community messages',
+    'help_text' => 'Use "*default*" for the system default or override with a custom URL',
+  ),
   'resCacheCode' => array(
     'group_name' => 'CiviCRM Preferences',
     'group' => 'core',
index ab4c2cfad5f526a199ec090bd249521e9fe7ba7b..53b4459d3e996634e6c3481a5ad98ae2137c94a6 100644 (file)
@@ -147,14 +147,21 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase {
     return $result;
   }
 
-  public function testGetDocument_disabled() {
+  public function testIsEnabled() {
+    $communityMessages = new CRM_Core_CommunityMessages(
+      $this->cache,
+      $this->expectNoHttpRequest()
+    );
+    $this->assertTrue($communityMessages->isEnabled());
+  }
+
+  public function testIsEnabled_false() {
     $communityMessages = new CRM_Core_CommunityMessages(
       $this->cache,
       $this->expectNoHttpRequest(),
       FALSE
     );
-    $doc = $communityMessages->getDocument();
-    $this->assertTrue(NULL === $doc);
+    $this->assertFalse($communityMessages->isEnabled());
   }
 
   /**