commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Core / Resources.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 */
53 private static $_singleton = NULL;
54
55 /**
56 * @var CRM_Extension_Mapper
57 */
58 private $extMapper = NULL;
59
60 /**
61 * @var CRM_Core_Resources_Strings
62 */
63 private $strings = NULL;
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
96 /**
97 * @var bool
98 */
99 public $ajaxPopupsEnabled;
100
101 /**
102 * Get or set the single instance of CRM_Core_Resources.
103 *
104 * @param CRM_Core_Resources $instance
105 * 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,
121 CRM_Core_Config::isUpgradeMode() ? NULL : 'resCacheCode'
122 );
123 }
124 return self::$_singleton;
125 }
126
127 /**
128 * Construct a resource manager.
129 *
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.
134 * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources
135 */
136 public function __construct($extMapper, $cache, $cacheCodeKey = NULL) {
137 $this->extMapper = $extMapper;
138 $this->strings = new CRM_Core_Resources_Strings($cache);
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 }
143 if (!$this->cacheCode) {
144 $this->resetCacheCode();
145 }
146 $this->ajaxPopupsEnabled = (bool) CRM_Core_BAO_Setting::getItem(
147 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE
148 );
149 }
150
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
174 /**
175 * Add a JavaScript file to the current page using <SCRIPT SRC>.
176 *
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'.
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.
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) {
195 $domain = ($translate === TRUE) ? $ext : $translate;
196 $this->addString($this->strings->get($domain, $this->getPath($ext, $file), 'text/javascript'), $domain);
197 }
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 }
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 *
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'.
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(
220 'name' => $url,
221 'type' => 'scriptUrl',
222 'scriptUrl' => $url,
223 'weight' => $weight,
224 'region' => $region,
225 ));
226 return $this;
227 }
228
229 /**
230 * Add a JavaScript file to the current page using <SCRIPT SRC>.
231 *
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'.
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(
242 // 'name' => automatic
243 'type' => 'script',
244 'script' => $code,
245 'weight' => $weight,
246 'region' => $region,
247 ));
248 return $this;
249 }
250
251 /**
252 * Add JavaScript variables to CRM.vars
253 *
254 * Example:
255 * From the server:
256 * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar'));
257 * Access var from javascript:
258 * CRM.vars.myNamespace.foo // "bar"
259 *
260 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference
261 *
262 * @param string $nameSpace
263 * Usually the name of your extension.
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 *
279 * @param array $settings
280 * @return CRM_Core_Resources
281 */
282 public function addSetting($settings) {
283 $this->settings = $this->mergeSettings($this->settings, $settings);
284 if (!$this->addedSettings) {
285 $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header';
286 $resources = $this;
287 CRM_Core_Region::instance($region)->add(array(
288 'callback' => function (&$snippet, &$html) use ($resources) {
289 $html .= "\n" . $resources->renderSetting();
290 },
291 'weight' => -100000,
292 ));
293 $this->addedSettings = TRUE;
294 }
295 return $this;
296 }
297
298 /**
299 * Add JavaScript variables to the global CRM object via a callback function.
300 *
301 * @param callable $callable
302 * @return CRM_Core_Resources
303 */
304 public function addSettingsFactory($callable) {
305 // Make sure our callback has been registered
306 $this->addSetting(array());
307 $this->settingsFactories[] = $callable;
308 return $this;
309 }
310
311 /**
312 * Helper fn for addSettingsFactory.
313 */
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
325 * @return array
326 * combination of $settings and $additions
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 /**
339 * Helper fn for addSetting.
340 * Render JavaScript variables for the global CRM object.
341 *
342 * @return string
343 */
344 public function renderSetting() {
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 }
353
354 //return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js);
355 return sprintf("<script type=\"text/javascript\">\n// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3\n%s\n// @license-end\n</script>\n", $js);
356 }
357
358 /**
359 * Add translated string to the js CRM object.
360 * It can then be retrived from the client-side ts() function
361 * Variable substitutions can happen from client-side
362 *
363 * Note: this function rarely needs to be called directly and is mostly for internal use.
364 * See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files
365 *
366 * Simple example:
367 * // From php:
368 * CRM_Core_Resources::singleton()->addString('Hello');
369 * // The string is now available to javascript code i.e.
370 * ts('Hello');
371 *
372 * Example with client-side substitutions:
373 * // From php:
374 * CRM_Core_Resources::singleton()->addString('Your %1 has been %2');
375 * // ts() in javascript works the same as in php, for example:
376 * ts('Your %1 has been %2', {1: objectName, 2: actionTaken});
377 *
378 * NOTE: This function does not work with server-side substitutions
379 * (as this might result in collisions and unwanted variable injections)
380 * Instead, use code like:
381 * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs)))));
382 * And from javascript access it at CRM.myNamespace.myString
383 *
384 * @param string|array $text
385 * @param string|NULL $domain
386 * @return CRM_Core_Resources
387 */
388 public function addString($text, $domain = 'civicrm') {
389 foreach ((array) $text as $str) {
390 $translated = ts($str, array(
391 'domain' => ($domain == 'civicrm') ? NULL : array($domain, NULL),
392 'raw' => TRUE,
393 ));
394
395 // We only need to push this string to client if the translation
396 // is actually different from the original
397 if ($translated != $str) {
398 $bucket = $domain == 'civicrm' ? 'strings' : 'strings::' . $domain;
399 $this->addSetting(array(
400 $bucket => array($str => $translated),
401 ));
402 }
403 }
404 return $this;
405 }
406
407 /**
408 * Add a CSS file to the current page using <LINK HREF>.
409 *
410 * @param string $ext
411 * extension name; use 'civicrm' for core.
412 * @param string $file
413 * file path -- relative to the extension base dir.
414 * @param int $weight
415 * relative weight within a given region.
416 * @param string $region
417 * location within the file; 'html-header', 'page-header', 'page-footer'.
418 * @return CRM_Core_Resources
419 */
420 public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
421 return $this->addStyleUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
422 }
423
424 /**
425 * Add a CSS file to the current page using <LINK HREF>.
426 *
427 * @param string $url
428 * @param int $weight
429 * relative weight within a given region.
430 * @param string $region
431 * location within the file; 'html-header', 'page-header', 'page-footer'.
432 * @return CRM_Core_Resources
433 */
434 public function addStyleUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
435 CRM_Core_Region::instance($region)->add(array(
436 'name' => $url,
437 'type' => 'styleUrl',
438 'styleUrl' => $url,
439 'weight' => $weight,
440 'region' => $region,
441 ));
442 return $this;
443 }
444
445 /**
446 * Add a CSS content to the current page using <STYLE>.
447 *
448 * @param string $code
449 * CSS source code.
450 * @param int $weight
451 * relative weight within a given region.
452 * @param string $region
453 * location within the file; 'html-header', 'page-header', 'page-footer'.
454 * @return CRM_Core_Resources
455 */
456 public function addStyle($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
457 CRM_Core_Region::instance($region)->add(array(
458 // 'name' => automatic
459 'type' => 'style',
460 'style' => $code,
461 'weight' => $weight,
462 'region' => $region,
463 ));
464 return $this;
465 }
466
467 /**
468 * Determine file path of a resource provided by an extension.
469 *
470 * @param string $ext
471 * extension name; use 'civicrm' for core.
472 * @param string|NULL $file
473 * file path -- relative to the extension base dir.
474 *
475 * @return bool|string
476 * full file path or FALSE if not found
477 */
478 public function getPath($ext, $file = NULL) {
479 // TODO consider caching results
480 if ($file === NULL) {
481 return $this->extMapper->keyToBasePath($ext);
482 }
483 $path = $this->extMapper->keyToBasePath($ext) . '/' . $file;
484 if (is_file($path)) {
485 return $path;
486 }
487 return FALSE;
488 }
489
490 /**
491 * Determine public URL of a resource provided by an extension.
492 *
493 * @param string $ext
494 * extension name; use 'civicrm' for core.
495 * @param string $file
496 * file path -- relative to the extension base dir.
497 * @param bool $addCacheCode
498 *
499 * @return string, URL
500 */
501 public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) {
502 if ($file === NULL) {
503 $file = '';
504 }
505 if ($addCacheCode) {
506 $file .= '?r=' . $this->getCacheCode();
507 }
508 // TODO consider caching results
509 return $this->extMapper->keyToUrl($ext) . '/' . $file;
510 }
511
512 /**
513 * Evaluate a glob pattern in the context of a particular extension.
514 *
515 * @param string $ext
516 * Extension name; use 'civicrm' for core.
517 * @param string|array $patterns
518 * Glob pattern; e.g. "*.html".
519 * @param null|int $flags
520 * See glob().
521 * @return array
522 * List of matching files, relative to the extension base dir.
523 * @see glob()
524 */
525 public function glob($ext, $patterns, $flags = NULL) {
526 $path = $this->getPath($ext);
527 $patterns = (array) $patterns;
528 $files = array();
529 foreach ($patterns as $pattern) {
530 if (CRM_Utils_File::isAbsolute($pattern)) {
531 // Absolute path.
532 $files = array_merge($files, (array) glob($pattern, $flags));
533 }
534 else {
535 // Relative path.
536 $files = array_merge($files, (array) glob("$path/$pattern", $flags));
537 }
538 }
539 sort($files); // Deterministic order.
540 $files = array_unique($files);
541 return array_map(function ($file) use ($path) {
542 return CRM_Utils_File::relativize($file, "$path/");
543 }, $files);
544 }
545
546 /**
547 * @return string
548 */
549 public function getCacheCode() {
550 return $this->cacheCode;
551 }
552
553 /**
554 * @param $value
555 * @return CRM_Core_Resources
556 */
557 public function setCacheCode($value) {
558 $this->cacheCode = $value;
559 if ($this->cacheCodeKey) {
560 CRM_Core_BAO_Setting::setItem($value, CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, $this->cacheCodeKey);
561 }
562 return $this;
563 }
564
565 /**
566 * @return CRM_Core_Resources
567 */
568 public function resetCacheCode() {
569 $this->setCacheCode(CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC));
570 // Also flush cms resource cache if needed
571 CRM_Core_Config::singleton()->userSystem->clearResourceCache();
572 return $this;
573 }
574
575 /**
576 * This adds CiviCRM's standard css and js to the specified region of the document.
577 * It will only run once.
578 *
579 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
580 * (like addCoreResources/addCoreStyles).
581 *
582 * @param string $region
583 * @return CRM_Core_Resources
584 */
585 public function addCoreResources($region = 'html-header') {
586 if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) {
587 $this->addedCoreResources[$region] = TRUE;
588 $config = CRM_Core_Config::singleton();
589
590 // Add resources from coreResourceList
591 $jsWeight = -9999;
592 foreach ($this->coreResourceList($region) as $file) {
593 if (substr($file, -2) == 'js') {
594 // Don't bother looking for ts() calls in packages, there aren't any
595 $translate = (substr($file, 0, 3) == 'js/');
596 $this->addScriptFile('civicrm', $file, $jsWeight++, $region, $translate);
597 }
598 else {
599 $this->addStyleFile('civicrm', $file, -100, $region);
600 }
601 }
602
603 // Dynamic localization script
604 $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $config->lcMessages, array('r' => $this->getCacheCode())), $jsWeight++, $region);
605
606 // Add global settings
607 $settings = array(
608 'config' => array(
609 'isFrontend' => $config->userFrameworkFrontend,
610 ),
611 );
612 // Disable profile creation if user lacks permission
613 if (!CRM_Core_Permission::check('edit all contacts') && !CRM_Core_Permission::check('add contacts')) {
614 $settings['config']['entityRef']['contactCreate'] = FALSE;
615 }
616 $this->addSetting($settings);
617
618 // Give control of jQuery and _ back to the CMS - this loads last
619 $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE);
620
621 $this->addCoreStyles($region);
622 }
623 return $this;
624 }
625
626 /**
627 * This will add CiviCRM's standard CSS
628 *
629 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
630 * (like addCoreResources/addCoreStyles).
631 *
632 * @param string $region
633 * @return CRM_Core_Resources
634 */
635 public function addCoreStyles($region = 'html-header') {
636 if (!isset($this->addedCoreStyles[$region])) {
637 $this->addedCoreStyles[$region] = TRUE;
638
639 // Load custom or core css
640 $config = CRM_Core_Config::singleton();
641 if (!empty($config->customCSSURL)) {
642 $this->addStyleUrl($config->customCSSURL, 99, $region);
643 }
644 if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'disable_core_css')) {
645 $this->addStyleFile('civicrm', 'css/civicrm.css', -99, $region);
646 }
647 }
648 return $this;
649 }
650
651 /**
652 * Flushes cached translated strings.
653 * @return CRM_Core_Resources
654 */
655 public function flushStrings() {
656 $this->strings->flush();
657 return $this;
658 }
659
660 /**
661 * @return CRM_Core_Resources_Strings
662 */
663 public function getStrings() {
664 return $this->strings;
665 }
666
667 /**
668 * Create dynamic script for localizing js widgets.
669 */
670 public static function outputLocalizationJS() {
671 CRM_Core_Page_AJAX::setJsHeaders();
672 $config = CRM_Core_Config::singleton();
673 $vars = array(
674 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)),
675 'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')),
676 'otherSearch' => json_encode(ts('Enter search term...')),
677 'entityRef' => array(
678 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(),
679 'filters' => self::getEntityRefFilters(),
680 ),
681 'ajaxPopupsEnabled' => self::singleton()->ajaxPopupsEnabled,
682 );
683 print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars);
684 CRM_Utils_System::civiExit();
685 }
686
687 /**
688 * List of core resources we add to every CiviCRM page.
689 *
690 * @param string $region
691 * @return array
692 */
693 public function coreResourceList($region) {
694 $config = CRM_Core_Config::singleton();
695 // Use minified files for production, uncompressed in debug mode
696 // Note, $this->addScriptFile would automatically search for the non-minified file in debug mode but this is probably faster
697 $min = $config->debug ? '' : '.min';
698
699 // Scripts needed by everyone, everywhere
700 // FIXME: This is too long; list needs finer-grained segmentation
701 $items = array(
702 "bower_components/jquery/dist/jquery.min.js",
703 "bower_components/jquery-ui/jquery-ui.min.js",
704 "bower_components/jquery-ui/themes/smoothness/jquery-ui$min.css",
705 "bower_components/lodash-compat/lodash.min.js",
706 "packages/jquery/plugins/jquery.mousewheel$min.js",
707 "bower_components/select2/select2$min.js",
708 "bower_components/select2/select2$min.css",
709 "packages/jquery/plugins/jquery.tableHeader.js",
710 "packages/jquery/plugins/jquery.textarearesizer.js",
711 "packages/jquery/plugins/jquery.form$min.js",
712 "packages/jquery/plugins/jquery.timeentry$min.js",
713 "packages/jquery/plugins/jquery.blockUI$min.js",
714 "packages/jquery/plugins/DataTables/media/js/jquery.dataTables$min.js",
715 "packages/jquery/plugins/DataTables/media/css/jquery.dataTables$min.css",
716 "bower_components/jquery-validation/dist/jquery.validate$min.js",
717 "packages/jquery/plugins/jquery.ui.datepicker.validation.pack.js",
718 "js/Common.js",
719 "js/crm.ajax.js",
720 );
721
722 // These scripts are only needed by back-office users
723 if (CRM_Core_Permission::check('access CiviCRM')) {
724 $items[] = "packages/jquery/plugins/jquery.menu$min.js";
725 $items[] = "css/civicrmNavigation.css";
726 $items[] = "packages/jquery/plugins/jquery.jeditable$min.js";
727 $items[] = "packages/jquery/plugins/jquery.notify$min.js";
728 $items[] = "js/jquery/jquery.crmeditable.js";
729 }
730
731 // JS for multilingual installations
732 if (!empty($config->languageLimit) && count($config->languageLimit) > 1 && CRM_Core_Permission::check('translate CiviCRM')) {
733 $items[] = "js/crm.multilingual.js";
734 }
735
736 // Enable administrators to edit option lists in a dialog
737 if (CRM_Core_Permission::check('administer CiviCRM') && $this->ajaxPopupsEnabled) {
738 $items[] = "js/crm.optionEdit.js";
739 }
740
741 // Add localized jQuery UI files
742 if ($config->lcMessages && $config->lcMessages != 'en_US') {
743 // Search for i18n file in order of specificity (try fr-CA, then fr)
744 list($lang) = explode('_', $config->lcMessages);
745 $path = "bower_components/jquery-ui/ui/i18n";
746 foreach (array(str_replace('_', '-', $config->lcMessages), $lang) as $language) {
747 $localizationFile = "$path/datepicker-{$language}.js";
748 if ($this->getPath('civicrm', $localizationFile)) {
749 $items[] = $localizationFile;
750 break;
751 }
752 }
753 }
754
755 // Allow hooks to modify this list
756 CRM_Utils_Hook::coreResourceList($items, $region);
757
758 return $items;
759 }
760
761 /**
762 * @return bool
763 * is this page request an ajax snippet?
764 */
765 public static function isAjaxMode() {
766 return in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array(
767 CRM_Core_Smarty::PRINT_SNIPPET,
768 CRM_Core_Smarty::PRINT_NOFORM,
769 CRM_Core_Smarty::PRINT_JSON,
770 ));
771 }
772
773 /**
774 * Provide a list of available entityRef filters.
775 * FIXME: This function doesn't really belong in this class
776 * @TODO: Provide a sane way to extend this list for other entities - a hook or??
777 * @return array
778 */
779 public static function getEntityRefFilters() {
780 $filters = array();
781
782 $filters['event'] = array(
783 array('key' => 'event_type_id', 'value' => ts('Event Type')),
784 array(
785 'key' => 'start_date',
786 'value' => ts('Start Date'),
787 'options' => array(
788 array('key' => '{">":"now"}', 'value' => ts('Upcoming')),
789 array('key' => '{"BETWEEN":["now - 3 month","now"]}', 'value' => ts('Past 3 Months')),
790 array('key' => '{"BETWEEN":["now - 6 month","now"]}', 'value' => ts('Past 6 Months')),
791 array('key' => '{"BETWEEN":["now - 1 year","now"]}', 'value' => ts('Past Year')),
792 ),
793 ),
794 );
795
796 $filters['activity'] = array(
797 array('key' => 'activity_type_id', 'value' => ts('Activity Type')),
798 array('key' => 'status_id', 'value' => ts('Activity Status')),
799 );
800
801 $filters['contact'] = array(
802 array('key' => 'contact_type', 'value' => ts('Contact Type')),
803 array('key' => 'group', 'value' => ts('Group'), 'entity' => 'group_contact'),
804 array('key' => 'tag', 'value' => ts('Tag'), 'entity' => 'entity_tag'),
805 array('key' => 'state_province', 'value' => ts('State/Province'), 'entity' => 'address'),
806 array('key' => 'country', 'value' => ts('Country'), 'entity' => 'address'),
807 array('key' => 'gender_id', 'value' => ts('Gender')),
808 array('key' => 'is_deceased', 'value' => ts('Deceased')),
809 );
810
811 return $filters;
812 }
813
814 }