Commit | Line | Data |
---|---|---|
16072ce1 TO |
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 | |
8671b4f2 | 20 | * The Civi extension which defines the Angular module. |
16072ce1 | 21 | * - js: array(string $relativeFilePath) |
8671b4f2 | 22 | * List of JS files (relative to the extension). |
16072ce1 | 23 | * - css: array(string $relativeFilePath) |
8671b4f2 | 24 | * List of CSS files (relative to the extension). |
16072ce1 | 25 | * - partials: array(string $relativeFilePath) |
8671b4f2 TO |
26 | * A list of partial-HTML folders (relative to the extension). |
27 | * This will be mapped to "~/moduleName" by crmResource. | |
1da632e0 TO |
28 | * - settings: array(string $key => mixed $value) |
29 | * List of settings to preload. | |
16072ce1 TO |
30 | */ |
31 | protected $modules = NULL; | |
32 | ||
33 | /** | |
34 | * @param \CRM_Core_Resources $res | |
35 | * The resource manager. | |
36 | */ | |
37 | public function __construct($res) { | |
38 | $this->res = $res; | |
39 | } | |
40 | ||
41 | /** | |
fe482240 | 42 | * Get a list of AngularJS modules which should be autoloaded. |
16072ce1 TO |
43 | * |
44 | * @return array | |
8671b4f2 TO |
45 | * Each item has some combination of these keys: |
46 | * - ext: string | |
47 | * The Civi extension which defines the Angular module. | |
48 | * - js: array(string $relativeFilePath) | |
49 | * List of JS files (relative to the extension). | |
50 | * - css: array(string $relativeFilePath) | |
51 | * List of CSS files (relative to the extension). | |
52 | * - partials: array(string $relativeFilePath) | |
53 | * A list of partial-HTML folders (relative to the extension). | |
54 | * This will be mapped to "~/moduleName" by crmResource. | |
1da632e0 TO |
55 | * - settings: array(string $key => mixed $value) |
56 | * List of settings to preload. | |
16072ce1 TO |
57 | */ |
58 | public function getModules() { | |
59 | if ($this->modules === NULL) { | |
1da632e0 | 60 | $config = \CRM_Core_Config::singleton(); |
16072ce1 TO |
61 | |
62 | $angularModules = array(); | |
63 | $angularModules['angularFileUpload'] = array( | |
64 | 'ext' => 'civicrm', | |
65 | 'js' => array('bower_components/angular-file-upload/angular-file-upload.min.js'), | |
66 | ); | |
67 | $angularModules['crmApp'] = array( | |
68 | 'ext' => 'civicrm', | |
94bc1db0 | 69 | 'js' => array('ang/crmApp.js'), |
16072ce1 TO |
70 | ); |
71 | $angularModules['crmAttachment'] = array( | |
72 | 'ext' => 'civicrm', | |
ad16ac09 TO |
73 | 'js' => array('ang/crmAttachment.js'), |
74 | 'css' => array('ang/crmAttachment.css'), | |
75 | 'partials' => array('ang/crmAttachment'), | |
1da632e0 TO |
76 | 'settings' => array( |
77 | 'token' => \CRM_Core_Page_AJAX_Attachment::createToken(), | |
78 | ), | |
16072ce1 | 79 | ); |
2386ec88 TO |
80 | $angularModules['crmAutosave'] = array( |
81 | 'ext' => 'civicrm', | |
19464351 | 82 | 'js' => array('ang/crmAutosave.js'), |
2386ec88 | 83 | ); |
098de400 TO |
84 | $angularModules['crmCxn'] = array( |
85 | 'ext' => 'civicrm', | |
86 | 'js' => array('ang/crmCxn.js', 'ang/crmCxn/*.js'), | |
87 | 'css' => array('ang/crmCxn.css'), | |
88 | 'partials' => array('ang/crmCxn'), | |
89 | ); | |
56f158de TO |
90 | //$angularModules['crmExample'] = array( |
91 | // 'ext' => 'civicrm', | |
8f1f2525 TO |
92 | // 'js' => array('ang/crmExample.js'), |
93 | // 'partials' => array('ang/crmExample'), | |
56f158de | 94 | //); |
a2dc0f82 TO |
95 | $angularModules['crmResource'] = array( |
96 | 'ext' => 'civicrm', | |
97 | // 'js' => array('js/angular-crmResource/byModule.js'), // One HTTP request per module. | |
98 | 'js' => array('js/angular-crmResource/all.js'), // One HTTP request for all modules. | |
99 | ); | |
16072ce1 TO |
100 | $angularModules['crmUi'] = array( |
101 | 'ext' => 'civicrm', | |
2bcf742a TO |
102 | 'js' => array('ang/crmUi.js'), |
103 | 'partials' => array('ang/crmUi'), | |
1da632e0 TO |
104 | 'settings' => array( |
105 | 'browseUrl' => $config->userFrameworkResourceURL . 'packages/kcfinder/browse.php', | |
106 | 'uploadUrl' => $config->userFrameworkResourceURL . 'packages/kcfinder/upload.php', | |
107 | ), | |
16072ce1 TO |
108 | ); |
109 | $angularModules['crmUtil'] = array( | |
110 | 'ext' => 'civicrm', | |
bf61d18d | 111 | 'js' => array('ang/crmUtil.js'), |
16072ce1 TO |
112 | ); |
113 | // https://github.com/jwstadler/angular-jquery-dialog-service | |
114 | $angularModules['dialogService'] = array( | |
115 | 'ext' => 'civicrm', | |
116 | 'js' => array('bower_components/angular-jquery-dialog-service/dialog-service.js'), | |
117 | ); | |
1c03fdf8 TO |
118 | $angularModules['ngRoute'] = array( |
119 | 'ext' => 'civicrm', | |
120 | 'js' => array('bower_components/angular-route/angular-route.min.js'), | |
121 | ); | |
28949a14 TO |
122 | $angularModules['ngSanitize'] = array( |
123 | 'ext' => 'civicrm', | |
124 | 'js' => array('bower_components/angular-sanitize/angular-sanitize.min.js'), | |
125 | ); | |
16072ce1 TO |
126 | $angularModules['ui.utils'] = array( |
127 | 'ext' => 'civicrm', | |
128 | 'js' => array('bower_components/angular-ui-utils/ui-utils.min.js'), | |
129 | ); | |
130 | $angularModules['ui.sortable'] = array( | |
131 | 'ext' => 'civicrm', | |
132 | 'js' => array('bower_components/angular-ui-sortable/sortable.min.js'), | |
133 | ); | |
134 | $angularModules['unsavedChanges'] = array( | |
135 | 'ext' => 'civicrm', | |
136 | 'js' => array('bower_components/angular-unsavedChanges/dist/unsavedChanges.min.js'), | |
137 | ); | |
138 | ||
139 | foreach (\CRM_Core_Component::getEnabledComponents() as $component) { | |
140 | $angularModules = array_merge($angularModules, $component->getAngularModules()); | |
141 | } | |
142 | \CRM_Utils_Hook::angularModules($angularModules); | |
143 | $this->modules = $this->resolvePatterns($angularModules); | |
144 | } | |
145 | ||
146 | return $this->modules; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Get the descriptor for an Angular module. | |
151 | * | |
152 | * @param string $name | |
153 | * Module name. | |
154 | * @return array | |
155 | * Details about the module: | |
156 | * - ext: string, the name of the Civi extension which defines the module | |
157 | * - js: array(string $relativeFilePath). | |
158 | * - css: array(string $relativeFilePath). | |
159 | * - partials: array(string $relativeFilePath). | |
160 | * @throws \Exception | |
161 | */ | |
162 | public function getModule($name) { | |
163 | $modules = $this->getModules(); | |
164 | if (!isset($modules[$name])) { | |
165 | throw new \Exception("Unrecognized Angular module"); | |
166 | } | |
167 | return $modules[$name]; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Convert any globs in an Angular module to file names. | |
172 | * | |
173 | * @param array $modules | |
174 | * List of Angular modules. | |
175 | * @return array | |
176 | * Updated list of Angular modules | |
177 | */ | |
178 | protected function resolvePatterns($modules) { | |
179 | $newModules = array(); | |
180 | ||
181 | foreach ($modules as $moduleKey => $module) { | |
182 | foreach (array('js', 'css', 'partials') as $fileset) { | |
183 | if (!isset($module[$fileset])) { | |
184 | continue; | |
185 | } | |
186 | $module[$fileset] = $this->res->glob($module['ext'], $module[$fileset]); | |
187 | } | |
188 | $newModules[$moduleKey] = $module; | |
189 | } | |
190 | ||
191 | return $newModules; | |
192 | } | |
193 | ||
194 | /** | |
195 | * Get the partial HTML documents for a module. | |
196 | * | |
197 | * @param string $name | |
198 | * Angular module name. | |
199 | * @return array | |
200 | * Array(string $extFilePath => string $html) | |
a2dc0f82 TO |
201 | * @throws \Exception |
202 | * Invalid partials configuration. | |
16072ce1 TO |
203 | */ |
204 | public function getPartials($name) { | |
205 | $module = $this->getModule($name); | |
206 | $result = array(); | |
207 | if (isset($module['partials'])) { | |
a2dc0f82 TO |
208 | foreach ($module['partials'] as $partialDir) { |
209 | $partialDir = $this->res->getPath($module['ext']) . '/' . $partialDir; | |
210 | $files = \CRM_Utils_File::findFiles($partialDir, '*.html', TRUE); | |
211 | foreach ($files as $file) { | |
212 | $filename = '~/' . $name . '/' . $file; | |
213 | $result[$filename] = file_get_contents($partialDir . '/' . $file); | |
214 | } | |
16072ce1 TO |
215 | } |
216 | } | |
217 | return $result; | |
218 | } | |
219 | ||
16072ce1 TO |
220 | /** |
221 | * Get list of translated strings for a module. | |
222 | * | |
223 | * @param string $name | |
224 | * Angular module name. | |
225 | * @return array | |
226 | * Translated strings: array(string $orig => string $translated). | |
227 | */ | |
228 | public function getTranslatedStrings($name) { | |
e3d90d6c | 229 | $module = $this->getModule($name); |
16072ce1 TO |
230 | $result = array(); |
231 | $strings = $this->getStrings($name); | |
232 | foreach ($strings as $string) { | |
233 | // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]? | |
234 | // It doesn't look like client side really supports the domain right now... | |
e3d90d6c TO |
235 | $translated = ts($string, array( |
236 | 'domain' => array($module['ext'], NULL), | |
237 | )); | |
16072ce1 TO |
238 | if ($translated != $string) { |
239 | $result[$string] = $translated; | |
240 | } | |
241 | } | |
242 | return $result; | |
243 | } | |
244 | ||
245 | /** | |
246 | * Get list of translatable strings for a module. | |
247 | * | |
248 | * @param string $name | |
249 | * Angular module name. | |
250 | * @return array | |
251 | * Translatable strings. | |
252 | */ | |
253 | public function getStrings($name) { | |
254 | $module = $this->getModule($name); | |
255 | $result = array(); | |
256 | if (isset($module['js'])) { | |
257 | foreach ($module['js'] as $file) { | |
258 | $strings = $this->res->getStrings()->get( | |
259 | $module['ext'], | |
260 | $this->res->getPath($module['ext'], $file), | |
261 | 'text/javascript' | |
262 | ); | |
263 | $result = array_unique(array_merge($result, $strings)); | |
264 | } | |
265 | } | |
266 | if (isset($module['partials'])) { | |
a2dc0f82 TO |
267 | foreach ($module['partials'] as $partialDir) { |
268 | $partialDir = $this->res->getPath($module['ext']) . '/' . $partialDir; | |
269 | $files = \CRM_Utils_File::findFiles($partialDir, '*.html'); | |
270 | foreach ($files as $file) { | |
271 | $strings = $this->res->getStrings()->get( | |
272 | $module['ext'], | |
273 | $file, | |
274 | 'text/html' | |
275 | ); | |
276 | $result = array_unique(array_merge($result, $strings)); | |
277 | } | |
16072ce1 TO |
278 | } |
279 | } | |
280 | return $result; | |
281 | } | |
282 | ||
283 | /** | |
27a90ef6 TO |
284 | * Get resources for one or more modules. |
285 | * | |
286 | * @param string|array $moduleNames | |
287 | * List of module names. | |
288 | * @param string $resType | |
1da632e0 | 289 | * Type of resource ('js', 'css', 'settings'). |
27a90ef6 | 290 | * @param string $refType |
1da632e0 | 291 | * Type of reference to the resource ('cacheUrl', 'rawUrl', 'path', 'settings'). |
16072ce1 | 292 | * @return array |
27a90ef6 TO |
293 | * List of URLs or paths. |
294 | * @throws \CRM_Core_Exception | |
16072ce1 | 295 | */ |
27a90ef6 | 296 | public function getResources($moduleNames, $resType, $refType) { |
16072ce1 | 297 | $result = array(); |
27a90ef6 TO |
298 | $moduleNames = (array) $moduleNames; |
299 | foreach ($moduleNames as $moduleName) { | |
300 | $module = $this->getModule($moduleName); | |
301 | if (isset($module[$resType])) { | |
302 | foreach ($module[$resType] as $file) { | |
303 | switch ($refType) { | |
304 | case 'path': | |
305 | $result[] = $this->res->getPath($module['ext'], $file); | |
306 | break; | |
16072ce1 | 307 | |
27a90ef6 TO |
308 | case 'rawUrl': |
309 | $result[] = $this->res->getUrl($module['ext'], $file); | |
310 | break; | |
311 | ||
312 | case 'cacheUrl': | |
313 | $result[] = $this->res->getUrl($module['ext'], $file, TRUE); | |
314 | break; | |
315 | ||
1da632e0 TO |
316 | case 'settings': |
317 | if (!empty($module[$resType])) { | |
318 | $result[$moduleName] = $module[$resType]; | |
319 | } | |
320 | break; | |
321 | ||
27a90ef6 TO |
322 | default: |
323 | throw new \CRM_Core_Exception("Unrecognized resource format"); | |
324 | } | |
325 | } | |
16072ce1 TO |
326 | } |
327 | } | |
328 | return $result; | |
329 | } | |
8671b4f2 | 330 | |
16072ce1 | 331 | } |