-
-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathResources.php
594 lines (547 loc) · 17.9 KB
/
Resources.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
use Civi\Core\Event\GenericHookEvent;
/**
* This class facilitates the loading of resources
* such as JavaScript files and CSS files.
*
* Any URLs generated for resources may include a 'cache-code'. By resetting the
* cache-code, one may force clients to re-download resource files (regardless of
* any HTTP caching rules).
*
* TODO: This is currently a thin wrapper over CRM_Core_Region. We
* should incorporte services for aggregation, minimization, etc.
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
class CRM_Core_Resources implements CRM_Core_Resources_CollectionAdderInterface {
const DEFAULT_WEIGHT = 0;
const DEFAULT_REGION = 'page-footer';
use CRM_Core_Resources_CollectionAdderTrait;
/**
* We don't have a container or dependency-injection, so use singleton instead
*
* @var object
*/
private static $_singleton = NULL;
/**
* @var CRM_Extension_Mapper
*/
private $extMapper = NULL;
/**
* @var CRM_Core_Resources_Strings
*/
private $strings = NULL;
/**
* Any bundles that have been added.
*
* Format is ($bundleName => bool).
*
* @var array
*/
protected $addedBundles = [];
/**
* Added core resources.
*
* Format is ($regionName => bool).
*
* @var array
*/
protected $addedCoreResources = [];
/**
* Added settings.
*
* Format is ($regionName => bool).
*
* @var array
*/
protected $addedSettings = [];
/**
* A value to append to JS/CSS URLs to coerce cache resets.
*
* @var string
*/
protected $cacheCode = NULL;
/**
* The name of a setting which persistently stores the cacheCode.
*
* @var string
*/
protected $cacheCodeKey = NULL;
/**
* Are ajax popup screens enabled.
*
* @var bool
*/
public $ajaxPopupsEnabled;
/**
* @var \Civi\Core\Paths
*/
protected $paths;
/**
* Get or set the single instance of CRM_Core_Resources.
*
* @param CRM_Core_Resources $instance
* New copy of the manager.
*
* @return CRM_Core_Resources
*/
public static function singleton(CRM_Core_Resources $instance = NULL) {
if ($instance !== NULL) {
self::$_singleton = $instance;
}
if (self::$_singleton === NULL) {
self::$_singleton = Civi::service('resources');
}
return self::$_singleton;
}
/**
* Construct a resource manager.
*
* @param CRM_Extension_Mapper $extMapper
* Map extension names to their base path or URLs.
* @param CRM_Core_Resources_Strings $strings
* JS-localization cache.
* @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources
*/
public function __construct($extMapper, $strings, $cacheCodeKey = NULL) {
$this->extMapper = $extMapper;
$this->strings = $strings;
$this->cacheCodeKey = $cacheCodeKey;
if ($cacheCodeKey !== NULL) {
$this->cacheCode = Civi::settings()->get($cacheCodeKey);
}
if (!$this->cacheCode) {
$this->resetCacheCode();
}
$this->ajaxPopupsEnabled = (bool) Civi::settings()->get('ajaxPopupsEnabled');
$this->paths = Civi::paths();
}
/**
* Add an item to the collection.
*
* @param array $snippet
* @return array
* The full/computed snippet (with defaults applied).
* @see CRM_Core_Resources_CollectionInterface::add()
*/
public function add($snippet) {
if (!isset($snippet['region'])) {
$snippet['region'] = self::DEFAULT_REGION;
}
if (!isset($snippet['weight'])) {
$snippet['weight'] = self::DEFAULT_WEIGHT;
}
return CRM_Core_Region::instance($snippet['region'])->add($snippet);
}
/**
* Locate the 'settings' snippet.
*
* @param array $options
* @return array
* @see CRM_Core_Resources_CollectionTrait::findCreateSettingSnippet()
*/
public function &findCreateSettingSnippet($options = []): array {
$options = CRM_Core_Resources_CollectionAdderTrait::mergeSettingOptions($options, [
'region' => NULL,
]);
return $this->getSettingRegion($options['region'])->findCreateSettingSnippet($options);
}
/**
* Assimilate all the resources listed in a bundle.
*
* @param iterable|string|\CRM_Core_Resources_Bundle $bundle
* Either bundle object, or the symbolic name of a bundle, or a list of bundles.
* Note: For symbolic names, the bundle must be a container service ('bundle.FOO').
* @return static
*/
public function addBundle($bundle) {
// There are two ways you might write this method: (1) immediately merge
// resources from the bundle, or (2) store a reference to the bundle and
// merge resources later. Both have pros/cons. The implementation does #1.
//
// The upshot of #1 is *multi-region* support. For example, a bundle might
// add some JS to `html-header` and then add some HTML to `page-header`.
// Implementing this requires splitting the bundle (ie copying specific
// resources to their respective regions). The timing of `addBundle()` is
// favorable to splitting.
//
// The upshot of #2 would be *reduced timing sensitivity for downstream*:
// if party A wants to include some bundle, and party B wants to refine
// the same bundle, then it wouldn't matter if A or B executed first.
// This should make DX generally more forgiving. But we can't split until
// everyone has their shot at tweaking the bundle.
//
// In theory, you could have both characteristics if you figure the right
// time at which to perform a split. Or maybe you could have both by tracking
// more detailed references+events among the bundles/regions. I haven't
// seen a simple way to do get both.
if (is_iterable($bundle)) {
foreach ($bundle as $b) {
$this->addBundle($b);
}
return $this;
}
if (is_string($bundle)) {
$bundle = Civi::service('bundle.' . $bundle);
}
if (isset($this->addedBundles[$bundle->name])) {
return $this;
}
$this->addedBundles[$bundle->name] = TRUE;
// Ensure that every asset has a region.
$bundle->filter(function($snippet) {
if (empty($snippet['region'])) {
$snippet['region'] = isset($snippet['settings'])
? $this->getSettingRegion()->_name
: self::DEFAULT_REGION;
}
return $snippet;
});
$byRegion = CRM_Utils_Array::index(['region', 'name'], $bundle->getAll());
foreach ($byRegion as $regionName => $snippets) {
CRM_Core_Region::instance($regionName)->merge($snippets);
}
return $this;
}
/**
* Helper fn for addSettingsFactory.
*/
public function getSettings($region = NULL) {
return $this->getSettingRegion($region)->getSettings();
}
/**
* Determine file path of a resource provided by an extension.
*
* @param string $ext
* extension name; use 'civicrm' for core.
* @param string|null $file
* file path -- relative to the extension base dir.
*
* @return bool|string
* full file path or FALSE if not found
*/
public function getPath($ext, $file = NULL) {
// TODO consider caching results
$base = $this->paths->hasVariable($ext)
? rtrim($this->paths->getVariable($ext, 'path'), '/')
: $this->extMapper->keyToBasePath($ext);
if ($file === NULL) {
return $base;
}
$path = $base . '/' . $file;
if (is_file($path)) {
return $path;
}
return FALSE;
}
/**
* Determine public URL of a resource provided by an extension.
*
* @param string $ext
* extension name; use 'civicrm' for core.
* @param string $file
* file path -- relative to the extension base dir.
* @param bool $addCacheCode
*
* @return string, URL
*/
public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) {
if ($file === NULL) {
$file = '';
}
if ($addCacheCode) {
$file = $this->addCacheCode($file);
}
// TODO consider caching results
$base = $this->paths->hasVariable($ext)
? $this->paths->getVariable($ext, 'url')
: ($this->extMapper->keyToUrl($ext) . '/');
return $base . $file;
}
/**
* Evaluate a glob pattern in the context of a particular extension.
*
* @param string $ext
* Extension name; use 'civicrm' for core.
* @param string|array $patterns
* Glob pattern; e.g. "*.html".
* @param null|int $flags
* See glob().
* @return array
* List of matching files, relative to the extension base dir.
* @see glob()
*/
public function glob($ext, $patterns, $flags = NULL) {
$path = $this->getPath($ext);
$patterns = (array) $patterns;
$files = [];
foreach ($patterns as $pattern) {
if (preg_match(';^(assetBuilder|ext)://;', $pattern)) {
$files[] = $pattern;
}
if (CRM_Utils_File::isAbsolute($pattern)) {
// Absolute path.
$files = array_merge($files, (array) glob($pattern, $flags));
}
else {
// Relative path.
$files = array_merge($files, (array) glob("$path/$pattern", $flags));
}
}
// Deterministic order.
sort($files);
$files = array_unique($files);
return array_map(function ($file) use ($path) {
return CRM_Utils_File::relativize($file, "$path/");
}, $files);
}
/**
* @return string
*/
public function getCacheCode() {
return $this->cacheCode;
}
/**
* @param $value
* @return CRM_Core_Resources
*/
public function setCacheCode($value) {
$this->cacheCode = $value;
if ($this->cacheCodeKey) {
Civi::settings()->set($this->cacheCodeKey, $value);
}
return $this;
}
/**
* @return CRM_Core_Resources
*/
public function resetCacheCode() {
$this->setCacheCode(CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC));
// Also flush cms resource cache if needed
CRM_Core_Config::singleton()->userSystem->clearResourceCache();
return $this;
}
/**
* This adds CiviCRM's standard css and js to the specified region of the document.
* It will only run once.
*
* @param string $region
* @return CRM_Core_Resources
*/
public function addCoreResources($region = 'html-header') {
if ($region !== 'html-header') {
// The signature of this method allowed different regions. However, this
// doesn't appear to be used - based on grepping `universe` generally
// and `civicrm-{core,backdrop,drupal,packages,wordpress,joomla}` specifically,
// it appears that all callers use 'html-header' (either implicitly or explicitly).
throw new \CRM_Core_Exception("Error: addCoreResources only supports html-header");
}
if (!self::isAjaxMode()) {
$this->addBundle('coreResources');
$this->addCoreStyles($region);
}
return $this;
}
/**
* This will add CiviCRM's standard CSS
*
* @param string $region
* @return CRM_Core_Resources
*/
public function addCoreStyles($region = 'html-header') {
if ($region !== 'html-header') {
// The signature of this method allowed different regions. However, this
// doesn't appear to be used - based on grepping `universe` generally
// and `civicrm-{core,backdrop,drupal,packages,wordpress,joomla}` specifically,
// it appears that all callers use 'html-header' (either implicitly or explicitly).
throw new \CRM_Core_Exception("Error: addCoreResources only supports html-header");
}
$this->addBundle('coreStyles');
return $this;
}
/**
* Flushes cached translated strings.
* @return CRM_Core_Resources
*/
public function flushStrings() {
$this->strings->flush();
return $this;
}
/**
* @return CRM_Core_Resources_Strings
*/
public function getStrings() {
return $this->strings;
}
/**
* Create dynamic script for localizing js widgets.
*/
public static function outputLocalizationJS() {
CRM_Core_Page_AJAX::setJsHeaders();
$config = CRM_Core_Config::singleton();
$vars = [
'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)),
'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')),
'otherSearch' => json_encode(ts('Enter search term...')),
'entityRef' => self::getEntityRefMetadata(),
'ajaxPopupsEnabled' => self::singleton()->ajaxPopupsEnabled,
'allowAlertAutodismissal' => (bool) Civi::settings()->get('allow_alert_autodismissal'),
'resourceCacheCode' => self::singleton()->getCacheCode(),
'locale' => CRM_Core_I18n::getLocale(),
'cid' => (int) CRM_Core_Session::getLoggedInContactID(),
];
print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars);
CRM_Utils_System::civiExit();
}
/**
* @return bool
* is this page request an ajax snippet?
*/
public static function isAjaxMode() {
if (in_array(CRM_Utils_Array::value('snippet', $_REQUEST), [
CRM_Core_Smarty::PRINT_SNIPPET,
CRM_Core_Smarty::PRINT_NOFORM,
CRM_Core_Smarty::PRINT_JSON,
])
) {
return TRUE;
}
list($arg0, $arg1) = array_pad(explode('/', CRM_Utils_System::currentPath()), 2, '');
return ($arg0 === 'civicrm' && in_array($arg1, ['ajax', 'angularprofiles', 'asset']));
}
/**
* @param \Civi\Core\Event\GenericHookEvent $e
* @see \CRM_Utils_Hook::buildAsset()
*/
public static function renderMenubarStylesheet(GenericHookEvent $e) {
if ($e->asset !== 'crm-menubar.css') {
return;
}
$e->mimeType = 'text/css';
$content = '';
$config = CRM_Core_Config::singleton();
$cms = strtolower($config->userFramework);
$cms = $cms === 'drupal' ? 'drupal7' : $cms;
$items = [
'bower_components/smartmenus/dist/css/sm-core-css.css',
'css/crm-menubar.css',
"css/menubar-$cms.css",
];
foreach ($items as $item) {
$content .= file_get_contents(self::singleton()->getPath('civicrm', $item));
}
$params = $e->params;
// "color" is deprecated in favor of the more specific "menubarColor"
$menubarColor = $params['color'] ?? $params['menubarColor'];
$vars = [
'$resourceBase' => rtrim($config->resourceBase, '/'),
'$menubarHeight' => $params['height'] . 'px',
'$breakMin' => $params['breakpoint'] . 'px',
'$breakMax' => ($params['breakpoint'] - 1) . 'px',
'$menubarColor' => $menubarColor,
'$menuItemColor' => $params['menuItemColor'] ?? $menubarColor,
'$highlightColor' => $params['highlightColor'] ?? CRM_Utils_Color::getHighlight($menubarColor),
'$textColor' => $params['textColor'] ?? CRM_Utils_Color::getContrast($menubarColor, '#333', '#ddd'),
];
$vars['$highlightTextColor'] = $params['highlightTextColor'] ?? CRM_Utils_Color::getContrast($vars['$highlightColor'], '#333', '#ddd');
$e->content = str_replace(array_keys($vars), array_values($vars), $content);
}
/**
* Provide a list of available entityRef filters.
*
* @return array
*/
public static function getEntityRefMetadata() {
$data = [
'filters' => [],
'links' => [],
];
$config = CRM_Core_Config::singleton();
$disabledComponents = [];
$dao = CRM_Core_DAO::executeQuery("SELECT name, namespace FROM civicrm_component");
while ($dao->fetch()) {
if (!in_array($dao->name, $config->enableComponents)) {
$disabledComponents[$dao->name] = $dao->namespace;
}
}
foreach (CRM_Core_DAO_AllCoreTables::daoToClass() as $entity => $daoName) {
// Skip DAOs of disabled components
foreach ($disabledComponents as $nameSpace) {
if (strpos($daoName, $nameSpace) === 0) {
continue 2;
}
}
$baoName = str_replace('_DAO_', '_BAO_', $daoName);
if (class_exists($baoName)) {
$filters = $baoName::getEntityRefFilters();
if ($filters) {
$data['filters'][$entity] = $filters;
}
if (is_callable([$baoName, 'getEntityRefCreateLinks'])) {
$createLinks = $baoName::getEntityRefCreateLinks();
if ($createLinks) {
$data['links'][$entity] = $createLinks;
}
}
}
}
CRM_Utils_Hook::entityRefFilters($data['filters'], $data['links']);
return $data;
}
/**
* Determine the minified file name.
*
* @param string $ext
* @param string $file
* @return string
* An updated $fileName. If a minified version exists and is supported by
* system policy, the minified version will be returned. Otherwise, the original.
*/
public function filterMinify($ext, $file) {
if (CRM_Core_Config::singleton()->debug && strpos($file, '.min.') !== FALSE) {
$nonMiniFile = str_replace('.min.', '.', $file);
if ($this->getPath($ext, $nonMiniFile)) {
$file = $nonMiniFile;
}
}
return $file;
}
/**
* @param string $url
* @return string
*/
public function addCacheCode($url) {
$hasQuery = strpos($url, '?') !== FALSE;
$operator = $hasQuery ? '&' : '?';
return $url . $operator . 'r=' . $this->cacheCode;
}
/**
* Checks if the given URL is fully-formed
*
* @param string $url
*
* @return bool
*/
public static function isFullyFormedUrl($url) {
return (substr($url, 0, 4) === 'http') || (substr($url, 0, 1) === '/');
}
/**
* @param string|NULL $region
* Optional request for a specific region. If NULL/omitted, use global default.
* @return \CRM_Core_Region
*/
private function getSettingRegion($region = NULL) {
$region = $region ?: (self::isAjaxMode() ? 'ajax-snippet' : 'html-header');
return CRM_Core_Region::instance($region);
}
}