Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
8d7a9d07 | 3 | +--------------------------------------------------------------------+ |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
8d7a9d07 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 40 | * @copyright CiviCRM LLC (c) 2004-2016 |
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) { | |
118 | $sys = CRM_Extension_System::singleton(); | |
a4704404 | 119 | $cache = Civi::cache('js_strings'); |
6a488035 TO |
120 | self::$_singleton = new CRM_Core_Resources( |
121 | $sys->getMapper(), | |
122 | $cache, | |
eb897088 | 123 | CRM_Core_Config::isUpgradeMode() ? NULL : 'resCacheCode' |
6a488035 TO |
124 | ); |
125 | } | |
126 | return self::$_singleton; | |
127 | } | |
128 | ||
129 | /** | |
d09edf64 | 130 | * Construct a resource manager. |
6a488035 | 131 | * |
6a0b768e TO |
132 | * @param CRM_Extension_Mapper $extMapper |
133 | * Map extension names to their base path or URLs. | |
134 | * @param CRM_Utils_Cache_Interface $cache | |
135 | * JS-localization cache. | |
3be754d6 | 136 | * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources |
6a488035 TO |
137 | */ |
138 | public function __construct($extMapper, $cache, $cacheCodeKey = NULL) { | |
139 | $this->extMapper = $extMapper; | |
fd7dc3f3 | 140 | $this->strings = new CRM_Core_Resources_Strings($cache); |
6a488035 TO |
141 | $this->cacheCodeKey = $cacheCodeKey; |
142 | if ($cacheCodeKey !== NULL) { | |
aaffa79f | 143 | $this->cacheCode = Civi::settings()->get($cacheCodeKey); |
6a488035 | 144 | } |
150f50c1 | 145 | if (!$this->cacheCode) { |
6a488035 TO |
146 | $this->resetCacheCode(); |
147 | } | |
84fb7424 | 148 | $this->ajaxPopupsEnabled = (bool) Civi::settings()->get('ajaxPopupsEnabled'); |
b698e2d5 | 149 | $this->paths = Civi::paths(); |
6a488035 TO |
150 | } |
151 | ||
90efc417 TO |
152 | /** |
153 | * Export permission data to the client to enable smarter GUIs. | |
154 | * | |
155 | * Note: Application security stems from the server's enforcement | |
156 | * of the security logic (e.g. in the API permissions). There's no way | |
157 | * the client can use this info to make the app more secure; however, | |
158 | * it can produce a better-tuned (non-broken) UI. | |
159 | * | |
160 | * @param array $permNames | |
161 | * List of permission names to check/export. | |
162 | * @return CRM_Core_Resources | |
163 | */ | |
164 | public function addPermissions($permNames) { | |
165 | $permNames = (array) $permNames; | |
166 | $perms = array(); | |
167 | foreach ($permNames as $permName) { | |
168 | $perms[$permName] = CRM_Core_Permission::check($permName); | |
169 | } | |
170 | return $this->addSetting(array( | |
171 | 'permissions' => $perms, | |
172 | )); | |
173 | } | |
174 | ||
6a488035 TO |
175 | /** |
176 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
177 | * | |
5a4f6742 CW |
178 | * @param string $ext |
179 | * extension name; use 'civicrm' for core. | |
180 | * @param string $file | |
181 | * file path -- relative to the extension base dir. | |
182 | * @param int $weight | |
183 | * relative weight within a given region. | |
184 | * @param string $region | |
185 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
3f3bba82 TO |
186 | * @param bool|string $translate |
187 | * Whether to load translated strings for this file. Use one of: | |
188 | * - FALSE: Do not load translated strings. | |
189 | * - TRUE: Load translated strings. Use the $ext's default domain. | |
190 | * - string: Load translated strings. Use a specific domain. | |
6a488035 TO |
191 | * |
192 | * @return CRM_Core_Resources | |
193 | */ | |
194 | public function addScriptFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION, $translate = TRUE) { | |
195 | if ($translate) { | |
3f3bba82 TO |
196 | $domain = ($translate === TRUE) ? $ext : $translate; |
197 | $this->addString($this->strings->get($domain, $this->getPath($ext, $file), 'text/javascript'), $domain); | |
6a488035 | 198 | } |
09a4dcd5 | 199 | $this->resolveFileName($file, $ext); |
6a488035 TO |
200 | return $this->addScriptUrl($this->getUrl($ext, $file, TRUE), $weight, $region); |
201 | } | |
202 | ||
203 | /** | |
204 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
205 | * | |
5a4f6742 CW |
206 | * @param string $url |
207 | * @param int $weight | |
208 | * relative weight within a given region. | |
209 | * @param string $region | |
210 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
211 | * @return CRM_Core_Resources |
212 | */ | |
213 | public function addScriptUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
214 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
215 | 'name' => $url, |
216 | 'type' => 'scriptUrl', | |
217 | 'scriptUrl' => $url, | |
218 | 'weight' => $weight, | |
219 | 'region' => $region, | |
220 | )); | |
6a488035 TO |
221 | return $this; |
222 | } | |
223 | ||
224 | /** | |
225 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
226 | * | |
5a4f6742 CW |
227 | * @param string $code |
228 | * JavaScript source code. | |
229 | * @param int $weight | |
230 | * relative weight within a given region. | |
231 | * @param string $region | |
232 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
233 | * @return CRM_Core_Resources |
234 | */ | |
235 | public function addScript($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
236 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
237 | // 'name' => automatic |
238 | 'type' => 'script', | |
239 | 'script' => $code, | |
240 | 'weight' => $weight, | |
241 | 'region' => $region, | |
242 | )); | |
6a488035 TO |
243 | return $this; |
244 | } | |
245 | ||
246 | /** | |
73bcd446 | 247 | * Add JavaScript variables to CRM.vars |
6a488035 | 248 | * |
132fc68e CW |
249 | * Example: |
250 | * From the server: | |
73bcd446 CW |
251 | * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar')); |
252 | * Access var from javascript: | |
253 | * CRM.vars.myNamespace.foo // "bar" | |
132fc68e | 254 | * |
01478033 | 255 | * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference |
132fc68e | 256 | * |
6a0b768e TO |
257 | * @param string $nameSpace |
258 | * Usually the name of your extension. | |
73bcd446 CW |
259 | * @param array $vars |
260 | * @return CRM_Core_Resources | |
261 | */ | |
262 | public function addVars($nameSpace, $vars) { | |
263 | $existing = CRM_Utils_Array::value($nameSpace, CRM_Utils_Array::value('vars', $this->settings), array()); | |
264 | $vars = $this->mergeSettings($existing, $vars); | |
265 | $this->addSetting(array('vars' => array($nameSpace => $vars))); | |
266 | return $this; | |
267 | } | |
268 | ||
269 | /** | |
270 | * Add JavaScript variables to the root of the CRM object. | |
271 | * This function is usually reserved for low-level system use. | |
272 | * Extensions and components should generally use addVars instead. | |
273 | * | |
5a4f6742 | 274 | * @param array $settings |
6a488035 TO |
275 | * @return CRM_Core_Resources |
276 | */ | |
277 | public function addSetting($settings) { | |
278 | $this->settings = $this->mergeSettings($this->settings, $settings); | |
279 | if (!$this->addedSettings) { | |
88cd8875 | 280 | $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header'; |
6a488035 | 281 | $resources = $this; |
156fd9b9 | 282 | CRM_Core_Region::instance($region)->add(array( |
353ffa53 TO |
283 | 'callback' => function (&$snippet, &$html) use ($resources) { |
284 | $html .= "\n" . $resources->renderSetting(); | |
285 | }, | |
8cb324cc | 286 | 'weight' => -100000, |
6a488035 TO |
287 | )); |
288 | $this->addedSettings = TRUE; | |
289 | } | |
290 | return $this; | |
291 | } | |
292 | ||
293 | /** | |
69847402 | 294 | * Add JavaScript variables to the global CRM object via a callback function. |
6a488035 | 295 | * |
3be754d6 | 296 | * @param callable $callable |
6a488035 TO |
297 | * @return CRM_Core_Resources |
298 | */ | |
299 | public function addSettingsFactory($callable) { | |
d937dbcd CW |
300 | // Make sure our callback has been registered |
301 | $this->addSetting(array()); | |
6a488035 TO |
302 | $this->settingsFactories[] = $callable; |
303 | return $this; | |
304 | } | |
305 | ||
69847402 | 306 | /** |
d09edf64 | 307 | * Helper fn for addSettingsFactory. |
69847402 | 308 | */ |
6a488035 TO |
309 | public function getSettings() { |
310 | $result = $this->settings; | |
311 | foreach ($this->settingsFactories as $callable) { | |
312 | $result = $this->mergeSettings($result, $callable()); | |
313 | } | |
314 | return $result; | |
315 | } | |
316 | ||
317 | /** | |
318 | * @param array $settings | |
319 | * @param array $additions | |
a6c01b45 CW |
320 | * @return array |
321 | * combination of $settings and $additions | |
6a488035 TO |
322 | */ |
323 | protected function mergeSettings($settings, $additions) { | |
324 | foreach ($additions as $k => $v) { | |
325 | if (isset($settings[$k]) && is_array($settings[$k]) && is_array($v)) { | |
326 | $v += $settings[$k]; | |
327 | } | |
328 | $settings[$k] = $v; | |
329 | } | |
330 | return $settings; | |
331 | } | |
332 | ||
333 | /** | |
d09edf64 | 334 | * Helper fn for addSetting. |
6a488035 TO |
335 | * Render JavaScript variables for the global CRM object. |
336 | * | |
6a488035 TO |
337 | * @return string |
338 | */ | |
339 | public function renderSetting() { | |
156fd9b9 CW |
340 | // On a standard page request we construct the CRM object from scratch |
341 | if (!self::isAjaxMode()) { | |
342 | $js = 'var CRM = ' . json_encode($this->getSettings()) . ';'; | |
343 | } | |
344 | // For an ajax request we append to it | |
345 | else { | |
346 | $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');'; | |
347 | } | |
d759bd10 | 348 | return sprintf("<script type=\"text/javascript\">\n%s\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) { |
3f3bba82 TO |
383 | $translated = ts($str, array( |
384 | 'domain' => ($domain == 'civicrm') ? NULL : array($domain, NULL), | |
1b4710da | 385 | 'raw' => TRUE, |
3f3bba82 TO |
386 | )); |
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 TO |
391 | $bucket = $domain == 'civicrm' ? 'strings' : 'strings::' . $domain; |
392 | $this->addSetting(array( | |
393 | $bucket => array($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) { | |
429 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
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) { | |
451 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
452 | // 'name' => automatic |
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) { | |
503 | $file .= '?r=' . $this->getCacheCode(); | |
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; | |
528 | $files = array(); | |
529 | foreach ($patterns as $pattern) { | |
9f87b14b | 530 | if (CRM_Utils_File::isAbsolute($pattern)) { |
16cd1eca TO |
531 | // Absolute path. |
532 | $files = array_merge($files, (array) glob($pattern, $flags)); | |
533 | } | |
534 | else { | |
535 | // Relative path. | |
536 | $files = array_merge($files, (array) glob("$path/$pattern", $flags)); | |
537 | } | |
538 | } | |
539 | sort($files); // Deterministic order. | |
540 | $files = array_unique($files); | |
541 | return array_map(function ($file) use ($path) { | |
542 | return CRM_Utils_File::relativize($file, "$path/"); | |
543 | }, $files); | |
544 | } | |
545 | ||
a0ee3941 EM |
546 | /** |
547 | * @return string | |
548 | */ | |
6a488035 TO |
549 | public function getCacheCode() { |
550 | return $this->cacheCode; | |
551 | } | |
552 | ||
a0ee3941 EM |
553 | /** |
554 | * @param $value | |
5badddc3 | 555 | * @return CRM_Core_Resources |
a0ee3941 | 556 | */ |
6a488035 TO |
557 | public function setCacheCode($value) { |
558 | $this->cacheCode = $value; | |
559 | if ($this->cacheCodeKey) { | |
08ef4ddd | 560 | Civi::settings()->set($this->cacheCodeKey, $value); |
6a488035 | 561 | } |
9762f6ff | 562 | return $this; |
6a488035 TO |
563 | } |
564 | ||
5badddc3 CW |
565 | /** |
566 | * @return CRM_Core_Resources | |
567 | */ | |
6a488035 TO |
568 | public function resetCacheCode() { |
569 | $this->setCacheCode(CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC)); | |
f091327b CW |
570 | // Also flush cms resource cache if needed |
571 | CRM_Core_Config::singleton()->userSystem->clearResourceCache(); | |
9762f6ff | 572 | return $this; |
6a488035 TO |
573 | } |
574 | ||
575 | /** | |
576 | * This adds CiviCRM's standard css and js to the specified region of the document. | |
577 | * It will only run once. | |
578 | * | |
579 | * TODO: Separate the functional code (like addStyle/addScript) from the policy code | |
580 | * (like addCoreResources/addCoreStyles). | |
581 | * | |
2a6da8d7 | 582 | * @param string $region |
6a488035 | 583 | * @return CRM_Core_Resources |
6a488035 TO |
584 | */ |
585 | public function addCoreResources($region = 'html-header') { | |
156fd9b9 | 586 | if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) { |
6a488035 TO |
587 | $this->addedCoreResources[$region] = TRUE; |
588 | $config = CRM_Core_Config::singleton(); | |
589 | ||
a43b1583 | 590 | // Add resources from coreResourceList |
6a488035 | 591 | $jsWeight = -9999; |
dbb12634 | 592 | foreach ($this->coreResourceList($region) as $item) { |
7266e09b CW |
593 | if (is_array($item)) { |
594 | $this->addSetting($item); | |
595 | } | |
596 | elseif (substr($item, -2) == 'js') { | |
74227f77 | 597 | // Don't bother looking for ts() calls in packages, there aren't any |
7266e09b CW |
598 | $translate = (substr($item, 0, 3) == 'js/'); |
599 | $this->addScriptFile('civicrm', $item, $jsWeight++, $region, $translate); | |
6a488035 | 600 | } |
a43b1583 | 601 | else { |
7266e09b | 602 | $this->addStyleFile('civicrm', $item, -100, $region); |
6a488035 TO |
603 | } |
604 | } | |
605 | ||
98466ff9 | 606 | $tsLocale = CRM_Core_I18n::getLocale(); |
4e56e743 | 607 | // Dynamic localization script |
5d26edf0 | 608 | $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $tsLocale, array('r' => $this->getCacheCode())), $jsWeight++, $region); |
4e56e743 | 609 | |
d759bd10 | 610 | // Add global settings |
2aa397bc | 611 | $settings = array( |
353ffa53 | 612 | 'config' => array( |
353ffa53 | 613 | 'isFrontend' => $config->userFrameworkFrontend, |
8d7a9d07 | 614 | ), |
353ffa53 | 615 | ); |
4e56e743 CW |
616 | // Disable profile creation if user lacks permission |
617 | if (!CRM_Core_Permission::check('edit all contacts') && !CRM_Core_Permission::check('add contacts')) { | |
b7ceb253 | 618 | $settings['config']['entityRef']['contactCreate'] = FALSE; |
3d527838 | 619 | } |
4e56e743 | 620 | $this->addSetting($settings); |
3d527838 CW |
621 | |
622 | // Give control of jQuery and _ back to the CMS - this loads last | |
c2777586 | 623 | $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE); |
6a488035 TO |
624 | |
625 | $this->addCoreStyles($region); | |
626 | } | |
627 | return $this; | |
628 | } | |
629 | ||
630 | /** | |
631 | * This will add CiviCRM's standard CSS | |
632 | * | |
633 | * TODO: Separate the functional code (like addStyle/addScript) from the policy code | |
634 | * (like addCoreResources/addCoreStyles). | |
635 | * | |
636 | * @param string $region | |
637 | * @return CRM_Core_Resources | |
638 | */ | |
639 | public function addCoreStyles($region = 'html-header') { | |
640 | if (!isset($this->addedCoreStyles[$region])) { | |
641 | $this->addedCoreStyles[$region] = TRUE; | |
642 | ||
643 | // Load custom or core css | |
644 | $config = CRM_Core_Config::singleton(); | |
645 | if (!empty($config->customCSSURL)) { | |
57018a3e | 646 | $this->addStyleUrl($config->customCSSURL, 99, $region); |
6a488035 | 647 | } |
aaffa79f | 648 | if (!Civi::settings()->get('disable_core_css')) { |
6a488035 | 649 | $this->addStyleFile('civicrm', 'css/civicrm.css', -99, $region); |
6a488035 | 650 | } |
a2c70872 AH |
651 | // crm-i.css added ahead of other styles so it can be overridden by FA. |
652 | $this->addStyleFile('civicrm', 'css/crm-i.css', -101, $region); | |
6a488035 TO |
653 | } |
654 | return $this; | |
655 | } | |
656 | ||
627668e8 | 657 | /** |
d09edf64 | 658 | * Flushes cached translated strings. |
5badddc3 | 659 | * @return CRM_Core_Resources |
627668e8 CW |
660 | */ |
661 | public function flushStrings() { | |
fd7dc3f3 | 662 | $this->strings->flush(); |
9762f6ff CW |
663 | return $this; |
664 | } | |
665 | ||
6a488035 | 666 | /** |
fd7dc3f3 TO |
667 | * @return CRM_Core_Resources_Strings |
668 | */ | |
669 | public function getStrings() { | |
670 | return $this->strings; | |
6a488035 TO |
671 | } |
672 | ||
19f7e35e | 673 | /** |
8d7a9d07 | 674 | * Create dynamic script for localizing js widgets. |
19f7e35e | 675 | */ |
00be9182 | 676 | public static function outputLocalizationJS() { |
4cc9b813 | 677 | CRM_Core_Page_AJAX::setJsHeaders(); |
a88cf11a | 678 | $config = CRM_Core_Config::singleton(); |
3d527838 CW |
679 | $vars = array( |
680 | 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)), | |
681 | 'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')), | |
682 | 'otherSearch' => json_encode(ts('Enter search term...')), | |
b7ceb253 CW |
683 | 'entityRef' => array( |
684 | 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(), | |
685 | 'filters' => self::getEntityRefFilters(), | |
686 | ), | |
7b83e312 | 687 | 'ajaxPopupsEnabled' => self::singleton()->ajaxPopupsEnabled, |
3d527838 | 688 | ); |
3d4fb0ed | 689 | print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars); |
4cc9b813 | 690 | CRM_Utils_System::civiExit(); |
c66581f5 CW |
691 | } |
692 | ||
6a488035 | 693 | /** |
d09edf64 | 694 | * List of core resources we add to every CiviCRM page. |
6a488035 | 695 | * |
e8038a7b CW |
696 | * Note: non-compressed versions of .min files will be used in debug mode |
697 | * | |
e3c1e85b | 698 | * @param string $region |
a43b1583 | 699 | * @return array |
6a488035 | 700 | */ |
e3c1e85b | 701 | public function coreResourceList($region) { |
dbe0bbc6 | 702 | $config = CRM_Core_Config::singleton(); |
a43b1583 | 703 | |
4968e732 CW |
704 | // Scripts needed by everyone, everywhere |
705 | // FIXME: This is too long; list needs finer-grained segmentation | |
a0c8b50e | 706 | $items = array( |
3c61fc8f CW |
707 | "bower_components/jquery/dist/jquery.min.js", |
708 | "bower_components/jquery-ui/jquery-ui.min.js", | |
e8038a7b | 709 | "bower_components/jquery-ui/themes/smoothness/jquery-ui.min.css", |
1891a856 | 710 | "bower_components/lodash-compat/lodash.min.js", |
e8038a7b CW |
711 | "packages/jquery/plugins/jquery.mousewheel.min.js", |
712 | "bower_components/select2/select2.min.js", | |
713 | "bower_components/select2/select2.min.css", | |
90000c30 | 714 | "bower_components/font-awesome/css/font-awesome.min.css", |
a0c8b50e | 715 | "packages/jquery/plugins/jquery.tableHeader.js", |
e8038a7b CW |
716 | "packages/jquery/plugins/jquery.form.min.js", |
717 | "packages/jquery/plugins/jquery.timeentry.min.js", | |
718 | "packages/jquery/plugins/jquery.blockUI.min.js", | |
719 | "bower_components/datatables/media/js/jquery.dataTables.min.js", | |
720 | "bower_components/datatables/media/css/jquery.dataTables.min.css", | |
721 | "bower_components/jquery-validation/dist/jquery.validate.min.js", | |
722 | "packages/jquery/plugins/jquery.ui.datepicker.validation.min.js", | |
a0c8b50e | 723 | "js/Common.js", |
53f2643c | 724 | "js/crm.ajax.js", |
7060d177 | 725 | "js/wysiwyg/crm.wysiwyg.js", |
a43b1583 | 726 | ); |
7c523661 | 727 | // add wysiwyg editor |
aaffa79f | 728 | $editor = Civi::settings()->get('editor_id'); |
b608cfb1 | 729 | if ($editor == "CKEditor") { |
b608cfb1 | 730 | $items[] = "js/wysiwyg/crm.ckeditor.js"; |
7266e09b CW |
731 | $ckConfig = CRM_Admin_Page_CKEditorConfig::getConfigUrl(); |
732 | if ($ckConfig) { | |
733 | $items[] = array('config' => array('CKEditorCustomConfig' => $ckConfig)); | |
734 | } | |
b608cfb1 | 735 | } |
dbe0bbc6 | 736 | |
4968e732 CW |
737 | // These scripts are only needed by back-office users |
738 | if (CRM_Core_Permission::check('access CiviCRM')) { | |
e8038a7b | 739 | $items[] = "packages/jquery/plugins/jquery.menu.min.js"; |
1d68b509 | 740 | $items[] = "css/civicrmNavigation.css"; |
e8038a7b CW |
741 | $items[] = "packages/jquery/plugins/jquery.jeditable.min.js"; |
742 | $items[] = "packages/jquery/plugins/jquery.notify.min.js"; | |
4968e732 | 743 | $items[] = "js/jquery/jquery.crmeditable.js"; |
4968e732 CW |
744 | } |
745 | ||
d606fff7 CW |
746 | // JS for multilingual installations |
747 | if (!empty($config->languageLimit) && count($config->languageLimit) > 1 && CRM_Core_Permission::check('translate CiviCRM')) { | |
748 | $items[] = "js/crm.multilingual.js"; | |
749 | } | |
750 | ||
53f2643c CW |
751 | // Enable administrators to edit option lists in a dialog |
752 | if (CRM_Core_Permission::check('administer CiviCRM') && $this->ajaxPopupsEnabled) { | |
753 | $items[] = "js/crm.optionEdit.js"; | |
754 | } | |
9efeb642 | 755 | |
98466ff9 | 756 | $tsLocale = CRM_Core_I18n::getLocale(); |
dbe0bbc6 | 757 | // Add localized jQuery UI files |
5d26edf0 | 758 | if ($tsLocale && $tsLocale != 'en_US') { |
dbe0bbc6 | 759 | // Search for i18n file in order of specificity (try fr-CA, then fr) |
5d26edf0 | 760 | list($lang) = explode('_', $tsLocale); |
3c61fc8f | 761 | $path = "bower_components/jquery-ui/ui/i18n"; |
5d26edf0 | 762 | foreach (array(str_replace('_', '-', $tsLocale), $lang) as $language) { |
f2f191fe | 763 | $localizationFile = "$path/datepicker-{$language}.js"; |
dbe0bbc6 CW |
764 | if ($this->getPath('civicrm', $localizationFile)) { |
765 | $items[] = $localizationFile; | |
766 | break; | |
767 | } | |
768 | } | |
769 | } | |
f9f361d0 | 770 | |
72e86d7d | 771 | // Allow hooks to modify this list |
e3c1e85b | 772 | CRM_Utils_Hook::coreResourceList($items, $region); |
f9f361d0 | 773 | |
6a488035 TO |
774 | return $items; |
775 | } | |
156fd9b9 CW |
776 | |
777 | /** | |
a6c01b45 CW |
778 | * @return bool |
779 | * is this page request an ajax snippet? | |
156fd9b9 | 780 | */ |
00be9182 | 781 | public static function isAjaxMode() { |
353ffa53 TO |
782 | return in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array( |
783 | CRM_Core_Smarty::PRINT_SNIPPET, | |
784 | CRM_Core_Smarty::PRINT_NOFORM, | |
8d7a9d07 | 785 | CRM_Core_Smarty::PRINT_JSON, |
353ffa53 | 786 | )); |
156fd9b9 | 787 | } |
b7ceb253 CW |
788 | |
789 | /** | |
f9e31d7f | 790 | * Provide a list of available entityRef filters. |
fd7c068f CW |
791 | * @todo: move component filters into their respective components (e.g. CiviEvent) |
792 | * | |
b7ceb253 CW |
793 | * @return array |
794 | */ | |
00be9182 | 795 | public static function getEntityRefFilters() { |
b7ceb253 | 796 | $filters = array(); |
06606cd1 | 797 | $config = CRM_Core_Config::singleton(); |
b7ceb253 | 798 | |
06606cd1 CW |
799 | if (in_array('CiviEvent', $config->enableComponents)) { |
800 | $filters['event'] = array( | |
801 | array('key' => 'event_type_id', 'value' => ts('Event Type')), | |
802 | array( | |
803 | 'key' => 'start_date', | |
804 | 'value' => ts('Start Date'), | |
805 | 'options' => array( | |
806 | array('key' => '{">":"now"}', 'value' => ts('Upcoming')), | |
807 | array( | |
808 | 'key' => '{"BETWEEN":["now - 3 month","now"]}', | |
809 | 'value' => ts('Past 3 Months'), | |
810 | ), | |
811 | array( | |
812 | 'key' => '{"BETWEEN":["now - 6 month","now"]}', | |
813 | 'value' => ts('Past 6 Months'), | |
814 | ), | |
815 | array( | |
816 | 'key' => '{"BETWEEN":["now - 1 year","now"]}', | |
817 | 'value' => ts('Past Year'), | |
818 | ), | |
819 | ), | |
8d7a9d07 | 820 | ), |
06606cd1 CW |
821 | ); |
822 | } | |
b7ceb253 CW |
823 | |
824 | $filters['activity'] = array( | |
825 | array('key' => 'activity_type_id', 'value' => ts('Activity Type')), | |
826 | array('key' => 'status_id', 'value' => ts('Activity Status')), | |
827 | ); | |
828 | ||
b7ceb253 | 829 | $filters['contact'] = array( |
bee6039a | 830 | array('key' => 'contact_type', 'value' => ts('Contact Type')), |
b7ceb253 CW |
831 | array('key' => 'group', 'value' => ts('Group'), 'entity' => 'group_contact'), |
832 | array('key' => 'tag', 'value' => ts('Tag'), 'entity' => 'entity_tag'), | |
833 | array('key' => 'state_province', 'value' => ts('State/Province'), 'entity' => 'address'), | |
834 | array('key' => 'country', 'value' => ts('Country'), 'entity' => 'address'), | |
835 | array('key' => 'gender_id', 'value' => ts('Gender')), | |
836 | array('key' => 'is_deceased', 'value' => ts('Deceased')), | |
fd7c068f | 837 | array('key' => 'source', 'value' => ts('Contact Source'), 'type' => 'text'), |
b7ceb253 CW |
838 | ); |
839 | ||
06606cd1 CW |
840 | if (in_array('CiviCase', $config->enableComponents)) { |
841 | $filters['case'] = array( | |
842 | array( | |
843 | 'key' => 'case_id.case_type_id', | |
844 | 'value' => ts('Case Type'), | |
845 | 'entity' => 'Case', | |
846 | ), | |
847 | array( | |
848 | 'key' => 'case_id.status_id', | |
849 | 'value' => ts('Case Status'), | |
850 | 'entity' => 'Case', | |
851 | ), | |
852 | ); | |
853 | foreach ($filters['contact'] as $filter) { | |
854 | $filter += array('entity' => 'contact'); | |
855 | $filter['key'] = 'contact_id.' . $filter['key']; | |
856 | $filters['case'][] = $filter; | |
857 | } | |
858 | } | |
859 | ||
fd7c068f CW |
860 | CRM_Utils_Hook::entityRefFilters($filters); |
861 | ||
b7ceb253 CW |
862 | return $filters; |
863 | } | |
96025800 | 864 | |
09a4dcd5 CW |
865 | /** |
866 | * In debug mode, look for a non-minified version of this file | |
867 | * | |
868 | * @param string $fileName | |
869 | * @param string $extName | |
870 | */ | |
871 | private function resolveFileName(&$fileName, $extName) { | |
872 | if (CRM_Core_Config::singleton()->debug && strpos($fileName, '.min.') !== FALSE) { | |
873 | $nonMiniFile = str_replace('.min.', '.', $fileName); | |
874 | if ($this->getPath($extName, $nonMiniFile)) { | |
875 | $fileName = $nonMiniFile; | |
876 | } | |
877 | } | |
878 | } | |
879 | ||
6a488035 | 880 | } |