Merge pull request #1868 from dlobo/CRM-13665
[civicrm-core.git] / CRM / Core / Resources.php
index a86cfab4679ac3ab3cd270c012af86261217032f..d2e79f229eb4cb5824ec91efbafdeefa62cbc26a 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -195,6 +195,14 @@ class CRM_Core_Resources {
   /**
    * Add JavaScript variables to the global CRM object.
    *
+   * Example:
+   * From the server:
+   * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('foo' => 'bar')));
+   * From javascript:
+   * CRM.myNamespace.foo // "bar"
+   *
+   * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference
+   *
    * @param $settings array
    * @return CRM_Core_Resources
    */
@@ -202,10 +210,11 @@ class CRM_Core_Resources {
     $this->settings = $this->mergeSettings($this->settings, $settings);
     if (!$this->addedSettings) {
       $resources = $this;
-      CRM_Core_Region::instance('settings')->add(array(
+      CRM_Core_Region::instance('html-header')->add(array(
         'callback' => function(&$snippet, &$html) use ($resources) {
           $html .= "\n" . $resources->renderSetting();
-        }
+        },
+        'weight' => -100000,
       ));
       $this->addedSettings = TRUE;
     }
@@ -213,17 +222,21 @@ class CRM_Core_Resources {
   }
 
   /**
-   * Add JavaScript variables to the global CRM object.
+   * Add JavaScript variables to the global CRM object via a callback function.
    *
    * @param $callable function
    * @return CRM_Core_Resources
    */
   public function addSettingsFactory($callable) {
-    $this->addSetting(array()); // ensure that 'settings' region is created, even if empty
+    // Make sure our callback has been registered
+    $this->addSetting(array());
     $this->settingsFactories[] = $callable;
     return $this;
   }
 
+  /**
+   * Helper fn for addSettingsFactory
+   */
   public function getSettings() {
     $result = $this->settings;
     foreach ($this->settingsFactories as $callable) {
@@ -251,23 +264,18 @@ class CRM_Core_Resources {
    * Helper fn for addSetting
    * Render JavaScript variables for the global CRM object.
    *
-   * Example:
-   * From the server:
-   * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('foo' => 'bar')));
-   * From javascript:
-   * CRM.myNamespace.foo // "bar"
-   *
    * @return string
    */
   public function renderSetting() {
-    return 'CRM = cj.extend(true, ' . json_encode($this->getSettings()) . ', CRM);';
+    $js = 'var CRM = ' . json_encode($this->getSettings()) . ';';
+    return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js);
   }
 
   /**
    * Add translated string to the js CRM object.
    * It can then be retrived from the client-side ts() function
    * Variable substitutions can happen from client-side
-   * 
+   *
    * Note: this function rarely needs to be called directly and is mostly for internal use.
    * @see CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files
    *
@@ -425,7 +433,9 @@ class CRM_Core_Resources {
       $jsWeight = -9999;
       foreach ($files as $file => $type) {
         if ($type == 'js') {
-          $this->addScriptFile('civicrm', $file, $jsWeight++, $region, FALSE);
+          // Don't bother  looking for ts() calls in packages, there aren't any
+          $translate = (substr($file, 0, 9) != 'packages/');
+          $this->addScriptFile('civicrm', $file, $jsWeight++, $region, $translate);
         }
         elseif ($type == 'css') {
           $this->addStyleFile('civicrm', $file, -100, $region);
@@ -443,6 +453,20 @@ class CRM_Core_Resources {
         }
       }
 
+      // Initialize CRM.url and CRM.formatMoney
+      $url = CRM_Utils_System::url('civicrm/example', 'placeholder', FALSE, NULL, FALSE);
+      $js = "CRM.url('init', '$url');\n";
+      $js .= "CRM.formatMoney('init', " . json_encode(CRM_Utils_Money::format(1234.56)) . ");";
+      $this->addScript($js, $jsWeight++, $region);
+
+      // Add global settings
+      $settings = array(
+        'userFramework' => $config->userFramework,
+        'resourceBase' => $config->resourceBase,
+        'lcMessages' => $config->lcMessages,
+      );
+      $this->addSetting(array('config' => $settings));
+
       // Give control of jQuery back to the CMS - this loads last
       $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE);