CRM-20600 - Civi\Angular\Manager - Add resolveDependencies(), resolveDefaultModules()
[civicrm-core.git] / Civi / Angular / Page / Main.php
CommitLineData
e7ff7042 1<?php
39c3d5e9 2namespace Civi\Angular\Page;
e7ff7042
TO
3
4/**
5 * This page is simply a container; any Angular modules defined by CiviCRM (or by CiviCRM extensions)
6 * will be activated on this page.
7 *
8 * @link https://issues.civicrm.org/jira/browse/CRM-14479
9 */
39c3d5e9 10class Main extends \CRM_Core_Page {
e7ff7042 11 /**
fe482240 12 * The weight to assign to any Angular JS module files.
e7ff7042
TO
13 */
14 const DEFAULT_MODULE_WEIGHT = 200;
15
2f6c50d5 16 /**
4d93c42f
TO
17 * The resource manager.
18 *
19 * Do not use publicly. Inject your own copy!
20 *
39c3d5e9 21 * @var \CRM_Core_Resources
2f6c50d5 22 */
4d93c42f 23 public $res;
2f6c50d5 24
16072ce1
TO
25
26 /**
4d93c42f
TO
27 * The Angular module manager.
28 *
29 * Do not use publicly. Inject your own copy!
30 *
39c3d5e9 31 * @var \Civi\Angular\Manager
16072ce1 32 */
4d93c42f 33 public $angular;
16072ce1 34
2c60bace
FG
35 /**
36 * The region of the page into which JavaScript will be loaded.
37 *
38 * @var String
39 */
40 public $region;
41
2f6c50d5
TO
42 /**
43 * @param string $title
44 * Title of the page.
45 * @param int $mode
46 * Mode of the page.
39c3d5e9 47 * @param \CRM_Core_Resources|null $res
2f6c50d5
TO
48 * Resource manager.
49 */
50 public function __construct($title = NULL, $mode = NULL, $res = NULL) {
51 parent::__construct($title, $mode);
39c3d5e9 52 $this->res = \CRM_Core_Resources::singleton();
048222df 53 $this->angular = \Civi::service('angular');
6aeeacaf 54 $this->region = \CRM_Utils_Request::retrieve('snippet', 'String') ? 'ajax-snippet' : 'html-header';
2f6c50d5
TO
55 }
56
b5c2afd0
EM
57 /**
58 * This function takes care of all the things common to all
59 * pages. This typically involves assigning the appropriate
60 * smarty variable :)
61 *
a6c01b45
CW
62 * @return string
63 * The content generated by running this page
b5c2afd0 64 */
00be9182 65 public function run() {
6aeeacaf 66 $this->registerResources();
4b07d5bd
TO
67 return parent::run();
68 }
69
a0ee3941 70 /**
2f6c50d5 71 * Register resources required by Angular.
a0ee3941 72 */
6aeeacaf 73 public function registerResources() {
4d93c42f 74 $page = $this; // PHP 5.3 does not propagate $this to inner functions.
e7ff7042 75
fe158128
TO
76 $allModuleNames = array_keys($this->angular->getModules());
77 $moduleNames = $this->getModules();
78 if (count(array_diff($allModuleNames, $moduleNames))) {
79 $assetParams = array('modules' => implode(',', $moduleNames));
80 }
81 else {
82 // The module list will be "all modules that the user can see".
83 $assetParams = array('nonce' => md5(implode(',', $moduleNames)));
84 }
85
86 $this->res->addSettingsFactory(function () use (&$moduleNames, $page, $assetParams) {
e7ff7042 87 // TODO optimization; client-side caching
fe158128 88 return array_merge($page->angular->getResources($moduleNames, 'settings', 'settings'), array(
6bd0dca9
FG
89 'resourceUrls' => \CRM_Extension_System::singleton()->getMapper()->getActiveModuleUrls(),
90 'angular' => array(
fe158128 91 'modules' => array_merge(array('ngRoute'), $moduleNames),
5438399c 92 'requires' => $page->angular->getResources($moduleNames, 'requires', 'requires'),
6bd0dca9 93 'cacheCode' => $page->res->getCacheCode(),
fe158128 94 'bundleUrl' => \Civi::service('asset_builder')->getUrl('angular-modules.json', $assetParams),
6bd0dca9 95 ),
1da632e0 96 ));
e7ff7042
TO
97 });
98
6aeeacaf 99 $this->res->addScriptFile('civicrm', 'bower_components/angular/angular.min.js', 100, $this->region, FALSE);
5438399c 100 $this->res->addScriptFile('civicrm', 'js/crm.angular.js', 101, $this->region, FALSE);
27a90ef6 101
6d57b745 102 $headOffset = 0;
6aeeacaf 103 $config = \CRM_Core_Config::singleton();
27a90ef6 104 if ($config->debug) {
fe158128 105 foreach ($moduleNames as $moduleName) {
27a90ef6 106 foreach ($this->angular->getResources($moduleName, 'css', 'cacheUrl') as $url) {
6aeeacaf 107 $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), $this->region);
27a90ef6
TO
108 }
109 foreach ($this->angular->getResources($moduleName, 'js', 'cacheUrl') as $url) {
6aeeacaf 110 $this->res->addScriptUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), $this->region);
27a90ef6
TO
111 // addScriptUrl() bypasses the normal string-localization of addScriptFile(),
112 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
113 }
2f6c50d5 114 }
2f6c50d5 115 }
27a90ef6
TO
116 else {
117 // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(),
118 // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
466e4b29 119 // $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
fe158128 120 $aggScriptUrl = \Civi::service('asset_builder')->getUrl('angular-modules.js', $assetParams);
6aeeacaf 121 $this->res->addScriptUrl($aggScriptUrl, 120, $this->region);
27a90ef6 122
6d57b745
TO
123 // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons.
124 //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
fe158128 125 //$aggStyleUrl = \Civi::service('asset_builder')->getUrl('angular-modules.css', $assetParams);
6aeeacaf 126 //$this->res->addStyleUrl($aggStyleUrl, 120, $this->region);
6d57b745 127
fe158128 128 foreach ($this->angular->getResources($moduleNames, 'css', 'cacheUrl') as $url) {
6aeeacaf 129 $this->res->addStyleUrl($url, self::DEFAULT_MODULE_WEIGHT + (++$headOffset), $this->region);
6d57b745 130 }
27a90ef6 131 }
81916bee 132
337fc3e6 133 // If trying to load an Angular page via AJAX, the route must be passed as a
2c60bace
FG
134 // URL parameter, since the server doesn't receive information about
135 // URL fragments (i.e, what comes after the #).
337fc3e6
FG
136 \CRM_Core_Resources::singleton()->addSetting(array(
137 'angularRoute' => \CRM_Utils_Request::retrieve('route', 'String'),
138 ));
2f6c50d5 139 }
e807a9a6 140
fe158128
TO
141 /**
142 * Get a list of Angular modules to include on this page.
143 *
144 * @return array
145 * List of module names.
146 * Ex: array('angularFileUpload', 'crmUi', 'crmUtil').
147 */
148 public function getModules() {
149 return array_keys($this->angular->getModules());
150 }
151
b5c2afd0 152}