Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
8d7a9d07 | 3 | +--------------------------------------------------------------------+ |
fee14197 | 4 | | CiviCRM version 5 | |
8d7a9d07 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
8d7a9d07 CB |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
1889d803 | 27 | use Civi\Core\Event\GenericHookEvent; |
6a488035 TO |
28 | |
29 | /** | |
30 | * This class facilitates the loading of resources | |
31 | * such as JavaScript files and CSS files. | |
32 | * | |
33 | * Any URLs generated for resources may include a 'cache-code'. By resetting the | |
34 | * cache-code, one may force clients to re-download resource files (regardless of | |
35 | * any HTTP caching rules). | |
36 | * | |
37 | * TODO: This is currently a thin wrapper over CRM_Core_Region. We | |
38 | * should incorporte services for aggregation, minimization, etc. | |
39 | * | |
40 | * @package CRM | |
6b83d5bd | 41 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
42 | * $Id$ |
43 | * | |
44 | */ | |
45 | class CRM_Core_Resources { | |
46 | const DEFAULT_WEIGHT = 0; | |
47 | const DEFAULT_REGION = 'page-footer'; | |
48 | ||
49 | /** | |
50 | * We don't have a container or dependency-injection, so use singleton instead | |
51 | * | |
52 | * @var object | |
6a488035 TO |
53 | */ |
54 | private static $_singleton = NULL; | |
55 | ||
56 | /** | |
57 | * @var CRM_Extension_Mapper | |
58 | */ | |
59 | private $extMapper = NULL; | |
60 | ||
61 | /** | |
fd7dc3f3 | 62 | * @var CRM_Core_Resources_Strings |
6a488035 | 63 | */ |
fd7dc3f3 | 64 | private $strings = NULL; |
6a488035 TO |
65 | |
66 | /** | |
67 | * @var array free-form data tree | |
68 | */ | |
be2fb01f | 69 | protected $settings = []; |
6a488035 TO |
70 | protected $addedSettings = FALSE; |
71 | ||
72 | /** | |
73 | * @var array of callables | |
74 | */ | |
be2fb01f | 75 | protected $settingsFactories = []; |
6a488035 TO |
76 | |
77 | /** | |
78 | * @var array ($regionName => bool) | |
79 | */ | |
be2fb01f | 80 | protected $addedCoreResources = []; |
6a488035 TO |
81 | |
82 | /** | |
83 | * @var array ($regionName => bool) | |
84 | */ | |
be2fb01f | 85 | protected $addedCoreStyles = []; |
6a488035 TO |
86 | |
87 | /** | |
88 | * @var string a value to append to JS/CSS URLs to coerce cache resets | |
89 | */ | |
90 | protected $cacheCode = NULL; | |
91 | ||
92 | /** | |
93 | * @var string the name of a setting which persistently stores the cacheCode | |
94 | */ | |
95 | protected $cacheCodeKey = NULL; | |
96 | ||
53f2643c CW |
97 | /** |
98 | * @var bool | |
99 | */ | |
100 | public $ajaxPopupsEnabled; | |
101 | ||
b698e2d5 TO |
102 | /** |
103 | * @var \Civi\Core\Paths | |
104 | */ | |
105 | protected $paths; | |
106 | ||
6a488035 | 107 | /** |
fe482240 | 108 | * Get or set the single instance of CRM_Core_Resources. |
6a488035 | 109 | * |
5a4f6742 CW |
110 | * @param CRM_Core_Resources $instance |
111 | * New copy of the manager. | |
6a488035 TO |
112 | * @return CRM_Core_Resources |
113 | */ | |
518fa0ee | 114 | public static function singleton(CRM_Core_Resources $instance = NULL) { |
6a488035 TO |
115 | if ($instance !== NULL) { |
116 | self::$_singleton = $instance; | |
117 | } | |
118 | if (self::$_singleton === NULL) { | |
223ba025 | 119 | self::$_singleton = Civi::service('resources'); |
6a488035 TO |
120 | } |
121 | return self::$_singleton; | |
122 | } | |
123 | ||
124 | /** | |
d09edf64 | 125 | * Construct a resource manager. |
6a488035 | 126 | * |
6a0b768e TO |
127 | * @param CRM_Extension_Mapper $extMapper |
128 | * Map extension names to their base path or URLs. | |
129 | * @param CRM_Utils_Cache_Interface $cache | |
130 | * JS-localization cache. | |
3be754d6 | 131 | * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources |
6a488035 TO |
132 | */ |
133 | public function __construct($extMapper, $cache, $cacheCodeKey = NULL) { | |
134 | $this->extMapper = $extMapper; | |
fd7dc3f3 | 135 | $this->strings = new CRM_Core_Resources_Strings($cache); |
6a488035 TO |
136 | $this->cacheCodeKey = $cacheCodeKey; |
137 | if ($cacheCodeKey !== NULL) { | |
aaffa79f | 138 | $this->cacheCode = Civi::settings()->get($cacheCodeKey); |
6a488035 | 139 | } |
150f50c1 | 140 | if (!$this->cacheCode) { |
6a488035 TO |
141 | $this->resetCacheCode(); |
142 | } | |
84fb7424 | 143 | $this->ajaxPopupsEnabled = (bool) Civi::settings()->get('ajaxPopupsEnabled'); |
b698e2d5 | 144 | $this->paths = Civi::paths(); |
6a488035 TO |
145 | } |
146 | ||
90efc417 TO |
147 | /** |
148 | * Export permission data to the client to enable smarter GUIs. | |
149 | * | |
150 | * Note: Application security stems from the server's enforcement | |
151 | * of the security logic (e.g. in the API permissions). There's no way | |
152 | * the client can use this info to make the app more secure; however, | |
153 | * it can produce a better-tuned (non-broken) UI. | |
154 | * | |
155 | * @param array $permNames | |
156 | * List of permission names to check/export. | |
157 | * @return CRM_Core_Resources | |
158 | */ | |
159 | public function addPermissions($permNames) { | |
160 | $permNames = (array) $permNames; | |
be2fb01f | 161 | $perms = []; |
90efc417 TO |
162 | foreach ($permNames as $permName) { |
163 | $perms[$permName] = CRM_Core_Permission::check($permName); | |
164 | } | |
be2fb01f | 165 | return $this->addSetting([ |
90efc417 | 166 | 'permissions' => $perms, |
be2fb01f | 167 | ]); |
90efc417 TO |
168 | } |
169 | ||
6a488035 TO |
170 | /** |
171 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
172 | * | |
5a4f6742 CW |
173 | * @param string $ext |
174 | * extension name; use 'civicrm' for core. | |
175 | * @param string $file | |
176 | * file path -- relative to the extension base dir. | |
177 | * @param int $weight | |
178 | * relative weight within a given region. | |
179 | * @param string $region | |
180 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
3f3bba82 TO |
181 | * @param bool|string $translate |
182 | * Whether to load translated strings for this file. Use one of: | |
183 | * - FALSE: Do not load translated strings. | |
184 | * - TRUE: Load translated strings. Use the $ext's default domain. | |
185 | * - string: Load translated strings. Use a specific domain. | |
6a488035 TO |
186 | * |
187 | * @return CRM_Core_Resources | |
188 | */ | |
189 | public function addScriptFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION, $translate = TRUE) { | |
190 | if ($translate) { | |
3f3bba82 TO |
191 | $domain = ($translate === TRUE) ? $ext : $translate; |
192 | $this->addString($this->strings->get($domain, $this->getPath($ext, $file), 'text/javascript'), $domain); | |
6a488035 | 193 | } |
09a4dcd5 | 194 | $this->resolveFileName($file, $ext); |
6a488035 TO |
195 | return $this->addScriptUrl($this->getUrl($ext, $file, TRUE), $weight, $region); |
196 | } | |
197 | ||
198 | /** | |
199 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
200 | * | |
5a4f6742 CW |
201 | * @param string $url |
202 | * @param int $weight | |
203 | * relative weight within a given region. | |
204 | * @param string $region | |
205 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
206 | * @return CRM_Core_Resources |
207 | */ | |
208 | public function addScriptUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
be2fb01f | 209 | CRM_Core_Region::instance($region)->add([ |
518fa0ee SL |
210 | 'name' => $url, |
211 | 'type' => 'scriptUrl', | |
212 | 'scriptUrl' => $url, | |
213 | 'weight' => $weight, | |
214 | 'region' => $region, | |
215 | ]); | |
6a488035 TO |
216 | return $this; |
217 | } | |
218 | ||
219 | /** | |
220 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
221 | * | |
5a4f6742 CW |
222 | * @param string $code |
223 | * JavaScript source code. | |
224 | * @param int $weight | |
225 | * relative weight within a given region. | |
226 | * @param string $region | |
227 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
228 | * @return CRM_Core_Resources |
229 | */ | |
230 | public function addScript($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
be2fb01f | 231 | CRM_Core_Region::instance($region)->add([ |
8d7a9d07 | 232 | // 'name' => automatic |
518fa0ee SL |
233 | 'type' => 'script', |
234 | 'script' => $code, | |
235 | 'weight' => $weight, | |
236 | 'region' => $region, | |
237 | ]); | |
6a488035 TO |
238 | return $this; |
239 | } | |
240 | ||
241 | /** | |
73bcd446 | 242 | * Add JavaScript variables to CRM.vars |
6a488035 | 243 | * |
132fc68e CW |
244 | * Example: |
245 | * From the server: | |
73bcd446 CW |
246 | * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar')); |
247 | * Access var from javascript: | |
248 | * CRM.vars.myNamespace.foo // "bar" | |
132fc68e | 249 | * |
01478033 | 250 | * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference |
132fc68e | 251 | * |
6a0b768e TO |
252 | * @param string $nameSpace |
253 | * Usually the name of your extension. | |
73bcd446 CW |
254 | * @param array $vars |
255 | * @return CRM_Core_Resources | |
256 | */ | |
257 | public function addVars($nameSpace, $vars) { | |
be2fb01f | 258 | $existing = CRM_Utils_Array::value($nameSpace, CRM_Utils_Array::value('vars', $this->settings), []); |
73bcd446 | 259 | $vars = $this->mergeSettings($existing, $vars); |
be2fb01f | 260 | $this->addSetting(['vars' => [$nameSpace => $vars]]); |
73bcd446 CW |
261 | return $this; |
262 | } | |
263 | ||
264 | /** | |
265 | * Add JavaScript variables to the root of the CRM object. | |
266 | * This function is usually reserved for low-level system use. | |
267 | * Extensions and components should generally use addVars instead. | |
268 | * | |
5a4f6742 | 269 | * @param array $settings |
6a488035 TO |
270 | * @return CRM_Core_Resources |
271 | */ | |
272 | public function addSetting($settings) { | |
273 | $this->settings = $this->mergeSettings($this->settings, $settings); | |
274 | if (!$this->addedSettings) { | |
88cd8875 | 275 | $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header'; |
6a488035 | 276 | $resources = $this; |
be2fb01f | 277 | CRM_Core_Region::instance($region)->add([ |
353ffa53 TO |
278 | 'callback' => function (&$snippet, &$html) use ($resources) { |
279 | $html .= "\n" . $resources->renderSetting(); | |
280 | }, | |
8cb324cc | 281 | 'weight' => -100000, |
be2fb01f | 282 | ]); |
6a488035 TO |
283 | $this->addedSettings = TRUE; |
284 | } | |
285 | return $this; | |
286 | } | |
287 | ||
288 | /** | |
69847402 | 289 | * Add JavaScript variables to the global CRM object via a callback function. |
6a488035 | 290 | * |
3be754d6 | 291 | * @param callable $callable |
6a488035 TO |
292 | * @return CRM_Core_Resources |
293 | */ | |
294 | public function addSettingsFactory($callable) { | |
d937dbcd | 295 | // Make sure our callback has been registered |
be2fb01f | 296 | $this->addSetting([]); |
6a488035 TO |
297 | $this->settingsFactories[] = $callable; |
298 | return $this; | |
299 | } | |
300 | ||
69847402 | 301 | /** |
d09edf64 | 302 | * Helper fn for addSettingsFactory. |
69847402 | 303 | */ |
6a488035 TO |
304 | public function getSettings() { |
305 | $result = $this->settings; | |
306 | foreach ($this->settingsFactories as $callable) { | |
307 | $result = $this->mergeSettings($result, $callable()); | |
308 | } | |
898951c6 | 309 | CRM_Utils_Hook::alterResourceSettings($result); |
6a488035 TO |
310 | return $result; |
311 | } | |
312 | ||
313 | /** | |
314 | * @param array $settings | |
315 | * @param array $additions | |
a6c01b45 CW |
316 | * @return array |
317 | * combination of $settings and $additions | |
6a488035 TO |
318 | */ |
319 | protected function mergeSettings($settings, $additions) { | |
320 | foreach ($additions as $k => $v) { | |
321 | if (isset($settings[$k]) && is_array($settings[$k]) && is_array($v)) { | |
322 | $v += $settings[$k]; | |
323 | } | |
324 | $settings[$k] = $v; | |
325 | } | |
326 | return $settings; | |
327 | } | |
328 | ||
329 | /** | |
d09edf64 | 330 | * Helper fn for addSetting. |
6a488035 TO |
331 | * Render JavaScript variables for the global CRM object. |
332 | * | |
6a488035 TO |
333 | * @return string |
334 | */ | |
335 | public function renderSetting() { | |
4acecdfd | 336 | /*// On a standard page request we construct the CRM object from scratch |
156fd9b9 CW |
337 | if (!self::isAjaxMode()) { |
338 | $js = 'var CRM = ' . json_encode($this->getSettings()) . ';'; | |
339 | } | |
340 | // For an ajax request we append to it | |
341 | else { | |
342 | $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');'; | |
343 | } | |
d759bd10 | 344 | return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js); |
4acecdfd LMM |
345 | */ |
346 | $js = 'var CRM = ' . json_encode($this->getSettings()) . ';'; | |
347 | //return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js); | |
348 | return sprintf("<script type=\"text/javascript\">\n// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3\n%s\n// @license-end\n</script>\n", $js); | |
6a488035 TO |
349 | } |
350 | ||
351 | /** | |
352 | * Add translated string to the js CRM object. | |
353 | * It can then be retrived from the client-side ts() function | |
354 | * Variable substitutions can happen from client-side | |
8ef12e64 | 355 | * |
6a488035 | 356 | * Note: this function rarely needs to be called directly and is mostly for internal use. |
d3e86119 | 357 | * See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files |
6a488035 TO |
358 | * |
359 | * Simple example: | |
360 | * // From php: | |
361 | * CRM_Core_Resources::singleton()->addString('Hello'); | |
362 | * // The string is now available to javascript code i.e. | |
363 | * ts('Hello'); | |
364 | * | |
365 | * Example with client-side substitutions: | |
366 | * // From php: | |
367 | * CRM_Core_Resources::singleton()->addString('Your %1 has been %2'); | |
368 | * // ts() in javascript works the same as in php, for example: | |
369 | * ts('Your %1 has been %2', {1: objectName, 2: actionTaken}); | |
370 | * | |
371 | * NOTE: This function does not work with server-side substitutions | |
372 | * (as this might result in collisions and unwanted variable injections) | |
373 | * Instead, use code like: | |
374 | * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs))))); | |
375 | * And from javascript access it at CRM.myNamespace.myString | |
376 | * | |
5a4f6742 | 377 | * @param string|array $text |
3f3bba82 | 378 | * @param string|NULL $domain |
6a488035 TO |
379 | * @return CRM_Core_Resources |
380 | */ | |
3f3bba82 | 381 | public function addString($text, $domain = 'civicrm') { |
6a488035 | 382 | foreach ((array) $text as $str) { |
be2fb01f CW |
383 | $translated = ts($str, [ |
384 | 'domain' => ($domain == 'civicrm') ? NULL : [$domain, NULL], | |
1b4710da | 385 | 'raw' => TRUE, |
be2fb01f | 386 | ]); |
3f3bba82 | 387 | |
6a488035 TO |
388 | // We only need to push this string to client if the translation |
389 | // is actually different from the original | |
390 | if ($translated != $str) { | |
3f3bba82 | 391 | $bucket = $domain == 'civicrm' ? 'strings' : 'strings::' . $domain; |
be2fb01f CW |
392 | $this->addSetting([ |
393 | $bucket => [$str => $translated], | |
394 | ]); | |
6a488035 TO |
395 | } |
396 | } | |
397 | return $this; | |
398 | } | |
399 | ||
400 | /** | |
401 | * Add a CSS file to the current page using <LINK HREF>. | |
402 | * | |
5a4f6742 CW |
403 | * @param string $ext |
404 | * extension name; use 'civicrm' for core. | |
405 | * @param string $file | |
406 | * file path -- relative to the extension base dir. | |
407 | * @param int $weight | |
408 | * relative weight within a given region. | |
409 | * @param string $region | |
410 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
411 | * @return CRM_Core_Resources |
412 | */ | |
413 | public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
09a4dcd5 | 414 | $this->resolveFileName($file, $ext); |
6a488035 TO |
415 | return $this->addStyleUrl($this->getUrl($ext, $file, TRUE), $weight, $region); |
416 | } | |
417 | ||
418 | /** | |
419 | * Add a CSS file to the current page using <LINK HREF>. | |
420 | * | |
5a4f6742 CW |
421 | * @param string $url |
422 | * @param int $weight | |
423 | * relative weight within a given region. | |
424 | * @param string $region | |
425 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
426 | * @return CRM_Core_Resources |
427 | */ | |
428 | public function addStyleUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
be2fb01f | 429 | CRM_Core_Region::instance($region)->add([ |
518fa0ee SL |
430 | 'name' => $url, |
431 | 'type' => 'styleUrl', | |
432 | 'styleUrl' => $url, | |
433 | 'weight' => $weight, | |
434 | 'region' => $region, | |
435 | ]); | |
6a488035 TO |
436 | return $this; |
437 | } | |
438 | ||
439 | /** | |
440 | * Add a CSS content to the current page using <STYLE>. | |
441 | * | |
5a4f6742 CW |
442 | * @param string $code |
443 | * CSS source code. | |
444 | * @param int $weight | |
445 | * relative weight within a given region. | |
446 | * @param string $region | |
447 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
448 | * @return CRM_Core_Resources |
449 | */ | |
450 | public function addStyle($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
be2fb01f | 451 | CRM_Core_Region::instance($region)->add([ |
8d7a9d07 | 452 | // 'name' => automatic |
518fa0ee SL |
453 | 'type' => 'style', |
454 | 'style' => $code, | |
455 | 'weight' => $weight, | |
456 | 'region' => $region, | |
457 | ]); | |
6a488035 TO |
458 | return $this; |
459 | } | |
460 | ||
461 | /** | |
d09edf64 | 462 | * Determine file path of a resource provided by an extension. |
6a488035 | 463 | * |
5a4f6742 CW |
464 | * @param string $ext |
465 | * extension name; use 'civicrm' for core. | |
16cd1eca | 466 | * @param string|NULL $file |
5a4f6742 | 467 | * file path -- relative to the extension base dir. |
6a488035 | 468 | * |
72b3a70c CW |
469 | * @return bool|string |
470 | * full file path or FALSE if not found | |
6a488035 | 471 | */ |
16cd1eca | 472 | public function getPath($ext, $file = NULL) { |
6a488035 | 473 | // TODO consider caching results |
b698e2d5 TO |
474 | $base = $this->paths->hasVariable($ext) |
475 | ? rtrim($this->paths->getVariable($ext, 'path'), '/') | |
476 | : $this->extMapper->keyToBasePath($ext); | |
16cd1eca | 477 | if ($file === NULL) { |
b698e2d5 | 478 | return $base; |
16cd1eca | 479 | } |
b698e2d5 | 480 | $path = $base . '/' . $file; |
6a488035 TO |
481 | if (is_file($path)) { |
482 | return $path; | |
483 | } | |
484 | return FALSE; | |
485 | } | |
486 | ||
487 | /** | |
d09edf64 | 488 | * Determine public URL of a resource provided by an extension. |
6a488035 | 489 | * |
5a4f6742 CW |
490 | * @param string $ext |
491 | * extension name; use 'civicrm' for core. | |
492 | * @param string $file | |
493 | * file path -- relative to the extension base dir. | |
2a6da8d7 EM |
494 | * @param bool $addCacheCode |
495 | * | |
6a488035 TO |
496 | * @return string, URL |
497 | */ | |
498 | public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) { | |
499 | if ($file === NULL) { | |
500 | $file = ''; | |
501 | } | |
502 | if ($addCacheCode) { | |
6f12c6eb | 503 | $file = $this->addCacheCode($file); |
6a488035 TO |
504 | } |
505 | // TODO consider caching results | |
b698e2d5 TO |
506 | $base = $this->paths->hasVariable($ext) |
507 | ? $this->paths->getVariable($ext, 'url') | |
508 | : ($this->extMapper->keyToUrl($ext) . '/'); | |
509 | return $base . $file; | |
6a488035 TO |
510 | } |
511 | ||
16cd1eca TO |
512 | /** |
513 | * Evaluate a glob pattern in the context of a particular extension. | |
514 | * | |
515 | * @param string $ext | |
516 | * Extension name; use 'civicrm' for core. | |
517 | * @param string|array $patterns | |
518 | * Glob pattern; e.g. "*.html". | |
519 | * @param null|int $flags | |
520 | * See glob(). | |
521 | * @return array | |
522 | * List of matching files, relative to the extension base dir. | |
523 | * @see glob() | |
524 | */ | |
525 | public function glob($ext, $patterns, $flags = NULL) { | |
526 | $path = $this->getPath($ext); | |
527 | $patterns = (array) $patterns; | |
be2fb01f | 528 | $files = []; |
16cd1eca | 529 | foreach ($patterns as $pattern) { |
e5c376e7 TO |
530 | if (preg_match(';^(assetBuilder|ext)://;', $pattern)) { |
531 | $files[] = $pattern; | |
532 | } | |
9f87b14b | 533 | if (CRM_Utils_File::isAbsolute($pattern)) { |
16cd1eca TO |
534 | // Absolute path. |
535 | $files = array_merge($files, (array) glob($pattern, $flags)); | |
536 | } | |
537 | else { | |
538 | // Relative path. | |
539 | $files = array_merge($files, (array) glob("$path/$pattern", $flags)); | |
540 | } | |
541 | } | |
518fa0ee SL |
542 | // Deterministic order. |
543 | sort($files); | |
16cd1eca TO |
544 | $files = array_unique($files); |
545 | return array_map(function ($file) use ($path) { | |
546 | return CRM_Utils_File::relativize($file, "$path/"); | |
547 | }, $files); | |
548 | } | |
549 | ||
a0ee3941 EM |
550 | /** |
551 | * @return string | |
552 | */ | |
6a488035 TO |
553 | public function getCacheCode() { |
554 | return $this->cacheCode; | |
555 | } | |
556 | ||
a0ee3941 EM |
557 | /** |
558 | * @param $value | |
5badddc3 | 559 | * @return CRM_Core_Resources |
a0ee3941 | 560 | */ |
6a488035 TO |
561 | public function setCacheCode($value) { |
562 | $this->cacheCode = $value; | |
563 | if ($this->cacheCodeKey) { | |
08ef4ddd | 564 | Civi::settings()->set($this->cacheCodeKey, $value); |
6a488035 | 565 | } |
9762f6ff | 566 | return $this; |
6a488035 TO |
567 | } |
568 | ||
5badddc3 CW |
569 | /** |
570 | * @return CRM_Core_Resources | |
571 | */ | |
6a488035 TO |
572 | public function resetCacheCode() { |
573 | $this->setCacheCode(CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC)); | |
f091327b CW |
574 | // Also flush cms resource cache if needed |
575 | CRM_Core_Config::singleton()->userSystem->clearResourceCache(); | |
9762f6ff | 576 | return $this; |
6a488035 TO |
577 | } |
578 | ||
579 | /** | |
580 | * This adds CiviCRM's standard css and js to the specified region of the document. | |
581 | * It will only run once. | |
582 | * | |
583 | * TODO: Separate the functional code (like addStyle/addScript) from the policy code | |
584 | * (like addCoreResources/addCoreStyles). | |
585 | * | |
2a6da8d7 | 586 | * @param string $region |
6a488035 | 587 | * @return CRM_Core_Resources |
6a488035 TO |
588 | */ |
589 | public function addCoreResources($region = 'html-header') { | |
156fd9b9 | 590 | if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) { |
6a488035 TO |
591 | $this->addedCoreResources[$region] = TRUE; |
592 | $config = CRM_Core_Config::singleton(); | |
593 | ||
a43b1583 | 594 | // Add resources from coreResourceList |
6a488035 | 595 | $jsWeight = -9999; |
dbb12634 | 596 | foreach ($this->coreResourceList($region) as $item) { |
7266e09b CW |
597 | if (is_array($item)) { |
598 | $this->addSetting($item); | |
599 | } | |
adcd4bf7 CW |
600 | elseif (strpos($item, '.css')) { |
601 | $this->isFullyFormedUrl($item) ? $this->addStyleUrl($item, -100, $region) : $this->addStyleFile('civicrm', $item, -100, $region); | |
602 | } | |
603 | elseif ($this->isFullyFormedUrl($item)) { | |
604 | $this->addScriptUrl($item, $jsWeight++, $region); | |
605 | } | |
606 | else { | |
74227f77 | 607 | // Don't bother looking for ts() calls in packages, there aren't any |
7266e09b CW |
608 | $translate = (substr($item, 0, 3) == 'js/'); |
609 | $this->addScriptFile('civicrm', $item, $jsWeight++, $region, $translate); | |
6a488035 | 610 | } |
6a488035 | 611 | } |
d759bd10 | 612 | // Add global settings |
be2fb01f CW |
613 | $settings = [ |
614 | 'config' => [ | |
353ffa53 | 615 | 'isFrontend' => $config->userFrameworkFrontend, |
be2fb01f CW |
616 | ], |
617 | ]; | |
4e56e743 CW |
618 | // Disable profile creation if user lacks permission |
619 | if (!CRM_Core_Permission::check('edit all contacts') && !CRM_Core_Permission::check('add contacts')) { | |
b7ceb253 | 620 | $settings['config']['entityRef']['contactCreate'] = FALSE; |
3d527838 | 621 | } |
4e56e743 | 622 | $this->addSetting($settings); |
3d527838 CW |
623 | |
624 | // Give control of jQuery and _ back to the CMS - this loads last | |
c2777586 | 625 | $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE); |
6a488035 TO |
626 | |
627 | $this->addCoreStyles($region); | |
628 | } | |
629 | return $this; | |
630 | } | |
631 | ||
632 | /** | |
633 | * This will add CiviCRM's standard CSS | |
634 | * | |
635 | * TODO: Separate the functional code (like addStyle/addScript) from the policy code | |
636 | * (like addCoreResources/addCoreStyles). | |
637 | * | |
638 | * @param string $region | |
639 | * @return CRM_Core_Resources | |
640 | */ | |
641 | public function addCoreStyles($region = 'html-header') { | |
642 | if (!isset($this->addedCoreStyles[$region])) { | |
643 | $this->addedCoreStyles[$region] = TRUE; | |
644 | ||
645 | // Load custom or core css | |
646 | $config = CRM_Core_Config::singleton(); | |
647 | if (!empty($config->customCSSURL)) { | |
6f12c6eb | 648 | $customCSSURL = $this->addCacheCode($config->customCSSURL); |
649 | $this->addStyleUrl($customCSSURL, 99, $region); | |
6a488035 | 650 | } |
aaffa79f | 651 | if (!Civi::settings()->get('disable_core_css')) { |
6a488035 | 652 | $this->addStyleFile('civicrm', 'css/civicrm.css', -99, $region); |
6a488035 | 653 | } |
a2c70872 AH |
654 | // crm-i.css added ahead of other styles so it can be overridden by FA. |
655 | $this->addStyleFile('civicrm', 'css/crm-i.css', -101, $region); | |
6a488035 TO |
656 | } |
657 | return $this; | |
658 | } | |
659 | ||
627668e8 | 660 | /** |
d09edf64 | 661 | * Flushes cached translated strings. |
5badddc3 | 662 | * @return CRM_Core_Resources |
627668e8 CW |
663 | */ |
664 | public function flushStrings() { | |
fd7dc3f3 | 665 | $this->strings->flush(); |
9762f6ff CW |
666 | return $this; |
667 | } | |
668 | ||
6a488035 | 669 | /** |
fd7dc3f3 TO |
670 | * @return CRM_Core_Resources_Strings |
671 | */ | |
672 | public function getStrings() { | |
673 | return $this->strings; | |
6a488035 TO |
674 | } |
675 | ||
19f7e35e | 676 | /** |
8d7a9d07 | 677 | * Create dynamic script for localizing js widgets. |
19f7e35e | 678 | */ |
00be9182 | 679 | public static function outputLocalizationJS() { |
4cc9b813 | 680 | CRM_Core_Page_AJAX::setJsHeaders(); |
a88cf11a | 681 | $config = CRM_Core_Config::singleton(); |
be2fb01f | 682 | $vars = [ |
3d527838 CW |
683 | 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)), |
684 | 'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')), | |
685 | 'otherSearch' => json_encode(ts('Enter search term...')), | |
e695ee7c | 686 | 'entityRef' => self::getEntityRefMetadata(), |
7b83e312 | 687 | 'ajaxPopupsEnabled' => self::singleton()->ajaxPopupsEnabled, |
a9fb6123 | 688 | 'allowAlertAutodismissal' => (bool) Civi::settings()->get('allow_alert_autodismissal'), |
c7e39a79 | 689 | 'resourceCacheCode' => self::singleton()->getCacheCode(), |
b30809e4 CW |
690 | 'locale' => CRM_Core_I18n::getLocale(), |
691 | 'cid' => (int) CRM_Core_Session::getLoggedInContactID(), | |
be2fb01f | 692 | ]; |
3d4fb0ed | 693 | print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars); |
4cc9b813 | 694 | CRM_Utils_System::civiExit(); |
c66581f5 CW |
695 | } |
696 | ||
6a488035 | 697 | /** |
d09edf64 | 698 | * List of core resources we add to every CiviCRM page. |
6a488035 | 699 | * |
e8038a7b CW |
700 | * Note: non-compressed versions of .min files will be used in debug mode |
701 | * | |
e3c1e85b | 702 | * @param string $region |
a43b1583 | 703 | * @return array |
6a488035 | 704 | */ |
e3c1e85b | 705 | public function coreResourceList($region) { |
dbe0bbc6 | 706 | $config = CRM_Core_Config::singleton(); |
a43b1583 | 707 | |
4968e732 CW |
708 | // Scripts needed by everyone, everywhere |
709 | // FIXME: This is too long; list needs finer-grained segmentation | |
be2fb01f | 710 | $items = [ |
3c61fc8f CW |
711 | "bower_components/jquery/dist/jquery.min.js", |
712 | "bower_components/jquery-ui/jquery-ui.min.js", | |
e8038a7b | 713 | "bower_components/jquery-ui/themes/smoothness/jquery-ui.min.css", |
1891a856 | 714 | "bower_components/lodash-compat/lodash.min.js", |
e8038a7b CW |
715 | "packages/jquery/plugins/jquery.mousewheel.min.js", |
716 | "bower_components/select2/select2.min.js", | |
717 | "bower_components/select2/select2.min.css", | |
90000c30 | 718 | "bower_components/font-awesome/css/font-awesome.min.css", |
e8038a7b CW |
719 | "packages/jquery/plugins/jquery.form.min.js", |
720 | "packages/jquery/plugins/jquery.timeentry.min.js", | |
721 | "packages/jquery/plugins/jquery.blockUI.min.js", | |
722 | "bower_components/datatables/media/js/jquery.dataTables.min.js", | |
723 | "bower_components/datatables/media/css/jquery.dataTables.min.css", | |
724 | "bower_components/jquery-validation/dist/jquery.validate.min.js", | |
725 | "packages/jquery/plugins/jquery.ui.datepicker.validation.min.js", | |
a0c8b50e | 726 | "js/Common.js", |
ff2eb9e8 | 727 | "js/crm.datepicker.js", |
53f2643c | 728 | "js/crm.ajax.js", |
7060d177 | 729 | "js/wysiwyg/crm.wysiwyg.js", |
be2fb01f | 730 | ]; |
adcd4bf7 CW |
731 | |
732 | // Dynamic localization script | |
733 | $items[] = $this->addCacheCode( | |
734 | CRM_Utils_System::url('civicrm/ajax/l10n-js/' . CRM_Core_I18n::getLocale(), | |
735 | ['cid' => CRM_Core_Session::getLoggedInContactID()], FALSE, NULL, FALSE) | |
736 | ); | |
737 | ||
7c523661 | 738 | // add wysiwyg editor |
aaffa79f | 739 | $editor = Civi::settings()->get('editor_id'); |
b608cfb1 | 740 | if ($editor == "CKEditor") { |
5e0b4b77 | 741 | CRM_Admin_Page_CKEditorConfig::setConfigDefault(); |
be2fb01f CW |
742 | $items[] = [ |
743 | 'config' => [ | |
286a7e5a CW |
744 | 'wysisygScriptLocation' => Civi::paths()->getUrl("[civicrm.root]/js/wysiwyg/crm.ckeditor.js"), |
745 | 'CKEditorCustomConfig' => CRM_Admin_Page_CKEditorConfig::getConfigUrl(), | |
be2fb01f CW |
746 | ], |
747 | ]; | |
b608cfb1 | 748 | } |
dbe0bbc6 | 749 | |
4968e732 CW |
750 | // These scripts are only needed by back-office users |
751 | if (CRM_Core_Permission::check('access CiviCRM')) { | |
642d43fa | 752 | $items[] = "packages/jquery/plugins/jquery.tableHeader.js"; |
e8038a7b | 753 | $items[] = "packages/jquery/plugins/jquery.notify.min.js"; |
4968e732 CW |
754 | } |
755 | ||
b30809e4 CW |
756 | $contactID = CRM_Core_Session::getLoggedInContactID(); |
757 | ||
758 | // Menubar | |
c931044d CW |
759 | $position = 'none'; |
760 | if ( | |
761 | $contactID && !$config->userFrameworkFrontend | |
762 | && CRM_Core_Permission::check('access CiviCRM') | |
763 | && !@constant('CIVICRM_DISABLE_DEFAULT_MENU') | |
764 | && !CRM_Core_Config::isUpgradeMode() | |
765 | ) { | |
766 | $position = Civi::settings()->get('menubar_position') ?: 'over-cms-menu'; | |
767 | } | |
768 | if ($position !== 'none') { | |
b30809e4 CW |
769 | $items[] = 'bower_components/smartmenus/dist/jquery.smartmenus.min.js'; |
770 | $items[] = 'bower_components/smartmenus/dist/addons/keyboard/jquery.smartmenus.keyboard.min.js'; | |
771 | $items[] = 'js/crm.menubar.js'; | |
08a3a519 TO |
772 | $items[] = Civi::service('asset_builder')->getUrl('crm-menubar.css', [ |
773 | 'color' => Civi::settings()->get('menubar_color'), | |
774 | ]); | |
b30809e4 CW |
775 | $items[] = [ |
776 | 'menubar' => [ | |
c931044d | 777 | 'position' => $position, |
b30809e4 CW |
778 | 'qfKey' => CRM_Core_Key::get('CRM_Contact_Controller_Search', TRUE), |
779 | 'cacheCode' => CRM_Core_BAO_Navigation::getCacheKey($contactID), | |
780 | ], | |
781 | ]; | |
782 | } | |
783 | ||
d606fff7 CW |
784 | // JS for multilingual installations |
785 | if (!empty($config->languageLimit) && count($config->languageLimit) > 1 && CRM_Core_Permission::check('translate CiviCRM')) { | |
786 | $items[] = "js/crm.multilingual.js"; | |
787 | } | |
788 | ||
53f2643c CW |
789 | // Enable administrators to edit option lists in a dialog |
790 | if (CRM_Core_Permission::check('administer CiviCRM') && $this->ajaxPopupsEnabled) { | |
791 | $items[] = "js/crm.optionEdit.js"; | |
792 | } | |
9efeb642 | 793 | |
98466ff9 | 794 | $tsLocale = CRM_Core_I18n::getLocale(); |
dbe0bbc6 | 795 | // Add localized jQuery UI files |
5d26edf0 | 796 | if ($tsLocale && $tsLocale != 'en_US') { |
dbe0bbc6 | 797 | // Search for i18n file in order of specificity (try fr-CA, then fr) |
5d26edf0 | 798 | list($lang) = explode('_', $tsLocale); |
3c61fc8f | 799 | $path = "bower_components/jquery-ui/ui/i18n"; |
be2fb01f | 800 | foreach ([str_replace('_', '-', $tsLocale), $lang] as $language) { |
f2f191fe | 801 | $localizationFile = "$path/datepicker-{$language}.js"; |
dbe0bbc6 CW |
802 | if ($this->getPath('civicrm', $localizationFile)) { |
803 | $items[] = $localizationFile; | |
804 | break; | |
805 | } | |
806 | } | |
807 | } | |
f9f361d0 | 808 | |
72e86d7d | 809 | // Allow hooks to modify this list |
e3c1e85b | 810 | CRM_Utils_Hook::coreResourceList($items, $region); |
f9f361d0 | 811 | |
6a488035 TO |
812 | return $items; |
813 | } | |
156fd9b9 CW |
814 | |
815 | /** | |
a6c01b45 CW |
816 | * @return bool |
817 | * is this page request an ajax snippet? | |
156fd9b9 | 818 | */ |
00be9182 | 819 | public static function isAjaxMode() { |
be2fb01f | 820 | if (in_array(CRM_Utils_Array::value('snippet', $_REQUEST), [ |
518fa0ee SL |
821 | CRM_Core_Smarty::PRINT_SNIPPET, |
822 | CRM_Core_Smarty::PRINT_NOFORM, | |
823 | CRM_Core_Smarty::PRINT_JSON, | |
824 | ]) | |
42a40a1c | 825 | ) { |
826 | return TRUE; | |
827 | } | |
e83561bf CW |
828 | $url = CRM_Utils_System::getUrlPath(); |
829 | return (strpos($url, 'civicrm/ajax') === 0) || (strpos($url, 'civicrm/angular') === 0); | |
156fd9b9 | 830 | } |
b7ceb253 | 831 | |
1889d803 | 832 | /** |
518fa0ee | 833 | * @param \Civi\Core\Event\GenericHookEvent $e |
1889d803 CW |
834 | * @see \CRM_Utils_Hook::buildAsset() |
835 | */ | |
836 | public static function renderMenubarStylesheet(GenericHookEvent $e) { | |
837 | if ($e->asset !== 'crm-menubar.css') { | |
838 | return; | |
839 | } | |
840 | $e->mimeType = 'text/css'; | |
841 | $e->content = ''; | |
842 | $config = CRM_Core_Config::singleton(); | |
843 | $cms = strtolower($config->userFramework); | |
844 | $cms = $cms === 'drupal' ? 'drupal7' : $cms; | |
845 | $items = [ | |
846 | 'bower_components/smartmenus/dist/css/sm-core-css.css', | |
847 | 'css/crm-menubar.css', | |
848 | "css/menubar-$cms.css", | |
849 | ]; | |
850 | foreach ($items as $item) { | |
851 | $e->content .= file_get_contents(self::singleton()->getPath('civicrm', $item)); | |
852 | } | |
08a3a519 | 853 | $color = $e->params['color']; |
8a52ae34 CW |
854 | if (!CRM_Utils_Rule::color($color)) { |
855 | $color = Civi::settings()->getDefault('menubar_color'); | |
856 | } | |
1889d803 CW |
857 | $vars = [ |
858 | 'resourceBase' => rtrim($config->resourceBase, '/'), | |
8a52ae34 CW |
859 | 'menubarColor' => $color, |
860 | 'semiTransparentMenuColor' => 'rgba(' . implode(', ', CRM_Utils_Color::getRgb($color)) . ', .85)', | |
861 | 'highlightColor' => CRM_Utils_Color::getHighlight($color), | |
862 | 'textColor' => CRM_Utils_Color::getContrast($color, '#333', '#ddd'), | |
1889d803 | 863 | ]; |
8a52ae34 | 864 | $vars['highlightTextColor'] = CRM_Utils_Color::getContrast($vars['highlightColor'], '#333', '#ddd'); |
1889d803 CW |
865 | foreach ($vars as $var => $val) { |
866 | $e->content = str_replace('$' . $var, $val, $e->content); | |
867 | } | |
868 | } | |
869 | ||
b7ceb253 | 870 | /** |
f9e31d7f | 871 | * Provide a list of available entityRef filters. |
fd7c068f | 872 | * |
b7ceb253 CW |
873 | * @return array |
874 | */ | |
e695ee7c CW |
875 | public static function getEntityRefMetadata() { |
876 | $data = [ | |
877 | 'filters' => [], | |
878 | 'links' => [], | |
879 | ]; | |
06606cd1 | 880 | $config = CRM_Core_Config::singleton(); |
b7ceb253 | 881 | |
1d6f94ab CW |
882 | $disabledComponents = []; |
883 | $dao = CRM_Core_DAO::executeQuery("SELECT name, namespace FROM civicrm_component"); | |
884 | while ($dao->fetch()) { | |
885 | if (!in_array($dao->name, $config->enableComponents)) { | |
886 | $disabledComponents[$dao->name] = $dao->namespace; | |
06606cd1 CW |
887 | } |
888 | } | |
889 | ||
1d6f94ab CW |
890 | foreach (CRM_Core_DAO_AllCoreTables::daoToClass() as $entity => $daoName) { |
891 | // Skip DAOs of disabled components | |
892 | foreach ($disabledComponents as $nameSpace) { | |
893 | if (strpos($daoName, $nameSpace) === 0) { | |
894 | continue 2; | |
895 | } | |
896 | } | |
897 | $baoName = str_replace('_DAO_', '_BAO_', $daoName); | |
898 | if (class_exists($baoName)) { | |
e695ee7c CW |
899 | $filters = $baoName::getEntityRefFilters(); |
900 | if ($filters) { | |
2229cf4f | 901 | $data['filters'][$entity] = $filters; |
e695ee7c CW |
902 | } |
903 | if (is_callable([$baoName, 'getEntityRefCreateLinks'])) { | |
904 | $createLinks = $baoName::getEntityRefCreateLinks(); | |
905 | if ($createLinks) { | |
2229cf4f | 906 | $data['links'][$entity] = $createLinks; |
e695ee7c | 907 | } |
1d6f94ab CW |
908 | } |
909 | } | |
b7b528bc CW |
910 | } |
911 | ||
e695ee7c | 912 | CRM_Utils_Hook::entityRefFilters($data['filters']); |
fd7c068f | 913 | |
e695ee7c | 914 | return $data; |
b7ceb253 | 915 | } |
96025800 | 916 | |
09a4dcd5 CW |
917 | /** |
918 | * In debug mode, look for a non-minified version of this file | |
919 | * | |
920 | * @param string $fileName | |
921 | * @param string $extName | |
922 | */ | |
923 | private function resolveFileName(&$fileName, $extName) { | |
924 | if (CRM_Core_Config::singleton()->debug && strpos($fileName, '.min.') !== FALSE) { | |
925 | $nonMiniFile = str_replace('.min.', '.', $fileName); | |
926 | if ($this->getPath($extName, $nonMiniFile)) { | |
927 | $fileName = $nonMiniFile; | |
928 | } | |
929 | } | |
930 | } | |
931 | ||
6f12c6eb | 932 | /** |
933 | * @param string $url | |
934 | * @return string | |
935 | */ | |
936 | public function addCacheCode($url) { | |
33603e1d | 937 | $hasQuery = strpos($url, '?') !== FALSE; |
03449a5b | 938 | $operator = $hasQuery ? '&' : '?'; |
6f12c6eb | 939 | |
03449a5b | 940 | return $url . $operator . 'r=' . $this->cacheCode; |
6f12c6eb | 941 | } |
33603e1d | 942 | |
adcd4bf7 CW |
943 | /** |
944 | * Checks if the given URL is fully-formed | |
945 | * | |
946 | * @param string $url | |
947 | * | |
948 | * @return bool | |
949 | */ | |
950 | public static function isFullyFormedUrl($url) { | |
951 | return (substr($url, 0, 4) === 'http') || (substr($url, 0, 1) === '/'); | |
952 | } | |
953 | ||
6a488035 | 954 | } |