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