Merge pull request #5392 from pradpnayak/CRM-16015
[civicrm-core.git] / CRM / Core / Resources.php
CommitLineData
6a488035
TO
1<?php
2/*
8d7a9d07
CB
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035
TO
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
6a488035
TO
52 */
53 private static $_singleton = NULL;
54
55 /**
56 * @var CRM_Extension_Mapper
57 */
58 private $extMapper = NULL;
59
60 /**
fd7dc3f3 61 * @var CRM_Core_Resources_Strings
6a488035 62 */
fd7dc3f3 63 private $strings = NULL;
6a488035
TO
64
65 /**
66 * @var array free-form data tree
67 */
68 protected $settings = array();
69 protected $addedSettings = FALSE;
70
71 /**
72 * @var array of callables
73 */
74 protected $settingsFactories = array();
75
76 /**
77 * @var array ($regionName => bool)
78 */
79 protected $addedCoreResources = array();
80
81 /**
82 * @var array ($regionName => bool)
83 */
84 protected $addedCoreStyles = array();
85
86 /**
87 * @var string a value to append to JS/CSS URLs to coerce cache resets
88 */
89 protected $cacheCode = NULL;
90
91 /**
92 * @var string the name of a setting which persistently stores the cacheCode
93 */
94 protected $cacheCodeKey = NULL;
95
53f2643c
CW
96 /**
97 * @var bool
98 */
99 public $ajaxPopupsEnabled;
100
6a488035 101 /**
fe482240 102 * Get or set the single instance of CRM_Core_Resources.
6a488035 103 *
5a4f6742
CW
104 * @param CRM_Core_Resources $instance
105 * New copy of the manager.
6a488035
TO
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(
8d7a9d07
CB
115 'group' => 'js-strings',
116 'prefetch' => FALSE,
117 ));
6a488035
TO
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 /**
d09edf64 128 * Construct a resource manager.
6a488035 129 *
6a0b768e
TO
130 * @param CRM_Extension_Mapper $extMapper
131 * Map extension names to their base path or URLs.
132 * @param CRM_Utils_Cache_Interface $cache
133 * JS-localization cache.
3be754d6 134 * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources
6a488035
TO
135 */
136 public function __construct($extMapper, $cache, $cacheCodeKey = NULL) {
137 $this->extMapper = $extMapper;
fd7dc3f3 138 $this->strings = new CRM_Core_Resources_Strings($cache);
6a488035
TO
139 $this->cacheCodeKey = $cacheCodeKey;
140 if ($cacheCodeKey !== NULL) {
141 $this->cacheCode = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, $cacheCodeKey);
142 }
150f50c1 143 if (!$this->cacheCode) {
6a488035
TO
144 $this->resetCacheCode();
145 }
53f2643c
CW
146 $this->ajaxPopupsEnabled = (bool) CRM_Core_BAO_Setting::getItem(
147 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE
148 );
6a488035
TO
149 }
150
90efc417
TO
151 /**
152 * Export permission data to the client to enable smarter GUIs.
153 *
154 * Note: Application security stems from the server's enforcement
155 * of the security logic (e.g. in the API permissions). There's no way
156 * the client can use this info to make the app more secure; however,
157 * it can produce a better-tuned (non-broken) UI.
158 *
159 * @param array $permNames
160 * List of permission names to check/export.
161 * @return CRM_Core_Resources
162 */
163 public function addPermissions($permNames) {
164 $permNames = (array) $permNames;
165 $perms = array();
166 foreach ($permNames as $permName) {
167 $perms[$permName] = CRM_Core_Permission::check($permName);
168 }
169 return $this->addSetting(array(
170 'permissions' => $perms,
171 ));
172 }
173
6a488035
TO
174 /**
175 * Add a JavaScript file to the current page using <SCRIPT SRC>.
176 *
5a4f6742
CW
177 * @param string $ext
178 * extension name; use 'civicrm' for core.
179 * @param string $file
180 * file path -- relative to the extension base dir.
181 * @param int $weight
182 * relative weight within a given region.
183 * @param string $region
184 * location within the file; 'html-header', 'page-header', 'page-footer'.
3f3bba82
TO
185 * @param bool|string $translate
186 * Whether to load translated strings for this file. Use one of:
187 * - FALSE: Do not load translated strings.
188 * - TRUE: Load translated strings. Use the $ext's default domain.
189 * - string: Load translated strings. Use a specific domain.
6a488035
TO
190 *
191 * @return CRM_Core_Resources
192 */
193 public function addScriptFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION, $translate = TRUE) {
194 if ($translate) {
3f3bba82
TO
195 $domain = ($translate === TRUE) ? $ext : $translate;
196 $this->addString($this->strings->get($domain, $this->getPath($ext, $file), 'text/javascript'), $domain);
6a488035 197 }
9ee32530
CW
198 // Look for non-minified version if we are in debug mode
199 if (CRM_Core_Config::singleton()->debug && strpos($file, '.min.js') !== FALSE) {
200 $nonMiniFile = str_replace('.min.js', '.js', $file);
201 if ($this->getPath($ext, $nonMiniFile)) {
202 $file = $nonMiniFile;
203 }
204 }
6a488035
TO
205 return $this->addScriptUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
206 }
207
208 /**
209 * Add a JavaScript file to the current page using <SCRIPT SRC>.
210 *
5a4f6742
CW
211 * @param string $url
212 * @param int $weight
213 * relative weight within a given region.
214 * @param string $region
215 * location within the file; 'html-header', 'page-header', 'page-footer'.
6a488035
TO
216 * @return CRM_Core_Resources
217 */
218 public function addScriptUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
219 CRM_Core_Region::instance($region)->add(array(
8d7a9d07
CB
220 'name' => $url,
221 'type' => 'scriptUrl',
222 'scriptUrl' => $url,
223 'weight' => $weight,
224 'region' => $region,
225 ));
6a488035
TO
226 return $this;
227 }
228
229 /**
230 * Add a JavaScript file to the current page using <SCRIPT SRC>.
231 *
5a4f6742
CW
232 * @param string $code
233 * JavaScript source code.
234 * @param int $weight
235 * relative weight within a given region.
236 * @param string $region
237 * location within the file; 'html-header', 'page-header', 'page-footer'.
6a488035
TO
238 * @return CRM_Core_Resources
239 */
240 public function addScript($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
241 CRM_Core_Region::instance($region)->add(array(
8d7a9d07
CB
242 // 'name' => automatic
243 'type' => 'script',
244 'script' => $code,
245 'weight' => $weight,
246 'region' => $region,
247 ));
6a488035
TO
248 return $this;
249 }
250
251 /**
73bcd446 252 * Add JavaScript variables to CRM.vars
6a488035 253 *
132fc68e
CW
254 * Example:
255 * From the server:
73bcd446
CW
256 * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar'));
257 * Access var from javascript:
258 * CRM.vars.myNamespace.foo // "bar"
132fc68e 259 *
01478033 260 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference
132fc68e 261 *
6a0b768e
TO
262 * @param string $nameSpace
263 * Usually the name of your extension.
73bcd446
CW
264 * @param array $vars
265 * @return CRM_Core_Resources
266 */
267 public function addVars($nameSpace, $vars) {
268 $existing = CRM_Utils_Array::value($nameSpace, CRM_Utils_Array::value('vars', $this->settings), array());
269 $vars = $this->mergeSettings($existing, $vars);
270 $this->addSetting(array('vars' => array($nameSpace => $vars)));
271 return $this;
272 }
273
274 /**
275 * Add JavaScript variables to the root of the CRM object.
276 * This function is usually reserved for low-level system use.
277 * Extensions and components should generally use addVars instead.
278 *
5a4f6742 279 * @param array $settings
6a488035
TO
280 * @return CRM_Core_Resources
281 */
282 public function addSetting($settings) {
283 $this->settings = $this->mergeSettings($this->settings, $settings);
284 if (!$this->addedSettings) {
88cd8875 285 $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header';
6a488035 286 $resources = $this;
156fd9b9 287 CRM_Core_Region::instance($region)->add(array(
353ffa53
TO
288 'callback' => function (&$snippet, &$html) use ($resources) {
289 $html .= "\n" . $resources->renderSetting();
290 },
8cb324cc 291 'weight' => -100000,
6a488035
TO
292 ));
293 $this->addedSettings = TRUE;
294 }
295 return $this;
296 }
297
298 /**
69847402 299 * Add JavaScript variables to the global CRM object via a callback function.
6a488035 300 *
3be754d6 301 * @param callable $callable
6a488035
TO
302 * @return CRM_Core_Resources
303 */
304 public function addSettingsFactory($callable) {
d937dbcd
CW
305 // Make sure our callback has been registered
306 $this->addSetting(array());
6a488035
TO
307 $this->settingsFactories[] = $callable;
308 return $this;
309 }
310
69847402 311 /**
d09edf64 312 * Helper fn for addSettingsFactory.
69847402 313 */
6a488035
TO
314 public function getSettings() {
315 $result = $this->settings;
316 foreach ($this->settingsFactories as $callable) {
317 $result = $this->mergeSettings($result, $callable());
318 }
319 return $result;
320 }
321
322 /**
323 * @param array $settings
324 * @param array $additions
a6c01b45
CW
325 * @return array
326 * combination of $settings and $additions
6a488035
TO
327 */
328 protected function mergeSettings($settings, $additions) {
329 foreach ($additions as $k => $v) {
330 if (isset($settings[$k]) && is_array($settings[$k]) && is_array($v)) {
331 $v += $settings[$k];
332 }
333 $settings[$k] = $v;
334 }
335 return $settings;
336 }
337
338 /**
d09edf64 339 * Helper fn for addSetting.
6a488035
TO
340 * Render JavaScript variables for the global CRM object.
341 *
6a488035
TO
342 * @return string
343 */
344 public function renderSetting() {
156fd9b9
CW
345 // On a standard page request we construct the CRM object from scratch
346 if (!self::isAjaxMode()) {
347 $js = 'var CRM = ' . json_encode($this->getSettings()) . ';';
348 }
349 // For an ajax request we append to it
350 else {
351 $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');';
352 }
d759bd10 353 return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js);
6a488035
TO
354 }
355
356 /**
357 * Add translated string to the js CRM object.
358 * It can then be retrived from the client-side ts() function
359 * Variable substitutions can happen from client-side
8ef12e64 360 *
6a488035 361 * Note: this function rarely needs to be called directly and is mostly for internal use.
d3e86119 362 * See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files
6a488035
TO
363 *
364 * Simple example:
365 * // From php:
366 * CRM_Core_Resources::singleton()->addString('Hello');
367 * // The string is now available to javascript code i.e.
368 * ts('Hello');
369 *
370 * Example with client-side substitutions:
371 * // From php:
372 * CRM_Core_Resources::singleton()->addString('Your %1 has been %2');
373 * // ts() in javascript works the same as in php, for example:
374 * ts('Your %1 has been %2', {1: objectName, 2: actionTaken});
375 *
376 * NOTE: This function does not work with server-side substitutions
377 * (as this might result in collisions and unwanted variable injections)
378 * Instead, use code like:
379 * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs)))));
380 * And from javascript access it at CRM.myNamespace.myString
381 *
5a4f6742 382 * @param string|array $text
3f3bba82 383 * @param string|NULL $domain
6a488035
TO
384 * @return CRM_Core_Resources
385 */
3f3bba82 386 public function addString($text, $domain = 'civicrm') {
6a488035 387 foreach ((array) $text as $str) {
3f3bba82
TO
388 $translated = ts($str, array(
389 'domain' => ($domain == 'civicrm') ? NULL : array($domain, NULL),
390 ));
391
6a488035
TO
392 // We only need to push this string to client if the translation
393 // is actually different from the original
394 if ($translated != $str) {
3f3bba82
TO
395 $bucket = $domain == 'civicrm' ? 'strings' : 'strings::' . $domain;
396 $this->addSetting(array(
397 $bucket => array($str => $translated),
398 ));
6a488035
TO
399 }
400 }
401 return $this;
402 }
403
404 /**
405 * Add a CSS file to the current page using <LINK HREF>.
406 *
5a4f6742
CW
407 * @param string $ext
408 * extension name; use 'civicrm' for core.
409 * @param string $file
410 * file path -- relative to the extension base dir.
411 * @param int $weight
412 * relative weight within a given region.
413 * @param string $region
414 * location within the file; 'html-header', 'page-header', 'page-footer'.
6a488035
TO
415 * @return CRM_Core_Resources
416 */
417 public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
418 return $this->addStyleUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
419 }
420
421 /**
422 * Add a CSS file to the current page using <LINK HREF>.
423 *
5a4f6742
CW
424 * @param string $url
425 * @param int $weight
426 * relative weight within a given region.
427 * @param string $region
428 * location within the file; 'html-header', 'page-header', 'page-footer'.
6a488035
TO
429 * @return CRM_Core_Resources
430 */
431 public function addStyleUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
432 CRM_Core_Region::instance($region)->add(array(
8d7a9d07
CB
433 'name' => $url,
434 'type' => 'styleUrl',
435 'styleUrl' => $url,
436 'weight' => $weight,
437 'region' => $region,
438 ));
6a488035
TO
439 return $this;
440 }
441
442 /**
443 * Add a CSS content to the current page using <STYLE>.
444 *
5a4f6742
CW
445 * @param string $code
446 * CSS source code.
447 * @param int $weight
448 * relative weight within a given region.
449 * @param string $region
450 * location within the file; 'html-header', 'page-header', 'page-footer'.
6a488035
TO
451 * @return CRM_Core_Resources
452 */
453 public function addStyle($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
454 CRM_Core_Region::instance($region)->add(array(
8d7a9d07
CB
455 // 'name' => automatic
456 'type' => 'style',
457 'style' => $code,
458 'weight' => $weight,
459 'region' => $region,
460 ));
6a488035
TO
461 return $this;
462 }
463
464 /**
d09edf64 465 * Determine file path of a resource provided by an extension.
6a488035 466 *
5a4f6742
CW
467 * @param string $ext
468 * extension name; use 'civicrm' for core.
16cd1eca 469 * @param string|NULL $file
5a4f6742 470 * file path -- relative to the extension base dir.
6a488035 471 *
72b3a70c
CW
472 * @return bool|string
473 * full file path or FALSE if not found
6a488035 474 */
16cd1eca 475 public function getPath($ext, $file = NULL) {
6a488035 476 // TODO consider caching results
16cd1eca
TO
477 if ($file === NULL) {
478 return $this->extMapper->keyToBasePath($ext);
479 }
6a488035
TO
480 $path = $this->extMapper->keyToBasePath($ext) . '/' . $file;
481 if (is_file($path)) {
482 return $path;
483 }
484 return FALSE;
485 }
486
487 /**
d09edf64 488 * Determine public URL of a resource provided by an extension.
6a488035 489 *
5a4f6742
CW
490 * @param string $ext
491 * extension name; use 'civicrm' for core.
492 * @param string $file
493 * file path -- relative to the extension base dir.
2a6da8d7
EM
494 * @param bool $addCacheCode
495 *
6a488035
TO
496 * @return string, URL
497 */
498 public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) {
499 if ($file === NULL) {
500 $file = '';
501 }
502 if ($addCacheCode) {
503 $file .= '?r=' . $this->getCacheCode();
504 }
505 // TODO consider caching results
506 return $this->extMapper->keyToUrl($ext) . '/' . $file;
507 }
508
16cd1eca
TO
509 /**
510 * Evaluate a glob pattern in the context of a particular extension.
511 *
512 * @param string $ext
513 * Extension name; use 'civicrm' for core.
514 * @param string|array $patterns
515 * Glob pattern; e.g. "*.html".
516 * @param null|int $flags
517 * See glob().
518 * @return array
519 * List of matching files, relative to the extension base dir.
520 * @see glob()
521 */
522 public function glob($ext, $patterns, $flags = NULL) {
523 $path = $this->getPath($ext);
524 $patterns = (array) $patterns;
525 $files = array();
526 foreach ($patterns as $pattern) {
527 if ($pattern{0} === '/') {
528 // Absolute path.
529 $files = array_merge($files, (array) glob($pattern, $flags));
530 }
531 else {
532 // Relative path.
533 $files = array_merge($files, (array) glob("$path/$pattern", $flags));
534 }
535 }
536 sort($files); // Deterministic order.
537 $files = array_unique($files);
538 return array_map(function ($file) use ($path) {
539 return CRM_Utils_File::relativize($file, "$path/");
540 }, $files);
541 }
542
a0ee3941
EM
543 /**
544 * @return string
545 */
6a488035
TO
546 public function getCacheCode() {
547 return $this->cacheCode;
548 }
549
a0ee3941
EM
550 /**
551 * @param $value
5badddc3 552 * @return CRM_Core_Resources
a0ee3941 553 */
6a488035
TO
554 public function setCacheCode($value) {
555 $this->cacheCode = $value;
556 if ($this->cacheCodeKey) {
557 CRM_Core_BAO_Setting::setItem($value, CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, $this->cacheCodeKey);
558 }
9762f6ff 559 return $this;
6a488035
TO
560 }
561
5badddc3
CW
562 /**
563 * @return CRM_Core_Resources
564 */
6a488035
TO
565 public function resetCacheCode() {
566 $this->setCacheCode(CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC));
f091327b
CW
567 // Also flush cms resource cache if needed
568 CRM_Core_Config::singleton()->userSystem->clearResourceCache();
9762f6ff 569 return $this;
6a488035
TO
570 }
571
572 /**
573 * This adds CiviCRM's standard css and js to the specified region of the document.
574 * It will only run once.
575 *
576 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
577 * (like addCoreResources/addCoreStyles).
578 *
2a6da8d7 579 * @param string $region
6a488035 580 * @return CRM_Core_Resources
6a488035
TO
581 */
582 public function addCoreResources($region = 'html-header') {
156fd9b9 583 if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) {
6a488035
TO
584 $this->addedCoreResources[$region] = TRUE;
585 $config = CRM_Core_Config::singleton();
586
a43b1583 587 // Add resources from coreResourceList
6a488035 588 $jsWeight = -9999;
5409628b 589 foreach ($this->coreResourceList() as $file) {
a43b1583 590 if (substr($file, -2) == 'js') {
74227f77 591 // Don't bother looking for ts() calls in packages, there aren't any
7789a8f6 592 $translate = (substr($file, 0, 3) == 'js/');
74227f77 593 $this->addScriptFile('civicrm', $file, $jsWeight++, $region, $translate);
6a488035 594 }
a43b1583 595 else {
6a488035
TO
596 $this->addStyleFile('civicrm', $file, -100, $region);
597 }
598 }
599
4e56e743 600 // Dynamic localization script
4cc9b813 601 $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $config->lcMessages, array('r' => $this->getCacheCode())), $jsWeight++, $region);
4e56e743 602
d759bd10 603 // Add global settings
2aa397bc 604 $settings = array(
353ffa53 605 'config' => array(
353ffa53 606 'isFrontend' => $config->userFrameworkFrontend,
8d7a9d07 607 ),
353ffa53 608 );
4e56e743
CW
609 // Disable profile creation if user lacks permission
610 if (!CRM_Core_Permission::check('edit all contacts') && !CRM_Core_Permission::check('add contacts')) {
b7ceb253 611 $settings['config']['entityRef']['contactCreate'] = FALSE;
3d527838 612 }
4e56e743 613 $this->addSetting($settings);
3d527838
CW
614
615 // Give control of jQuery and _ back to the CMS - this loads last
c2777586 616 $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE);
6a488035
TO
617
618 $this->addCoreStyles($region);
619 }
620 return $this;
621 }
622
623 /**
624 * This will add CiviCRM's standard CSS
625 *
626 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
627 * (like addCoreResources/addCoreStyles).
628 *
629 * @param string $region
630 * @return CRM_Core_Resources
631 */
632 public function addCoreStyles($region = 'html-header') {
633 if (!isset($this->addedCoreStyles[$region])) {
634 $this->addedCoreStyles[$region] = TRUE;
635
636 // Load custom or core css
637 $config = CRM_Core_Config::singleton();
638 if (!empty($config->customCSSURL)) {
57018a3e 639 $this->addStyleUrl($config->customCSSURL, 99, $region);
6a488035 640 }
14f20d22 641 if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'disable_core_css')) {
6a488035 642 $this->addStyleFile('civicrm', 'css/civicrm.css', -99, $region);
6a488035
TO
643 }
644 }
645 return $this;
646 }
647
627668e8 648 /**
d09edf64 649 * Flushes cached translated strings.
5badddc3 650 * @return CRM_Core_Resources
627668e8
CW
651 */
652 public function flushStrings() {
fd7dc3f3 653 $this->strings->flush();
9762f6ff
CW
654 return $this;
655 }
656
6a488035 657 /**
fd7dc3f3
TO
658 * @return CRM_Core_Resources_Strings
659 */
660 public function getStrings() {
661 return $this->strings;
6a488035
TO
662 }
663
19f7e35e 664 /**
8d7a9d07 665 * Create dynamic script for localizing js widgets.
19f7e35e 666 */
00be9182 667 public static function outputLocalizationJS() {
4cc9b813 668 CRM_Core_Page_AJAX::setJsHeaders();
a88cf11a 669 $config = CRM_Core_Config::singleton();
3d527838
CW
670 $vars = array(
671 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)),
672 'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')),
673 'otherSearch' => json_encode(ts('Enter search term...')),
b7ceb253
CW
674 'entityRef' => array(
675 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(),
676 'filters' => self::getEntityRefFilters(),
677 ),
7b83e312 678 'ajaxPopupsEnabled' => self::singleton()->ajaxPopupsEnabled,
3d527838 679 );
3d4fb0ed 680 print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars);
4cc9b813 681 CRM_Utils_System::civiExit();
c66581f5
CW
682 }
683
6a488035 684 /**
d09edf64 685 * List of core resources we add to every CiviCRM page.
6a488035 686 *
a43b1583 687 * @return array
6a488035 688 */
dbe0bbc6
CW
689 public function coreResourceList() {
690 $config = CRM_Core_Config::singleton();
691 // Use minified files for production, uncompressed in debug mode
9ee32530 692 // Note, $this->addScriptFile would automatically search for the non-minified file in debug mode but this is probably faster
dbe0bbc6 693 $min = $config->debug ? '' : '.min';
a43b1583 694
4968e732
CW
695 // Scripts needed by everyone, everywhere
696 // FIXME: This is too long; list needs finer-grained segmentation
a0c8b50e 697 $items = array(
3c61fc8f
CW
698 "bower_components/jquery/dist/jquery.min.js",
699 "bower_components/jquery-ui/jquery-ui.min.js",
700 "bower_components/jquery-ui/themes/smoothness/jquery-ui$min.css",
1891a856 701 "bower_components/lodash-compat/lodash.min.js",
c04d6c92 702 "packages/jquery/plugins/jquery.mousewheel$min.js",
119934a7 703 "packages/jquery/plugins/select2/select2$min.js",
f7b92fcd 704 "packages/jquery/plugins/select2/select2.css",
a0c8b50e 705 "packages/jquery/plugins/jquery.tableHeader.js",
a0c8b50e 706 "packages/jquery/plugins/jquery.textarearesizer.js",
a0c8b50e 707 "packages/jquery/plugins/jquery.form$min.js",
a0c8b50e 708 "packages/jquery/plugins/jquery.timeentry$min.js",
c542c8b8 709 "packages/jquery/plugins/jquery.blockUI$min.js",
a0c8b50e 710 "packages/jquery/plugins/DataTables/media/js/jquery.dataTables$min.js",
45e44b23 711 "packages/jquery/plugins/DataTables/media/css/jquery.dataTables$min.css",
a0c8b50e
CW
712 "packages/jquery/plugins/jquery.validate$min.js",
713 "packages/jquery/plugins/jquery.ui.datepicker.validation.pack.js",
a0c8b50e 714 "js/Common.js",
53f2643c 715 "js/crm.ajax.js",
a43b1583 716 );
dbe0bbc6 717
4968e732
CW
718 // These scripts are only needed by back-office users
719 if (CRM_Core_Permission::check('access CiviCRM')) {
720 $items[] = "packages/jquery/plugins/jquery.menu$min.js";
a2e1885c 721 $items[] = "css/navigation.css";
4968e732 722 $items[] = "packages/jquery/plugins/jquery.jeditable$min.js";
4968e732
CW
723 $items[] = "packages/jquery/plugins/jquery.notify$min.js";
724 $items[] = "js/jquery/jquery.crmeditable.js";
4968e732
CW
725 }
726
d606fff7
CW
727 // JS for multilingual installations
728 if (!empty($config->languageLimit) && count($config->languageLimit) > 1 && CRM_Core_Permission::check('translate CiviCRM')) {
729 $items[] = "js/crm.multilingual.js";
730 }
731
53f2643c
CW
732 // Enable administrators to edit option lists in a dialog
733 if (CRM_Core_Permission::check('administer CiviCRM') && $this->ajaxPopupsEnabled) {
734 $items[] = "js/crm.optionEdit.js";
735 }
736
dbe0bbc6
CW
737 // Add localized jQuery UI files
738 if ($config->lcMessages && $config->lcMessages != 'en_US') {
739 // Search for i18n file in order of specificity (try fr-CA, then fr)
740 list($lang) = explode('_', $config->lcMessages);
3c61fc8f 741 $path = "bower_components/jquery-ui/ui/i18n";
dbe0bbc6 742 foreach (array(str_replace('_', '-', $config->lcMessages), $lang) as $language) {
f2f191fe 743 $localizationFile = "$path/datepicker-{$language}.js";
dbe0bbc6
CW
744 if ($this->getPath('civicrm', $localizationFile)) {
745 $items[] = $localizationFile;
746 break;
747 }
748 }
749 }
f9f361d0
CW
750
751 // CMS-specific resources
752 $config->userSystem->appendCoreResources($items);
753
6a488035
TO
754 return $items;
755 }
156fd9b9
CW
756
757 /**
a6c01b45
CW
758 * @return bool
759 * is this page request an ajax snippet?
156fd9b9 760 */
00be9182 761 public static function isAjaxMode() {
353ffa53
TO
762 return in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array(
763 CRM_Core_Smarty::PRINT_SNIPPET,
764 CRM_Core_Smarty::PRINT_NOFORM,
8d7a9d07 765 CRM_Core_Smarty::PRINT_JSON,
353ffa53 766 ));
156fd9b9 767 }
b7ceb253
CW
768
769 /**
f9e31d7f 770 * Provide a list of available entityRef filters.
b7ceb253
CW
771 * FIXME: This function doesn't really belong in this class
772 * @TODO: Provide a sane way to extend this list for other entities - a hook or??
773 * @return array
774 */
00be9182 775 public static function getEntityRefFilters() {
b7ceb253
CW
776 $filters = array();
777
778 $filters['event'] = array(
779 array('key' => 'event_type_id', 'value' => ts('Event Type')),
2aa397bc 780 array(
353ffa53
TO
781 'key' => 'start_date',
782 'value' => ts('Start Date'),
783 'options' => array(
784 array('key' => '{">":"now"}', 'value' => ts('Upcoming')),
785 array('key' => '{"BETWEEN":["now - 3 month","now"]}', 'value' => ts('Past 3 Months')),
786 array('key' => '{"BETWEEN":["now - 6 month","now"]}', 'value' => ts('Past 6 Months')),
787 array('key' => '{"BETWEEN":["now - 1 year","now"]}', 'value' => ts('Past Year')),
8d7a9d07 788 ),
353ffa53 789 ),
b7ceb253
CW
790 );
791
792 $filters['activity'] = array(
793 array('key' => 'activity_type_id', 'value' => ts('Activity Type')),
794 array('key' => 'status_id', 'value' => ts('Activity Status')),
795 );
796
b7ceb253 797 $filters['contact'] = array(
bee6039a 798 array('key' => 'contact_type', 'value' => ts('Contact Type')),
b7ceb253
CW
799 array('key' => 'group', 'value' => ts('Group'), 'entity' => 'group_contact'),
800 array('key' => 'tag', 'value' => ts('Tag'), 'entity' => 'entity_tag'),
801 array('key' => 'state_province', 'value' => ts('State/Province'), 'entity' => 'address'),
802 array('key' => 'country', 'value' => ts('Country'), 'entity' => 'address'),
803 array('key' => 'gender_id', 'value' => ts('Gender')),
804 array('key' => 'is_deceased', 'value' => ts('Deceased')),
805 );
806
807 return $filters;
808 }
96025800 809
6a488035 810}