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