3159c08dce49d608130f944d84cb875e86f77c84
[civicrm-core.git] / CRM / Core / Resources / Common.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Define some common, global lists of resources.
14 */
15 class CRM_Core_Resources_Common {
16
17 const REGION = 'html-header';
18
19 /**
20 * The 'bundle.coreStyles' service is a collection of resources used on some
21 * non-Civi pages (wherein Civi may be mixed-in).
22 *
23 * @param string $name
24 * i.e. 'coreStyles'
25 * @return \CRM_Core_Resources_Bundle
26 */
27 public static function createStyleBundle($name) {
28 $bundle = new CRM_Core_Resources_Bundle($name);
29
30 // Load custom or core css
31 $config = CRM_Core_Config::singleton();
32 if (!empty($config->customCSSURL)) {
33 $customCSSURL = Civi::resources()->addCacheCode($config->customCSSURL);
34 $bundle->addStyleUrl($customCSSURL, 99);
35 }
36 if (!Civi::settings()->get('disable_core_css')) {
37 $bundle->addStyleFile('civicrm', 'css/civicrm.css', -99);
38 }
39 // crm-i.css added ahead of other styles so it can be overridden by FA.
40 $bundle->addStyleFile('civicrm', 'css/crm-i.css', -101);
41
42 CRM_Utils_Hook::alterBundle($bundle);
43 self::useRegion($bundle, self::REGION);
44 return $bundle;
45 }
46
47 /**
48 * The 'bundle.coreResources' service is a collection of resources
49 * shared by Civi pages (ie pages where Civi controls rendering).
50 *
51 * @param string $name
52 * i.e. 'coreResources'
53 * @return \CRM_Core_Resources_Bundle
54 */
55 public static function createFullBundle($name) {
56 $bundle = new CRM_Core_Resources_Bundle($name);
57 // TODO
58 CRM_Utils_Hook::alterBundle($bundle);
59 self::useRegion($bundle, self::REGION);
60 return $bundle;
61 }
62
63 /**
64 * Ensure that all elements of the bundle are in the same region.
65 *
66 * @param CRM_Core_Resources_Bundle $bundle
67 * @param string $region
68 * @return CRM_Core_Resources_Bundle
69 */
70 protected static function useRegion($bundle, $region) {
71 $bundle->filter(function ($s) use ($region) {
72 if ($s['type'] !== 'settings' && !isset($s['region'])) {
73 $s['region'] = $region;
74 }
75 return $s;
76 });
77 return $bundle;
78 }
79
80 }