Merge pull request #4898 from monishdeb/CRM-15619-fix
[civicrm-core.git] / CRM / Core / Resources.php
1 <?php
2 /*
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 +--------------------------------------------------------------------+
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
40 * @copyright CiviCRM LLC (c) 2004-2014
41 * $Id$
42 *
43 */
44 class 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
97 /**
98 * @var bool
99 */
100 public $ajaxPopupsEnabled;
101
102 /**
103 * Get or set the single instance of CRM_Core_Resources
104 *
105 * @param CRM_Core_Resources $instance
106 * New copy of the manager.
107 * @return CRM_Core_Resources
108 */
109 static public function singleton(CRM_Core_Resources $instance = NULL) {
110 if ($instance !== NULL) {
111 self::$_singleton = $instance;
112 }
113 if (self::$_singleton === NULL) {
114 $sys = CRM_Extension_System::singleton();
115 $cache = new CRM_Utils_Cache_SqlGroup(array(
116 'group' => 'js-strings',
117 'prefetch' => FALSE,
118 ));
119 self::$_singleton = new CRM_Core_Resources(
120 $sys->getMapper(),
121 $cache,
122 CRM_Core_Config::isUpgradeMode() ? NULL : 'resCacheCode'
123 );
124 }
125 return self::$_singleton;
126 }
127
128 /**
129 * Construct a resource manager
130 *
131 * @param CRM_Extension_Mapper $extMapper
132 * Map extension names to their base path or URLs.
133 * @param CRM_Utils_Cache_Interface $cache
134 * JS-localization cache.
135 * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources
136 */
137 public function __construct($extMapper, $cache, $cacheCodeKey = NULL) {
138 $this->extMapper = $extMapper;
139 $this->cache = $cache;
140 $this->cacheCodeKey = $cacheCodeKey;
141 if ($cacheCodeKey !== NULL) {
142 $this->cacheCode = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, $cacheCodeKey);
143 }
144 if (!$this->cacheCode) {
145 $this->resetCacheCode();
146 }
147 $this->ajaxPopupsEnabled = (bool) CRM_Core_BAO_Setting::getItem(
148 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE
149 );
150 }
151
152 /**
153 * Add a JavaScript file to the current page using <SCRIPT SRC>.
154 *
155 * @param string $ext
156 * extension name; use 'civicrm' for core.
157 * @param string $file
158 * file path -- relative to the extension base dir.
159 * @param int $weight
160 * relative weight within a given region.
161 * @param string $region
162 * location within the file; 'html-header', 'page-header', 'page-footer'.
163 * @param $translate , whether to parse this file for strings enclosed in ts()
164 *
165 * @return CRM_Core_Resources
166 */
167 public function addScriptFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION, $translate = TRUE) {
168 if ($translate) {
169 $this->translateScript($ext, $file);
170 }
171 // Look for non-minified version if we are in debug mode
172 if (CRM_Core_Config::singleton()->debug && strpos($file, '.min.js') !== FALSE) {
173 $nonMiniFile = str_replace('.min.js', '.js', $file);
174 if ($this->getPath($ext, $nonMiniFile)) {
175 $file = $nonMiniFile;
176 }
177 }
178 return $this->addScriptUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
179 }
180
181 /**
182 * Add a JavaScript file to the current page using <SCRIPT SRC>.
183 *
184 * @param string $url
185 * @param int $weight
186 * relative weight within a given region.
187 * @param string $region
188 * location within the file; 'html-header', 'page-header', 'page-footer'.
189 * @return CRM_Core_Resources
190 */
191 public function addScriptUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
192 CRM_Core_Region::instance($region)->add(array(
193 'name' => $url,
194 'type' => 'scriptUrl',
195 'scriptUrl' => $url,
196 'weight' => $weight,
197 'region' => $region,
198 ));
199 return $this;
200 }
201
202 /**
203 * Add a JavaScript file to the current page using <SCRIPT SRC>.
204 *
205 * @param string $code
206 * JavaScript source code.
207 * @param int $weight
208 * relative weight within a given region.
209 * @param string $region
210 * location within the file; 'html-header', 'page-header', 'page-footer'.
211 * @return CRM_Core_Resources
212 */
213 public function addScript($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
214 CRM_Core_Region::instance($region)->add(array(
215 // 'name' => automatic
216 'type' => 'script',
217 'script' => $code,
218 'weight' => $weight,
219 'region' => $region,
220 ));
221 return $this;
222 }
223
224 /**
225 * Add JavaScript variables to CRM.vars
226 *
227 * Example:
228 * From the server:
229 * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar'));
230 * Access var from javascript:
231 * CRM.vars.myNamespace.foo // "bar"
232 *
233 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference
234 *
235 * @param string $nameSpace
236 * Usually the name of your extension.
237 * @param array $vars
238 * @return CRM_Core_Resources
239 */
240 public function addVars($nameSpace, $vars) {
241 $existing = CRM_Utils_Array::value($nameSpace, CRM_Utils_Array::value('vars', $this->settings), array());
242 $vars = $this->mergeSettings($existing, $vars);
243 $this->addSetting(array('vars' => array($nameSpace => $vars)));
244 return $this;
245 }
246
247 /**
248 * Add JavaScript variables to the root of the CRM object.
249 * This function is usually reserved for low-level system use.
250 * Extensions and components should generally use addVars instead.
251 *
252 * @param array $settings
253 * @return CRM_Core_Resources
254 */
255 public function addSetting($settings) {
256 $this->settings = $this->mergeSettings($this->settings, $settings);
257 if (!$this->addedSettings) {
258 $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header';
259 $resources = $this;
260 CRM_Core_Region::instance($region)->add(array(
261 'callback' => function (&$snippet, &$html) use ($resources) {
262 $html .= "\n" . $resources->renderSetting();
263 },
264 'weight' => -100000,
265 ));
266 $this->addedSettings = TRUE;
267 }
268 return $this;
269 }
270
271 /**
272 * Add JavaScript variables to the global CRM object via a callback function.
273 *
274 * @param callable $callable
275 * @return CRM_Core_Resources
276 */
277 public function addSettingsFactory($callable) {
278 // Make sure our callback has been registered
279 $this->addSetting(array());
280 $this->settingsFactories[] = $callable;
281 return $this;
282 }
283
284 /**
285 * Helper fn for addSettingsFactory
286 */
287 public function getSettings() {
288 $result = $this->settings;
289 foreach ($this->settingsFactories as $callable) {
290 $result = $this->mergeSettings($result, $callable());
291 }
292 return $result;
293 }
294
295 /**
296 * @param array $settings
297 * @param array $additions
298 * @return array
299 * combination of $settings and $additions
300 */
301 protected function mergeSettings($settings, $additions) {
302 foreach ($additions as $k => $v) {
303 if (isset($settings[$k]) && is_array($settings[$k]) && is_array($v)) {
304 $v += $settings[$k];
305 }
306 $settings[$k] = $v;
307 }
308 return $settings;
309 }
310
311 /**
312 * Helper fn for addSetting
313 * Render JavaScript variables for the global CRM object.
314 *
315 * @return string
316 */
317 public function renderSetting() {
318 // On a standard page request we construct the CRM object from scratch
319 if (!self::isAjaxMode()) {
320 $js = 'var CRM = ' . json_encode($this->getSettings()) . ';';
321 }
322 // For an ajax request we append to it
323 else {
324 $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');';
325 }
326 return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js);
327 }
328
329 /**
330 * Add translated string to the js CRM object.
331 * It can then be retrived from the client-side ts() function
332 * Variable substitutions can happen from client-side
333 *
334 * Note: this function rarely needs to be called directly and is mostly for internal use.
335 * See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files
336 *
337 * Simple example:
338 * // From php:
339 * CRM_Core_Resources::singleton()->addString('Hello');
340 * // The string is now available to javascript code i.e.
341 * ts('Hello');
342 *
343 * Example with client-side substitutions:
344 * // From php:
345 * CRM_Core_Resources::singleton()->addString('Your %1 has been %2');
346 * // ts() in javascript works the same as in php, for example:
347 * ts('Your %1 has been %2', {1: objectName, 2: actionTaken});
348 *
349 * NOTE: This function does not work with server-side substitutions
350 * (as this might result in collisions and unwanted variable injections)
351 * Instead, use code like:
352 * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs)))));
353 * And from javascript access it at CRM.myNamespace.myString
354 *
355 * @param string|array $text
356 * @return CRM_Core_Resources
357 */
358 public function addString($text) {
359 foreach ((array) $text as $str) {
360 $translated = ts($str);
361 // We only need to push this string to client if the translation
362 // is actually different from the original
363 if ($translated != $str) {
364 $this->addSetting(array('strings' => array($str => $translated)));
365 }
366 }
367 return $this;
368 }
369
370 /**
371 * Add a CSS file to the current page using <LINK HREF>.
372 *
373 * @param string $ext
374 * extension name; use 'civicrm' for core.
375 * @param string $file
376 * file path -- relative to the extension base dir.
377 * @param int $weight
378 * relative weight within a given region.
379 * @param string $region
380 * location within the file; 'html-header', 'page-header', 'page-footer'.
381 * @return CRM_Core_Resources
382 */
383 public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
384 return $this->addStyleUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
385 }
386
387 /**
388 * Add a CSS file to the current page using <LINK HREF>.
389 *
390 * @param string $url
391 * @param int $weight
392 * relative weight within a given region.
393 * @param string $region
394 * location within the file; 'html-header', 'page-header', 'page-footer'.
395 * @return CRM_Core_Resources
396 */
397 public function addStyleUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
398 CRM_Core_Region::instance($region)->add(array(
399 'name' => $url,
400 'type' => 'styleUrl',
401 'styleUrl' => $url,
402 'weight' => $weight,
403 'region' => $region,
404 ));
405 return $this;
406 }
407
408 /**
409 * Add a CSS content to the current page using <STYLE>.
410 *
411 * @param string $code
412 * CSS source code.
413 * @param int $weight
414 * relative weight within a given region.
415 * @param string $region
416 * location within the file; 'html-header', 'page-header', 'page-footer'.
417 * @return CRM_Core_Resources
418 */
419 public function addStyle($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
420 CRM_Core_Region::instance($region)->add(array(
421 // 'name' => automatic
422 'type' => 'style',
423 'style' => $code,
424 'weight' => $weight,
425 'region' => $region,
426 ));
427 return $this;
428 }
429
430 /**
431 * Determine file path of a resource provided by an extension
432 *
433 * @param string $ext
434 * extension name; use 'civicrm' for core.
435 * @param string $file
436 * file path -- relative to the extension base dir.
437 *
438 * @return bool|string
439 * full file path or FALSE if not found
440 */
441 public function getPath($ext, $file) {
442 // TODO consider caching results
443 $path = $this->extMapper->keyToBasePath($ext) . '/' . $file;
444 if (is_file($path)) {
445 return $path;
446 }
447 return FALSE;
448 }
449
450 /**
451 * Determine public URL of a resource provided by an extension
452 *
453 * @param string $ext
454 * extension name; use 'civicrm' for core.
455 * @param string $file
456 * file path -- relative to the extension base dir.
457 * @param bool $addCacheCode
458 *
459 * @return string, URL
460 */
461 public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) {
462 if ($file === NULL) {
463 $file = '';
464 }
465 if ($addCacheCode) {
466 $file .= '?r=' . $this->getCacheCode();
467 }
468 // TODO consider caching results
469 return $this->extMapper->keyToUrl($ext) . '/' . $file;
470 }
471
472 /**
473 * @return string
474 */
475 public function getCacheCode() {
476 return $this->cacheCode;
477 }
478
479 /**
480 * @param $value
481 * @return CRM_Core_Resources
482 */
483 public function setCacheCode($value) {
484 $this->cacheCode = $value;
485 if ($this->cacheCodeKey) {
486 CRM_Core_BAO_Setting::setItem($value, CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, $this->cacheCodeKey);
487 }
488 return $this;
489 }
490
491 /**
492 * @return CRM_Core_Resources
493 */
494 public function resetCacheCode() {
495 $this->setCacheCode(CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC));
496 // Also flush cms resource cache if needed
497 CRM_Core_Config::singleton()->userSystem->clearResourceCache();
498 return $this;
499 }
500
501 /**
502 * This adds CiviCRM's standard css and js to the specified region of the document.
503 * It will only run once.
504 *
505 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
506 * (like addCoreResources/addCoreStyles).
507 *
508 * @param string $region
509 * @return CRM_Core_Resources
510 */
511 public function addCoreResources($region = 'html-header') {
512 if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) {
513 $this->addedCoreResources[$region] = TRUE;
514 $config = CRM_Core_Config::singleton();
515
516 // Add resources from coreResourceList
517 $jsWeight = -9999;
518 foreach ($this->coreResourceList() as $file) {
519 if (substr($file, -2) == 'js') {
520 // Don't bother looking for ts() calls in packages, there aren't any
521 $translate = (substr($file, 0, 9) != 'packages/');
522 $this->addScriptFile('civicrm', $file, $jsWeight++, $region, $translate);
523 }
524 else {
525 $this->addStyleFile('civicrm', $file, -100, $region);
526 }
527 }
528
529 // Dynamic localization script
530 $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $config->lcMessages, array('r' => $this->getCacheCode())), $jsWeight++, $region);
531
532 // Add global settings
533 $settings = array(
534 'config' => array(
535 'ajaxPopupsEnabled' => $this->ajaxPopupsEnabled,
536 'isFrontend' => $config->userFrameworkFrontend,
537 )
538 );
539 // Disable profile creation if user lacks permission
540 if (!CRM_Core_Permission::check('edit all contacts') && !CRM_Core_Permission::check('add contacts')) {
541 $settings['config']['entityRef']['contactCreate'] = FALSE;
542 }
543 $this->addSetting($settings);
544
545 // Give control of jQuery and _ back to the CMS - this loads last
546 $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE);
547
548 $this->addCoreStyles($region);
549 }
550 return $this;
551 }
552
553 /**
554 * This will add CiviCRM's standard CSS
555 *
556 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
557 * (like addCoreResources/addCoreStyles).
558 *
559 * @param string $region
560 * @return CRM_Core_Resources
561 */
562 public function addCoreStyles($region = 'html-header') {
563 if (!isset($this->addedCoreStyles[$region])) {
564 $this->addedCoreStyles[$region] = TRUE;
565
566 // Load custom or core css
567 $config = CRM_Core_Config::singleton();
568 if (!empty($config->customCSSURL)) {
569 $this->addStyleUrl($config->customCSSURL, 99, $region);
570 }
571 if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'disable_core_css')) {
572 $this->addStyleFile('civicrm', 'css/civicrm.css', -99, $region);
573 }
574 }
575 return $this;
576 }
577
578 /**
579 * Flushes cached translated strings
580 * @return CRM_Core_Resources
581 */
582 public function flushStrings() {
583 $this->cache->flush();
584 return $this;
585 }
586
587 /**
588 * Translate strings in a javascript file
589 *
590 * @param string $ext
591 * extension name.
592 * @param string $file
593 * file path.
594 * @return void
595 */
596 private function translateScript($ext, $file) {
597 // For each extension, maintain one cache record which
598 // includes parsed (translatable) strings for all its JS files.
599 $stringsByFile = $this->cache->get($ext); // array($file => array(...strings...))
600 if (!$stringsByFile) {
601 $stringsByFile = array();
602 }
603 if (!isset($stringsByFile[$file])) {
604 $filePath = $this->getPath($ext, $file);
605 if ($filePath && is_readable($filePath)) {
606 $stringsByFile[$file] = CRM_Utils_JS::parseStrings(file_get_contents($filePath));
607 }
608 else {
609 $stringsByFile[$file] = array();
610 }
611 $this->cache->set($ext, $stringsByFile);
612 }
613 $this->addString($stringsByFile[$file]);
614 }
615
616 /**
617 * Create dynamic script for localizing js widgets
618 *
619 * @return string
620 * javascript content
621 */
622 public static function outputLocalizationJS() {
623 CRM_Core_Page_AJAX::setJsHeaders();
624 $config = CRM_Core_Config::singleton();
625 $vars = array(
626 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)),
627 'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')),
628 'otherSearch' => json_encode(ts('Enter search term...')),
629 'entityRef' => array(
630 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(),
631 'filters' => self::getEntityRefFilters(),
632 ),
633 );
634 print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars);
635 CRM_Utils_System::civiExit();
636 }
637
638 /**
639 * List of core resources we add to every CiviCRM page
640 *
641 * @return array
642 */
643 public function coreResourceList() {
644 $config = CRM_Core_Config::singleton();
645 // Use minified files for production, uncompressed in debug mode
646 // Note, $this->addScriptFile would automatically search for the non-minified file in debug mode but this is probably faster
647 $min = $config->debug ? '' : '.min';
648
649 // Scripts needed by everyone, everywhere
650 // FIXME: This is too long; list needs finer-grained segmentation
651 $items = array(
652 "packages/jquery/jquery-1.11.1$min.js",
653 "packages/jquery/jquery-ui/jquery-ui$min.js",
654 "packages/jquery/jquery-ui/jquery-ui$min.css",
655 "packages/backbone/lodash.compat$min.js",
656 "packages/jquery/plugins/jquery.mousewheel$min.js",
657 "packages/jquery/plugins/select2/select2$min.js",
658 "packages/jquery/plugins/select2/select2.css",
659 "packages/jquery/plugins/jquery.tableHeader.js",
660 "packages/jquery/plugins/jquery.textarearesizer.js",
661 "packages/jquery/plugins/jquery.form$min.js",
662 "packages/jquery/plugins/jquery.timeentry$min.js",
663 "packages/jquery/plugins/jquery.blockUI$min.js",
664 "packages/jquery/plugins/DataTables/media/js/jquery.dataTables$min.js",
665 "packages/jquery/plugins/DataTables/media/css/jquery.dataTables$min.css",
666 "packages/jquery/plugins/jquery.validate$min.js",
667 "packages/jquery/plugins/jquery.ui.datepicker.validation.pack.js",
668 "js/Common.js",
669 "js/crm.ajax.js",
670 );
671
672 // These scripts are only needed by back-office users
673 if (CRM_Core_Permission::check('access CiviCRM')) {
674 $items[] = "packages/jquery/plugins/jquery.menu$min.js";
675 $items[] = "packages/jquery/css/menu.css";
676 $items[] = "packages/jquery/plugins/jquery.jeditable$min.js";
677 $items[] = "packages/jquery/plugins/jquery.notify$min.js";
678 $items[] = "js/jquery/jquery.crmeditable.js";
679 }
680
681 // JS for multilingual installations
682 if (!empty($config->languageLimit) && count($config->languageLimit) > 1 && CRM_Core_Permission::check('translate CiviCRM')) {
683 $items[] = "js/crm.multilingual.js";
684 }
685
686 // Enable administrators to edit option lists in a dialog
687 if (CRM_Core_Permission::check('administer CiviCRM') && $this->ajaxPopupsEnabled) {
688 $items[] = "js/crm.optionEdit.js";
689 }
690
691 // Add localized jQuery UI files
692 if ($config->lcMessages && $config->lcMessages != 'en_US') {
693 // Search for i18n file in order of specificity (try fr-CA, then fr)
694 list($lang) = explode('_', $config->lcMessages);
695 $path = "packages/jquery/jquery-ui/i18n";
696 foreach (array(str_replace('_', '-', $config->lcMessages), $lang) as $language) {
697 $localizationFile = "$path/datepicker-{$language}.js";
698 if ($this->getPath('civicrm', $localizationFile)) {
699 $items[] = $localizationFile;
700 break;
701 }
702 }
703 }
704
705 // CMS-specific resources
706 $config->userSystem->appendCoreResources($items);
707
708 return $items;
709 }
710
711 /**
712 * @return bool
713 * is this page request an ajax snippet?
714 */
715 public static function isAjaxMode() {
716 return in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array(
717 CRM_Core_Smarty::PRINT_SNIPPET,
718 CRM_Core_Smarty::PRINT_NOFORM,
719 CRM_Core_Smarty::PRINT_JSON
720 ));
721 }
722
723 /**
724 * Provide a list of available entityRef filters
725 * FIXME: This function doesn't really belong in this class
726 * @TODO: Provide a sane way to extend this list for other entities - a hook or??
727 * @return array
728 */
729 public static function getEntityRefFilters() {
730 $filters = array();
731
732 $filters['event'] = array(
733 array('key' => 'event_type_id', 'value' => ts('Event Type')),
734 array(
735 'key' => 'start_date',
736 'value' => ts('Start Date'),
737 'options' => array(
738 array('key' => '{">":"now"}', 'value' => ts('Upcoming')),
739 array('key' => '{"BETWEEN":["now - 3 month","now"]}', 'value' => ts('Past 3 Months')),
740 array('key' => '{"BETWEEN":["now - 6 month","now"]}', 'value' => ts('Past 6 Months')),
741 array('key' => '{"BETWEEN":["now - 1 year","now"]}', 'value' => ts('Past Year')),
742 )
743 ),
744 );
745
746 $filters['activity'] = array(
747 array('key' => 'activity_type_id', 'value' => ts('Activity Type')),
748 array('key' => 'status_id', 'value' => ts('Activity Status')),
749 );
750
751 $filters['contact'] = array(
752 array('key' => 'contact_type', 'value' => ts('Contact Type')),
753 array('key' => 'group', 'value' => ts('Group'), 'entity' => 'group_contact'),
754 array('key' => 'tag', 'value' => ts('Tag'), 'entity' => 'entity_tag'),
755 array('key' => 'state_province', 'value' => ts('State/Province'), 'entity' => 'address'),
756 array('key' => 'country', 'value' => ts('Country'), 'entity' => 'address'),
757 array('key' => 'gender_id', 'value' => ts('Gender')),
758 array('key' => 'is_deceased', 'value' => ts('Deceased')),
759 );
760
761 return $filters;
762 }
763 }