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