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