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