Set version to 4.7.28
[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.bootstrap'] = include "$civicrm_root/ang/ui.bootstrap.ang.php";
93 $angularModules['ui.sortable'] = include "$civicrm_root/ang/ui.sortable.ang.php";
94 $angularModules['unsavedChanges'] = include "$civicrm_root/ang/unsavedChanges.ang.php";
95 $angularModules['statuspage'] = include "$civicrm_root/ang/crmStatusPage.ang.php";
96
97 foreach (\CRM_Core_Component::getEnabledComponents() as $component) {
98 $angularModules = array_merge($angularModules, $component->getAngularModules());
99 }
100 \CRM_Utils_Hook::angularModules($angularModules);
101 foreach (array_keys($angularModules) as $module) {
102 if (!isset($angularModules[$module]['basePages'])) {
103 $angularModules[$module]['basePages'] = array('civicrm/a');
104 }
105 }
106 $this->modules = $this->resolvePatterns($angularModules);
107 }
108
109 return $this->modules;
110 }
111
112 /**
113 * Get the descriptor for an Angular module.
114 *
115 * @param string $name
116 * Module name.
117 * @return array
118 * Details about the module:
119 * - ext: string, the name of the Civi extension which defines the module
120 * - js: array(string $relativeFilePath).
121 * - css: array(string $relativeFilePath).
122 * - partials: array(string $relativeFilePath).
123 * @throws \Exception
124 */
125 public function getModule($name) {
126 $modules = $this->getModules();
127 if (!isset($modules[$name])) {
128 throw new \Exception("Unrecognized Angular module");
129 }
130 return $modules[$name];
131 }
132
133 /**
134 * Resolve a full list of Angular dependencies.
135 *
136 * @param array $names
137 * List of Angular modules.
138 * Ex: array('crmMailing').
139 * @return array
140 * List of Angular modules, include all dependencies.
141 * Ex: array('crmMailing', 'crmUi', 'crmUtil', 'ngRoute').
142 */
143 public function resolveDependencies($names) {
144 $allModules = $this->getModules();
145 $visited = array();
146 $result = $names;
147 while (($missingModules = array_diff($result, array_keys($visited))) && !empty($missingModules)) {
148 foreach ($missingModules as $module) {
149 $visited[$module] = 1;
150 if (!isset($allModules[$module])) {
151 \Civi::log()->warning('Unrecognized Angular module {name}. Please ensure that all Angular modules are declared.', array(
152 'name' => $module,
153 'civi.tag' => 'deprecated',
154 ));
155 }
156 elseif (isset($allModules[$module]['requires'])) {
157 $result = array_unique(array_merge($result, $allModules[$module]['requires']));
158 }
159 }
160 }
161 sort($result);
162 return $result;
163 }
164
165 /**
166 * Get a list of Angular modules that should be loaded on the given
167 * base-page.
168 *
169 * @param string $basePage
170 * The name of the base-page for which we want a list of moudles.
171 * @return array
172 * List of Angular modules.
173 * Ex: array('crmMailing', 'crmUi', 'crmUtil', 'ngRoute').
174 */
175 public function resolveDefaultModules($basePage) {
176 $modules = $this->getModules();
177 $result = array();
178 foreach ($modules as $moduleName => $module) {
179 if (in_array($basePage, $module['basePages']) || in_array('*', $module['basePages'])) {
180 $result[] = $moduleName;
181 }
182 }
183 return $result;
184 }
185
186 /**
187 * Convert any globs in an Angular module to file names.
188 *
189 * @param array $modules
190 * List of Angular modules.
191 * @return array
192 * Updated list of Angular modules
193 */
194 protected function resolvePatterns($modules) {
195 $newModules = array();
196
197 foreach ($modules as $moduleKey => $module) {
198 foreach (array('js', 'css', 'partials') as $fileset) {
199 if (!isset($module[$fileset])) {
200 continue;
201 }
202 $module[$fileset] = $this->res->glob($module['ext'], $module[$fileset]);
203 }
204 $newModules[$moduleKey] = $module;
205 }
206
207 return $newModules;
208 }
209
210 /**
211 * Get the partial HTML documents for a module (unfiltered).
212 *
213 * @param string $name
214 * Angular module name.
215 * @return array
216 * Array(string $extFilePath => string $html)
217 * @throws \Exception
218 * Invalid partials configuration.
219 */
220 public function getRawPartials($name) {
221 $module = $this->getModule($name);
222 $result = array();
223 if (isset($module['partials'])) {
224 foreach ($module['partials'] as $partialDir) {
225 $partialDir = $this->res->getPath($module['ext']) . '/' . $partialDir;
226 $files = \CRM_Utils_File::findFiles($partialDir, '*.html', TRUE);
227 foreach ($files as $file) {
228 $filename = '~/' . $name . '/' . $file;
229 $result[$filename] = file_get_contents($partialDir . '/' . $file);
230 }
231 }
232 return $result;
233 }
234 return $result;
235 }
236
237 /**
238 * Get the partial HTML documents for a module.
239 *
240 * @param string $name
241 * Angular module name.
242 * @return array
243 * Array(string $extFilePath => string $html)
244 * @throws \Exception
245 * Invalid partials configuration.
246 */
247 public function getPartials($name) {
248 $cacheKey = "angular-partials::$name";
249 $cacheValue = $this->cache->get($cacheKey);
250 if ($cacheValue === NULL) {
251 $cacheValue = ChangeSet::applyResourceFilters($this->getChangeSets(), 'partials', $this->getRawPartials($name));
252 $this->cache->set($cacheKey, $cacheValue);
253 }
254 return $cacheValue;
255 }
256
257 /**
258 * Get list of translated strings for a module.
259 *
260 * @param string $name
261 * Angular module name.
262 * @return array
263 * Translated strings: array(string $orig => string $translated).
264 */
265 public function getTranslatedStrings($name) {
266 $module = $this->getModule($name);
267 $result = array();
268 $strings = $this->getStrings($name);
269 foreach ($strings as $string) {
270 // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]?
271 // It doesn't look like client side really supports the domain right now...
272 $translated = ts($string, array(
273 'domain' => array($module['ext'], NULL),
274 ));
275 if ($translated != $string) {
276 $result[$string] = $translated;
277 }
278 }
279 return $result;
280 }
281
282 /**
283 * Get list of translatable strings for a module.
284 *
285 * @param string $name
286 * Angular module name.
287 * @return array
288 * Translatable strings.
289 */
290 public function getStrings($name) {
291 $module = $this->getModule($name);
292 $result = array();
293 if (isset($module['js'])) {
294 foreach ($module['js'] as $file) {
295 $strings = $this->res->getStrings()->get(
296 $module['ext'],
297 $this->res->getPath($module['ext'], $file),
298 'text/javascript'
299 );
300 $result = array_unique(array_merge($result, $strings));
301 }
302 }
303 $partials = $this->getPartials($name);
304 foreach ($partials as $partial) {
305 $result = array_unique(array_merge($result, \CRM_Utils_JS::parseStrings($partial)));
306 }
307 return $result;
308 }
309
310 /**
311 * Get resources for one or more modules.
312 *
313 * @param string|array $moduleNames
314 * List of module names.
315 * @param string $resType
316 * Type of resource ('js', 'css', 'settings').
317 * @param string $refType
318 * Type of reference to the resource ('cacheUrl', 'rawUrl', 'path', 'settings').
319 * @return array
320 * List of URLs or paths.
321 * @throws \CRM_Core_Exception
322 */
323 public function getResources($moduleNames, $resType, $refType) {
324 $result = array();
325 $moduleNames = (array) $moduleNames;
326 foreach ($moduleNames as $moduleName) {
327 $module = $this->getModule($moduleName);
328 if (isset($module[$resType])) {
329 foreach ($module[$resType] as $file) {
330 $refTypeSuffix = '';
331 if (is_string($file) && preg_match(';^(assetBuilder|ext)://;', $file)) {
332 $refTypeSuffix = '-' . parse_url($file, PHP_URL_SCHEME);
333 }
334
335 switch ($refType . $refTypeSuffix) {
336 case 'path':
337 $result[] = $this->res->getPath($module['ext'], $file);
338 break;
339
340 case 'rawUrl':
341 $result[] = $this->res->getUrl($module['ext'], $file);
342 break;
343
344 case 'cacheUrl':
345 $result[] = $this->res->getUrl($module['ext'], $file, TRUE);
346 break;
347
348 case 'path-assetBuilder':
349 $assetName = parse_url($file, PHP_URL_HOST) . parse_url($file, PHP_URL_PATH);
350 $assetParams = array();
351 parse_str('' . parse_url($file, PHP_URL_QUERY), $assetParams);
352 $result[] = \Civi::service('asset_builder')->getPath($assetName, $assetParams);
353 break;
354
355 case 'rawUrl-assetBuilder':
356 case 'cacheUrl-assetBuilder':
357 $assetName = parse_url($file, PHP_URL_HOST) . parse_url($file, PHP_URL_PATH);
358 $assetParams = array();
359 parse_str('' . parse_url($file, PHP_URL_QUERY), $assetParams);
360 $result[] = \Civi::service('asset_builder')->getUrl($assetName, $assetParams);
361 break;
362
363 case 'path-ext':
364 $result[] = $this->res->getPath(parse_url($file, PHP_URL_HOST), ltrim(parse_url($file, PHP_URL_PATH), '/'));
365 break;
366
367 case 'rawUrl-ext':
368 $result[] = $this->res->getUrl(parse_url($file, PHP_URL_HOST), ltrim(parse_url($file, PHP_URL_PATH), '/'));
369 break;
370
371 case 'cacheUrl-ext':
372 $result[] = $this->res->getUrl(parse_url($file, PHP_URL_HOST), ltrim(parse_url($file, PHP_URL_PATH), '/'), TRUE);
373 break;
374
375 case 'settings':
376 case 'requires':
377 if (!empty($module[$resType])) {
378 $result[$moduleName] = $module[$resType];
379 }
380 break;
381
382 default:
383 throw new \CRM_Core_Exception("Unrecognized resource format");
384 }
385 }
386 }
387 }
388
389 return ChangeSet::applyResourceFilters($this->getChangeSets(), $resType, $result);
390 }
391
392 /**
393 * @return array
394 * Array(string $name => ChangeSet $changeSet).
395 */
396 public function getChangeSets() {
397 if ($this->changeSets === NULL) {
398 $this->changeSets = array();
399 \CRM_Utils_Hook::alterAngular($this);
400 }
401 return $this->changeSets;
402 }
403
404 /**
405 * @param ChangeSet $changeSet
406 * @return \Civi\Angular\Manager
407 */
408 public function add($changeSet) {
409 $this->changeSets[$changeSet->getName()] = $changeSet;
410 return $this;
411 }
412
413 }