APIv4 - Enable getFields to find fields across implicit FK joins
[civicrm-core.git] / Civi / Angular / Page / Modules.php
CommitLineData
e5afbdad
TO
1<?php
2
3namespace Civi\Angular\Page;
4
5/**
27a90ef6
TO
6 * This page aggregates data from Angular modules.
7 *
8 * Example: Aggregate metadata about all modules in JSON format.
9 * civicrm/ajax/angular-modules?format=json
10 *
11 * Example: Aggregate metadata for crmUi and crmUtil modules.
12 * civicrm/ajax/angular-modules?format=json&modules=crmUi,crmUtil
13 *
14 * Example: Aggregate *.js files for all modules.
15 * civicrm/ajax/angular-modules?format=js
16 *
17 * Example: Aggregate *.css files for all modules.
18 * civicrm/ajax/angular-modules?format=css
e5afbdad
TO
19 */
20class Modules extends \CRM_Core_Page {
21
22 /**
466e4b29
TO
23 * Generate asset content (when accessed via older, custom
24 * "civicrm/ajax/anulgar-modules" route).
25 *
26 * @deprecated
ee3db087
SL
27 *
28 * @throws \CRM_Core_Exception
e5afbdad
TO
29 */
30 public function run() {
e5afbdad
TO
31 /**
32 * @var \Civi\Angular\Manager $angular
33 */
048222df 34 $angular = \Civi::service('angular');
27a90ef6
TO
35 $moduleNames = $this->parseModuleNames(\CRM_Utils_Request::retrieve('modules', 'String'), $angular);
36
37 switch (\CRM_Utils_Request::retrieve('format', 'String')) {
38 case 'json':
39 case '':
40 $this->send(
41 'application/javascript',
42 json_encode($this->getMetadata($moduleNames, $angular))
43 );
44 break;
45
46 case 'js':
47 $this->send(
48 'application/javascript',
ad295ca9 49 $this->digestJs($angular->getResources($moduleNames, 'js', 'path'))
27a90ef6
TO
50 );
51 break;
52
53 case 'css':
54 $this->send(
55 'text/css',
56 \CRM_Utils_File::concat($angular->getResources($moduleNames, 'css', 'path'), "\n")
57 );
58 break;
59
60 default:
ee3db087 61 throw new \CRM_Core_Exception("Unrecognized format");
27a90ef6
TO
62 }
63
64 \CRM_Utils_System::civiExit();
65 }
e5afbdad 66
466e4b29
TO
67 /**
68 * Generate asset content (when accessed via AssetBuilder).
69 *
70 * @param \Civi\Core\Event\GenericHookEvent $event
71 * @see CRM_Utils_hook::buildAsset()
72 * @see \Civi\Core\AssetBuilder
73 */
74 public static function buildAngularModules($event) {
75 $page = new Modules();
76 $angular = \Civi::service('angular');
77
78 switch ($event->asset) {
79 case 'angular-modules.json':
9e10fb6b 80 $moduleNames = $page->parseModuleNames($event->params['modules'] ?? NULL, $angular);
466e4b29
TO
81 $event->mimeType = 'application/json';
82 $event->content = json_encode($page->getMetadata($moduleNames, $angular));
83 break;
84
85 case 'angular-modules.js':
9e10fb6b 86 $moduleNames = $page->parseModuleNames($event->params['modules'] ?? NULL, $angular);
466e4b29
TO
87 $event->mimeType = 'application/javascript';
88 $event->content = $page->digestJs($angular->getResources($moduleNames, 'js', 'path'));
89 break;
90
91 case 'angular-modules.css':
9e10fb6b 92 $moduleNames = $page->parseModuleNames($event->params['modules'] ?? NULL, $angular);
466e4b29
TO
93 $event->mimeType = 'text/css';
94 $event->content = \CRM_Utils_File::concat($angular->getResources($moduleNames, 'css', 'path'), "\n");
95
96 default:
97 // Not our problem.
98 }
99 }
100
ad295ca9
TO
101 /**
102 * @param array $files
103 * File paths.
104 * @return string
105 */
106 public function digestJs($files) {
c64f69d9 107 $scripts = [];
ad295ca9 108 foreach ($files as $file) {
0290b3eb 109 $scripts[] = \CRM_Utils_JS::stripComments(file_get_contents($file));
ad295ca9
TO
110 }
111 $scripts = \CRM_Utils_JS::dedupeClosures(
112 $scripts,
c64f69d9
CW
113 ['angular', '$', '_'],
114 ['angular', 'CRM.$', 'CRM._']
ad295ca9 115 );
0290b3eb 116 return implode("\n", $scripts);
ad295ca9
TO
117 }
118
27a90ef6
TO
119 /**
120 * @param string $modulesExpr
121 * Comma-separated list of module names.
122 * @param \Civi\Angular\Manager $angular
123 * @return array
124 * Any well-formed module names. All if moduleExpr is blank.
125 */
126 public function parseModuleNames($modulesExpr, $angular) {
e5afbdad
TO
127 if ($modulesExpr) {
128 $moduleNames = preg_grep(
129 '/^[a-zA-Z0-9\-_\.]+$/',
130 explode(',', $modulesExpr)
131 );
27a90ef6 132 return $moduleNames;
e5afbdad
TO
133 }
134 else {
27a90ef6
TO
135 $moduleNames = array_keys($angular->getModules());
136 return $moduleNames;
e5afbdad 137 }
27a90ef6 138 }
e5afbdad 139
27a90ef6
TO
140 /**
141 * @param array $moduleNames
142 * List of module names.
143 * @param \Civi\Angular\Manager $angular
144 * @return array
145 */
146 public function getMetadata($moduleNames, $angular) {
147 $modules = $angular->getModules();
c64f69d9 148 $result = [];
e5afbdad
TO
149 foreach ($moduleNames as $moduleName) {
150 if (isset($modules[$moduleName])) {
c64f69d9 151 $result[$moduleName] = [];
e3d90d6c 152 $result[$moduleName]['domain'] = $modules[$moduleName]['ext'];
27a90ef6
TO
153 $result[$moduleName]['js'] = $angular->getResources($moduleName, 'js', 'rawUrl');
154 $result[$moduleName]['css'] = $angular->getResources($moduleName, 'css', 'rawUrl');
e5afbdad
TO
155 $result[$moduleName]['partials'] = $angular->getPartials($moduleName);
156 $result[$moduleName]['strings'] = $angular->getTranslatedStrings($moduleName);
157 }
158 }
27a90ef6
TO
159 return $result;
160 }
e5afbdad 161
27a90ef6
TO
162 /**
163 * Send a response.
164 *
165 * @param string $type
166 * Content type.
167 * @param string $data
168 * Content.
169 */
170 public function send($type, $data) {
171 // Encourage browsers to cache for a long time - 1 year
172 $ttl = 60 * 60 * 24 * 364;
956d2f84
CW
173 \CRM_Utils_System::setHttpHeader('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $ttl));
174 \CRM_Utils_System::setHttpHeader("Content-Type", $type);
175 \CRM_Utils_System::setHttpHeader("Cache-Control", "max-age=$ttl, public");
27a90ef6 176 echo $data;
e5afbdad
TO
177 }
178
179}