Merge pull request #10498 from JMAConsulting/CRM-20722
[civicrm-core.git] / Civi / Angular / Manager.php
1 <?php
2 namespace Civi\Angular;
3
4 /**
5 * Manage Angular resources.
6 *
7 * @package Civi\Angular
8 */
9 class Manager {
10
11 /**
12 * @var \CRM_Core_Resources
13 */
14 protected $res = NULL;
15
16 /**
17 * @var array|NULL
18 * Each item has some combination of these keys:
19 * - ext: string
20 * The Civi extension which defines the Angular module.
21 * - js: array(string $relativeFilePath)
22 * List of JS files (relative to the extension).
23 * - css: array(string $relativeFilePath)
24 * List of CSS files (relative to the extension).
25 * - partials: array(string $relativeFilePath)
26 * A list of partial-HTML folders (relative to the extension).
27 * This will be mapped to "~/moduleName" by crmResource.
28 * - settings: array(string $key => mixed $value)
29 * List of settings to preload.
30 */
31 protected $modules = NULL;
32
33 /**
34 * @var \CRM_Utils_Cache_Interface
35 */
36 protected $cache;
37
38 /**
39 * @var array
40 * Array(string $name => ChangeSet $change).
41 */
42 protected $changeSets = NULL;
43
44 /**
45 * @param \CRM_Core_Resources $res
46 * The resource manager.
47 */
48 public function __construct($res, \CRM_Utils_Cache_Interface $cache = NULL) {
49 $this->res = $res;
50 $this->cache = $cache ? $cache : new \CRM_Utils_Cache_Arraycache(array());
51 }
52
53 /**
54 * Get a list of AngularJS modules which should be autoloaded.
55 *
56 * @return array
57 * Each item has some combination of these keys:
58 * - ext: string
59 * The Civi extension which defines the Angular module.
60 * - js: array(string $relativeFilePath)
61 * List of JS files (relative to the extension).
62 * - css: array(string $relativeFilePath)
63 * List of CSS files (relative to the extension).
64 * - partials: array(string $relativeFilePath)
65 * A list of partial-HTML folders (relative to the extension).
66 * This will be mapped to "~/moduleName" by crmResource.
67 * - settings: array(string $key => mixed $value)
68 * List of settings to preload.
69 */
70 public function getModules() {
71 if ($this->modules === NULL) {
72 $config = \CRM_Core_Config::singleton();
73 global $civicrm_root;
74
75 // Note: It would be nice to just glob("$civicrm_root/ang/*.ang.php"), but at time
76 // of writing CiviMail and CiviCase have special conditionals.
77
78 $angularModules = array();
79 $angularModules['angularFileUpload'] = include "$civicrm_root/ang/angularFileUpload.ang.php";
80 $angularModules['crmApp'] = include "$civicrm_root/ang/crmApp.ang.php";
81 $angularModules['crmAttachment'] = include "$civicrm_root/ang/crmAttachment.ang.php";
82 $angularModules['crmAutosave'] = include "$civicrm_root/ang/crmAutosave.ang.php";
83 $angularModules['crmCxn'] = include "$civicrm_root/ang/crmCxn.ang.php";
84 // $angularModules['crmExample'] = include "$civicrm_root/ang/crmExample.ang.php";
85 $angularModules['crmResource'] = include "$civicrm_root/ang/crmResource.ang.php";
86 $angularModules['crmUi'] = include "$civicrm_root/ang/crmUi.ang.php";
87 $angularModules['crmUtil'] = include "$civicrm_root/ang/crmUtil.ang.php";
88 $angularModules['dialogService'] = include "$civicrm_root/ang/dialogService.ang.php";
89 $angularModules['ngRoute'] = include "$civicrm_root/ang/ngRoute.ang.php";
90 $angularModules['ngSanitize'] = include "$civicrm_root/ang/ngSanitize.ang.php";
91 $angularModules['ui.utils'] = include "$civicrm_root/ang/ui.utils.ang.php";
92 $angularModules['ui.sortable'] = include "$civicrm_root/ang/ui.sortable.ang.php";
93 $angularModules['unsavedChanges'] = include "$civicrm_root/ang/unsavedChanges.ang.php";
94 $angularModules['statuspage'] = include "$civicrm_root/ang/crmStatusPage.ang.php";
95
96 foreach (\CRM_Core_Component::getEnabledComponents() as $component) {
97 $angularModules = array_merge($angularModules, $component->getAngularModules());
98 }
99 \CRM_Utils_Hook::angularModules($angularModules);
100 foreach (array_keys($angularModules) as $module) {
101 if (!isset($angularModules[$module]['basePages'])) {
102 $angularModules[$module]['basePages'] = array('civicrm/a');
103 }
104 }
105 $this->modules = $this->resolvePatterns($angularModules);
106 }
107
108 return $this->modules;
109 }
110
111 /**
112 * Get the descriptor for an Angular module.
113 *
114 * @param string $name
115 * Module name.
116 * @return array
117 * Details about the module:
118 * - ext: string, the name of the Civi extension which defines the module
119 * - js: array(string $relativeFilePath).
120 * - css: array(string $relativeFilePath).
121 * - partials: array(string $relativeFilePath).
122 * @throws \Exception
123 */
124 public function getModule($name) {
125 $modules = $this->getModules();
126 if (!isset($modules[$name])) {
127 throw new \Exception("Unrecognized Angular module");
128 }
129 return $modules[$name];
130 }
131
132 /**
133 * Resolve a full list of Angular dependencies.
134 *
135 * @param array $names
136 * List of Angular modules.
137 * Ex: array('crmMailing').
138 * @return array
139 * List of Angular modules, include all dependencies.
140 * Ex: array('crmMailing', 'crmUi', 'crmUtil', 'ngRoute').
141 */
142 public function resolveDependencies($names) {
143 $allModules = $this->getModules();
144 $visited = array();
145 $result = $names;
146 while (($missingModules = array_diff($result, array_keys($visited))) && !empty($missingModules)) {
147 foreach ($missingModules as $module) {
148 $visited[$module] = 1;
149 if (!isset($allModules[$module])) {
150 \Civi::log()->warning('Unrecognized Angular module {name}. Please ensure that all Angular modules are declared.', array(
151 'name' => $module,
152 'civi.tag' => 'deprecated',
153 ));
154 }
155 elseif (isset($allModules[$module]['requires'])) {
156 $result = array_unique(array_merge($result, $allModules[$module]['requires']));
157 }
158 }
159 }
160 sort($result);
161 return $result;
162 }
163
164 /**
165 * Get a list of Angular modules that should be loaded on the given
166 * base-page.
167 *
168 * @param string $basePage
169 * The name of the base-page for which we want a list of moudles.
170 * @return array
171 * List of Angular modules.
172 * Ex: array('crmMailing', 'crmUi', 'crmUtil', 'ngRoute').
173 */
174 public function resolveDefaultModules($basePage) {
175 $modules = $this->getModules();
176 $result = array();
177 foreach ($modules as $moduleName => $module) {
178 if (in_array($basePage, $module['basePages']) || in_array('*', $module['basePages'])) {
179 $result[] = $moduleName;
180 }
181 }
182 return $result;
183 }
184
185 /**
186 * Convert any globs in an Angular module to file names.
187 *
188 * @param array $modules
189 * List of Angular modules.
190 * @return array
191 * Updated list of Angular modules
192 */
193 protected function resolvePatterns($modules) {
194 $newModules = array();
195
196 foreach ($modules as $moduleKey => $module) {
197 foreach (array('js', 'css', 'partials') as $fileset) {
198 if (!isset($module[$fileset])) {
199 continue;
200 }
201 $module[$fileset] = $this->res->glob($module['ext'], $module[$fileset]);
202 }
203 $newModules[$moduleKey] = $module;
204 }
205
206 return $newModules;
207 }
208
209 /**
210 * Get the partial HTML documents for a module (unfiltered).
211 *
212 * @param string $name
213 * Angular module name.
214 * @return array
215 * Array(string $extFilePath => string $html)
216 * @throws \Exception
217 * Invalid partials configuration.
218 */
219 public function getRawPartials($name) {
220 $module = $this->getModule($name);
221 $result = array();
222 if (isset($module['partials'])) {
223 foreach ($module['partials'] as $partialDir) {
224 $partialDir = $this->res->getPath($module['ext']) . '/' . $partialDir;
225 $files = \CRM_Utils_File::findFiles($partialDir, '*.html', TRUE);
226 foreach ($files as $file) {
227 $filename = '~/' . $name . '/' . $file;
228 $result[$filename] = file_get_contents($partialDir . '/' . $file);
229 }
230 }
231 return $result;
232 }
233 return $result;
234 }
235
236 /**
237 * Get the partial HTML documents for a module.
238 *
239 * @param string $name
240 * Angular module name.
241 * @return array
242 * Array(string $extFilePath => string $html)
243 * @throws \Exception
244 * Invalid partials configuration.
245 */
246 public function getPartials($name) {
247 $cacheKey = "angular-partials::$name";
248 $cacheValue = $this->cache->get($cacheKey);
249 if ($cacheValue === NULL) {
250 $cacheValue = ChangeSet::applyResourceFilters($this->getChangeSets(), 'partials', $this->getRawPartials($name));
251 $this->cache->set($cacheKey, $cacheValue);
252 }
253 return $cacheValue;
254 }
255
256 /**
257 * Get list of translated strings for a module.
258 *
259 * @param string $name
260 * Angular module name.
261 * @return array
262 * Translated strings: array(string $orig => string $translated).
263 */
264 public function getTranslatedStrings($name) {
265 $module = $this->getModule($name);
266 $result = array();
267 $strings = $this->getStrings($name);
268 foreach ($strings as $string) {
269 // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]?
270 // It doesn't look like client side really supports the domain right now...
271 $translated = ts($string, array(
272 'domain' => array($module['ext'], NULL),
273 ));
274 if ($translated != $string) {
275 $result[$string] = $translated;
276 }
277 }
278 return $result;
279 }
280
281 /**
282 * Get list of translatable strings for a module.
283 *
284 * @param string $name
285 * Angular module name.
286 * @return array
287 * Translatable strings.
288 */
289 public function getStrings($name) {
290 $module = $this->getModule($name);
291 $result = array();
292 if (isset($module['js'])) {
293 foreach ($module['js'] as $file) {
294 $strings = $this->res->getStrings()->get(
295 $module['ext'],
296 $this->res->getPath($module['ext'], $file),
297 'text/javascript'
298 );
299 $result = array_unique(array_merge($result, $strings));
300 }
301 }
302 $partials = $this->getPartials($name);
303 foreach ($partials as $partial) {
304 $result = array_unique(array_merge($result, \CRM_Utils_JS::parseStrings($partial)));
305 }
306 return $result;
307 }
308
309 /**
310 * Get resources for one or more modules.
311 *
312 * @param string|array $moduleNames
313 * List of module names.
314 * @param string $resType
315 * Type of resource ('js', 'css', 'settings').
316 * @param string $refType
317 * Type of reference to the resource ('cacheUrl', 'rawUrl', 'path', 'settings').
318 * @return array
319 * List of URLs or paths.
320 * @throws \CRM_Core_Exception
321 */
322 public function getResources($moduleNames, $resType, $refType) {
323 $result = array();
324 $moduleNames = (array) $moduleNames;
325 foreach ($moduleNames as $moduleName) {
326 $module = $this->getModule($moduleName);
327 if (isset($module[$resType])) {
328 foreach ($module[$resType] as $file) {
329 switch ($refType) {
330 case 'path':
331 $result[] = $this->res->getPath($module['ext'], $file);
332 break;
333
334 case 'rawUrl':
335 $result[] = $this->res->getUrl($module['ext'], $file);
336 break;
337
338 case 'cacheUrl':
339 $result[] = $this->res->getUrl($module['ext'], $file, TRUE);
340 break;
341
342 case 'settings':
343 case 'requires':
344 if (!empty($module[$resType])) {
345 $result[$moduleName] = $module[$resType];
346 }
347 break;
348
349 default:
350 throw new \CRM_Core_Exception("Unrecognized resource format");
351 }
352 }
353 }
354 }
355
356 return ChangeSet::applyResourceFilters($this->getChangeSets(), $resType, $result);
357 }
358
359 /**
360 * @return array
361 * Array(string $name => ChangeSet $changeSet).
362 */
363 public function getChangeSets() {
364 if ($this->changeSets === NULL) {
365 $this->changeSets = array();
366 \CRM_Utils_Hook::alterAngular($this);
367 }
368 return $this->changeSets;
369 }
370
371 /**
372 * @param ChangeSet $changeSet
373 * @return \Civi\Angular\Manager
374 */
375 public function add($changeSet) {
376 $this->changeSets[$changeSet->getName()] = $changeSet;
377 return $this;
378 }
379
380 }