CRM-15172 - Fix profile state/county required options
[civicrm-core.git] / CRM / Core / Resources.php
index 4661484d287dc9e5d3b394297d9bb6e4b5dfb82b..b1f0df1c901a19507e8419aa33393f6ee06acbfc 100644 (file)
@@ -417,10 +417,16 @@ class CRM_Core_Resources {
     return $this->extMapper->keyToUrl($ext) . '/' . $file;
   }
 
+  /**
+   * @return string
+   */
   public function getCacheCode() {
     return $this->cacheCode;
   }
 
+  /**
+   * @param $value
+   */
   public function setCacheCode($value) {
     $this->cacheCode = $value;
     if ($this->cacheCodeKey) {
@@ -461,24 +467,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->addLocalization($js);
-      $this->addScript($js, $jsWeight++, $region);
+      // Dynamic localization script
+      $this->addScriptUrl($this->addLocalizationJs(), $jsWeight++, $region);
 
       // Add global settings
-      $settings = array(
-        'userFramework' => $config->userFramework,
-        'resourceBase' => $config->resourceBase,
-        'lcMessages' => $config->lcMessages,
+      $settings = array('config' => array(
         'ajaxPopupsEnabled' => $this->ajaxPopupsEnabled,
-      );
-      $this->addSetting(array('config' => $settings));
+      ));
+      // Disable profile creation if user lacks permission
+      if (!CRM_Core_Permission::check('edit all contacts') && !CRM_Core_Permission::check('add contacts')) {
+        $settings['profileCreate'] = FALSE;
+      }
+      $this->addSetting($settings);
 
-      // Give control of jQuery back to the CMS - this loads last
+      // Give control of jQuery and _ back to the CMS - this loads last
       $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE);
 
       $this->addCoreStyles($region);
@@ -545,26 +547,34 @@ class CRM_Core_Resources {
   }
 
   /**
-   * Add inline scripts needed to localize js widgets
-   * @param string $js
+   * Add dynamic l10n js
+   *
+   * @return string URL of JS file
    */
-  function addLocalization(&$js) {
+  private function addLocalizationJs() {
     $config = CRM_Core_Config::singleton();
-
-    // Localize select2 strings
-    $contactSearch = json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...'));
-    $otherSearch = json_encode(ts('Enter search term...'));
-    $js .= "
-      $.fn.select2.defaults.formatNoMatches = " . json_encode(ts("None found.")) . ";
-      $.fn.select2.defaults.formatLoadMore = " . json_encode(ts("Loading...")) . ";
-      $.fn.select2.defaults.formatSearching = " . json_encode(ts("Searching...")) . ";
-      $.fn.select2.defaults.formatInputTooShort = function(){return CRM.$(this).data('api-entity') == 'contact' ? $contactSearch : $otherSearch};
-    ";
-
-    // Contact create profiles with localized names
-    if (CRM_Core_Permission::check('edit all contacts') || CRM_Core_Permission::check('add contacts')) {
-      $this->addSetting(array('profile' => array('contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks())));
+    $fileName = 'l10n-' . $config->lcMessages . '.js';
+    if (!is_file(CRM_Utils_File::dynamicResourcePath($fileName))) {
+      CRM_Utils_File::addDynamicResource($fileName, $this->createLocalizationJs());
     }
+    // Dynamic localization script
+    return CRM_Utils_File::dynamicResourceUrl($fileName);
+  }
+
+  /**
+   * Create dynamic script for localizing js widgets
+   *
+   * @return string javascript content
+   */
+  private function createLocalizationJs() {
+    $config = CRM_Core_Config::singleton();
+    $vars = array(
+      'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)),
+      'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')),
+      'otherSearch' => json_encode(ts('Enter search term...')),
+      'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(),
+    );
+    return CRM_Core_Smarty::singleton()->fetchWith('CRM/common/localization.js.tpl', $vars);
   }
 
   /**
@@ -581,9 +591,9 @@ class CRM_Core_Resources {
     // Scripts needed by everyone, everywhere
     // FIXME: This is too long; list needs finer-grained segmentation
     $items = array(
-      "packages/jquery/jquery-1.11.0$min.js",
-      "packages/jquery/jquery-ui/js/jquery-ui-1.10.4.custom$min.js",
-      "packages/jquery/jquery-ui/css/theme/jquery-ui-1.10.4.custom$min.css",
+      "packages/jquery/jquery-1.11.1$min.js",
+      "packages/jquery/jquery-ui/jquery-ui$min.js",
+      "packages/jquery/jquery-ui/jquery-ui$min.css",
 
       "packages/backbone/lodash.compat$min.js",
 
@@ -600,7 +610,10 @@ class CRM_Core_Resources {
 
       "packages/jquery/plugins/jquery.timeentry$min.js",
 
+      "packages/jquery/plugins/jquery.blockUI$min.js",
+
       "packages/jquery/plugins/DataTables/media/js/jquery.dataTables$min.js",
+      "packages/jquery/plugins/DataTables/media/css/jquery.dataTables$min.css",
 
       "packages/jquery/plugins/jquery.validate$min.js",
       "packages/jquery/plugins/jquery.ui.datepicker.validation.pack.js",
@@ -614,7 +627,6 @@ class CRM_Core_Resources {
       $items[] = "packages/jquery/plugins/jquery.menu$min.js";
       $items[] = "packages/jquery/css/menu.css";
       $items[] = "packages/jquery/plugins/jquery.jeditable$min.js";
-      $items[] = "packages/jquery/plugins/jquery.blockUI$min.js";
       $items[] = "packages/jquery/plugins/jquery.notify$min.js";
       $items[] = "js/jquery/jquery.crmeditable.js";
     }
@@ -628,9 +640,9 @@ class CRM_Core_Resources {
     if ($config->lcMessages && $config->lcMessages != 'en_US') {
       // Search for i18n file in order of specificity (try fr-CA, then fr)
       list($lang) = explode('_', $config->lcMessages);
-      $path = "packages/jquery/jquery-ui/development-bundle/ui/" . ($min ? 'minified/' : '') . "i18n";
+      $path = "packages/jquery/jquery-ui/i18n";
       foreach (array(str_replace('_', '-', $config->lcMessages), $lang) as $language) {
-        $localizationFile = "$path/jquery.ui.datepicker-{$language}{$min}.js";
+        $localizationFile = "$path/datepicker-{$language}.js";
         if ($this->getPath('civicrm', $localizationFile)) {
           $items[] = $localizationFile;
           break;