3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
11 use Civi\Core\Event\GenericHookEvent
;
14 * This class facilitates the loading of resources
15 * such as JavaScript files and CSS files.
17 * Any URLs generated for resources may include a 'cache-code'. By resetting the
18 * cache-code, one may force clients to re-download resource files (regardless of
19 * any HTTP caching rules).
21 * TODO: This is currently a thin wrapper over CRM_Core_Region. We
22 * should incorporte services for aggregation, minimization, etc.
25 * @copyright CiviCRM LLC https://civicrm.org/licensing
27 class CRM_Core_Resources
{
28 const DEFAULT_WEIGHT
= 0;
29 const DEFAULT_REGION
= 'page-footer';
32 * We don't have a container or dependency-injection, so use singleton instead
36 private static $_singleton = NULL;
39 * @var CRM_Extension_Mapper
41 private $extMapper = NULL;
44 * @var CRM_Core_Resources_Strings
46 private $strings = NULL;
49 * Added core resources.
51 * Format is ($regionName => bool).
55 protected $addedCoreResources = [];
60 * Format is ($regionName => bool).
64 protected $addedCoreStyles = [];
69 * Format is ($regionName => bool).
73 protected $addedSettings = [];
76 * A value to append to JS/CSS URLs to coerce cache resets.
80 protected $cacheCode = NULL;
83 * The name of a setting which persistently stores the cacheCode.
87 protected $cacheCodeKey = NULL;
90 * Are ajax popup screens enabled.
94 public $ajaxPopupsEnabled;
97 * @var \Civi\Core\Paths
102 * Get or set the single instance of CRM_Core_Resources.
104 * @param CRM_Core_Resources $instance
105 * New copy of the manager.
107 * @return CRM_Core_Resources
109 public static function singleton(CRM_Core_Resources
$instance = NULL) {
110 if ($instance !== NULL) {
111 self
::$_singleton = $instance;
113 if (self
::$_singleton === NULL) {
114 self
::$_singleton = Civi
::service('resources');
116 return self
::$_singleton;
120 * Construct a resource manager.
122 * @param CRM_Extension_Mapper $extMapper
123 * Map extension names to their base path or URLs.
124 * @param CRM_Core_Resources_Strings $strings
125 * JS-localization cache.
126 * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources
128 public function __construct($extMapper, $strings, $cacheCodeKey = NULL) {
129 $this->extMapper
= $extMapper;
130 $this->strings
= $strings;
131 $this->cacheCodeKey
= $cacheCodeKey;
132 if ($cacheCodeKey !== NULL) {
133 $this->cacheCode
= Civi
::settings()->get($cacheCodeKey);
135 if (!$this->cacheCode
) {
136 $this->resetCacheCode();
138 $this->ajaxPopupsEnabled
= (bool) Civi
::settings()->get('ajaxPopupsEnabled');
139 $this->paths
= Civi
::paths();
143 * Export permission data to the client to enable smarter GUIs.
145 * Note: Application security stems from the server's enforcement
146 * of the security logic (e.g. in the API permissions). There's no way
147 * the client can use this info to make the app more secure; however,
148 * it can produce a better-tuned (non-broken) UI.
150 * @param string|iterable $permNames
151 * List of permission names to check/export.
152 * @return CRM_Core_Resources
154 public function addPermissions($permNames) {
155 $this->getSettingRegion()->addPermissions($permNames);
160 * Add a JavaScript file to the current page using <SCRIPT SRC>.
163 * extension name; use 'civicrm' for core.
164 * @param string $file
165 * file path -- relative to the extension base dir.
167 * relative weight within a given region.
168 * @param string $region
169 * location within the file; 'html-header', 'page-header', 'page-footer'.
170 * @param bool|string $translate
171 * Whether to load translated strings for this file. Use one of:
172 * - FALSE: Do not load translated strings.
173 * - TRUE: Load translated strings. Use the $ext's default domain.
174 * - string: Load translated strings. Use a specific domain.
176 * @return CRM_Core_Resources
178 * @throws \CRM_Core_Exception
180 public function addScriptFile($ext, $file, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
, $translate = TRUE) {
181 CRM_Core_Region
::instance($region)->addScriptFile($ext, $file, [
183 'translate' => $translate,
184 'name' => "$ext:$file",
185 // Setting the name above may appear superfluous, but it preserves a historical quirk
186 // where Region::add() and Resources::addScriptFile() produce slightly different orderings..
192 * Add a JavaScript file to the current page using <SCRIPT SRC>.
196 * relative weight within a given region.
197 * @param string $region
198 * location within the file; 'html-header', 'page-header', 'page-footer'.
199 * @return CRM_Core_Resources
201 public function addScriptUrl($url, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
) {
202 CRM_Core_Region
::instance($region)->add([
206 // Setting the name above may appear superfluous, but it preserves a historical quirk
207 // where Region::add() and Resources::addScriptUrl() produce slightly different orderings..
213 * Add a JavaScript file to the current page using <SCRIPT SRC>.
215 * @param string $code
216 * JavaScript source code.
218 * relative weight within a given region.
219 * @param string $region
220 * location within the file; 'html-header', 'page-header', 'page-footer'.
221 * @return CRM_Core_Resources
223 public function addScript($code, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
) {
224 CRM_Core_Region
::instance($region)->add(['script' => $code, 'weight' => $weight]);
229 * Add JavaScript variables to CRM.vars
233 * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar'));
234 * Access var from javascript:
235 * CRM.vars.myNamespace.foo // "bar"
237 * @see https://docs.civicrm.org/dev/en/latest/standards/javascript/
239 * @param string $nameSpace
240 * Usually the name of your extension.
242 * @param string $region
243 * The region to add settings to (eg. for payment processors usually billing-block)
245 * @return CRM_Core_Resources
247 public function addVars($nameSpace, $vars, $region = NULL) {
248 $this->getSettingRegion($region)->addVars($nameSpace, $vars);
253 * Add JavaScript variables to the root of the CRM object.
254 * This function is usually reserved for low-level system use.
255 * Extensions and components should generally use addVars instead.
257 * @param array $settings
258 * @param string $region
259 * The region to add settings to (eg. for payment processors usually billing-block)
261 * @return CRM_Core_Resources
263 public function addSetting($settings, $region = NULL) {
264 $this->getSettingRegion($region)->addSetting($settings);
269 * Add JavaScript variables to the global CRM object via a callback function.
271 * @param callable $callable
272 * @return CRM_Core_Resources
274 public function addSettingsFactory($callable) {
275 $this->getSettingRegion()->addSettingsFactory($callable);
280 * Helper fn for addSettingsFactory.
282 public function getSettings() {
283 return $this->getSettingRegion()->getSettings();
287 * Add translated string to the js CRM object.
288 * It can then be retrived from the client-side ts() function
289 * Variable substitutions can happen from client-side
291 * Note: this function rarely needs to be called directly and is mostly for internal use.
292 * See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files
296 * CRM_Core_Resources::singleton()->addString('Hello');
297 * // The string is now available to javascript code i.e.
300 * Example with client-side substitutions:
302 * CRM_Core_Resources::singleton()->addString('Your %1 has been %2');
303 * // ts() in javascript works the same as in php, for example:
304 * ts('Your %1 has been %2', {1: objectName, 2: actionTaken});
306 * NOTE: This function does not work with server-side substitutions
307 * (as this might result in collisions and unwanted variable injections)
308 * Instead, use code like:
309 * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs)))));
310 * And from javascript access it at CRM.myNamespace.myString
312 * @param string|array $text
313 * @param string|null $domain
314 * @return CRM_Core_Resources
316 public function addString($text, $domain = 'civicrm') {
317 $this->getSettingRegion()->addString($text, $domain);
322 * Add a CSS file to the current page using <LINK HREF>.
325 * extension name; use 'civicrm' for core.
326 * @param string $file
327 * file path -- relative to the extension base dir.
329 * relative weight within a given region.
330 * @param string $region
331 * location within the file; 'html-header', 'page-header', 'page-footer'.
332 * @return CRM_Core_Resources
334 public function addStyleFile($ext, $file, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
) {
335 CRM_Core_Region
::instance($region)->addStyleFile($ext, $file, [
337 'name' => "$ext:$file",
338 // Setting the name above may appear superfluous, but it preserves a historical quirk
339 // where Region::add() and Resources::addScriptUrl() produce slightly different orderings..
345 * Add a CSS file to the current page using <LINK HREF>.
349 * relative weight within a given region.
350 * @param string $region
351 * location within the file; 'html-header', 'page-header', 'page-footer'.
352 * @return CRM_Core_Resources
354 public function addStyleUrl($url, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
) {
355 CRM_Core_Region
::instance($region)->add([
359 // Setting the name above may appear superfluous, but it preserves a historical quirk
360 // where Region::add() and Resources::addScriptUrl() produce slightly different orderings..
366 * Add a CSS content to the current page using <STYLE>.
368 * @param string $code
371 * relative weight within a given region.
372 * @param string $region
373 * location within the file; 'html-header', 'page-header', 'page-footer'.
374 * @return CRM_Core_Resources
376 public function addStyle($code, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
) {
377 CRM_Core_Region
::instance($region)->add(['style' => $code, 'weight' => $weight]);
382 * Determine file path of a resource provided by an extension.
385 * extension name; use 'civicrm' for core.
386 * @param string|null $file
387 * file path -- relative to the extension base dir.
389 * @return bool|string
390 * full file path or FALSE if not found
392 public function getPath($ext, $file = NULL) {
393 // TODO consider caching results
394 $base = $this->paths
->hasVariable($ext)
395 ?
rtrim($this->paths
->getVariable($ext, 'path'), '/')
396 : $this->extMapper
->keyToBasePath($ext);
397 if ($file === NULL) {
400 $path = $base . '/' . $file;
401 if (is_file($path)) {
408 * Determine public URL of a resource provided by an extension.
411 * extension name; use 'civicrm' for core.
412 * @param string $file
413 * file path -- relative to the extension base dir.
414 * @param bool $addCacheCode
416 * @return string, URL
418 public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) {
419 if ($file === NULL) {
423 $file = $this->addCacheCode($file);
425 // TODO consider caching results
426 $base = $this->paths
->hasVariable($ext)
427 ?
$this->paths
->getVariable($ext, 'url')
428 : ($this->extMapper
->keyToUrl($ext) . '/');
429 return $base . $file;
433 * Evaluate a glob pattern in the context of a particular extension.
436 * Extension name; use 'civicrm' for core.
437 * @param string|array $patterns
438 * Glob pattern; e.g. "*.html".
439 * @param null|int $flags
442 * List of matching files, relative to the extension base dir.
445 public function glob($ext, $patterns, $flags = NULL) {
446 $path = $this->getPath($ext);
447 $patterns = (array) $patterns;
449 foreach ($patterns as $pattern) {
450 if (preg_match(';^(assetBuilder|ext)://;', $pattern)) {
453 if (CRM_Utils_File
::isAbsolute($pattern)) {
455 $files = array_merge($files, (array) glob($pattern, $flags));
459 $files = array_merge($files, (array) glob("$path/$pattern", $flags));
462 // Deterministic order.
464 $files = array_unique($files);
465 return array_map(function ($file) use ($path) {
466 return CRM_Utils_File
::relativize($file, "$path/");
473 public function getCacheCode() {
474 return $this->cacheCode
;
479 * @return CRM_Core_Resources
481 public function setCacheCode($value) {
482 $this->cacheCode
= $value;
483 if ($this->cacheCodeKey
) {
484 Civi
::settings()->set($this->cacheCodeKey
, $value);
490 * @return CRM_Core_Resources
492 public function resetCacheCode() {
493 $this->setCacheCode(CRM_Utils_String
::createRandom(5, CRM_Utils_String
::ALPHANUMERIC
));
494 // Also flush cms resource cache if needed
495 CRM_Core_Config
::singleton()->userSystem
->clearResourceCache();
500 * This adds CiviCRM's standard css and js to the specified region of the document.
501 * It will only run once.
503 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
504 * (like addCoreResources/addCoreStyles).
506 * @param string $region
507 * @return CRM_Core_Resources
509 public function addCoreResources($region = 'html-header') {
510 if (!isset($this->addedCoreResources
[$region]) && !self
::isAjaxMode()) {
511 $this->addedCoreResources
[$region] = TRUE;
512 $config = CRM_Core_Config
::singleton();
514 // Add resources from coreResourceList
516 foreach ($this->coreResourceList($region) as $item) {
517 if (is_array($item)) {
518 $this->addSetting($item);
520 elseif (strpos($item, '.css')) {
521 $this->isFullyFormedUrl($item) ?
$this->addStyleUrl($item, -100, $region) : $this->addStyleFile('civicrm', $item, -100, $region);
523 elseif ($this->isFullyFormedUrl($item)) {
524 $this->addScriptUrl($item, $jsWeight++
, $region);
527 // Don't bother looking for ts() calls in packages, there aren't any
528 $translate = (substr($item, 0, 3) == 'js/');
529 $this->addScriptFile('civicrm', $item, $jsWeight++
, $region, $translate);
532 // Add global settings
535 'isFrontend' => $config->userFrameworkFrontend
,
538 // Disable profile creation if user lacks permission
539 if (!CRM_Core_Permission
::check('edit all contacts') && !CRM_Core_Permission
::check('add contacts')) {
540 $settings['config']['entityRef']['contactCreate'] = FALSE;
542 $this->addSetting($settings);
544 // Give control of jQuery and _ back to the CMS - this loads last
545 $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE);
547 $this->addCoreStyles($region);
553 * This will add CiviCRM's standard CSS
555 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
556 * (like addCoreResources/addCoreStyles).
558 * @param string $region
559 * @return CRM_Core_Resources
561 public function addCoreStyles($region = 'html-header') {
562 if (!isset($this->addedCoreStyles
[$region])) {
563 $this->addedCoreStyles
[$region] = TRUE;
565 // Load custom or core css
566 $config = CRM_Core_Config
::singleton();
567 if (!empty($config->customCSSURL
)) {
568 $customCSSURL = $this->addCacheCode($config->customCSSURL
);
569 $this->addStyleUrl($customCSSURL, 99, $region);
571 if (!Civi
::settings()->get('disable_core_css')) {
572 $this->addStyleFile('civicrm', 'css/civicrm.css', -99, $region);
574 // crm-i.css added ahead of other styles so it can be overridden by FA.
575 $this->addStyleFile('civicrm', 'css/crm-i.css', -101, $region);
581 * Flushes cached translated strings.
582 * @return CRM_Core_Resources
584 public function flushStrings() {
585 $this->strings
->flush();
590 * @return CRM_Core_Resources_Strings
592 public function getStrings() {
593 return $this->strings
;
597 * Create dynamic script for localizing js widgets.
599 public static function outputLocalizationJS() {
600 CRM_Core_Page_AJAX
::setJsHeaders();
601 $config = CRM_Core_Config
::singleton();
603 'moneyFormat' => json_encode(CRM_Utils_Money
::format(1234.56)),
604 'contactSearch' => json_encode($config->includeEmailInName ?
ts('Start typing a name or email...') : ts('Start typing a name...')),
605 'otherSearch' => json_encode(ts('Enter search term...')),
606 'entityRef' => self
::getEntityRefMetadata(),
607 'ajaxPopupsEnabled' => self
::singleton()->ajaxPopupsEnabled
,
608 'allowAlertAutodismissal' => (bool) Civi
::settings()->get('allow_alert_autodismissal'),
609 'resourceCacheCode' => self
::singleton()->getCacheCode(),
610 'locale' => CRM_Core_I18n
::getLocale(),
611 'cid' => (int) CRM_Core_Session
::getLoggedInContactID(),
613 print CRM_Core_Smarty
::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars);
614 CRM_Utils_System
::civiExit();
618 * List of core resources we add to every CiviCRM page.
620 * Note: non-compressed versions of .min files will be used in debug mode
622 * @param string $region
625 public function coreResourceList($region) {
626 $config = CRM_Core_Config
::singleton();
628 // Scripts needed by everyone, everywhere
629 // FIXME: This is too long; list needs finer-grained segmentation
631 "bower_components/jquery/dist/jquery.min.js",
632 "bower_components/jquery-ui/jquery-ui.min.js",
633 "bower_components/jquery-ui/themes/smoothness/jquery-ui.min.css",
634 "bower_components/lodash-compat/lodash.min.js",
635 "packages/jquery/plugins/jquery.mousewheel.min.js",
636 "bower_components/select2/select2.min.js",
637 "bower_components/select2/select2.min.css",
638 "bower_components/font-awesome/css/font-awesome.min.css",
639 "packages/jquery/plugins/jquery.form.min.js",
640 "packages/jquery/plugins/jquery.timeentry.min.js",
641 "packages/jquery/plugins/jquery.blockUI.min.js",
642 "bower_components/datatables/media/js/jquery.dataTables.min.js",
643 "bower_components/datatables/media/css/jquery.dataTables.min.css",
644 "bower_components/jquery-validation/dist/jquery.validate.min.js",
645 "bower_components/jquery-validation/dist/additional-methods.min.js",
646 "packages/jquery/plugins/jquery.ui.datepicker.validation.min.js",
648 "js/crm.datepicker.js",
650 "js/wysiwyg/crm.wysiwyg.js",
653 // Dynamic localization script
654 $items[] = $this->addCacheCode(
655 CRM_Utils_System
::url('civicrm/ajax/l10n-js/' . CRM_Core_I18n
::getLocale(),
656 ['cid' => CRM_Core_Session
::getLoggedInContactID()], FALSE, NULL, FALSE)
659 // add wysiwyg editor
660 $editor = Civi
::settings()->get('editor_id');
661 if ($editor == "CKEditor") {
662 CRM_Admin_Form_CKEditorConfig
::setConfigDefault();
665 'wysisygScriptLocation' => Civi
::paths()->getUrl("[civicrm.root]/js/wysiwyg/crm.ckeditor.js"),
666 'CKEditorCustomConfig' => CRM_Admin_Form_CKEditorConfig
::getConfigUrl(),
671 // These scripts are only needed by back-office users
672 if (CRM_Core_Permission
::check('access CiviCRM')) {
673 $items[] = "packages/jquery/plugins/jquery.tableHeader.js";
674 $items[] = "packages/jquery/plugins/jquery.notify.min.js";
677 $contactID = CRM_Core_Session
::getLoggedInContactID();
682 $contactID && !$config->userFrameworkFrontend
683 && CRM_Core_Permission
::check('access CiviCRM')
684 && !@constant
('CIVICRM_DISABLE_DEFAULT_MENU')
685 && !CRM_Core_Config
::isUpgradeMode()
687 $position = Civi
::settings()->get('menubar_position') ?
: 'over-cms-menu';
689 if ($position !== 'none') {
690 $items[] = 'bower_components/smartmenus/dist/jquery.smartmenus.min.js';
691 $items[] = 'bower_components/smartmenus/dist/addons/keyboard/jquery.smartmenus.keyboard.min.js';
692 $items[] = 'js/crm.menubar.js';
693 // @see CRM_Core_Resources::renderMenubarStylesheet
694 $items[] = Civi
::service('asset_builder')->getUrl('crm-menubar.css', [
695 'menubarColor' => Civi
::settings()->get('menubar_color'),
699 // Variables for crm.menubar.js
702 'position' => $position,
703 'qfKey' => CRM_Core_Key
::get('CRM_Contact_Controller_Search', TRUE),
704 'cacheCode' => CRM_Core_BAO_Navigation
::getCacheKey($contactID),
709 // JS for multilingual installations
710 if (!empty($config->languageLimit
) && count($config->languageLimit
) > 1 && CRM_Core_Permission
::check('translate CiviCRM')) {
711 $items[] = "js/crm.multilingual.js";
714 // Enable administrators to edit option lists in a dialog
715 if (CRM_Core_Permission
::check('administer CiviCRM') && $this->ajaxPopupsEnabled
) {
716 $items[] = "js/crm.optionEdit.js";
719 $tsLocale = CRM_Core_I18n
::getLocale();
720 // Add localized jQuery UI files
721 if ($tsLocale && $tsLocale != 'en_US') {
722 // Search for i18n file in order of specificity (try fr-CA, then fr)
723 list($lang) = explode('_', $tsLocale);
724 $path = "bower_components/jquery-ui/ui/i18n";
725 foreach ([str_replace('_', '-', $tsLocale), $lang] as $language) {
726 $localizationFile = "$path/datepicker-{$language}.js";
727 if ($this->getPath('civicrm', $localizationFile)) {
728 $items[] = $localizationFile;
734 // Allow hooks to modify this list
735 CRM_Utils_Hook
::coreResourceList($items, $region);
737 // Oof, existing listeners would expect $items to typically begin with 'bower_components/' or 'packages/'
738 // (using an implicit base of `[civicrm.root]`). We preserve the hook contract and cleanup $items post-hook.
740 'bower_components' => rtrim(Civi
::paths()->getUrl('[civicrm.bower]/.', 'absolute'), '/'),
741 'packages' => rtrim(Civi
::paths()->getUrl('[civicrm.packages]/.', 'absolute'), '/'),
743 $filter = function($m) use ($map) {
744 return $map[$m[1]] . $m[2];
746 $items = array_map(function($item) use ($filter) {
747 return is_array($item) ?
$item : preg_replace_callback(';^(bower_components|packages)(/.*);', $filter, $item);
755 * is this page request an ajax snippet?
757 public static function isAjaxMode() {
758 if (in_array(CRM_Utils_Array
::value('snippet', $_REQUEST), [
759 CRM_Core_Smarty
::PRINT_SNIPPET
,
760 CRM_Core_Smarty
::PRINT_NOFORM
,
761 CRM_Core_Smarty
::PRINT_JSON
,
766 list($arg0, $arg1) = array_pad(explode('/', CRM_Utils_System
::currentPath()), 2, '');
767 return ($arg0 === 'civicrm' && in_array($arg1, ['ajax', 'angularprofiles', 'asset']));
771 * @param \Civi\Core\Event\GenericHookEvent $e
772 * @see \CRM_Utils_Hook::buildAsset()
774 public static function renderMenubarStylesheet(GenericHookEvent
$e) {
775 if ($e->asset
!== 'crm-menubar.css') {
778 $e->mimeType
= 'text/css';
780 $config = CRM_Core_Config
::singleton();
781 $cms = strtolower($config->userFramework
);
782 $cms = $cms === 'drupal' ?
'drupal7' : $cms;
784 'bower_components/smartmenus/dist/css/sm-core-css.css',
785 'css/crm-menubar.css',
786 "css/menubar-$cms.css",
788 foreach ($items as $item) {
789 $content .= file_get_contents(self
::singleton()->getPath('civicrm', $item));
791 $params = $e->params
;
792 // "color" is deprecated in favor of the more specific "menubarColor"
793 $menubarColor = $params['color'] ??
$params['menubarColor'];
795 '$resourceBase' => rtrim($config->resourceBase
, '/'),
796 '$menubarHeight' => $params['height'] . 'px',
797 '$breakMin' => $params['breakpoint'] . 'px',
798 '$breakMax' => ($params['breakpoint'] - 1) . 'px',
799 '$menubarColor' => $menubarColor,
800 '$menuItemColor' => $params['menuItemColor'] ??
$menubarColor,
801 '$highlightColor' => $params['highlightColor'] ?? CRM_Utils_Color
::getHighlight($menubarColor),
802 '$textColor' => $params['textColor'] ?? CRM_Utils_Color
::getContrast($menubarColor, '#333', '#ddd'),
804 $vars['$highlightTextColor'] = $params['highlightTextColor'] ?? CRM_Utils_Color
::getContrast($vars['$highlightColor'], '#333', '#ddd');
805 $e->content
= str_replace(array_keys($vars), array_values($vars), $content);
809 * Provide a list of available entityRef filters.
813 public static function getEntityRefMetadata() {
818 $config = CRM_Core_Config
::singleton();
820 $disabledComponents = [];
821 $dao = CRM_Core_DAO
::executeQuery("SELECT name, namespace FROM civicrm_component");
822 while ($dao->fetch()) {
823 if (!in_array($dao->name
, $config->enableComponents
)) {
824 $disabledComponents[$dao->name
] = $dao->namespace;
828 foreach (CRM_Core_DAO_AllCoreTables
::daoToClass() as $entity => $daoName) {
829 // Skip DAOs of disabled components
830 foreach ($disabledComponents as $nameSpace) {
831 if (strpos($daoName, $nameSpace) === 0) {
835 $baoName = str_replace('_DAO_', '_BAO_', $daoName);
836 if (class_exists($baoName)) {
837 $filters = $baoName::getEntityRefFilters();
839 $data['filters'][$entity] = $filters;
841 if (is_callable([$baoName, 'getEntityRefCreateLinks'])) {
842 $createLinks = $baoName::getEntityRefCreateLinks();
844 $data['links'][$entity] = $createLinks;
850 CRM_Utils_Hook
::entityRefFilters($data['filters'], $data['links']);
856 * Determine the minified file name.
859 * @param string $file
861 * An updated $fileName. If a minified version exists and is supported by
862 * system policy, the minified version will be returned. Otherwise, the original.
864 public function filterMinify($ext, $file) {
865 if (CRM_Core_Config
::singleton()->debug
&& strpos($file, '.min.') !== FALSE) {
866 $nonMiniFile = str_replace('.min.', '.', $file);
867 if ($this->getPath($ext, $nonMiniFile)) {
868 $file = $nonMiniFile;
878 public function addCacheCode($url) {
879 $hasQuery = strpos($url, '?') !== FALSE;
880 $operator = $hasQuery ?
'&' : '?';
882 return $url . $operator . 'r=' . $this->cacheCode
;
886 * Checks if the given URL is fully-formed
892 public static function isFullyFormedUrl($url) {
893 return (substr($url, 0, 4) === 'http') ||
(substr($url, 0, 1) === '/');
897 * @param string|NULL $region
898 * Optional request for a specific region. If NULL/omitted, use global default.
899 * @return \CRM_Core_Region
901 private function getSettingRegion($region = NULL) {
902 $region = $region ?
: (self
::isAjaxMode() ?
'ajax-snippet' : 'html-header');
903 return CRM_Core_Region
::instance($region);