Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
8d7a9d07 CB |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 4.6 | | |
5 | +--------------------------------------------------------------------+ | |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 40 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | ||
6a488035 | 101 | /** |
fe482240 | 102 | * Get or set the single instance of CRM_Core_Resources. |
6a488035 | 103 | * |
5a4f6742 CW |
104 | * @param CRM_Core_Resources $instance |
105 | * New copy of the manager. | |
6a488035 TO |
106 | * @return CRM_Core_Resources |
107 | */ | |
108 | static public function singleton(CRM_Core_Resources $instance = NULL) { | |
109 | if ($instance !== NULL) { | |
110 | self::$_singleton = $instance; | |
111 | } | |
112 | if (self::$_singleton === NULL) { | |
113 | $sys = CRM_Extension_System::singleton(); | |
114 | $cache = new CRM_Utils_Cache_SqlGroup(array( | |
8d7a9d07 CB |
115 | 'group' => 'js-strings', |
116 | 'prefetch' => FALSE, | |
117 | )); | |
6a488035 TO |
118 | self::$_singleton = new CRM_Core_Resources( |
119 | $sys->getMapper(), | |
120 | $cache, | |
eb897088 | 121 | CRM_Core_Config::isUpgradeMode() ? NULL : 'resCacheCode' |
6a488035 TO |
122 | ); |
123 | } | |
124 | return self::$_singleton; | |
125 | } | |
126 | ||
127 | /** | |
d09edf64 | 128 | * Construct a resource manager. |
6a488035 | 129 | * |
6a0b768e TO |
130 | * @param CRM_Extension_Mapper $extMapper |
131 | * Map extension names to their base path or URLs. | |
132 | * @param CRM_Utils_Cache_Interface $cache | |
133 | * JS-localization cache. | |
3be754d6 | 134 | * @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources |
6a488035 TO |
135 | */ |
136 | public function __construct($extMapper, $cache, $cacheCodeKey = NULL) { | |
137 | $this->extMapper = $extMapper; | |
fd7dc3f3 | 138 | $this->strings = new CRM_Core_Resources_Strings($cache); |
6a488035 TO |
139 | $this->cacheCodeKey = $cacheCodeKey; |
140 | if ($cacheCodeKey !== NULL) { | |
141 | $this->cacheCode = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, $cacheCodeKey); | |
142 | } | |
150f50c1 | 143 | if (!$this->cacheCode) { |
6a488035 TO |
144 | $this->resetCacheCode(); |
145 | } | |
53f2643c CW |
146 | $this->ajaxPopupsEnabled = (bool) CRM_Core_BAO_Setting::getItem( |
147 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajaxPopupsEnabled', NULL, TRUE | |
148 | ); | |
6a488035 TO |
149 | } |
150 | ||
90efc417 TO |
151 | /** |
152 | * Export permission data to the client to enable smarter GUIs. | |
153 | * | |
154 | * Note: Application security stems from the server's enforcement | |
155 | * of the security logic (e.g. in the API permissions). There's no way | |
156 | * the client can use this info to make the app more secure; however, | |
157 | * it can produce a better-tuned (non-broken) UI. | |
158 | * | |
159 | * @param array $permNames | |
160 | * List of permission names to check/export. | |
161 | * @return CRM_Core_Resources | |
162 | */ | |
163 | public function addPermissions($permNames) { | |
164 | $permNames = (array) $permNames; | |
165 | $perms = array(); | |
166 | foreach ($permNames as $permName) { | |
167 | $perms[$permName] = CRM_Core_Permission::check($permName); | |
168 | } | |
169 | return $this->addSetting(array( | |
170 | 'permissions' => $perms, | |
171 | )); | |
172 | } | |
173 | ||
6a488035 TO |
174 | /** |
175 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
176 | * | |
5a4f6742 CW |
177 | * @param string $ext |
178 | * extension name; use 'civicrm' for core. | |
179 | * @param string $file | |
180 | * file path -- relative to the extension base dir. | |
181 | * @param int $weight | |
182 | * relative weight within a given region. | |
183 | * @param string $region | |
184 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
3f3bba82 TO |
185 | * @param bool|string $translate |
186 | * Whether to load translated strings for this file. Use one of: | |
187 | * - FALSE: Do not load translated strings. | |
188 | * - TRUE: Load translated strings. Use the $ext's default domain. | |
189 | * - string: Load translated strings. Use a specific domain. | |
6a488035 TO |
190 | * |
191 | * @return CRM_Core_Resources | |
192 | */ | |
193 | public function addScriptFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION, $translate = TRUE) { | |
194 | if ($translate) { | |
3f3bba82 TO |
195 | $domain = ($translate === TRUE) ? $ext : $translate; |
196 | $this->addString($this->strings->get($domain, $this->getPath($ext, $file), 'text/javascript'), $domain); | |
6a488035 | 197 | } |
09a4dcd5 | 198 | $this->resolveFileName($file, $ext); |
6a488035 TO |
199 | return $this->addScriptUrl($this->getUrl($ext, $file, TRUE), $weight, $region); |
200 | } | |
201 | ||
202 | /** | |
203 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
204 | * | |
5a4f6742 CW |
205 | * @param string $url |
206 | * @param int $weight | |
207 | * relative weight within a given region. | |
208 | * @param string $region | |
209 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
210 | * @return CRM_Core_Resources |
211 | */ | |
212 | public function addScriptUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
213 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
214 | 'name' => $url, |
215 | 'type' => 'scriptUrl', | |
216 | 'scriptUrl' => $url, | |
217 | 'weight' => $weight, | |
218 | 'region' => $region, | |
219 | )); | |
6a488035 TO |
220 | return $this; |
221 | } | |
222 | ||
223 | /** | |
224 | * Add a JavaScript file to the current page using <SCRIPT SRC>. | |
225 | * | |
5a4f6742 CW |
226 | * @param string $code |
227 | * JavaScript source code. | |
228 | * @param int $weight | |
229 | * relative weight within a given region. | |
230 | * @param string $region | |
231 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
232 | * @return CRM_Core_Resources |
233 | */ | |
234 | public function addScript($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
235 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
236 | // 'name' => automatic |
237 | 'type' => 'script', | |
238 | 'script' => $code, | |
239 | 'weight' => $weight, | |
240 | 'region' => $region, | |
241 | )); | |
6a488035 TO |
242 | return $this; |
243 | } | |
244 | ||
245 | /** | |
73bcd446 | 246 | * Add JavaScript variables to CRM.vars |
6a488035 | 247 | * |
132fc68e CW |
248 | * Example: |
249 | * From the server: | |
73bcd446 CW |
250 | * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar')); |
251 | * Access var from javascript: | |
252 | * CRM.vars.myNamespace.foo // "bar" | |
132fc68e | 253 | * |
01478033 | 254 | * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference |
132fc68e | 255 | * |
6a0b768e TO |
256 | * @param string $nameSpace |
257 | * Usually the name of your extension. | |
73bcd446 CW |
258 | * @param array $vars |
259 | * @return CRM_Core_Resources | |
260 | */ | |
261 | public function addVars($nameSpace, $vars) { | |
262 | $existing = CRM_Utils_Array::value($nameSpace, CRM_Utils_Array::value('vars', $this->settings), array()); | |
263 | $vars = $this->mergeSettings($existing, $vars); | |
264 | $this->addSetting(array('vars' => array($nameSpace => $vars))); | |
265 | return $this; | |
266 | } | |
267 | ||
268 | /** | |
269 | * Add JavaScript variables to the root of the CRM object. | |
270 | * This function is usually reserved for low-level system use. | |
271 | * Extensions and components should generally use addVars instead. | |
272 | * | |
5a4f6742 | 273 | * @param array $settings |
6a488035 TO |
274 | * @return CRM_Core_Resources |
275 | */ | |
276 | public function addSetting($settings) { | |
277 | $this->settings = $this->mergeSettings($this->settings, $settings); | |
278 | if (!$this->addedSettings) { | |
88cd8875 | 279 | $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header'; |
6a488035 | 280 | $resources = $this; |
156fd9b9 | 281 | CRM_Core_Region::instance($region)->add(array( |
353ffa53 TO |
282 | 'callback' => function (&$snippet, &$html) use ($resources) { |
283 | $html .= "\n" . $resources->renderSetting(); | |
284 | }, | |
8cb324cc | 285 | 'weight' => -100000, |
6a488035 TO |
286 | )); |
287 | $this->addedSettings = TRUE; | |
288 | } | |
289 | return $this; | |
290 | } | |
291 | ||
292 | /** | |
69847402 | 293 | * Add JavaScript variables to the global CRM object via a callback function. |
6a488035 | 294 | * |
3be754d6 | 295 | * @param callable $callable |
6a488035 TO |
296 | * @return CRM_Core_Resources |
297 | */ | |
298 | public function addSettingsFactory($callable) { | |
d937dbcd CW |
299 | // Make sure our callback has been registered |
300 | $this->addSetting(array()); | |
6a488035 TO |
301 | $this->settingsFactories[] = $callable; |
302 | return $this; | |
303 | } | |
304 | ||
69847402 | 305 | /** |
d09edf64 | 306 | * Helper fn for addSettingsFactory. |
69847402 | 307 | */ |
6a488035 TO |
308 | public function getSettings() { |
309 | $result = $this->settings; | |
310 | foreach ($this->settingsFactories as $callable) { | |
311 | $result = $this->mergeSettings($result, $callable()); | |
312 | } | |
313 | return $result; | |
314 | } | |
315 | ||
316 | /** | |
317 | * @param array $settings | |
318 | * @param array $additions | |
a6c01b45 CW |
319 | * @return array |
320 | * combination of $settings and $additions | |
6a488035 TO |
321 | */ |
322 | protected function mergeSettings($settings, $additions) { | |
323 | foreach ($additions as $k => $v) { | |
324 | if (isset($settings[$k]) && is_array($settings[$k]) && is_array($v)) { | |
325 | $v += $settings[$k]; | |
326 | } | |
327 | $settings[$k] = $v; | |
328 | } | |
329 | return $settings; | |
330 | } | |
331 | ||
332 | /** | |
d09edf64 | 333 | * Helper fn for addSetting. |
6a488035 TO |
334 | * Render JavaScript variables for the global CRM object. |
335 | * | |
6a488035 TO |
336 | * @return string |
337 | */ | |
338 | public function renderSetting() { | |
156fd9b9 CW |
339 | // On a standard page request we construct the CRM object from scratch |
340 | if (!self::isAjaxMode()) { | |
341 | $js = 'var CRM = ' . json_encode($this->getSettings()) . ';'; | |
342 | } | |
343 | // For an ajax request we append to it | |
344 | else { | |
345 | $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');'; | |
346 | } | |
d759bd10 | 347 | return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js); |
6a488035 TO |
348 | } |
349 | ||
350 | /** | |
351 | * Add translated string to the js CRM object. | |
352 | * It can then be retrived from the client-side ts() function | |
353 | * Variable substitutions can happen from client-side | |
8ef12e64 | 354 | * |
6a488035 | 355 | * Note: this function rarely needs to be called directly and is mostly for internal use. |
d3e86119 | 356 | * See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files |
6a488035 TO |
357 | * |
358 | * Simple example: | |
359 | * // From php: | |
360 | * CRM_Core_Resources::singleton()->addString('Hello'); | |
361 | * // The string is now available to javascript code i.e. | |
362 | * ts('Hello'); | |
363 | * | |
364 | * Example with client-side substitutions: | |
365 | * // From php: | |
366 | * CRM_Core_Resources::singleton()->addString('Your %1 has been %2'); | |
367 | * // ts() in javascript works the same as in php, for example: | |
368 | * ts('Your %1 has been %2', {1: objectName, 2: actionTaken}); | |
369 | * | |
370 | * NOTE: This function does not work with server-side substitutions | |
371 | * (as this might result in collisions and unwanted variable injections) | |
372 | * Instead, use code like: | |
373 | * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs))))); | |
374 | * And from javascript access it at CRM.myNamespace.myString | |
375 | * | |
5a4f6742 | 376 | * @param string|array $text |
3f3bba82 | 377 | * @param string|NULL $domain |
6a488035 TO |
378 | * @return CRM_Core_Resources |
379 | */ | |
3f3bba82 | 380 | public function addString($text, $domain = 'civicrm') { |
6a488035 | 381 | foreach ((array) $text as $str) { |
3f3bba82 TO |
382 | $translated = ts($str, array( |
383 | 'domain' => ($domain == 'civicrm') ? NULL : array($domain, NULL), | |
1b4710da | 384 | 'raw' => TRUE, |
3f3bba82 TO |
385 | )); |
386 | ||
6a488035 TO |
387 | // We only need to push this string to client if the translation |
388 | // is actually different from the original | |
389 | if ($translated != $str) { | |
3f3bba82 TO |
390 | $bucket = $domain == 'civicrm' ? 'strings' : 'strings::' . $domain; |
391 | $this->addSetting(array( | |
392 | $bucket => array($str => $translated), | |
393 | )); | |
6a488035 TO |
394 | } |
395 | } | |
396 | return $this; | |
397 | } | |
398 | ||
399 | /** | |
400 | * Add a CSS file to the current page using <LINK HREF>. | |
401 | * | |
5a4f6742 CW |
402 | * @param string $ext |
403 | * extension name; use 'civicrm' for core. | |
404 | * @param string $file | |
405 | * file path -- relative to the extension base dir. | |
406 | * @param int $weight | |
407 | * relative weight within a given region. | |
408 | * @param string $region | |
409 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
410 | * @return CRM_Core_Resources |
411 | */ | |
412 | public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
09a4dcd5 | 413 | $this->resolveFileName($file, $ext); |
6a488035 TO |
414 | return $this->addStyleUrl($this->getUrl($ext, $file, TRUE), $weight, $region); |
415 | } | |
416 | ||
417 | /** | |
418 | * Add a CSS file to the current page using <LINK HREF>. | |
419 | * | |
5a4f6742 CW |
420 | * @param string $url |
421 | * @param int $weight | |
422 | * relative weight within a given region. | |
423 | * @param string $region | |
424 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
425 | * @return CRM_Core_Resources |
426 | */ | |
427 | public function addStyleUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
428 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
429 | 'name' => $url, |
430 | 'type' => 'styleUrl', | |
431 | 'styleUrl' => $url, | |
432 | 'weight' => $weight, | |
433 | 'region' => $region, | |
434 | )); | |
6a488035 TO |
435 | return $this; |
436 | } | |
437 | ||
438 | /** | |
439 | * Add a CSS content to the current page using <STYLE>. | |
440 | * | |
5a4f6742 CW |
441 | * @param string $code |
442 | * CSS source code. | |
443 | * @param int $weight | |
444 | * relative weight within a given region. | |
445 | * @param string $region | |
446 | * location within the file; 'html-header', 'page-header', 'page-footer'. | |
6a488035 TO |
447 | * @return CRM_Core_Resources |
448 | */ | |
449 | public function addStyle($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { | |
450 | CRM_Core_Region::instance($region)->add(array( | |
8d7a9d07 CB |
451 | // 'name' => automatic |
452 | 'type' => 'style', | |
453 | 'style' => $code, | |
454 | 'weight' => $weight, | |
455 | 'region' => $region, | |
456 | )); | |
6a488035 TO |
457 | return $this; |
458 | } | |
459 | ||
460 | /** | |
d09edf64 | 461 | * Determine file path of a resource provided by an extension. |
6a488035 | 462 | * |
5a4f6742 CW |
463 | * @param string $ext |
464 | * extension name; use 'civicrm' for core. | |
16cd1eca | 465 | * @param string|NULL $file |
5a4f6742 | 466 | * file path -- relative to the extension base dir. |
6a488035 | 467 | * |
72b3a70c CW |
468 | * @return bool|string |
469 | * full file path or FALSE if not found | |
6a488035 | 470 | */ |
16cd1eca | 471 | public function getPath($ext, $file = NULL) { |
6a488035 | 472 | // TODO consider caching results |
16cd1eca TO |
473 | if ($file === NULL) { |
474 | return $this->extMapper->keyToBasePath($ext); | |
475 | } | |
6a488035 TO |
476 | $path = $this->extMapper->keyToBasePath($ext) . '/' . $file; |
477 | if (is_file($path)) { | |
478 | return $path; | |
479 | } | |
480 | return FALSE; | |
481 | } | |
482 | ||
483 | /** | |
d09edf64 | 484 | * Determine public URL of a resource provided by an extension. |
6a488035 | 485 | * |
5a4f6742 CW |
486 | * @param string $ext |
487 | * extension name; use 'civicrm' for core. | |
488 | * @param string $file | |
489 | * file path -- relative to the extension base dir. | |
2a6da8d7 EM |
490 | * @param bool $addCacheCode |
491 | * | |
6a488035 TO |
492 | * @return string, URL |
493 | */ | |
494 | public function getUrl($ext, $file = NULL, $addCacheCode = FALSE) { | |
495 | if ($file === NULL) { | |
496 | $file = ''; | |
497 | } | |
498 | if ($addCacheCode) { | |
499 | $file .= '?r=' . $this->getCacheCode(); | |
500 | } | |
501 | // TODO consider caching results | |
502 | return $this->extMapper->keyToUrl($ext) . '/' . $file; | |
503 | } | |
504 | ||
16cd1eca TO |
505 | /** |
506 | * Evaluate a glob pattern in the context of a particular extension. | |
507 | * | |
508 | * @param string $ext | |
509 | * Extension name; use 'civicrm' for core. | |
510 | * @param string|array $patterns | |
511 | * Glob pattern; e.g. "*.html". | |
512 | * @param null|int $flags | |
513 | * See glob(). | |
514 | * @return array | |
515 | * List of matching files, relative to the extension base dir. | |
516 | * @see glob() | |
517 | */ | |
518 | public function glob($ext, $patterns, $flags = NULL) { | |
519 | $path = $this->getPath($ext); | |
520 | $patterns = (array) $patterns; | |
521 | $files = array(); | |
522 | foreach ($patterns as $pattern) { | |
9f87b14b | 523 | if (CRM_Utils_File::isAbsolute($pattern)) { |
16cd1eca TO |
524 | // Absolute path. |
525 | $files = array_merge($files, (array) glob($pattern, $flags)); | |
526 | } | |
527 | else { | |
528 | // Relative path. | |
529 | $files = array_merge($files, (array) glob("$path/$pattern", $flags)); | |
530 | } | |
531 | } | |
532 | sort($files); // Deterministic order. | |
533 | $files = array_unique($files); | |
534 | return array_map(function ($file) use ($path) { | |
535 | return CRM_Utils_File::relativize($file, "$path/"); | |
536 | }, $files); | |
537 | } | |
538 | ||
a0ee3941 EM |
539 | /** |
540 | * @return string | |
541 | */ | |
6a488035 TO |
542 | public function getCacheCode() { |
543 | return $this->cacheCode; | |
544 | } | |
545 | ||
a0ee3941 EM |
546 | /** |
547 | * @param $value | |
5badddc3 | 548 | * @return CRM_Core_Resources |
a0ee3941 | 549 | */ |
6a488035 TO |
550 | public function setCacheCode($value) { |
551 | $this->cacheCode = $value; | |
552 | if ($this->cacheCodeKey) { | |
553 | CRM_Core_BAO_Setting::setItem($value, CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, $this->cacheCodeKey); | |
554 | } | |
9762f6ff | 555 | return $this; |
6a488035 TO |
556 | } |
557 | ||
5badddc3 CW |
558 | /** |
559 | * @return CRM_Core_Resources | |
560 | */ | |
6a488035 TO |
561 | public function resetCacheCode() { |
562 | $this->setCacheCode(CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC)); | |
f091327b CW |
563 | // Also flush cms resource cache if needed |
564 | CRM_Core_Config::singleton()->userSystem->clearResourceCache(); | |
9762f6ff | 565 | return $this; |
6a488035 TO |
566 | } |
567 | ||
568 | /** | |
569 | * This adds CiviCRM's standard css and js to the specified region of the document. | |
570 | * It will only run once. | |
571 | * | |
572 | * TODO: Separate the functional code (like addStyle/addScript) from the policy code | |
573 | * (like addCoreResources/addCoreStyles). | |
574 | * | |
2a6da8d7 | 575 | * @param string $region |
6a488035 | 576 | * @return CRM_Core_Resources |
6a488035 TO |
577 | */ |
578 | public function addCoreResources($region = 'html-header') { | |
156fd9b9 | 579 | if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) { |
6a488035 TO |
580 | $this->addedCoreResources[$region] = TRUE; |
581 | $config = CRM_Core_Config::singleton(); | |
582 | ||
a43b1583 | 583 | // Add resources from coreResourceList |
6a488035 | 584 | $jsWeight = -9999; |
7266e09b CW |
585 | foreach ($this->coreResourceList() as $item) { |
586 | if (is_array($item)) { | |
587 | $this->addSetting($item); | |
588 | } | |
589 | elseif (substr($item, -2) == 'js') { | |
74227f77 | 590 | // Don't bother looking for ts() calls in packages, there aren't any |
7266e09b CW |
591 | $translate = (substr($item, 0, 3) == 'js/'); |
592 | $this->addScriptFile('civicrm', $item, $jsWeight++, $region, $translate); | |
6a488035 | 593 | } |
a43b1583 | 594 | else { |
7266e09b | 595 | $this->addStyleFile('civicrm', $item, -100, $region); |
6a488035 TO |
596 | } |
597 | } | |
598 | ||
4e56e743 | 599 | // Dynamic localization script |
4cc9b813 | 600 | $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $config->lcMessages, array('r' => $this->getCacheCode())), $jsWeight++, $region); |
4e56e743 | 601 | |
d759bd10 | 602 | // Add global settings |
2aa397bc | 603 | $settings = array( |
353ffa53 | 604 | 'config' => array( |
353ffa53 | 605 | 'isFrontend' => $config->userFrameworkFrontend, |
8d7a9d07 | 606 | ), |
353ffa53 | 607 | ); |
4e56e743 CW |
608 | // Disable profile creation if user lacks permission |
609 | if (!CRM_Core_Permission::check('edit all contacts') && !CRM_Core_Permission::check('add contacts')) { | |
b7ceb253 | 610 | $settings['config']['entityRef']['contactCreate'] = FALSE; |
3d527838 | 611 | } |
4e56e743 | 612 | $this->addSetting($settings); |
3d527838 CW |
613 | |
614 | // Give control of jQuery and _ back to the CMS - this loads last | |
c2777586 | 615 | $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE); |
6a488035 TO |
616 | |
617 | $this->addCoreStyles($region); | |
618 | } | |
619 | return $this; | |
620 | } | |
621 | ||
622 | /** | |
623 | * This will add CiviCRM's standard CSS | |
624 | * | |
625 | * TODO: Separate the functional code (like addStyle/addScript) from the policy code | |
626 | * (like addCoreResources/addCoreStyles). | |
627 | * | |
628 | * @param string $region | |
629 | * @return CRM_Core_Resources | |
630 | */ | |
631 | public function addCoreStyles($region = 'html-header') { | |
632 | if (!isset($this->addedCoreStyles[$region])) { | |
633 | $this->addedCoreStyles[$region] = TRUE; | |
634 | ||
635 | // Load custom or core css | |
636 | $config = CRM_Core_Config::singleton(); | |
637 | if (!empty($config->customCSSURL)) { | |
57018a3e | 638 | $this->addStyleUrl($config->customCSSURL, 99, $region); |
6a488035 | 639 | } |
14f20d22 | 640 | if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'disable_core_css')) { |
6a488035 | 641 | $this->addStyleFile('civicrm', 'css/civicrm.css', -99, $region); |
6a488035 TO |
642 | } |
643 | } | |
644 | return $this; | |
645 | } | |
646 | ||
627668e8 | 647 | /** |
d09edf64 | 648 | * Flushes cached translated strings. |
5badddc3 | 649 | * @return CRM_Core_Resources |
627668e8 CW |
650 | */ |
651 | public function flushStrings() { | |
fd7dc3f3 | 652 | $this->strings->flush(); |
9762f6ff CW |
653 | return $this; |
654 | } | |
655 | ||
6a488035 | 656 | /** |
fd7dc3f3 TO |
657 | * @return CRM_Core_Resources_Strings |
658 | */ | |
659 | public function getStrings() { | |
660 | return $this->strings; | |
6a488035 TO |
661 | } |
662 | ||
19f7e35e | 663 | /** |
8d7a9d07 | 664 | * Create dynamic script for localizing js widgets. |
19f7e35e | 665 | */ |
00be9182 | 666 | public static function outputLocalizationJS() { |
4cc9b813 | 667 | CRM_Core_Page_AJAX::setJsHeaders(); |
a88cf11a | 668 | $config = CRM_Core_Config::singleton(); |
3d527838 CW |
669 | $vars = array( |
670 | 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)), | |
671 | 'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')), | |
672 | 'otherSearch' => json_encode(ts('Enter search term...')), | |
b7ceb253 CW |
673 | 'entityRef' => array( |
674 | 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(), | |
675 | 'filters' => self::getEntityRefFilters(), | |
676 | ), | |
7b83e312 | 677 | 'ajaxPopupsEnabled' => self::singleton()->ajaxPopupsEnabled, |
3d527838 | 678 | ); |
3d4fb0ed | 679 | print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/l10n.js.tpl', $vars); |
4cc9b813 | 680 | CRM_Utils_System::civiExit(); |
c66581f5 CW |
681 | } |
682 | ||
6a488035 | 683 | /** |
d09edf64 | 684 | * List of core resources we add to every CiviCRM page. |
6a488035 | 685 | * |
e8038a7b CW |
686 | * Note: non-compressed versions of .min files will be used in debug mode |
687 | * | |
a43b1583 | 688 | * @return array |
6a488035 | 689 | */ |
dbe0bbc6 CW |
690 | public function coreResourceList() { |
691 | $config = CRM_Core_Config::singleton(); | |
a43b1583 | 692 | |
4968e732 CW |
693 | // Scripts needed by everyone, everywhere |
694 | // FIXME: This is too long; list needs finer-grained segmentation | |
a0c8b50e | 695 | $items = array( |
3c61fc8f CW |
696 | "bower_components/jquery/dist/jquery.min.js", |
697 | "bower_components/jquery-ui/jquery-ui.min.js", | |
e8038a7b | 698 | "bower_components/jquery-ui/themes/smoothness/jquery-ui.min.css", |
1891a856 | 699 | "bower_components/lodash-compat/lodash.min.js", |
e8038a7b CW |
700 | "packages/jquery/plugins/jquery.mousewheel.min.js", |
701 | "bower_components/select2/select2.min.js", | |
702 | "bower_components/select2/select2.min.css", | |
a0c8b50e | 703 | "packages/jquery/plugins/jquery.tableHeader.js", |
e8038a7b CW |
704 | "packages/jquery/plugins/jquery.form.min.js", |
705 | "packages/jquery/plugins/jquery.timeentry.min.js", | |
706 | "packages/jquery/plugins/jquery.blockUI.min.js", | |
707 | "bower_components/datatables/media/js/jquery.dataTables.min.js", | |
708 | "bower_components/datatables/media/css/jquery.dataTables.min.css", | |
709 | "bower_components/jquery-validation/dist/jquery.validate.min.js", | |
710 | "packages/jquery/plugins/jquery.ui.datepicker.validation.min.js", | |
a0c8b50e | 711 | "js/Common.js", |
53f2643c | 712 | "js/crm.ajax.js", |
a43b1583 | 713 | ); |
7c523661 | 714 | // add wysiwyg editor |
72f03b4f | 715 | $editor = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'editor_id'); |
7c523661 | 716 | $items[] = "js/wysiwyg/crm.wysiwyg.js"; |
b608cfb1 TC |
717 | if ($editor == "CKEditor") { |
718 | $items[] = "bower_components/ckeditor/ckeditor.js"; | |
719 | $items[] = "js/wysiwyg/crm.ckeditor.js"; | |
7266e09b CW |
720 | $ckConfig = CRM_Admin_Page_CKEditorConfig::getConfigUrl(); |
721 | if ($ckConfig) { | |
722 | $items[] = array('config' => array('CKEditorCustomConfig' => $ckConfig)); | |
723 | } | |
b608cfb1 | 724 | } |
dbe0bbc6 | 725 | |
4968e732 CW |
726 | // These scripts are only needed by back-office users |
727 | if (CRM_Core_Permission::check('access CiviCRM')) { | |
e8038a7b | 728 | $items[] = "packages/jquery/plugins/jquery.menu.min.js"; |
a2e1885c | 729 | $items[] = "css/navigation.css"; |
e8038a7b CW |
730 | $items[] = "packages/jquery/plugins/jquery.jeditable.min.js"; |
731 | $items[] = "packages/jquery/plugins/jquery.notify.min.js"; | |
4968e732 | 732 | $items[] = "js/jquery/jquery.crmeditable.js"; |
4968e732 CW |
733 | } |
734 | ||
d606fff7 CW |
735 | // JS for multilingual installations |
736 | if (!empty($config->languageLimit) && count($config->languageLimit) > 1 && CRM_Core_Permission::check('translate CiviCRM')) { | |
737 | $items[] = "js/crm.multilingual.js"; | |
738 | } | |
739 | ||
53f2643c CW |
740 | // Enable administrators to edit option lists in a dialog |
741 | if (CRM_Core_Permission::check('administer CiviCRM') && $this->ajaxPopupsEnabled) { | |
742 | $items[] = "js/crm.optionEdit.js"; | |
743 | } | |
744 | ||
dbe0bbc6 CW |
745 | // Add localized jQuery UI files |
746 | if ($config->lcMessages && $config->lcMessages != 'en_US') { | |
747 | // Search for i18n file in order of specificity (try fr-CA, then fr) | |
748 | list($lang) = explode('_', $config->lcMessages); | |
3c61fc8f | 749 | $path = "bower_components/jquery-ui/ui/i18n"; |
dbe0bbc6 | 750 | foreach (array(str_replace('_', '-', $config->lcMessages), $lang) as $language) { |
f2f191fe | 751 | $localizationFile = "$path/datepicker-{$language}.js"; |
dbe0bbc6 CW |
752 | if ($this->getPath('civicrm', $localizationFile)) { |
753 | $items[] = $localizationFile; | |
754 | break; | |
755 | } | |
756 | } | |
757 | } | |
f9f361d0 CW |
758 | |
759 | // CMS-specific resources | |
760 | $config->userSystem->appendCoreResources($items); | |
761 | ||
6a488035 TO |
762 | return $items; |
763 | } | |
156fd9b9 CW |
764 | |
765 | /** | |
a6c01b45 CW |
766 | * @return bool |
767 | * is this page request an ajax snippet? | |
156fd9b9 | 768 | */ |
00be9182 | 769 | public static function isAjaxMode() { |
353ffa53 TO |
770 | return in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array( |
771 | CRM_Core_Smarty::PRINT_SNIPPET, | |
772 | CRM_Core_Smarty::PRINT_NOFORM, | |
8d7a9d07 | 773 | CRM_Core_Smarty::PRINT_JSON, |
353ffa53 | 774 | )); |
156fd9b9 | 775 | } |
b7ceb253 CW |
776 | |
777 | /** | |
f9e31d7f | 778 | * Provide a list of available entityRef filters. |
b7ceb253 CW |
779 | * FIXME: This function doesn't really belong in this class |
780 | * @TODO: Provide a sane way to extend this list for other entities - a hook or?? | |
781 | * @return array | |
782 | */ | |
00be9182 | 783 | public static function getEntityRefFilters() { |
b7ceb253 CW |
784 | $filters = array(); |
785 | ||
786 | $filters['event'] = array( | |
787 | array('key' => 'event_type_id', 'value' => ts('Event Type')), | |
2aa397bc | 788 | array( |
353ffa53 TO |
789 | 'key' => 'start_date', |
790 | 'value' => ts('Start Date'), | |
791 | 'options' => array( | |
792 | array('key' => '{">":"now"}', 'value' => ts('Upcoming')), | |
793 | array('key' => '{"BETWEEN":["now - 3 month","now"]}', 'value' => ts('Past 3 Months')), | |
794 | array('key' => '{"BETWEEN":["now - 6 month","now"]}', 'value' => ts('Past 6 Months')), | |
795 | array('key' => '{"BETWEEN":["now - 1 year","now"]}', 'value' => ts('Past Year')), | |
8d7a9d07 | 796 | ), |
353ffa53 | 797 | ), |
b7ceb253 CW |
798 | ); |
799 | ||
800 | $filters['activity'] = array( | |
801 | array('key' => 'activity_type_id', 'value' => ts('Activity Type')), | |
802 | array('key' => 'status_id', 'value' => ts('Activity Status')), | |
803 | ); | |
804 | ||
b7ceb253 | 805 | $filters['contact'] = array( |
bee6039a | 806 | array('key' => 'contact_type', 'value' => ts('Contact Type')), |
b7ceb253 CW |
807 | array('key' => 'group', 'value' => ts('Group'), 'entity' => 'group_contact'), |
808 | array('key' => 'tag', 'value' => ts('Tag'), 'entity' => 'entity_tag'), | |
809 | array('key' => 'state_province', 'value' => ts('State/Province'), 'entity' => 'address'), | |
810 | array('key' => 'country', 'value' => ts('Country'), 'entity' => 'address'), | |
811 | array('key' => 'gender_id', 'value' => ts('Gender')), | |
812 | array('key' => 'is_deceased', 'value' => ts('Deceased')), | |
813 | ); | |
814 | ||
815 | return $filters; | |
816 | } | |
96025800 | 817 | |
09a4dcd5 CW |
818 | /** |
819 | * In debug mode, look for a non-minified version of this file | |
820 | * | |
821 | * @param string $fileName | |
822 | * @param string $extName | |
823 | */ | |
824 | private function resolveFileName(&$fileName, $extName) { | |
825 | if (CRM_Core_Config::singleton()->debug && strpos($fileName, '.min.') !== FALSE) { | |
826 | $nonMiniFile = str_replace('.min.', '.', $fileName); | |
827 | if ($this->getPath($extName, $nonMiniFile)) { | |
828 | $fileName = $nonMiniFile; | |
829 | } | |
830 | } | |
831 | } | |
832 | ||
6a488035 | 833 | } |