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