public function addSetting($settings) {
$this->settings = $this->mergeSettings($this->settings, $settings);
if (!$this->addedSettings) {
+ $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header';
$resources = $this;
- CRM_Core_Region::instance('html-header')->add(array(
+ CRM_Core_Region::instance($region)->add(array(
'callback' => function(&$snippet, &$html) use ($resources) {
$html .= "\n" . $resources->renderSetting();
},
* @return string
*/
public function renderSetting() {
- $js = 'var CRM = ' . json_encode($this->getSettings()) . ';';
+ // On a standard page request we construct the CRM object from scratch
+ if (!self::isAjaxMode()) {
+ $js = 'var CRM = ' . json_encode($this->getSettings()) . ';';
+ }
+ // For an ajax request we append to it
+ else {
+ $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');';
+ }
return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js);
}
/**
* @param $value
+ * @return CRM_Core_Resources
*/
public function setCacheCode($value) {
$this->cacheCode = $value;
if ($this->cacheCodeKey) {
CRM_Core_BAO_Setting::setItem($value, CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, $this->cacheCodeKey);
}
+ return $this;
}
+ /**
+ * @return CRM_Core_Resources
+ */
public function resetCacheCode() {
$this->setCacheCode(CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC));
+ // Also flush cms resource cache if needed
+ CRM_Core_Config::singleton()->userSystem->clearResourceCache();
+ return $this;
}
/**
* @access public
*/
public function addCoreResources($region = 'html-header') {
- if (!isset($this->addedCoreResources[$region])) {
+ if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) {
$this->addedCoreResources[$region] = TRUE;
$config = CRM_Core_Config::singleton();
}
}
- // 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(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $config->lcMessages, array('r' => $this->getCacheCode())), $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));
+ 'isFrontend' => $config->userFrameworkFrontend,
+ ));
+ // 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);
/**
* Flushes cached translated strings
+ * @return CRM_Core_Resources
*/
public function flushStrings() {
$this->cache->flush();
+ return $this;
}
/**
}
/**
- * Add inline scripts needed to localize js widgets
- * @param string $js
+ * Create dynamic script for localizing js widgets
+ *
+ * @return string javascript content
*/
- function addLocalization(&$js) {
+ static function outputLocalizationJS() {
+ CRM_Core_Page_AJAX::setJsHeaders();
$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())));
- }
+ $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(),
+ );
+ print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars);
+ CRM_Utils_System::civiExit();
}
/**
// 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",
"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",
$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";
}
+ // JS for multilingual installations
+ if (!empty($config->languageLimit) && count($config->languageLimit) > 1 && CRM_Core_Permission::check('translate CiviCRM')) {
+ $items[] = "js/crm.multilingual.js";
+ }
+
// Enable administrators to edit option lists in a dialog
if (CRM_Core_Permission::check('administer CiviCRM') && $this->ajaxPopupsEnabled) {
$items[] = "js/crm.optionEdit.js";
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;
}
}
}
+
+ // CMS-specific resources
+ $config->userSystem->appendCoreResources($items);
+
return $items;
}
+
+ /**
+ * @return bool - is this page request an ajax snippet?
+ */
+ static function isAjaxMode() {
+ return in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array(CRM_Core_Smarty::PRINT_SNIPPET, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON));
+ }
}