Merge pull request #4579 from colemanw/CRM_Utils_Array
[civicrm-core.git] / CRM / Core / Resources.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 * This class facilitates the loading of resources
30 * such as JavaScript files and CSS files.
31 *
32 * Any URLs generated for resources may include a 'cache-code'. By resetting the
33 * cache-code, one may force clients to re-download resource files (regardless of
34 * any HTTP caching rules).
35 *
36 * TODO: This is currently a thin wrapper over CRM_Core_Region. We
37 * should incorporte services for aggregation, minimization, etc.
38 *
39 * @package CRM
06b69b18 40 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
41 * $Id$
42 *
43 */
44class CRM_Core_Resources {
45 const DEFAULT_WEIGHT = 0;
46 const DEFAULT_REGION = 'page-footer';
47
48 /**
49 * We don't have a container or dependency-injection, so use singleton instead
50 *
51 * @var object
52 * @static
53 */
54 private static $_singleton = NULL;
55
56 /**
57 * @var CRM_Extension_Mapper
58 */
59 private $extMapper = NULL;
60
61 /**
62 * @var CRM_Utils_Cache_Interface
63 */
64 private $cache = NULL;
65
66 /**
67 * @var array free-form data tree
68 */
69 protected $settings = array();
70 protected $addedSettings = FALSE;
71
72 /**
73 * @var array of callables
74 */
75 protected $settingsFactories = array();
76
77 /**
78 * @var array ($regionName => bool)
79 */
80 protected $addedCoreResources = array();
81
82 /**
83 * @var array ($regionName => bool)
84 */
85 protected $addedCoreStyles = array();
86
87 /**
88 * @var string a value to append to JS/CSS URLs to coerce cache resets
89 */
90 protected $cacheCode = NULL;
91
92 /**
93 * @var string the name of a setting which persistently stores the cacheCode
94 */
95 protected $cacheCodeKey = NULL;
96
53f2643c
CW
97 /**
98 * @var bool
99 */
100 public $ajaxPopupsEnabled;
101
6a488035
TO
102 /**
103 * Get or set the single instance of CRM_Core_Resources
104 *
105 * @param $instance CRM_Core_Resources, new copy of the manager
106 * @return CRM_Core_Resources
107 */
108 static public function singleton(CRM_Core_Resources $instance = NULL) {
109 if ($instance !== NULL) {
110 self::$_singleton = $instance;
111 }
112 if (self::$_singleton === NULL) {
113 $sys = CRM_Extension_System::singleton();
114 $cache = new CRM_Utils_Cache_SqlGroup(array(
115 'group' => 'js-strings',
116 'prefetch' => FALSE,
117 ));
118 self::$_singleton = new CRM_Core_Resources(
119 $sys->getMapper(),
120 $cache,
eb897088 121 CRM_Core_Config::isUpgradeMode() ? NULL : 'resCacheCode'
6a488035
TO
122 );
123 }
124 return self::$_singleton;
125 }
126
127 /**
128 * Construct a resource manager
129 *
130 * @param CRM_Extension_Mapper $extMapper Map extension names to their base path or URLs.
3be754d6
TO
131 * @param CRM_Utils_Cache_Interface $cache JS-localization cache
132 * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources
6a488035
TO
133 */
134 public function __construct($extMapper, $cache, $cacheCodeKey = NULL) {
135 $this->extMapper = $extMapper;
136 $this->cache = $cache;
137 $this->cacheCodeKey = $cacheCodeKey;
138 if ($cacheCodeKey !== NULL) {
139 $this->cacheCode = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, $cacheCodeKey);
140 }
150f50c1 141 if (!$this->cacheCode) {
6a488035
TO
142 $this->resetCacheCode();
143 }
53f2643c
CW
144 $this->ajaxPopupsEnabled = (bool) CRM_Core_BAO_Setting::getItem(
145 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE
146 );
6a488035
TO
147 }
148
149 /**
150 * Add a JavaScript file to the current page using <SCRIPT SRC>.
151 *
152 * @param $ext string, extension name; use 'civicrm' for core
153 * @param $file string, file path -- relative to the extension base dir
154 * @param $weight int, relative weight within a given region
155 * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
156 * @param $translate, whether to parse this file for strings enclosed in ts()
157 *
158 * @return CRM_Core_Resources
159 */
160 public function addScriptFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION, $translate = TRUE) {
161 if ($translate) {
162 $this->translateScript($ext, $file);
163 }
9ee32530
CW
164 // Look for non-minified version if we are in debug mode
165 if (CRM_Core_Config::singleton()->debug && strpos($file, '.min.js') !== FALSE) {
166 $nonMiniFile = str_replace('.min.js', '.js', $file);
167 if ($this->getPath($ext, $nonMiniFile)) {
168 $file = $nonMiniFile;
169 }
170 }
6a488035
TO
171 return $this->addScriptUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
172 }
173
174 /**
175 * Add a JavaScript file to the current page using <SCRIPT SRC>.
176 *
177 * @param $url string
178 * @param $weight int, relative weight within a given region
179 * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
180 * @return CRM_Core_Resources
181 */
182 public function addScriptUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
183 CRM_Core_Region::instance($region)->add(array(
184 'name' => $url,
185 'type' => 'scriptUrl',
186 'scriptUrl' => $url,
187 'weight' => $weight,
188 'region' => $region,
189 ));
190 return $this;
191 }
192
193 /**
194 * Add a JavaScript file to the current page using <SCRIPT SRC>.
195 *
196 * @param $code string, JavaScript source code
197 * @param $weight int, relative weight within a given region
198 * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
199 * @return CRM_Core_Resources
200 */
201 public function addScript($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
202 CRM_Core_Region::instance($region)->add(array(
203 // 'name' => automatic
204 'type' => 'script',
205 'script' => $code,
206 'weight' => $weight,
207 'region' => $region,
208 ));
209 return $this;
210 }
211
212 /**
213 * Add JavaScript variables to the global CRM object.
214 *
132fc68e
CW
215 * Example:
216 * From the server:
217 * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('foo' => 'bar')));
218 * From javascript:
219 * CRM.myNamespace.foo // "bar"
220 *
01478033 221 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference
132fc68e 222 *
6a488035
TO
223 * @param $settings array
224 * @return CRM_Core_Resources
225 */
226 public function addSetting($settings) {
227 $this->settings = $this->mergeSettings($this->settings, $settings);
228 if (!$this->addedSettings) {
88cd8875 229 $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header';
6a488035 230 $resources = $this;
156fd9b9 231 CRM_Core_Region::instance($region)->add(array(
6a488035
TO
232 'callback' => function(&$snippet, &$html) use ($resources) {
233 $html .= "\n" . $resources->renderSetting();
8cb324cc
CW
234 },
235 'weight' => -100000,
6a488035
TO
236 ));
237 $this->addedSettings = TRUE;
238 }
239 return $this;
240 }
241
242 /**
69847402 243 * Add JavaScript variables to the global CRM object via a callback function.
6a488035 244 *
3be754d6 245 * @param callable $callable
6a488035
TO
246 * @return CRM_Core_Resources
247 */
248 public function addSettingsFactory($callable) {
d937dbcd
CW
249 // Make sure our callback has been registered
250 $this->addSetting(array());
6a488035
TO
251 $this->settingsFactories[] = $callable;
252 return $this;
253 }
254
69847402
CW
255 /**
256 * Helper fn for addSettingsFactory
257 */
6a488035
TO
258 public function getSettings() {
259 $result = $this->settings;
260 foreach ($this->settingsFactories as $callable) {
261 $result = $this->mergeSettings($result, $callable());
262 }
263 return $result;
264 }
265
266 /**
267 * @param array $settings
268 * @param array $additions
269 * @return array combination of $settings and $additions
270 */
271 protected function mergeSettings($settings, $additions) {
272 foreach ($additions as $k => $v) {
273 if (isset($settings[$k]) && is_array($settings[$k]) && is_array($v)) {
274 $v += $settings[$k];
275 }
276 $settings[$k] = $v;
277 }
278 return $settings;
279 }
280
281 /**
282 * Helper fn for addSetting
283 * Render JavaScript variables for the global CRM object.
284 *
6a488035
TO
285 * @return string
286 */
287 public function renderSetting() {
156fd9b9
CW
288 // On a standard page request we construct the CRM object from scratch
289 if (!self::isAjaxMode()) {
290 $js = 'var CRM = ' . json_encode($this->getSettings()) . ';';
291 }
292 // For an ajax request we append to it
293 else {
294 $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');';
295 }
d759bd10 296 return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js);
6a488035
TO
297 }
298
299 /**
300 * Add translated string to the js CRM object.
301 * It can then be retrived from the client-side ts() function
302 * Variable substitutions can happen from client-side
8ef12e64 303 *
6a488035
TO
304 * Note: this function rarely needs to be called directly and is mostly for internal use.
305 * @see CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files
306 *
307 * Simple example:
308 * // From php:
309 * CRM_Core_Resources::singleton()->addString('Hello');
310 * // The string is now available to javascript code i.e.
311 * ts('Hello');
312 *
313 * Example with client-side substitutions:
314 * // From php:
315 * CRM_Core_Resources::singleton()->addString('Your %1 has been %2');
316 * // ts() in javascript works the same as in php, for example:
317 * ts('Your %1 has been %2', {1: objectName, 2: actionTaken});
318 *
319 * NOTE: This function does not work with server-side substitutions
320 * (as this might result in collisions and unwanted variable injections)
321 * Instead, use code like:
322 * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs)))));
323 * And from javascript access it at CRM.myNamespace.myString
324 *
325 * @param $text string|array
326 * @return CRM_Core_Resources
327 */
328 public function addString($text) {
329 foreach ((array) $text as $str) {
330 $translated = ts($str);
331 // We only need to push this string to client if the translation
332 // is actually different from the original
333 if ($translated != $str) {
334 $this->addSetting(array('strings' => array($str => $translated)));
335 }
336 }
337 return $this;
338 }
339
340 /**
341 * Add a CSS file to the current page using <LINK HREF>.
342 *
343 * @param $ext string, extension name; use 'civicrm' for core
344 * @param $file string, file path -- relative to the extension base dir
345 * @param $weight int, relative weight within a given region
346 * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
347 * @return CRM_Core_Resources
348 */
349 public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
350 return $this->addStyleUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
351 }
352
353 /**
354 * Add a CSS file to the current page using <LINK HREF>.
355 *
356 * @param $url string
357 * @param $weight int, relative weight within a given region
358 * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
359 * @return CRM_Core_Resources
360 */
361 public function addStyleUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
362 CRM_Core_Region::instance($region)->add(array(
363 'name' => $url,
364 'type' => 'styleUrl',
365 'styleUrl' => $url,
366 'weight' => $weight,
367 'region' => $region,
368 ));
369 return $this;
370 }
371
372 /**
373 * Add a CSS content to the current page using <STYLE>.
374 *
375 * @param $code string, CSS source code
376 * @param $weight int, relative weight within a given region
377 * @param $region string, location within the file; 'html-header', 'page-header', 'page-footer'
378 * @return CRM_Core_Resources
379 */
380 public function addStyle($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
381 CRM_Core_Region::instance($region)->add(array(
382 // 'name' => automatic
383 'type' => 'style',
384 'style' => $code,
385 'weight' => $weight,
386 'region' => $region,
387 ));
388 return $this;
389 }
390
391 /**
392 * Determine file path of a resource provided by an extension
393 *
394 * @param $ext string, extension name; use 'civicrm' for core
395 * @param $file string, file path -- relative to the extension base dir
396 *
2a6da8d7 397 * @return bool|string (string|bool), full file path or FALSE if not found
6a488035
TO
398 */
399 public function getPath($ext, $file) {
400 // TODO consider caching results
401 $path = $this->extMapper->keyToBasePath($ext) . '/' . $file;
402 if (is_file($path)) {
403 return $path;
404 }
405 return FALSE;
406 }
407
408 /**
409 * Determine public URL of a resource provided by an extension
410 *
411 * @param $ext string, extension name; use 'civicrm' for core
412 * @param $file string, file path -- relative to the extension base dir
2a6da8d7
EM
413 * @param bool $addCacheCode
414 *
6a488035
TO
415 * @return string, URL
416 */
417 public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) {
418 if ($file === NULL) {
419 $file = '';
420 }
421 if ($addCacheCode) {
422 $file .= '?r=' . $this->getCacheCode();
423 }
424 // TODO consider caching results
425 return $this->extMapper->keyToUrl($ext) . '/' . $file;
426 }
427
a0ee3941
EM
428 /**
429 * @return string
430 */
6a488035
TO
431 public function getCacheCode() {
432 return $this->cacheCode;
433 }
434
a0ee3941
EM
435 /**
436 * @param $value
5badddc3 437 * @return CRM_Core_Resources
a0ee3941 438 */
6a488035
TO
439 public function setCacheCode($value) {
440 $this->cacheCode = $value;
441 if ($this->cacheCodeKey) {
442 CRM_Core_BAO_Setting::setItem($value, CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, $this->cacheCodeKey);
443 }
9762f6ff 444 return $this;
6a488035
TO
445 }
446
5badddc3
CW
447 /**
448 * @return CRM_Core_Resources
449 */
6a488035
TO
450 public function resetCacheCode() {
451 $this->setCacheCode(CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC));
f091327b
CW
452 // Also flush cms resource cache if needed
453 CRM_Core_Config::singleton()->userSystem->clearResourceCache();
9762f6ff 454 return $this;
6a488035
TO
455 }
456
457 /**
458 * This adds CiviCRM's standard css and js to the specified region of the document.
459 * It will only run once.
460 *
461 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
462 * (like addCoreResources/addCoreStyles).
463 *
2a6da8d7 464 * @param string $region
6a488035
TO
465 * @return CRM_Core_Resources
466 * @access public
467 */
468 public function addCoreResources($region = 'html-header') {
156fd9b9 469 if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) {
6a488035
TO
470 $this->addedCoreResources[$region] = TRUE;
471 $config = CRM_Core_Config::singleton();
472
a43b1583 473 // Add resources from coreResourceList
6a488035 474 $jsWeight = -9999;
5409628b 475 foreach ($this->coreResourceList() as $file) {
a43b1583 476 if (substr($file, -2) == 'js') {
74227f77
CW
477 // Don't bother looking for ts() calls in packages, there aren't any
478 $translate = (substr($file, 0, 9) != 'packages/');
479 $this->addScriptFile('civicrm', $file, $jsWeight++, $region, $translate);
6a488035 480 }
a43b1583 481 else {
6a488035
TO
482 $this->addStyleFile('civicrm', $file, -100, $region);
483 }
484 }
485
4e56e743 486 // Dynamic localization script
4cc9b813 487 $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $config->lcMessages, array('r' => $this->getCacheCode())), $jsWeight++, $region);
4e56e743 488
d759bd10 489 // Add global settings
4e56e743 490 $settings = array('config' => array(
53f2643c 491 'ajaxPopupsEnabled' => $this->ajaxPopupsEnabled,
8d83b77c 492 'isFrontend' => $config->userFrameworkFrontend,
4e56e743
CW
493 ));
494 // Disable profile creation if user lacks permission
495 if (!CRM_Core_Permission::check('edit all contacts') && !CRM_Core_Permission::check('add contacts')) {
496 $settings['profileCreate'] = FALSE;
3d527838 497 }
4e56e743 498 $this->addSetting($settings);
3d527838
CW
499
500 // Give control of jQuery and _ back to the CMS - this loads last
c2777586 501 $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE);
6a488035
TO
502
503 $this->addCoreStyles($region);
504 }
505 return $this;
506 }
507
508 /**
509 * This will add CiviCRM's standard CSS
510 *
511 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
512 * (like addCoreResources/addCoreStyles).
513 *
514 * @param string $region
515 * @return CRM_Core_Resources
516 */
517 public function addCoreStyles($region = 'html-header') {
518 if (!isset($this->addedCoreStyles[$region])) {
519 $this->addedCoreStyles[$region] = TRUE;
520
521 // Load custom or core css
522 $config = CRM_Core_Config::singleton();
523 if (!empty($config->customCSSURL)) {
57018a3e 524 $this->addStyleUrl($config->customCSSURL, 99, $region);
6a488035 525 }
14f20d22 526 if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'disable_core_css')) {
6a488035 527 $this->addStyleFile('civicrm', 'css/civicrm.css', -99, $region);
6a488035
TO
528 }
529 }
530 return $this;
531 }
532
627668e8
CW
533 /**
534 * Flushes cached translated strings
5badddc3 535 * @return CRM_Core_Resources
627668e8
CW
536 */
537 public function flushStrings() {
538 $this->cache->flush();
9762f6ff
CW
539 return $this;
540 }
541
6a488035
TO
542 /**
543 * Translate strings in a javascript file
544 *
545 * @param $ext string, extension name
546 * @param $file string, file path
547 * @return void
548 */
549 private function translateScript($ext, $file) {
550 // For each extension, maintain one cache record which
551 // includes parsed (translatable) strings for all its JS files.
552 $stringsByFile = $this->cache->get($ext); // array($file => array(...strings...))
553 if (!$stringsByFile) {
554 $stringsByFile = array();
555 }
556 if (!isset($stringsByFile[$file])) {
557 $filePath = $this->getPath($ext, $file);
558 if ($filePath && is_readable($filePath)) {
559 $stringsByFile[$file] = CRM_Utils_JS::parseStrings(file_get_contents($filePath));
560 } else {
561 $stringsByFile[$file] = array();
562 }
563 $this->cache->set($ext, $stringsByFile);
564 }
565 $this->addString($stringsByFile[$file]);
566 }
567
19f7e35e 568 /**
0811b210
TO
569 * Create dynamic script for localizing js widgets
570 *
571 * @return string javascript content
19f7e35e 572 */
4cc9b813
CW
573 static function outputLocalizationJS() {
574 CRM_Core_Page_AJAX::setJsHeaders();
a88cf11a 575 $config = CRM_Core_Config::singleton();
3d527838
CW
576 $vars = array(
577 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)),
578 'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')),
579 'otherSearch' => json_encode(ts('Enter search term...')),
4e56e743 580 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(),
3d527838 581 );
3d4fb0ed 582 print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars);
4cc9b813 583 CRM_Utils_System::civiExit();
c66581f5
CW
584 }
585
6a488035 586 /**
a43b1583 587 * List of core resources we add to every CiviCRM page
6a488035 588 *
a43b1583 589 * @return array
6a488035 590 */
dbe0bbc6
CW
591 public function coreResourceList() {
592 $config = CRM_Core_Config::singleton();
593 // Use minified files for production, uncompressed in debug mode
9ee32530 594 // Note, $this->addScriptFile would automatically search for the non-minified file in debug mode but this is probably faster
dbe0bbc6 595 $min = $config->debug ? '' : '.min';
a43b1583 596
4968e732
CW
597 // Scripts needed by everyone, everywhere
598 // FIXME: This is too long; list needs finer-grained segmentation
a0c8b50e 599 $items = array(
f2f191fe
CW
600 "packages/jquery/jquery-1.11.1$min.js",
601 "packages/jquery/jquery-ui/jquery-ui$min.js",
602 "packages/jquery/jquery-ui/jquery-ui$min.css",
79ae07d9 603
a88cf11a 604 "packages/backbone/lodash.compat$min.js",
430b9048 605
c04d6c92 606 "packages/jquery/plugins/jquery.mousewheel$min.js",
b77d5b35 607
119934a7 608 "packages/jquery/plugins/select2/select2$min.js",
f7b92fcd
CW
609 "packages/jquery/plugins/select2/select2.css",
610
a0c8b50e 611 "packages/jquery/plugins/jquery.tableHeader.js",
a43b1583 612
a0c8b50e 613 "packages/jquery/plugins/jquery.textarearesizer.js",
a43b1583 614
a0c8b50e 615 "packages/jquery/plugins/jquery.form$min.js",
a43b1583 616
a0c8b50e 617 "packages/jquery/plugins/jquery.timeentry$min.js",
a43b1583 618
c542c8b8
CW
619 "packages/jquery/plugins/jquery.blockUI$min.js",
620
a0c8b50e 621 "packages/jquery/plugins/DataTables/media/js/jquery.dataTables$min.js",
45e44b23 622 "packages/jquery/plugins/DataTables/media/css/jquery.dataTables$min.css",
a43b1583 623
a0c8b50e
CW
624 "packages/jquery/plugins/jquery.validate$min.js",
625 "packages/jquery/plugins/jquery.ui.datepicker.validation.pack.js",
a43b1583 626
a0c8b50e 627 "js/Common.js",
53f2643c 628 "js/crm.ajax.js",
a43b1583 629 );
dbe0bbc6 630
4968e732
CW
631 // These scripts are only needed by back-office users
632 if (CRM_Core_Permission::check('access CiviCRM')) {
633 $items[] = "packages/jquery/plugins/jquery.menu$min.js";
634 $items[] = "packages/jquery/css/menu.css";
635 $items[] = "packages/jquery/plugins/jquery.jeditable$min.js";
4968e732
CW
636 $items[] = "packages/jquery/plugins/jquery.notify$min.js";
637 $items[] = "js/jquery/jquery.crmeditable.js";
4968e732
CW
638 }
639
d606fff7
CW
640 // JS for multilingual installations
641 if (!empty($config->languageLimit) && count($config->languageLimit) > 1 && CRM_Core_Permission::check('translate CiviCRM')) {
642 $items[] = "js/crm.multilingual.js";
643 }
644
53f2643c
CW
645 // Enable administrators to edit option lists in a dialog
646 if (CRM_Core_Permission::check('administer CiviCRM') && $this->ajaxPopupsEnabled) {
647 $items[] = "js/crm.optionEdit.js";
648 }
649
dbe0bbc6
CW
650 // Add localized jQuery UI files
651 if ($config->lcMessages && $config->lcMessages != 'en_US') {
652 // Search for i18n file in order of specificity (try fr-CA, then fr)
653 list($lang) = explode('_', $config->lcMessages);
f2f191fe 654 $path = "packages/jquery/jquery-ui/i18n";
dbe0bbc6 655 foreach (array(str_replace('_', '-', $config->lcMessages), $lang) as $language) {
f2f191fe 656 $localizationFile = "$path/datepicker-{$language}.js";
dbe0bbc6
CW
657 if ($this->getPath('civicrm', $localizationFile)) {
658 $items[] = $localizationFile;
659 break;
660 }
661 }
662 }
f9f361d0
CW
663
664 // CMS-specific resources
665 $config->userSystem->appendCoreResources($items);
666
6a488035
TO
667 return $items;
668 }
156fd9b9
CW
669
670 /**
671 * @return bool - is this page request an ajax snippet?
672 */
673 static function isAjaxMode() {
674 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));
675 }
6a488035 676}