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 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * This class facilitates the loading of resources | |
30 | * such as JavaScript files and CSS files. | |
31 | * | |
32 | * Any URLs generated for resources may include a 'cache-code'. By resetting the | |
33 | * cache-code, one may force clients to re-download resource files (regardless of | |
34 | * any HTTP caching rules). | |
35 | * | |
36 | * TODO: This is currently a thin wrapper over CRM_Core_Region. We | |
37 | * should incorporte services for aggregation, minimization, etc. | |
38 | * | |
39 | * @package CRM | |
6b83d5bd | 40 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
41 | * $Id$ |
42 | * | |
43 | */ | |
44 | class CRM_Core_Resources { | |
45 | const DEFAULT_WEIGHT = 0; | |
46 | const DEFAULT_REGION = 'page-footer'; | |
47 | ||
48 | /** | |
49 | * We don't have a container or dependency-injection, so use singleton instead | |
50 | * | |
51 | * @var object | |
6a488035 TO |
52 | */ |
53 | private static $_singleton = NULL; | |
54 | ||
55 | /** | |
56 | * @var CRM_Extension_Mapper | |
57 | */ | |
58 | private $extMapper = NULL; | |
59 | ||
60 | /** | |
fd7dc3f3 | 61 | * @var CRM_Core_Resources_Strings |
6a488035 | 62 | */ |
fd7dc3f3 | 63 | private $strings = NULL; |
6a488035 TO |
64 | |
65 | /** | |
66 | * @var array free-form data tree | |
67 | */ | |
68 | protected $settings = array(); | |
69 | protected $addedSettings = FALSE; | |
70 | ||
71 | /** | |
72 | * @var array of callables | |
73 | */ | |
74 | protected $settingsFactories = array(); | |
75 | ||
76 | /** | |
77 | * @var array ($regionName => bool) | |
78 | */ | |
79 | protected $addedCoreResources = array(); | |
80 | ||
81 | /** | |
82 | * @var array ($regionName => bool) | |
83 | */ | |
84 | protected $addedCoreStyles = array(); | |
85 | ||
86 | /** | |
87 | * @var string a value to append to JS/CSS URLs to coerce cache resets | |
88 | */ | |
89 | protected $cacheCode = NULL; | |
90 | ||
91 | /** | |
92 | * @var string the name of a setting which persistently stores the cacheCode | |
93 | */ | |
94 | protected $cacheCodeKey = NULL; | |
95 | ||
53f2643c CW |
96 | /** |
97 | * @var bool | |
98 | */ | |
99 | public $ajaxPopupsEnabled; | |
100 | ||
b698e2d5 TO |
101 | /** |
102 | * @var \Civi\Core\Paths | |
103 | */ | |
104 | protected $paths; | |
105 | ||
6a488035 | 106 | /** |
fe482240 | 107 | * Get or set the single instance of CRM_Core_Resources. |
6a488035 | 108 | * |
5a4f6742 CW |
109 | * @param CRM_Core_Resources $instance |
110 | * New copy of the manager. | |
6a488035 TO |
111 | * @return CRM_Core_Resources |
112 | */ | |
113 | static public function singleton(CRM_Core_Resources $instance = NULL) { | |
114 | if ($instance !== NULL) { | |
115 | self::$_singleton = $instance; | |
116 | } | |
117 | if (self::$_singleton === NULL) { | |
223ba025 | 118 | self::$_singleton = Civi::service('resources'); |
6a488035 TO |
119 | } |
120 | return self::$_singleton; | |
121 | } | |
122 | ||
123 | /** | |
d09edf64 | 124 | * Construct a resource manager. |
6a488035 | 125 | * |
6a0b768e TO |
126 | * @param CRM_Extension_Mapper $extMapper |
127 | * Map extension names to their base path or URLs. | |
128 | * @param CRM_Utils_Cache_Interface $cache | |
129 | * JS-localization cache. | |
3be754d6 | 130 | * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources |
6a488035 TO |
131 | */ |
132 | public function __construct($extMapper, $cache, $cacheCodeKey = NULL) { | |
133 | $this->extMapper = $extMapper; | |
fd7dc3f3 | 134 | $this->strings = new CRM_Core_Resources_Strings($cache); |
6a488035 TO |
135 | $this->cacheCodeKey = $cacheCodeKey; |
136 | if ($cacheCodeKey !== NULL) { | |
aaffa79f | 137 | $this->cacheCode = Civi::settings()->get($cacheCodeKey); |
6a488035 | 138 | } |
150f50c1 | 139 | if (!$this->cacheCode) { |
6a488035 TO |
140 | $this->resetCacheCode(); |
141 | } | |
84fb7424 | 142 | $this->ajaxPopupsEnabled = (bool) Civi::settings()->get('ajaxPopupsEnabled'); |
b698e2d5 | 143 | $this->paths = Civi::paths(); |
6a488035 TO |
144 | } |
145 | ||
90efc417 TO |
146 | /** |
147 | * Export permission data to the client to enable smarter GUIs. | |
148 | * | |
149 | * Note: Application security stems from the server's enforcement | |
150 | * of the security logic (e.g. in the API permissions). There's no way | |
151 | * the client can use this info to make the app more secure; however, | |
152 | * it can produce a better-tuned (non-broken) UI. | |
153 | * | |
154 | * @param array $permNames | |
155 | * List of permission names to check/export. | |
156 | * @return CRM_Core_Resources | |
157 | */ | |
158 | public function addPermissions($permNames) { | |
159 | $permNames = (array) $permNames; | |
160 | $perms = array(); | |
161 | foreach ($permNames as $permName) { | |
162 | $perms[$permName] = CRM_Core_Permission::check($permName); | |
163 | } | |
164 | return $this->addSetting(array( | |
165 | 'permissions' => $perms, | |
166 | )); | |
167 | } | |
168 | ||
6a488035 TO |
169 | /** |
170 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
171 | * | |
5a4f6742 CW |
172 | * @param string $ext |
173 | * extension name; use 'civicrm' for core. | |
174 | * @param string $file | |
175 | * file path -- relative to the extension base dir. | |
176 | * @param int $weight | |
177 | * relative weight within a given region. | |
178 | * @param string $region | |
179 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
3f3bba82 TO |
180 | * @param bool|string $translate |
181 | * Whether to load translated strings for this file. Use one of: | |
182 | * - FALSE: Do not load translated strings. | |
183 | * - TRUE: Load translated strings. Use the $ext's default domain. | |
184 | * - string: Load translated strings. Use a specific domain. | |
6a488035 TO |
185 | * |
186 | * @return CRM_Core_Resources | |
187 | */ | |
188 | public function addScriptFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION, $translate = TRUE) { | |
189 | if ($translate) { | |
3f3bba82 TO |
190 | $domain = ($translate === TRUE) ? $ext : $translate; |
191 | $this->addString($this->strings->get($domain, $this->getPath($ext, $file), 'text/javascript'), $domain); | |
6a488035 | 192 | } |
09a4dcd5 | 193 | $this->resolveFileName($file, $ext); |
6a488035 TO |
194 | return $this->addScriptUrl($this->getUrl($ext, $file, TRUE), $weight, $region); |
195 | } | |
196 | ||
197 | /** | |
198 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
199 | * | |
5a4f6742 CW |
200 | * @param string $url |
201 | * @param int $weight | |
202 | * relative weight within a given region. | |
203 | * @param string $region | |
204 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
205 | * @return CRM_Core_Resources |
206 | */ | |
207 | public function addScriptUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
208 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
209 | 'name' => $url, |
210 | 'type' => 'scriptUrl', | |
211 | 'scriptUrl' => $url, | |
212 | 'weight' => $weight, | |
213 | 'region' => $region, | |
214 | )); | |
6a488035 TO |
215 | return $this; |
216 | } | |
217 | ||
218 | /** | |
219 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
220 | * | |
5a4f6742 CW |
221 | * @param string $code |
222 | * JavaScript source code. | |
223 | * @param int $weight | |
224 | * relative weight within a given region. | |
225 | * @param string $region | |
226 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
227 | * @return CRM_Core_Resources |
228 | */ | |
229 | public function addScript($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
230 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
231 | // 'name' => automatic |
232 | 'type' => 'script', | |
233 | 'script' => $code, | |
234 | 'weight' => $weight, | |
235 | 'region' => $region, | |
236 | )); | |
6a488035 TO |
237 | return $this; |
238 | } | |
239 | ||
240 | /** | |
73bcd446 | 241 | * Add JavaScript variables to CRM.vars |
6a488035 | 242 | * |
132fc68e CW |
243 | * Example: |
244 | * From the server: | |
73bcd446 CW |
245 | * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar')); |
246 | * Access var from javascript: | |
247 | * CRM.vars.myNamespace.foo // "bar" | |
132fc68e | 248 | * |
01478033 | 249 | * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference |
132fc68e | 250 | * |
6a0b768e TO |
251 | * @param string $nameSpace |
252 | * Usually the name of your extension. | |
73bcd446 CW |
253 | * @param array $vars |
254 | * @return CRM_Core_Resources | |
255 | */ | |
256 | public function addVars($nameSpace, $vars) { | |
257 | $existing = CRM_Utils_Array::value($nameSpace, CRM_Utils_Array::value('vars', $this->settings), array()); | |
258 | $vars = $this->mergeSettings($existing, $vars); | |
259 | $this->addSetting(array('vars' => array($nameSpace => $vars))); | |
260 | return $this; | |
261 | } | |
262 | ||
263 | /** | |
264 | * Add JavaScript variables to the root of the CRM object. | |
265 | * This function is usually reserved for low-level system use. | |
266 | * Extensions and components should generally use addVars instead. | |
267 | * | |
5a4f6742 | 268 | * @param array $settings |
6a488035 TO |
269 | * @return CRM_Core_Resources |
270 | */ | |
271 | public function addSetting($settings) { | |
272 | $this->settings = $this->mergeSettings($this->settings, $settings); | |
273 | if (!$this->addedSettings) { | |
88cd8875 | 274 | $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header'; |
6a488035 | 275 | $resources = $this; |
156fd9b9 | 276 | CRM_Core_Region::instance($region)->add(array( |
353ffa53 TO |
277 | 'callback' => function (&$snippet, &$html) use ($resources) { |
278 | $html .= "\n" . $resources->renderSetting(); | |
279 | }, | |
8cb324cc | 280 | 'weight' => -100000, |
6a488035 TO |
281 | )); |
282 | $this->addedSettings = TRUE; | |
283 | } | |
284 | return $this; | |
285 | } | |
286 | ||
287 | /** | |
69847402 | 288 | * Add JavaScript variables to the global CRM object via a callback function. |
6a488035 | 289 | * |
3be754d6 | 290 | * @param callable $callable |
6a488035 TO |
291 | * @return CRM_Core_Resources |
292 | */ | |
293 | public function addSettingsFactory($callable) { | |
d937dbcd CW |
294 | // Make sure our callback has been registered |
295 | $this->addSetting(array()); | |
6a488035 TO |
296 | $this->settingsFactories[] = $callable; |
297 | return $this; | |
298 | } | |
299 | ||
69847402 | 300 | /** |
d09edf64 | 301 | * Helper fn for addSettingsFactory. |
69847402 | 302 | */ |
6a488035 TO |
303 | public function getSettings() { |
304 | $result = $this->settings; | |
305 | foreach ($this->settingsFactories as $callable) { | |
306 | $result = $this->mergeSettings($result, $callable()); | |
307 | } | |
898951c6 | 308 | CRM_Utils_Hook::alterResourceSettings($result); |
6a488035 TO |
309 | return $result; |
310 | } | |
311 | ||
312 | /** | |
313 | * @param array $settings | |
314 | * @param array $additions | |
a6c01b45 CW |
315 | * @return array |
316 | * combination of $settings and $additions | |
6a488035 TO |
317 | */ |
318 | protected function mergeSettings($settings, $additions) { | |
319 | foreach ($additions as $k => $v) { | |
320 | if (isset($settings[$k]) && is_array($settings[$k]) && is_array($v)) { | |
321 | $v += $settings[$k]; | |
322 | } | |
323 | $settings[$k] = $v; | |
324 | } | |
325 | return $settings; | |
326 | } | |
327 | ||
328 | /** | |
d09edf64 | 329 | * Helper fn for addSetting. |
6a488035 TO |
330 | * Render JavaScript variables for the global CRM object. |
331 | * | |
6a488035 TO |
332 | * @return string |
333 | */ | |
334 | public function renderSetting() { | |
156fd9b9 CW |
335 | // On a standard page request we construct the CRM object from scratch |
336 | if (!self::isAjaxMode()) { | |
337 | $js = 'var CRM = ' . json_encode($this->getSettings()) . ';'; | |
338 | } | |
339 | // For an ajax request we append to it | |
340 | else { | |
341 | $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');'; | |
342 | } | |
d759bd10 | 343 | return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js); |
6a488035 TO |
344 | } |
345 | ||
346 | /** | |
347 | * Add translated string to the js CRM object. | |
348 | * It can then be retrived from the client-side ts() function | |
349 | * Variable substitutions can happen from client-side | |
8ef12e64 | 350 | * |
6a488035 | 351 | * Note: this function rarely needs to be called directly and is mostly for internal use. |
d3e86119 | 352 | * See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files |
6a488035 TO |
353 | * |
354 | * Simple example: | |
355 | * // From php: | |
356 | * CRM_Core_Resources::singleton()->addString('Hello'); | |
357 | * // The string is now available to javascript code i.e. | |
358 | * ts('Hello'); | |
359 | * | |
360 | * Example with client-side substitutions: | |
361 | * // From php: | |
362 | * CRM_Core_Resources::singleton()->addString('Your %1 has been %2'); | |
363 | * // ts() in javascript works the same as in php, for example: | |
364 | * ts('Your %1 has been %2', {1: objectName, 2: actionTaken}); | |
365 | * | |
366 | * NOTE: This function does not work with server-side substitutions | |
367 | * (as this might result in collisions and unwanted variable injections) | |
368 | * Instead, use code like: | |
369 | * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs))))); | |
370 | * And from javascript access it at CRM.myNamespace.myString | |
371 | * | |
5a4f6742 | 372 | * @param string|array $text |
3f3bba82 | 373 | * @param string|NULL $domain |
6a488035 TO |
374 | * @return CRM_Core_Resources |
375 | */ | |
3f3bba82 | 376 | public function addString($text, $domain = 'civicrm') { |
6a488035 | 377 | foreach ((array) $text as $str) { |
3f3bba82 TO |
378 | $translated = ts($str, array( |
379 | 'domain' => ($domain == 'civicrm') ? NULL : array($domain, NULL), | |
1b4710da | 380 | 'raw' => TRUE, |
3f3bba82 TO |
381 | )); |
382 | ||
6a488035 TO |
383 | // We only need to push this string to client if the translation |
384 | // is actually different from the original | |
385 | if ($translated != $str) { | |
3f3bba82 TO |
386 | $bucket = $domain == 'civicrm' ? 'strings' : 'strings::' . $domain; |
387 | $this->addSetting(array( | |
388 | $bucket => array($str => $translated), | |
389 | )); | |
6a488035 TO |
390 | } |
391 | } | |
392 | return $this; | |
393 | } | |
394 | ||
395 | /** | |
396 | * Add a CSS file to the current page using <LINK HREF>. | |
397 | * | |
5a4f6742 CW |
398 | * @param string $ext |
399 | * extension name; use 'civicrm' for core. | |
400 | * @param string $file | |
401 | * file path -- relative to the extension base dir. | |
402 | * @param int $weight | |
403 | * relative weight within a given region. | |
404 | * @param string $region | |
405 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
406 | * @return CRM_Core_Resources |
407 | */ | |
408 | public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
09a4dcd5 | 409 | $this->resolveFileName($file, $ext); |
6a488035 TO |
410 | return $this->addStyleUrl($this->getUrl($ext, $file, TRUE), $weight, $region); |
411 | } | |
412 | ||
413 | /** | |
414 | * Add a CSS file to the current page using <LINK HREF>. | |
415 | * | |
5a4f6742 CW |
416 | * @param string $url |
417 | * @param int $weight | |
418 | * relative weight within a given region. | |
419 | * @param string $region | |
420 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
421 | * @return CRM_Core_Resources |
422 | */ | |
423 | public function addStyleUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
424 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
425 | 'name' => $url, |
426 | 'type' => 'styleUrl', | |
427 | 'styleUrl' => $url, | |
428 | 'weight' => $weight, | |
429 | 'region' => $region, | |
430 | )); | |
6a488035 TO |
431 | return $this; |
432 | } | |
433 | ||
434 | /** | |
435 | * Add a CSS content to the current page using <STYLE>. | |
436 | * | |
5a4f6742 CW |
437 | * @param string $code |
438 | * CSS source code. | |
439 | * @param int $weight | |
440 | * relative weight within a given region. | |
441 | * @param string $region | |
442 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
443 | * @return CRM_Core_Resources |
444 | */ | |
445 | public function addStyle($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
446 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
447 | // 'name' => automatic |
448 | 'type' => 'style', | |
449 | 'style' => $code, | |
450 | 'weight' => $weight, | |
451 | 'region' => $region, | |
452 | )); | |
6a488035 TO |
453 | return $this; |
454 | } | |
455 | ||
456 | /** | |
d09edf64 | 457 | * Determine file path of a resource provided by an extension. |
6a488035 | 458 | * |
5a4f6742 CW |
459 | * @param string $ext |
460 | * extension name; use 'civicrm' for core. | |
16cd1eca | 461 | * @param string|NULL $file |
5a4f6742 | 462 | * file path -- relative to the extension base dir. |
6a488035 | 463 | * |
72b3a70c CW |
464 | * @return bool|string |
465 | * full file path or FALSE if not found | |
6a488035 | 466 | */ |
16cd1eca | 467 | public function getPath($ext, $file = NULL) { |
6a488035 | 468 | // TODO consider caching results |
b698e2d5 TO |
469 | $base = $this->paths->hasVariable($ext) |
470 | ? rtrim($this->paths->getVariable($ext, 'path'), '/') | |
471 | : $this->extMapper->keyToBasePath($ext); | |
16cd1eca | 472 | if ($file === NULL) { |
b698e2d5 | 473 | return $base; |
16cd1eca | 474 | } |
b698e2d5 | 475 | $path = $base . '/' . $file; |
6a488035 TO |
476 | if (is_file($path)) { |
477 | return $path; | |
478 | } | |
479 | return FALSE; | |
480 | } | |
481 | ||
482 | /** | |
d09edf64 | 483 | * Determine public URL of a resource provided by an extension. |
6a488035 | 484 | * |
5a4f6742 CW |
485 | * @param string $ext |
486 | * extension name; use 'civicrm' for core. | |
487 | * @param string $file | |
488 | * file path -- relative to the extension base dir. | |
2a6da8d7 EM |
489 | * @param bool $addCacheCode |
490 | * | |
6a488035 TO |
491 | * @return string, URL |
492 | */ | |
493 | public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) { | |
494 | if ($file === NULL) { | |
495 | $file = ''; | |
496 | } | |
497 | if ($addCacheCode) { | |
6f12c6eb | 498 | $file = $this->addCacheCode($file); |
6a488035 TO |
499 | } |
500 | // TODO consider caching results | |
b698e2d5 TO |
501 | $base = $this->paths->hasVariable($ext) |
502 | ? $this->paths->getVariable($ext, 'url') | |
503 | : ($this->extMapper->keyToUrl($ext) . '/'); | |
504 | return $base . $file; | |
6a488035 TO |
505 | } |
506 | ||
16cd1eca TO |
507 | /** |
508 | * Evaluate a glob pattern in the context of a particular extension. | |
509 | * | |
510 | * @param string $ext | |
511 | * Extension name; use 'civicrm' for core. | |
512 | * @param string|array $patterns | |
513 | * Glob pattern; e.g. "*.html". | |
514 | * @param null|int $flags | |
515 | * See glob(). | |
516 | * @return array | |
517 | * List of matching files, relative to the extension base dir. | |
518 | * @see glob() | |
519 | */ | |
520 | public function glob($ext, $patterns, $flags = NULL) { | |
521 | $path = $this->getPath($ext); | |
522 | $patterns = (array) $patterns; | |
523 | $files = array(); | |
524 | foreach ($patterns as $pattern) { | |
e5c376e7 TO |
525 | if (preg_match(';^(assetBuilder|ext)://;', $pattern)) { |
526 | $files[] = $pattern; | |
527 | } | |
9f87b14b | 528 | if (CRM_Utils_File::isAbsolute($pattern)) { |
16cd1eca TO |
529 | // Absolute path. |
530 | $files = array_merge($files, (array) glob($pattern, $flags)); | |
531 | } | |
532 | else { | |
533 | // Relative path. | |
534 | $files = array_merge($files, (array) glob("$path/$pattern", $flags)); | |
535 | } | |
536 | } | |
537 | sort($files); // Deterministic order. | |
538 | $files = array_unique($files); | |
539 | return array_map(function ($file) use ($path) { | |
540 | return CRM_Utils_File::relativize($file, "$path/"); | |
541 | }, $files); | |
542 | } | |
543 | ||
a0ee3941 EM |
544 | /** |
545 | * @return string | |
546 | */ | |
6a488035 TO |
547 | public function getCacheCode() { |
548 | return $this->cacheCode; | |
549 | } | |
550 | ||
a0ee3941 EM |
551 | /** |
552 | * @param $value | |
5badddc3 | 553 | * @return CRM_Core_Resources |
a0ee3941 | 554 | */ |
6a488035 TO |
555 | public function setCacheCode($value) { |
556 | $this->cacheCode = $value; | |
557 | if ($this->cacheCodeKey) { | |
08ef4ddd | 558 | Civi::settings()->set($this->cacheCodeKey, $value); |
6a488035 | 559 | } |
9762f6ff | 560 | return $this; |
6a488035 TO |
561 | } |
562 | ||
5badddc3 CW |
563 | /** |
564 | * @return CRM_Core_Resources | |
565 | */ | |
6a488035 TO |
566 | public function resetCacheCode() { |
567 | $this->setCacheCode(CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC)); | |
f091327b CW |
568 | // Also flush cms resource cache if needed |
569 | CRM_Core_Config::singleton()->userSystem->clearResourceCache(); | |
9762f6ff | 570 | return $this; |
6a488035 TO |
571 | } |
572 | ||
573 | /** | |
574 | * This adds CiviCRM's standard css and js to the specified region of the document. | |
575 | * It will only run once. | |
576 | * | |
577 | * TODO: Separate the functional code (like addStyle/addScript) from the policy code | |
578 | * (like addCoreResources/addCoreStyles). | |
579 | * | |
2a6da8d7 | 580 | * @param string $region |
6a488035 | 581 | * @return CRM_Core_Resources |
6a488035 TO |
582 | */ |
583 | public function addCoreResources($region = 'html-header') { | |
156fd9b9 | 584 | if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) { |
6a488035 TO |
585 | $this->addedCoreResources[$region] = TRUE; |
586 | $config = CRM_Core_Config::singleton(); | |
587 | ||
a43b1583 | 588 | // Add resources from coreResourceList |
6a488035 | 589 | $jsWeight = -9999; |
dbb12634 | 590 | foreach ($this->coreResourceList($region) as $item) { |
7266e09b CW |
591 | if (is_array($item)) { |
592 | $this->addSetting($item); | |
593 | } | |
594 | elseif (substr($item, -2) == 'js') { | |
74227f77 | 595 | // Don't bother looking for ts() calls in packages, there aren't any |
7266e09b CW |
596 | $translate = (substr($item, 0, 3) == 'js/'); |
597 | $this->addScriptFile('civicrm', $item, $jsWeight++, $region, $translate); | |
6a488035 | 598 | } |
a43b1583 | 599 | else { |
7266e09b | 600 | $this->addStyleFile('civicrm', $item, -100, $region); |
6a488035 TO |
601 | } |
602 | } | |
603 | ||
98466ff9 | 604 | $tsLocale = CRM_Core_I18n::getLocale(); |
4e56e743 | 605 | // Dynamic localization script |
5d26edf0 | 606 | $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $tsLocale, array('r' => $this->getCacheCode())), $jsWeight++, $region); |
4e56e743 | 607 | |
d759bd10 | 608 | // Add global settings |
2aa397bc | 609 | $settings = array( |
353ffa53 | 610 | 'config' => array( |
353ffa53 | 611 | 'isFrontend' => $config->userFrameworkFrontend, |
8d7a9d07 | 612 | ), |
353ffa53 | 613 | ); |
c7e39a79 CW |
614 | $contactID = CRM_Core_Session::getLoggedInContactID(); |
615 | if ($contactID) { | |
616 | $settings['config']['menuCacheCode'] = CRM_Core_BAO_Navigation::getCacheKey($contactID); | |
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(); |
3d527838 CW |
682 | $vars = array( |
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...')), | |
b7ceb253 CW |
686 | 'entityRef' => array( |
687 | 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(), | |
688 | 'filters' => self::getEntityRefFilters(), | |
689 | ), | |
7b83e312 | 690 | 'ajaxPopupsEnabled' => self::singleton()->ajaxPopupsEnabled, |
a9fb6123 | 691 | 'allowAlertAutodismissal' => (bool) Civi::settings()->get('allow_alert_autodismissal'), |
c7e39a79 | 692 | 'resourceCacheCode' => self::singleton()->getCacheCode(), |
3d527838 | 693 | ); |
3d4fb0ed | 694 | print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars); |
4cc9b813 | 695 | CRM_Utils_System::civiExit(); |
c66581f5 CW |
696 | } |
697 | ||
6a488035 | 698 | /** |
d09edf64 | 699 | * List of core resources we add to every CiviCRM page. |
6a488035 | 700 | * |
e8038a7b CW |
701 | * Note: non-compressed versions of .min files will be used in debug mode |
702 | * | |
e3c1e85b | 703 | * @param string $region |
a43b1583 | 704 | * @return array |
6a488035 | 705 | */ |
e3c1e85b | 706 | public function coreResourceList($region) { |
dbe0bbc6 | 707 | $config = CRM_Core_Config::singleton(); |
a43b1583 | 708 | |
4968e732 CW |
709 | // Scripts needed by everyone, everywhere |
710 | // FIXME: This is too long; list needs finer-grained segmentation | |
a0c8b50e | 711 | $items = array( |
3c61fc8f CW |
712 | "bower_components/jquery/dist/jquery.min.js", |
713 | "bower_components/jquery-ui/jquery-ui.min.js", | |
e8038a7b | 714 | "bower_components/jquery-ui/themes/smoothness/jquery-ui.min.css", |
1891a856 | 715 | "bower_components/lodash-compat/lodash.min.js", |
e8038a7b CW |
716 | "packages/jquery/plugins/jquery.mousewheel.min.js", |
717 | "bower_components/select2/select2.min.js", | |
718 | "bower_components/select2/select2.min.css", | |
90000c30 | 719 | "bower_components/font-awesome/css/font-awesome.min.css", |
e8038a7b CW |
720 | "packages/jquery/plugins/jquery.form.min.js", |
721 | "packages/jquery/plugins/jquery.timeentry.min.js", | |
722 | "packages/jquery/plugins/jquery.blockUI.min.js", | |
723 | "bower_components/datatables/media/js/jquery.dataTables.min.js", | |
724 | "bower_components/datatables/media/css/jquery.dataTables.min.css", | |
725 | "bower_components/jquery-validation/dist/jquery.validate.min.js", | |
726 | "packages/jquery/plugins/jquery.ui.datepicker.validation.min.js", | |
a0c8b50e | 727 | "js/Common.js", |
ff2eb9e8 | 728 | "js/crm.datepicker.js", |
53f2643c | 729 | "js/crm.ajax.js", |
7060d177 | 730 | "js/wysiwyg/crm.wysiwyg.js", |
a43b1583 | 731 | ); |
7c523661 | 732 | // add wysiwyg editor |
aaffa79f | 733 | $editor = Civi::settings()->get('editor_id'); |
b608cfb1 | 734 | if ($editor == "CKEditor") { |
5e0b4b77 | 735 | CRM_Admin_Page_CKEditorConfig::setConfigDefault(); |
286a7e5a CW |
736 | $items[] = array( |
737 | 'config' => array( | |
738 | 'wysisygScriptLocation' => Civi::paths()->getUrl("[civicrm.root]/js/wysiwyg/crm.ckeditor.js"), | |
739 | 'CKEditorCustomConfig' => CRM_Admin_Page_CKEditorConfig::getConfigUrl(), | |
740 | ), | |
741 | ); | |
b608cfb1 | 742 | } |
dbe0bbc6 | 743 | |
4968e732 CW |
744 | // These scripts are only needed by back-office users |
745 | if (CRM_Core_Permission::check('access CiviCRM')) { | |
642d43fa | 746 | $items[] = "packages/jquery/plugins/jquery.tableHeader.js"; |
e8038a7b | 747 | $items[] = "packages/jquery/plugins/jquery.menu.min.js"; |
1d68b509 | 748 | $items[] = "css/civicrmNavigation.css"; |
e8038a7b | 749 | $items[] = "packages/jquery/plugins/jquery.notify.min.js"; |
4968e732 CW |
750 | } |
751 | ||
d606fff7 CW |
752 | // JS for multilingual installations |
753 | if (!empty($config->languageLimit) && count($config->languageLimit) > 1 && CRM_Core_Permission::check('translate CiviCRM')) { | |
754 | $items[] = "js/crm.multilingual.js"; | |
755 | } | |
756 | ||
53f2643c CW |
757 | // Enable administrators to edit option lists in a dialog |
758 | if (CRM_Core_Permission::check('administer CiviCRM') && $this->ajaxPopupsEnabled) { | |
759 | $items[] = "js/crm.optionEdit.js"; | |
760 | } | |
9efeb642 | 761 | |
98466ff9 | 762 | $tsLocale = CRM_Core_I18n::getLocale(); |
dbe0bbc6 | 763 | // Add localized jQuery UI files |
5d26edf0 | 764 | if ($tsLocale && $tsLocale != 'en_US') { |
dbe0bbc6 | 765 | // Search for i18n file in order of specificity (try fr-CA, then fr) |
5d26edf0 | 766 | list($lang) = explode('_', $tsLocale); |
3c61fc8f | 767 | $path = "bower_components/jquery-ui/ui/i18n"; |
5d26edf0 | 768 | foreach (array(str_replace('_', '-', $tsLocale), $lang) as $language) { |
f2f191fe | 769 | $localizationFile = "$path/datepicker-{$language}.js"; |
dbe0bbc6 CW |
770 | if ($this->getPath('civicrm', $localizationFile)) { |
771 | $items[] = $localizationFile; | |
772 | break; | |
773 | } | |
774 | } | |
775 | } | |
f9f361d0 | 776 | |
72e86d7d | 777 | // Allow hooks to modify this list |
e3c1e85b | 778 | CRM_Utils_Hook::coreResourceList($items, $region); |
f9f361d0 | 779 | |
6a488035 TO |
780 | return $items; |
781 | } | |
156fd9b9 CW |
782 | |
783 | /** | |
a6c01b45 CW |
784 | * @return bool |
785 | * is this page request an ajax snippet? | |
156fd9b9 | 786 | */ |
00be9182 | 787 | public static function isAjaxMode() { |
42a40a1c | 788 | if (in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array( |
353ffa53 TO |
789 | CRM_Core_Smarty::PRINT_SNIPPET, |
790 | CRM_Core_Smarty::PRINT_NOFORM, | |
8d7a9d07 | 791 | CRM_Core_Smarty::PRINT_JSON, |
42a40a1c | 792 | )) |
793 | ) { | |
794 | return TRUE; | |
795 | } | |
e83561bf CW |
796 | $url = CRM_Utils_System::getUrlPath(); |
797 | return (strpos($url, 'civicrm/ajax') === 0) || (strpos($url, 'civicrm/angular') === 0); | |
156fd9b9 | 798 | } |
b7ceb253 CW |
799 | |
800 | /** | |
f9e31d7f | 801 | * Provide a list of available entityRef filters. |
fd7c068f CW |
802 | * @todo: move component filters into their respective components (e.g. CiviEvent) |
803 | * | |
b7ceb253 CW |
804 | * @return array |
805 | */ | |
00be9182 | 806 | public static function getEntityRefFilters() { |
b7ceb253 | 807 | $filters = array(); |
06606cd1 | 808 | $config = CRM_Core_Config::singleton(); |
b7ceb253 | 809 | |
06606cd1 CW |
810 | if (in_array('CiviEvent', $config->enableComponents)) { |
811 | $filters['event'] = array( | |
812 | array('key' => 'event_type_id', 'value' => ts('Event Type')), | |
813 | array( | |
814 | 'key' => 'start_date', | |
815 | 'value' => ts('Start Date'), | |
816 | 'options' => array( | |
817 | array('key' => '{">":"now"}', 'value' => ts('Upcoming')), | |
818 | array( | |
819 | 'key' => '{"BETWEEN":["now - 3 month","now"]}', | |
820 | 'value' => ts('Past 3 Months'), | |
821 | ), | |
822 | array( | |
823 | 'key' => '{"BETWEEN":["now - 6 month","now"]}', | |
824 | 'value' => ts('Past 6 Months'), | |
825 | ), | |
826 | array( | |
827 | 'key' => '{"BETWEEN":["now - 1 year","now"]}', | |
828 | 'value' => ts('Past Year'), | |
829 | ), | |
830 | ), | |
8d7a9d07 | 831 | ), |
06606cd1 CW |
832 | ); |
833 | } | |
b7ceb253 CW |
834 | |
835 | $filters['activity'] = array( | |
836 | array('key' => 'activity_type_id', 'value' => ts('Activity Type')), | |
837 | array('key' => 'status_id', 'value' => ts('Activity Status')), | |
838 | ); | |
839 | ||
b7ceb253 | 840 | $filters['contact'] = array( |
bee6039a | 841 | array('key' => 'contact_type', 'value' => ts('Contact Type')), |
b7ceb253 CW |
842 | array('key' => 'group', 'value' => ts('Group'), 'entity' => 'group_contact'), |
843 | array('key' => 'tag', 'value' => ts('Tag'), 'entity' => 'entity_tag'), | |
844 | array('key' => 'state_province', 'value' => ts('State/Province'), 'entity' => 'address'), | |
845 | array('key' => 'country', 'value' => ts('Country'), 'entity' => 'address'), | |
846 | array('key' => 'gender_id', 'value' => ts('Gender')), | |
847 | array('key' => 'is_deceased', 'value' => ts('Deceased')), | |
93d20146 YC |
848 | array('key' => 'contact_id', 'value' => ts('Contact ID'), 'type' => 'text'), |
849 | array('key' => 'external_identifier', 'value' => ts('External ID'), 'type' => 'text'), | |
fd7c068f | 850 | array('key' => 'source', 'value' => ts('Contact Source'), 'type' => 'text'), |
b7ceb253 CW |
851 | ); |
852 | ||
06606cd1 CW |
853 | if (in_array('CiviCase', $config->enableComponents)) { |
854 | $filters['case'] = array( | |
855 | array( | |
856 | 'key' => 'case_id.case_type_id', | |
857 | 'value' => ts('Case Type'), | |
858 | 'entity' => 'Case', | |
859 | ), | |
860 | array( | |
861 | 'key' => 'case_id.status_id', | |
862 | 'value' => ts('Case Status'), | |
863 | 'entity' => 'Case', | |
864 | ), | |
865 | ); | |
866 | foreach ($filters['contact'] as $filter) { | |
867 | $filter += array('entity' => 'contact'); | |
868 | $filter['key'] = 'contact_id.' . $filter['key']; | |
869 | $filters['case'][] = $filter; | |
870 | } | |
871 | } | |
872 | ||
fd7c068f CW |
873 | CRM_Utils_Hook::entityRefFilters($filters); |
874 | ||
b7ceb253 CW |
875 | return $filters; |
876 | } | |
96025800 | 877 | |
09a4dcd5 CW |
878 | /** |
879 | * In debug mode, look for a non-minified version of this file | |
880 | * | |
881 | * @param string $fileName | |
882 | * @param string $extName | |
883 | */ | |
884 | private function resolveFileName(&$fileName, $extName) { | |
885 | if (CRM_Core_Config::singleton()->debug && strpos($fileName, '.min.') !== FALSE) { | |
886 | $nonMiniFile = str_replace('.min.', '.', $fileName); | |
887 | if ($this->getPath($extName, $nonMiniFile)) { | |
888 | $fileName = $nonMiniFile; | |
889 | } | |
890 | } | |
891 | } | |
892 | ||
6f12c6eb | 893 | /** |
894 | * @param string $url | |
895 | * @return string | |
896 | */ | |
897 | public function addCacheCode($url) { | |
33603e1d | 898 | $hasQuery = strpos($url, '?') !== FALSE; |
03449a5b | 899 | $operator = $hasQuery ? '&' : '?'; |
6f12c6eb | 900 | |
03449a5b | 901 | return $url . $operator . 'r=' . $this->cacheCode; |
6f12c6eb | 902 | } |
33603e1d | 903 | |
6a488035 | 904 | } |