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