Merge pull request #19081 from civicrm/5.32
[civicrm-core.git] / Civi / Angular / AngularLoader.php
1 <?php
2 namespace Civi\Angular;
3
4 /**
5 * The AngularLoader loads any JS/CSS/JSON resources
6 * required for setting up AngularJS.
7 *
8 * The AngularLoader stops short of bootstrapping AngularJS. You may
9 * need to `<div ng-app="..."></div>` or `angular.bootstrap(...)`.
10 *
11 * ```
12 * $loader = new AngularLoader();
13 * $loader->setPageName('civicrm/case/a');
14 * $loader->setModules(array('crmApp'));
15 * $loader->load();
16 * ```
17 *
18 * @link https://docs.angularjs.org/guide/bootstrap
19 */
20 class AngularLoader {
21
22 /**
23 * The weight to assign to any Angular JS module files.
24 */
25 const DEFAULT_MODULE_WEIGHT = 200;
26
27 /**
28 * The resource manager.
29 *
30 * Do not use publicly. Inject your own copy!
31 *
32 * @var \CRM_Core_Resources
33 */
34 protected $res;
35
36 /**
37 * The Angular module manager.
38 *
39 * Do not use publicly. Inject your own copy!
40 *
41 * @var \Civi\Angular\Manager
42 */
43 protected $angular;
44
45 /**
46 * The region of the page into which JavaScript will be loaded.
47 *
48 * @var string
49 */
50 protected $region;
51
52 /**
53 * @var string
54 * Ex: 'civicrm/a'.
55 */
56 protected $pageName;
57
58 /**
59 * @var array
60 * A list of modules to load.
61 */
62 protected $modules;
63
64 /**
65 * @var array|null
66 */
67 protected $crmApp = NULL;
68
69 /**
70 * AngularLoader constructor.
71 */
72 public function __construct() {
73 $this->res = \CRM_Core_Resources::singleton();
74 $this->angular = \Civi::service('angular');
75 $this->region = \CRM_Utils_Request::retrieve('snippet', 'String') ? 'ajax-snippet' : 'html-header';
76 $this->pageName = $_GET['q'] ?? NULL;
77 $this->modules = [];
78 }
79
80 /**
81 * Register resources required by Angular.
82 *
83 * @return AngularLoader
84 */
85 public function load() {
86 $angular = $this->getAngular();
87 $res = $this->getRes();
88
89 if ($this->crmApp !== NULL) {
90 $this->addModules($this->crmApp['modules']);
91 $region = \CRM_Core_Region::instance($this->crmApp['region']);
92 $region->update('default', ['disabled' => TRUE]);
93 $region->add(['template' => $this->crmApp['file'], 'weight' => 0]);
94 $this->res->addSetting([
95 'crmApp' => [
96 'defaultRoute' => $this->crmApp['defaultRoute'],
97 ],
98 ]);
99
100 // If trying to load an Angular page via AJAX, the route must be passed as a
101 // URL parameter, since the server doesn't receive information about
102 // URL fragments (i.e, what comes after the #).
103 $this->res->addSetting([
104 'angularRoute' => $this->crmApp['activeRoute'],
105 ]);
106 }
107
108 $moduleNames = $this->findActiveModules();
109 if (!$this->isAllModules($moduleNames)) {
110 $assetParams = ['modules' => implode(',', $moduleNames)];
111 }
112 else {
113 // The module list will be "all modules that the user can see".
114 $assetParams = ['nonce' => md5(implode(',', $moduleNames))];
115 }
116
117 $res->addSettingsFactory(function () use (&$moduleNames, $angular, $res, $assetParams) {
118 // Merge static settings with the results of settingsFactory functions
119 $settingsByModule = $angular->getResources($moduleNames, 'settings', 'settings');
120 foreach ($angular->getResources($moduleNames, 'settingsFactory', 'settingsFactory') as $moduleName => $factory) {
121 $settingsByModule[$moduleName] = array_merge($settingsByModule[$moduleName] ?? [], $factory());
122 }
123 // Add clientside permissions
124 $permissions = [];
125 $toCheck = $angular->getResources($moduleNames, 'permissions', 'permissions');
126 foreach ($toCheck as $perms) {
127 foreach ((array) $perms as $perm) {
128 if (!isset($permissions[$perm])) {
129 $permissions[$perm] = \CRM_Core_Permission::check($perm);
130 }
131 }
132 }
133 // TODO optimization; client-side caching
134 return array_merge($settingsByModule, ['permissions' => $permissions], [
135 'resourceUrls' => \CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(),
136 'angular' => [
137 'modules' => $moduleNames,
138 'requires' => $angular->getResources($moduleNames, 'requires', 'requires'),
139 'cacheCode' => $res->getCacheCode(),
140 'bundleUrl' => \Civi::service('asset_builder')->getUrl('angular-modules.json', $assetParams),
141 ],
142 ]);
143 });
144
145 $res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, $this->getRegion(), FALSE);
146 $res->addScriptFile('civicrm', 'js/crm.angular.js', 101, $this->getRegion(), FALSE);
147
148 $headOffset = 0;
149 $config = \CRM_Core_Config::singleton();
150 if ($config->debug) {
151 foreach ($moduleNames as $moduleName) {
152 foreach ($this->angular->getResources($moduleName, 'css', 'cacheUrl') as $url) {
153 $res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), $this->getRegion());
154 }
155 foreach ($this->angular->getResources($moduleName, 'js', 'cacheUrl') as $url) {
156 $res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), $this->getRegion());
157 // addScriptUrl() bypasses the normal string-localization of addScriptFile(),
158 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
159 }
160 }
161 }
162 else {
163 // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(),
164 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
165 // $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $res->getCacheCode(), FALSE, NULL, FALSE);
166 $aggScriptUrl = \Civi::service('asset_builder')->getUrl('angular-modules.js', $assetParams);
167 $res->addScriptUrl($aggScriptUrl, 120, $this->getRegion());
168
169 // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons.
170 //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $res->getCacheCode(), FALSE, NULL, FALSE);
171 //$aggStyleUrl = \Civi::service('asset_builder')->getUrl('angular-modules.css', $assetParams);
172 //$res->addStyleUrl($aggStyleUrl, 120, $this->getRegion());
173
174 foreach ($this->angular->getResources($moduleNames, 'css', 'cacheUrl') as $url) {
175 $res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), $this->getRegion());
176 }
177 }
178 // Add bundles
179 foreach ($this->angular->getResources($moduleNames, 'bundles', 'bundles') as $bundles) {
180 $res->addBundle($bundles);
181 }
182
183 return $this;
184 }
185
186 /**
187 * Use Civi's generic "application" module.
188 *
189 * This is suitable for use on a basic, standalone Angular page
190 * like `civicrm/a`. (If you need to integrate Angular with pre-existing,
191 * non-Angular pages... then this probably won't help.)
192 *
193 * The Angular bootstrap process requires an HTML directive like
194 * `<div ng-app="foo">`.
195 *
196 * Calling useApp() will replace the page's main body with the
197 * `<div ng-app="crmApp">...</div>` and apply some configuration options
198 * for the `crmApp` module.
199 *
200 * @param array $settings
201 * A list of settings. Accepted values:
202 * - activeRoute: string, the route to open up immediately
203 * Ex: '/case/list'
204 * - defaultRoute: string, use this to redirect the default route (`/`) to another page
205 * Ex: '/case/list'
206 * - region: string, the place on the page where we should insert the angular app
207 * Ex: 'page-body'
208 * @return AngularLoader
209 * @link https://code.angularjs.org/1.5.11/docs/guide/bootstrap
210 */
211 public function useApp($settings = []) {
212 $defaults = [
213 'modules' => ['crmApp'],
214 'activeRoute' => NULL,
215 'defaultRoute' => NULL,
216 'region' => 'page-body',
217 'file' => 'Civi/Angular/Page/Main.tpl',
218 ];
219 $this->crmApp = array_merge($defaults, $settings);
220 return $this;
221 }
222
223 /**
224 * Get a list of all Angular modules which should be activated on this
225 * page.
226 *
227 * @return array
228 * List of module names.
229 * Ex: array('angularFileUpload', 'crmUi', 'crmUtil').
230 */
231 public function findActiveModules() {
232 return $this->angular->resolveDependencies(array_merge(
233 $this->getModules(),
234 $this->angular->resolveDefaultModules($this->getPageName())
235 ));
236 }
237
238 /**
239 * @param $moduleNames
240 * @return int
241 */
242 private function isAllModules($moduleNames) {
243 $allModuleNames = array_keys($this->angular->getModules());
244 return count(array_diff($allModuleNames, $moduleNames)) === 0;
245 }
246
247 /**
248 * @return \CRM_Core_Resources
249 */
250 public function getRes() {
251 return $this->res;
252 }
253
254 /**
255 * @param \CRM_Core_Resources $res
256 * @return AngularLoader
257 */
258 public function setRes($res) {
259 $this->res = $res;
260 return $this;
261 }
262
263 /**
264 * @return \Civi\Angular\Manager
265 */
266 public function getAngular() {
267 return $this->angular;
268 }
269
270 /**
271 * @param \Civi\Angular\Manager $angular
272 * @return AngularLoader
273 */
274 public function setAngular($angular) {
275 $this->angular = $angular;
276 return $this;
277 }
278
279 /**
280 * @return string
281 */
282 public function getRegion() {
283 return $this->region;
284 }
285
286 /**
287 * @param string $region
288 * @return AngularLoader
289 */
290 public function setRegion($region) {
291 $this->region = $region;
292 return $this;
293 }
294
295 /**
296 * @return string
297 * Ex: 'civicrm/a'.
298 */
299 public function getPageName() {
300 return $this->pageName;
301 }
302
303 /**
304 * @param string $pageName
305 * Ex: 'civicrm/a'.
306 * @return AngularLoader
307 */
308 public function setPageName($pageName) {
309 $this->pageName = $pageName;
310 return $this;
311 }
312
313 /**
314 * @param array|string $modules
315 * @return AngularLoader
316 */
317 public function addModules($modules) {
318 $modules = (array) $modules;
319 $this->modules = array_unique(array_merge($this->modules, $modules));
320 return $this;
321 }
322
323 /**
324 * @return array
325 */
326 public function getModules() {
327 return $this->modules;
328 }
329
330 /**
331 * @param array $modules
332 * @return AngularLoader
333 */
334 public function setModules($modules) {
335 $this->modules = $modules;
336 return $this;
337 }
338
339 }