3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
27 use Civi\Core\Event\GenericHookEvent
;
30 * This class facilitates the loading of resources
31 * such as JavaScript files and CSS files.
33 * Any URLs generated for resources may include a 'cache-code'. By resetting the
34 * cache-code, one may force clients to re-download resource files (regardless of
35 * any HTTP caching rules).
37 * TODO: This is currently a thin wrapper over CRM_Core_Region. We
38 * should incorporte services for aggregation, minimization, etc.
41 * @copyright CiviCRM LLC (c) 2004-2019
45 class CRM_Core_Resources
{
46 const DEFAULT_WEIGHT
= 0;
47 const DEFAULT_REGION
= 'page-footer';
50 * We don't have a container or dependency-injection, so use singleton instead
54 private static $_singleton = NULL;
57 * @var CRM_Extension_Mapper
59 private $extMapper = NULL;
62 * @var CRM_Core_Resources_Strings
64 private $strings = NULL;
67 * @var array free-form data tree
69 protected $settings = [];
70 protected $addedSettings = FALSE;
73 * @var array of callables
75 protected $settingsFactories = [];
78 * @var array ($regionName => bool)
80 protected $addedCoreResources = [];
83 * @var array ($regionName => bool)
85 protected $addedCoreStyles = [];
88 * @var string a value to append to JS/CSS URLs to coerce cache resets
90 protected $cacheCode = NULL;
93 * @var string the name of a setting which persistently stores the cacheCode
95 protected $cacheCodeKey = NULL;
100 public $ajaxPopupsEnabled;
103 * @var \Civi\Core\Paths
108 * Get or set the single instance of CRM_Core_Resources.
110 * @param CRM_Core_Resources $instance
111 * New copy of the manager.
112 * @return CRM_Core_Resources
114 public static function singleton(CRM_Core_Resources
$instance = NULL) {
115 if ($instance !== NULL) {
116 self
::$_singleton = $instance;
118 if (self
::$_singleton === NULL) {
119 self
::$_singleton = Civi
::service('resources');
121 return self
::$_singleton;
125 * Construct a resource manager.
127 * @param CRM_Extension_Mapper $extMapper
128 * Map extension names to their base path or URLs.
129 * @param CRM_Utils_Cache_Interface $cache
130 * JS-localization cache.
131 * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources
133 public function __construct($extMapper, $cache, $cacheCodeKey = NULL) {
134 $this->extMapper
= $extMapper;
135 $this->strings
= new CRM_Core_Resources_Strings($cache);
136 $this->cacheCodeKey
= $cacheCodeKey;
137 if ($cacheCodeKey !== NULL) {
138 $this->cacheCode
= Civi
::settings()->get($cacheCodeKey);
140 if (!$this->cacheCode
) {
141 $this->resetCacheCode();
143 $this->ajaxPopupsEnabled
= (bool) Civi
::settings()->get('ajaxPopupsEnabled');
144 $this->paths
= Civi
::paths();
148 * Export permission data to the client to enable smarter GUIs.
150 * Note: Application security stems from the server's enforcement
151 * of the security logic (e.g. in the API permissions). There's no way
152 * the client can use this info to make the app more secure; however,
153 * it can produce a better-tuned (non-broken) UI.
155 * @param array $permNames
156 * List of permission names to check/export.
157 * @return CRM_Core_Resources
159 public function addPermissions($permNames) {
160 $permNames = (array) $permNames;
162 foreach ($permNames as $permName) {
163 $perms[$permName] = CRM_Core_Permission
::check($permName);
165 return $this->addSetting([
166 'permissions' => $perms,
171 * Add a JavaScript file to the current page using <SCRIPT SRC>.
174 * extension name; use 'civicrm' for core.
175 * @param string $file
176 * file path -- relative to the extension base dir.
178 * relative weight within a given region.
179 * @param string $region
180 * location within the file; 'html-header', 'page-header', 'page-footer'.
181 * @param bool|string $translate
182 * Whether to load translated strings for this file. Use one of:
183 * - FALSE: Do not load translated strings.
184 * - TRUE: Load translated strings. Use the $ext's default domain.
185 * - string: Load translated strings. Use a specific domain.
187 * @return CRM_Core_Resources
189 public function addScriptFile($ext, $file, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
, $translate = TRUE) {
191 $domain = ($translate === TRUE) ?
$ext : $translate;
192 $this->addString($this->strings
->get($domain, $this->getPath($ext, $file), 'text/javascript'), $domain);
194 $this->resolveFileName($file, $ext);
195 return $this->addScriptUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
199 * Add a JavaScript file to the current page using <SCRIPT SRC>.
203 * relative weight within a given region.
204 * @param string $region
205 * location within the file; 'html-header', 'page-header', 'page-footer'.
206 * @return CRM_Core_Resources
208 public function addScriptUrl($url, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
) {
209 CRM_Core_Region
::instance($region)->add([
211 'type' => 'scriptUrl',
220 * Add a JavaScript file to the current page using <SCRIPT SRC>.
222 * @param string $code
223 * JavaScript source code.
225 * relative weight within a given region.
226 * @param string $region
227 * location within the file; 'html-header', 'page-header', 'page-footer'.
228 * @return CRM_Core_Resources
230 public function addScript($code, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
) {
231 CRM_Core_Region
::instance($region)->add([
232 // 'name' => automatic
242 * Add JavaScript variables to CRM.vars
246 * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar'));
247 * Access var from javascript:
248 * CRM.vars.myNamespace.foo // "bar"
250 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference
252 * @param string $nameSpace
253 * Usually the name of your extension.
255 * @return CRM_Core_Resources
257 public function addVars($nameSpace, $vars) {
258 $existing = CRM_Utils_Array
::value($nameSpace, CRM_Utils_Array
::value('vars', $this->settings
), []);
259 $vars = $this->mergeSettings($existing, $vars);
260 $this->addSetting(['vars' => [$nameSpace => $vars]]);
265 * Add JavaScript variables to the root of the CRM object.
266 * This function is usually reserved for low-level system use.
267 * Extensions and components should generally use addVars instead.
269 * @param array $settings
270 * @return CRM_Core_Resources
272 public function addSetting($settings) {
273 $this->settings
= $this->mergeSettings($this->settings
, $settings);
274 if (!$this->addedSettings
) {
275 $region = self
::isAjaxMode() ?
'ajax-snippet' : 'html-header';
277 CRM_Core_Region
::instance($region)->add([
278 'callback' => function (&$snippet, &$html) use ($resources) {
279 $html .= "\n" . $resources->renderSetting();
283 $this->addedSettings
= TRUE;
289 * Add JavaScript variables to the global CRM object via a callback function.
291 * @param callable $callable
292 * @return CRM_Core_Resources
294 public function addSettingsFactory($callable) {
295 // Make sure our callback has been registered
296 $this->addSetting([]);
297 $this->settingsFactories
[] = $callable;
302 * Helper fn for addSettingsFactory.
304 public function getSettings() {
305 $result = $this->settings
;
306 foreach ($this->settingsFactories
as $callable) {
307 $result = $this->mergeSettings($result, $callable());
309 CRM_Utils_Hook
::alterResourceSettings($result);
314 * @param array $settings
315 * @param array $additions
317 * combination of $settings and $additions
319 protected function mergeSettings($settings, $additions) {
320 foreach ($additions as $k => $v) {
321 if (isset($settings[$k]) && is_array($settings[$k]) && is_array($v)) {
330 * Helper fn for addSetting.
331 * Render JavaScript variables for the global CRM object.
335 public function renderSetting() {
336 // On a standard page request we construct the CRM object from scratch
337 if (!self
::isAjaxMode()) {
338 $js = 'var CRM = ' . json_encode($this->getSettings()) . ';';
340 // For an ajax request we append to it
342 $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');';
344 return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js);
348 * Add translated string to the js CRM object.
349 * It can then be retrived from the client-side ts() function
350 * Variable substitutions can happen from client-side
352 * Note: this function rarely needs to be called directly and is mostly for internal use.
353 * See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files
357 * CRM_Core_Resources::singleton()->addString('Hello');
358 * // The string is now available to javascript code i.e.
361 * Example with client-side substitutions:
363 * CRM_Core_Resources::singleton()->addString('Your %1 has been %2');
364 * // ts() in javascript works the same as in php, for example:
365 * ts('Your %1 has been %2', {1: objectName, 2: actionTaken});
367 * NOTE: This function does not work with server-side substitutions
368 * (as this might result in collisions and unwanted variable injections)
369 * Instead, use code like:
370 * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs)))));
371 * And from javascript access it at CRM.myNamespace.myString
373 * @param string|array $text
374 * @param string|NULL $domain
375 * @return CRM_Core_Resources
377 public function addString($text, $domain = 'civicrm') {
378 foreach ((array) $text as $str) {
379 $translated = ts($str, [
380 'domain' => ($domain == 'civicrm') ?
NULL : [$domain, NULL],
384 // We only need to push this string to client if the translation
385 // is actually different from the original
386 if ($translated != $str) {
387 $bucket = $domain == 'civicrm' ?
'strings' : 'strings::' . $domain;
389 $bucket => [$str => $translated],
397 * Add a CSS file to the current page using <LINK HREF>.
400 * extension name; use 'civicrm' for core.
401 * @param string $file
402 * file path -- relative to the extension base dir.
404 * relative weight within a given region.
405 * @param string $region
406 * location within the file; 'html-header', 'page-header', 'page-footer'.
407 * @return CRM_Core_Resources
409 public function addStyleFile($ext, $file, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
) {
410 $this->resolveFileName($file, $ext);
411 return $this->addStyleUrl($this->getUrl($ext, $file, TRUE), $weight, $region);
415 * Add a CSS file to the current page using <LINK HREF>.
419 * relative weight within a given region.
420 * @param string $region
421 * location within the file; 'html-header', 'page-header', 'page-footer'.
422 * @return CRM_Core_Resources
424 public function addStyleUrl($url, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
) {
425 CRM_Core_Region
::instance($region)->add([
427 'type' => 'styleUrl',
436 * Add a CSS content to the current page using <STYLE>.
438 * @param string $code
441 * relative weight within a given region.
442 * @param string $region
443 * location within the file; 'html-header', 'page-header', 'page-footer'.
444 * @return CRM_Core_Resources
446 public function addStyle($code, $weight = self
::DEFAULT_WEIGHT
, $region = self
::DEFAULT_REGION
) {
447 CRM_Core_Region
::instance($region)->add([
448 // 'name' => automatic
458 * Determine file path of a resource provided by an extension.
461 * extension name; use 'civicrm' for core.
462 * @param string|NULL $file
463 * file path -- relative to the extension base dir.
465 * @return bool|string
466 * full file path or FALSE if not found
468 public function getPath($ext, $file = NULL) {
469 // TODO consider caching results
470 $base = $this->paths
->hasVariable($ext)
471 ?
rtrim($this->paths
->getVariable($ext, 'path'), '/')
472 : $this->extMapper
->keyToBasePath($ext);
473 if ($file === NULL) {
476 $path = $base . '/' . $file;
477 if (is_file($path)) {
484 * Determine public URL of a resource provided by an extension.
487 * extension name; use 'civicrm' for core.
488 * @param string $file
489 * file path -- relative to the extension base dir.
490 * @param bool $addCacheCode
492 * @return string, URL
494 public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) {
495 if ($file === NULL) {
499 $file = $this->addCacheCode($file);
501 // TODO consider caching results
502 $base = $this->paths
->hasVariable($ext)
503 ?
$this->paths
->getVariable($ext, 'url')
504 : ($this->extMapper
->keyToUrl($ext) . '/');
505 return $base . $file;
509 * Evaluate a glob pattern in the context of a particular extension.
512 * Extension name; use 'civicrm' for core.
513 * @param string|array $patterns
514 * Glob pattern; e.g. "*.html".
515 * @param null|int $flags
518 * List of matching files, relative to the extension base dir.
521 public function glob($ext, $patterns, $flags = NULL) {
522 $path = $this->getPath($ext);
523 $patterns = (array) $patterns;
525 foreach ($patterns as $pattern) {
526 if (preg_match(';^(assetBuilder|ext)://;', $pattern)) {
529 if (CRM_Utils_File
::isAbsolute($pattern)) {
531 $files = array_merge($files, (array) glob($pattern, $flags));
535 $files = array_merge($files, (array) glob("$path/$pattern", $flags));
538 // Deterministic order.
540 $files = array_unique($files);
541 return array_map(function ($file) use ($path) {
542 return CRM_Utils_File
::relativize($file, "$path/");
549 public function getCacheCode() {
550 return $this->cacheCode
;
555 * @return CRM_Core_Resources
557 public function setCacheCode($value) {
558 $this->cacheCode
= $value;
559 if ($this->cacheCodeKey
) {
560 Civi
::settings()->set($this->cacheCodeKey
, $value);
566 * @return CRM_Core_Resources
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();
576 * This adds CiviCRM's standard css and js to the specified region of the document.
577 * It will only run once.
579 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
580 * (like addCoreResources/addCoreStyles).
582 * @param string $region
583 * @return CRM_Core_Resources
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();
590 // Add resources from coreResourceList
592 foreach ($this->coreResourceList($region) as $item) {
593 if (is_array($item)) {
594 $this->addSetting($item);
596 elseif (strpos($item, '.css')) {
597 $this->isFullyFormedUrl($item) ?
$this->addStyleUrl($item, -100, $region) : $this->addStyleFile('civicrm', $item, -100, $region);
599 elseif ($this->isFullyFormedUrl($item)) {
600 $this->addScriptUrl($item, $jsWeight++
, $region);
603 // Don't bother looking for ts() calls in packages, there aren't any
604 $translate = (substr($item, 0, 3) == 'js/');
605 $this->addScriptFile('civicrm', $item, $jsWeight++
, $region, $translate);
608 // Add global settings
611 'isFrontend' => $config->userFrameworkFrontend
,
614 // Disable profile creation if user lacks permission
615 if (!CRM_Core_Permission
::check('edit all contacts') && !CRM_Core_Permission
::check('add contacts')) {
616 $settings['config']['entityRef']['contactCreate'] = FALSE;
618 $this->addSetting($settings);
620 // Give control of jQuery and _ back to the CMS - this loads last
621 $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE);
623 $this->addCoreStyles($region);
629 * This will add CiviCRM's standard CSS
631 * TODO: Separate the functional code (like addStyle/addScript) from the policy code
632 * (like addCoreResources/addCoreStyles).
634 * @param string $region
635 * @return CRM_Core_Resources
637 public function addCoreStyles($region = 'html-header') {
638 if (!isset($this->addedCoreStyles
[$region])) {
639 $this->addedCoreStyles
[$region] = TRUE;
641 // Load custom or core css
642 $config = CRM_Core_Config
::singleton();
643 if (!empty($config->customCSSURL
)) {
644 $customCSSURL = $this->addCacheCode($config->customCSSURL
);
645 $this->addStyleUrl($customCSSURL, 99, $region);
647 if (!Civi
::settings()->get('disable_core_css')) {
648 $this->addStyleFile('civicrm', 'css/civicrm.css', -99, $region);
650 // crm-i.css added ahead of other styles so it can be overridden by FA.
651 $this->addStyleFile('civicrm', 'css/crm-i.css', -101, $region);
657 * Flushes cached translated strings.
658 * @return CRM_Core_Resources
660 public function flushStrings() {
661 $this->strings
->flush();
666 * @return CRM_Core_Resources_Strings
668 public function getStrings() {
669 return $this->strings
;
673 * Create dynamic script for localizing js widgets.
675 public static function outputLocalizationJS() {
676 CRM_Core_Page_AJAX
::setJsHeaders();
677 $config = CRM_Core_Config
::singleton();
679 'moneyFormat' => json_encode(CRM_Utils_Money
::format(1234.56)),
680 'contactSearch' => json_encode($config->includeEmailInName ?
ts('Start typing a name or email...') : ts('Start typing a name...')),
681 'otherSearch' => json_encode(ts('Enter search term...')),
682 'entityRef' => self
::getEntityRefMetadata(),
683 'ajaxPopupsEnabled' => self
::singleton()->ajaxPopupsEnabled
,
684 'allowAlertAutodismissal' => (bool) Civi
::settings()->get('allow_alert_autodismissal'),
685 'resourceCacheCode' => self
::singleton()->getCacheCode(),
686 'locale' => CRM_Core_I18n
::getLocale(),
687 'cid' => (int) CRM_Core_Session
::getLoggedInContactID(),
689 print CRM_Core_Smarty
::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars);
690 CRM_Utils_System
::civiExit();
694 * List of core resources we add to every CiviCRM page.
696 * Note: non-compressed versions of .min files will be used in debug mode
698 * @param string $region
701 public function coreResourceList($region) {
702 $config = CRM_Core_Config
::singleton();
704 // Scripts needed by everyone, everywhere
705 // FIXME: This is too long; list needs finer-grained segmentation
707 "bower_components/jquery/dist/jquery.min.js",
708 "bower_components/jquery-ui/jquery-ui.min.js",
709 "bower_components/jquery-ui/themes/smoothness/jquery-ui.min.css",
710 "bower_components/lodash-compat/lodash.min.js",
711 "packages/jquery/plugins/jquery.mousewheel.min.js",
712 "bower_components/select2/select2.min.js",
713 "bower_components/select2/select2.min.css",
714 "bower_components/font-awesome/css/font-awesome.min.css",
715 "packages/jquery/plugins/jquery.form.min.js",
716 "packages/jquery/plugins/jquery.timeentry.min.js",
717 "packages/jquery/plugins/jquery.blockUI.min.js",
718 "bower_components/datatables/media/js/jquery.dataTables.min.js",
719 "bower_components/datatables/media/css/jquery.dataTables.min.css",
720 "bower_components/jquery-validation/dist/jquery.validate.min.js",
721 "packages/jquery/plugins/jquery.ui.datepicker.validation.min.js",
723 "js/crm.datepicker.js",
725 "js/wysiwyg/crm.wysiwyg.js",
728 // Dynamic localization script
729 $items[] = $this->addCacheCode(
730 CRM_Utils_System
::url('civicrm/ajax/l10n-js/' . CRM_Core_I18n
::getLocale(),
731 ['cid' => CRM_Core_Session
::getLoggedInContactID()], FALSE, NULL, FALSE)
734 // add wysiwyg editor
735 $editor = Civi
::settings()->get('editor_id');
736 if ($editor == "CKEditor") {
737 CRM_Admin_Page_CKEditorConfig
::setConfigDefault();
740 'wysisygScriptLocation' => Civi
::paths()->getUrl("[civicrm.root]/js/wysiwyg/crm.ckeditor.js"),
741 'CKEditorCustomConfig' => CRM_Admin_Page_CKEditorConfig
::getConfigUrl(),
746 // These scripts are only needed by back-office users
747 if (CRM_Core_Permission
::check('access CiviCRM')) {
748 $items[] = "packages/jquery/plugins/jquery.tableHeader.js";
749 $items[] = "packages/jquery/plugins/jquery.notify.min.js";
752 $contactID = CRM_Core_Session
::getLoggedInContactID();
757 $contactID && !$config->userFrameworkFrontend
758 && CRM_Core_Permission
::check('access CiviCRM')
759 && !@constant
('CIVICRM_DISABLE_DEFAULT_MENU')
760 && !CRM_Core_Config
::isUpgradeMode()
762 $position = Civi
::settings()->get('menubar_position') ?
: 'over-cms-menu';
764 if ($position !== 'none') {
765 $items[] = 'bower_components/smartmenus/dist/jquery.smartmenus.min.js';
766 $items[] = 'bower_components/smartmenus/dist/addons/keyboard/jquery.smartmenus.keyboard.min.js';
767 $items[] = 'js/crm.menubar.js';
768 $items[] = Civi
::service('asset_builder')->getUrl('crm-menubar.css', [
769 'color' => Civi
::settings()->get('menubar_color'),
776 'position' => $position,
777 'qfKey' => CRM_Core_Key
::get('CRM_Contact_Controller_Search', TRUE),
778 'cacheCode' => CRM_Core_BAO_Navigation
::getCacheKey($contactID),
783 // JS for multilingual installations
784 if (!empty($config->languageLimit
) && count($config->languageLimit
) > 1 && CRM_Core_Permission
::check('translate CiviCRM')) {
785 $items[] = "js/crm.multilingual.js";
788 // Enable administrators to edit option lists in a dialog
789 if (CRM_Core_Permission
::check('administer CiviCRM') && $this->ajaxPopupsEnabled
) {
790 $items[] = "js/crm.optionEdit.js";
793 $tsLocale = CRM_Core_I18n
::getLocale();
794 // Add localized jQuery UI files
795 if ($tsLocale && $tsLocale != 'en_US') {
796 // Search for i18n file in order of specificity (try fr-CA, then fr)
797 list($lang) = explode('_', $tsLocale);
798 $path = "bower_components/jquery-ui/ui/i18n";
799 foreach ([str_replace('_', '-', $tsLocale), $lang] as $language) {
800 $localizationFile = "$path/datepicker-{$language}.js";
801 if ($this->getPath('civicrm', $localizationFile)) {
802 $items[] = $localizationFile;
808 // Allow hooks to modify this list
809 CRM_Utils_Hook
::coreResourceList($items, $region);
816 * is this page request an ajax snippet?
818 public static function isAjaxMode() {
819 if (in_array(CRM_Utils_Array
::value('snippet', $_REQUEST), [
820 CRM_Core_Smarty
::PRINT_SNIPPET
,
821 CRM_Core_Smarty
::PRINT_NOFORM
,
822 CRM_Core_Smarty
::PRINT_JSON
,
827 list($arg0, $arg1) = array_pad(explode('/', CRM_Utils_System
::getUrlPath()), 2, '');
828 return ($arg0 === 'civicrm' && in_array($arg1, ['ajax', 'angularprofiles', 'asset']));
832 * @param \Civi\Core\Event\GenericHookEvent $e
833 * @see \CRM_Utils_Hook::buildAsset()
835 public static function renderMenubarStylesheet(GenericHookEvent
$e) {
836 if ($e->asset
!== 'crm-menubar.css') {
839 $e->mimeType
= 'text/css';
841 $config = CRM_Core_Config
::singleton();
842 $cms = strtolower($config->userFramework
);
843 $cms = $cms === 'drupal' ?
'drupal7' : $cms;
845 'bower_components/smartmenus/dist/css/sm-core-css.css',
846 'css/crm-menubar.css',
847 "css/menubar-$cms.css",
849 foreach ($items as $item) {
850 $e->content
.= file_get_contents(self
::singleton()->getPath('civicrm', $item));
852 $color = $e->params
['color'];
853 if (!CRM_Utils_Rule
::color($color)) {
854 $color = Civi
::settings()->getDefault('menubar_color');
857 'resourceBase' => rtrim($config->resourceBase
, '/'),
858 'menubarHeight' => $e->params
['height'] . 'px',
859 'breakMin' => $e->params
['breakpoint'] . 'px',
860 'breakMax' => ($e->params
['breakpoint'] - 1) . 'px',
861 'menubarColor' => $color,
862 'menuItemColor' => 'rgba(' . implode(', ', CRM_Utils_Color
::getRgb($color)) . ", {$e->params['opacity']})",
863 'highlightColor' => CRM_Utils_Color
::getHighlight($color),
864 'textColor' => CRM_Utils_Color
::getContrast($color, '#333', '#ddd'),
866 $vars['highlightTextColor'] = CRM_Utils_Color
::getContrast($vars['highlightColor'], '#333', '#ddd');
867 foreach ($vars as $var => $val) {
868 $e->content
= str_replace('$' . $var, $val, $e->content
);
873 * Provide a list of available entityRef filters.
877 public static function getEntityRefMetadata() {
882 $config = CRM_Core_Config
::singleton();
884 $disabledComponents = [];
885 $dao = CRM_Core_DAO
::executeQuery("SELECT name, namespace FROM civicrm_component");
886 while ($dao->fetch()) {
887 if (!in_array($dao->name
, $config->enableComponents
)) {
888 $disabledComponents[$dao->name
] = $dao->namespace;
892 foreach (CRM_Core_DAO_AllCoreTables
::daoToClass() as $entity => $daoName) {
893 // Skip DAOs of disabled components
894 foreach ($disabledComponents as $nameSpace) {
895 if (strpos($daoName, $nameSpace) === 0) {
899 $baoName = str_replace('_DAO_', '_BAO_', $daoName);
900 if (class_exists($baoName)) {
901 $filters = $baoName::getEntityRefFilters();
903 $data['filters'][$entity] = $filters;
905 if (is_callable([$baoName, 'getEntityRefCreateLinks'])) {
906 $createLinks = $baoName::getEntityRefCreateLinks();
908 $data['links'][$entity] = $createLinks;
914 CRM_Utils_Hook
::entityRefFilters($data['filters']);
920 * In debug mode, look for a non-minified version of this file
922 * @param string $fileName
923 * @param string $extName
925 private function resolveFileName(&$fileName, $extName) {
926 if (CRM_Core_Config
::singleton()->debug
&& strpos($fileName, '.min.') !== FALSE) {
927 $nonMiniFile = str_replace('.min.', '.', $fileName);
928 if ($this->getPath($extName, $nonMiniFile)) {
929 $fileName = $nonMiniFile;
938 public function addCacheCode($url) {
939 $hasQuery = strpos($url, '?') !== FALSE;
940 $operator = $hasQuery ?
'&' : '?';
942 return $url . $operator . 'r=' . $this->cacheCode
;
946 * Checks if the given URL is fully-formed
952 public static function isFullyFormedUrl($url) {
953 return (substr($url, 0, 4) === 'http') ||
(substr($url, 0, 1) === '/');