AllCoreTables - Fix getClassForTable in multilingual databases
[civicrm-core.git] / CRM / Core / Page.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * A Page is basically data in a nice pretty format.
20 *
21 * Pages should not have any form actions / elements in them. If they
22 * do, make sure you use CRM_Core_Form and the related structures. You can
23 * embed simple forms in Page and do your own form handling.
24 *
25 */
26class CRM_Core_Page {
27
28 /**
29 * The name of the page (auto generated from class name)
30 *
31 * @var string
6a488035
TO
32 */
33 protected $_name;
34
35 /**
fe482240 36 * The title associated with this page.
6a488035
TO
37 *
38 * @var object
6a488035
TO
39 */
40 protected $_title;
41
42 /**
43 * A page can have multiple modes. (i.e. displays
44 * a different set of data based on the input
45 * @var int
6a488035
TO
46 */
47 protected $_mode;
48
49 /**
50 * Is this object being embedded in another object. If
51 * so the display routine needs to not do any work. (The
52 * parent object takes care of the display)
53 *
b67daa72 54 * @var bool
6a488035
TO
55 */
56 protected $_embedded = FALSE;
57
58 /**
59 * Are we in print mode? if so we need to modify the display
60 * functionality to do a minimal display :)
61 *
b67daa72 62 * @var bool
6a488035
TO
63 */
64 protected $_print = FALSE;
65
66 /**
100fef9d 67 * Cache the smarty template for efficiency reasons
6a488035
TO
68 *
69 * @var CRM_Core_Smarty
6a488035
TO
70 */
71 static protected $_template;
72
73 /**
100fef9d 74 * Cache the session for efficiency reasons
6a488035
TO
75 *
76 * @var CRM_Core_Session
6a488035
TO
77 */
78 static protected $_session;
79
fc05b8da
CW
80 /**
81 * What to return to the client if in ajax mode (snippet=json)
82 *
83 * @var array
84 */
be2fb01f 85 public $ajaxResponse = [];
fc05b8da 86
7d93bcc4
CW
87 /**
88 * Url path used to reach this page
89 *
90 * @var array
91 */
be2fb01f 92 public $urlPath = [];
7d93bcc4 93
96f50de2
CW
94 /**
95 * Should crm.livePage.js be added to the page?
96 * @var bool
97 */
98 public $useLivePageJS;
99
40ad8f6a
EM
100 /**
101 * Variables smarty expects to have set.
102 *
103 * We ensure these are assigned (value = NULL) when Smarty is instantiated in
104 * order to avoid e-notices / having to use empty or isset in the template layer.
105 *
106 * @var string[]
107 */
ae9e89a0
EM
108 public $expectedSmartyVariables = [
109 'breadcrumb',
110 'pageTitle',
111 'isForm',
112 'hookContent',
113 'hookContentPlacement',
114 // required for footer.tpl
115 'contactId',
25f044f6
EM
116 // required for info.tpl
117 'infoMessage',
118 'infoTitle',
119 'infoType',
120 'infoOptions',
47b87e6c
EM
121 // required for Summary.tpl (contact summary) but seems
122 // likely to be used more broadly to warrant inclusion here.
123 'context',
ae9e89a0 124 ];
40ad8f6a 125
6a488035 126 /**
fe482240 127 * Class constructor.
6a488035 128 *
6a0b768e
TO
129 * @param string $title
130 * Title of the page.
131 * @param int $mode
132 * Mode of the page.
6a488035
TO
133 *
134 * @return CRM_Core_Page
135 */
00be9182 136 public function __construct($title = NULL, $mode = NULL) {
353ffa53 137 $this->_name = CRM_Utils_System::getClassName($this);
6a488035 138 $this->_title = $title;
353ffa53 139 $this->_mode = $mode;
6a488035
TO
140
141 // let the constructor initialize this, should happen only once
142 if (!isset(self::$_template)) {
143 self::$_template = CRM_Core_Smarty::singleton();
144 self::$_session = CRM_Core_Session::singleton();
145 }
4ab7be1f 146 // Smarty $_template is a static var which persists between tests, so
147 // if something calls clearTemplateVars(), the static still exists but
148 // our ensured variables get blown away, so we need to set them even if
149 // it's already been initialized.
150 self::$_template->ensureVariablesAreAssigned($this->expectedSmartyVariables);
6a488035 151
0e017a41
CW
152 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
153 if (!empty($_REQUEST['snippet'])) {
03a7ec8f 154 if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
6a488035
TO
155 $this->_print = CRM_Core_Smarty::PRINT_PDF;
156 }
03a7ec8f 157 // FIXME - why does this number not match the constant?
0e017a41 158 elseif ($_REQUEST['snippet'] == 5) {
6a488035
TO
159 $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
160 }
fc05b8da 161 // Support 'json' as well as legacy value '6'
be2fb01f 162 elseif (in_array($_REQUEST['snippet'], [CRM_Core_Smarty::PRINT_JSON, 6])) {
0e017a41
CW
163 $this->_print = CRM_Core_Smarty::PRINT_JSON;
164 }
6a488035
TO
165 else {
166 $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
167 }
168 }
169
170 // if the request has a reset value, initialize the controller session
a7488080 171 if (!empty($_REQUEST['reset'])) {
6a488035
TO
172 $this->reset();
173 }
174 }
175
176 /**
8eedd10a 177 * This function takes care of all the things common to all pages.
6a488035 178 *
8eedd10a 179 * This typically involves assigning the appropriate smarty variables :)
6a488035 180 */
00be9182 181 public function run() {
6a488035 182 if ($this->_embedded) {
8d7a9d07 183 return NULL;
6a488035
TO
184 }
185
186 self::$_template->assign('mode', $this->_mode);
187
8aac22c8 188 $pageTemplateFile = $this->getHookedTemplateFileName();
6a488035
TO
189 self::$_template->assign('tplFile', $pageTemplateFile);
190
191 // invoke the pagRun hook, CRM-3906
192 CRM_Utils_Hook::pageRun($this);
193
194 if ($this->_print) {
be2fb01f 195 if (in_array($this->_print, [
353ffa53
TO
196 CRM_Core_Smarty::PRINT_SNIPPET,
197 CRM_Core_Smarty::PRINT_PDF,
198 CRM_Core_Smarty::PRINT_NOFORM,
8d7a9d07 199 CRM_Core_Smarty::PRINT_JSON,
be2fb01f 200 ])) {
6a488035
TO
201 $content = self::$_template->fetch('CRM/common/snippet.tpl');
202 }
203 else {
204 $content = self::$_template->fetch('CRM/common/print.tpl');
205 }
206
207 CRM_Utils_System::appendTPLFile($pageTemplateFile,
208 $content,
209 $this->overrideExtraTemplateFileName()
210 );
211
212 //its time to call the hook.
213 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
214
215 if ($this->_print == CRM_Core_Smarty::PRINT_PDF) {
1097c394 216 CRM_Utils_PDF_Utils::html2pdf($content, "{$this->_name}.pdf", FALSE);
6a488035 217 }
0e017a41 218 elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) {
fc05b8da
CW
219 $this->ajaxResponse['content'] = $content;
220 CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse);
0e017a41 221 }
6a488035
TO
222 else {
223 echo $content;
224 }
225 CRM_Utils_System::civiExit();
226 }
227
228 $config = CRM_Core_Config::singleton();
229
a5dfa653
MWMC
230 // @fixme this is probably the wrong place for this. It is required by jsortable.tpl which is inherited from many page templates.
231 // So we have to add it here to deprecate $config->defaultCurrencySymbol
232 $this->assign('defaultCurrencySymbol', CRM_Core_BAO_Country::defaultCurrencySymbol());
233
5c33ad28 234 // Intermittent alert to admins
c90a093a 235 CRM_Utils_Check::singleton()->showPeriodicAlerts();
8ef12e64 236
84fb7424 237 if ($this->useLivePageJS && Civi::settings()->get('ajaxPopupsEnabled')) {
96ed17aa 238 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header');
6a488035
TO
239 }
240
241 $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl');
242
243 // Render page header
9dc21423 244 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
245 CRM_Utils_System::addHTMLHead($region->render(''));
246 }
247 CRM_Utils_System::appendTPLFile($pageTemplateFile, $content);
248
249 //its time to call the hook.
250 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
251
252 echo CRM_Utils_System::theme($content, $this->_print);
6a488035
TO
253 }
254
255 /**
fe482240 256 * Store the variable with the value in the form scope.
6a488035 257 *
6a0b768e
TO
258 * @param string|array $name name of the variable or an assoc array of name/value pairs
259 * @param mixed $value
260 * Value of the variable if string.
6a488035 261 */
00be9182 262 public function set($name, $value = NULL) {
6a488035
TO
263 self::$_session->set($name, $value, $this->_name);
264 }
265
266 /**
fe482240 267 * Get the variable from the form scope.
6a488035 268 *
8d7a9d07 269 * @param string $name name of the variable
6a488035
TO
270 *
271 * @return mixed
6a488035 272 */
00be9182 273 public function get($name) {
6a488035
TO
274 return self::$_session->get($name, $this->_name);
275 }
276
277 /**
fe482240 278 * Assign value to name in template.
6a488035 279 *
c490a46a 280 * @param string $var
6a0b768e 281 * @param mixed $value
8eedd10a 282 * Value of variable.
6a488035 283 */
00be9182 284 public function assign($var, $value = NULL) {
6a488035
TO
285 self::$_template->assign($var, $value);
286 }
287
288 /**
fe482240 289 * Assign value to name in template by reference.
6a488035 290 *
c490a46a 291 * @param string $var
6a0b768e 292 * @param mixed $value
8eedd10a 293 * (reference) value of variable.
6a488035 294 */
00be9182 295 public function assign_by_ref($var, &$value) {
6a488035
TO
296 self::$_template->assign_by_ref($var, $value);
297 }
298
4a9538ac 299 /**
fe482240 300 * Appends values to template variables.
4a9538ac
CW
301 *
302 * @param array|string $tpl_var the template variable name(s)
6a0b768e
TO
303 * @param mixed $value
304 * The value to append.
4a9538ac
CW
305 * @param bool $merge
306 */
2aa397bc 307 public function append($tpl_var, $value = NULL, $merge = FALSE) {
4a9538ac
CW
308 self::$_template->append($tpl_var, $value, $merge);
309 }
310
311 /**
fe482240 312 * Returns an array containing template variables.
4a9538ac
CW
313 *
314 * @param string $name
fd31fa4c 315 *
4a9538ac
CW
316 * @return array
317 */
2aa397bc 318 public function get_template_vars($name = NULL) {
4a9538ac
CW
319 return self::$_template->get_template_vars($name);
320 }
321
6a488035 322 /**
100fef9d 323 * Destroy all the session state of this page.
6a488035 324 */
00be9182 325 public function reset() {
6a488035
TO
326 self::$_session->resetScope($this->_name);
327 }
328
329 /**
fe482240 330 * Use the form name to create the tpl file name.
6a488035
TO
331 *
332 * @return string
6a488035 333 */
00be9182 334 public function getTemplateFileName() {
9b591d79
TO
335 return strtr(
336 CRM_Utils_System::getClassName($this),
be2fb01f 337 [
9b591d79
TO
338 '_' => DIRECTORY_SEPARATOR,
339 '\\' => DIRECTORY_SEPARATOR,
be2fb01f 340 ]
6a488035
TO
341 ) . '.tpl';
342 }
343
8aac22c8 344 /**
345 * A wrapper for getTemplateFileName that includes calling the hook to
346 * prevent us from having to copy & paste the logic of calling the hook
347 */
00be9182 348 public function getHookedTemplateFileName() {
8aac22c8 349 $pageTemplateFile = $this->getTemplateFileName();
350 CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
351 return $pageTemplateFile;
352 }
353
6a488035
TO
354 /**
355 * Default extra tpl file basically just replaces .tpl with .extra.tpl
356 * i.e. we dont override
357 *
358 * @return string
6a488035 359 */
00be9182 360 public function overrideExtraTemplateFileName() {
6a488035
TO
361 return NULL;
362 }
363
364 /**
fe482240 365 * Setter for embedded.
6a488035 366 *
6a0b768e 367 * @param bool $embedded
6a488035 368 */
00be9182 369 public function setEmbedded($embedded) {
6a488035
TO
370 $this->_embedded = $embedded;
371 }
372
373 /**
fe482240 374 * Getter for embedded.
6a488035 375 *
8d7a9d07 376 * @return bool
a6c01b45 377 * return the embedded value
6a488035 378 */
00be9182 379 public function getEmbedded() {
6a488035
TO
380 return $this->_embedded;
381 }
382
383 /**
fe482240 384 * Setter for print.
6a488035 385 *
6a0b768e 386 * @param bool $print
6a488035 387 */
00be9182 388 public function setPrint($print) {
6a488035
TO
389 $this->_print = $print;
390 }
391
392 /**
fe482240 393 * Getter for print.
6a488035 394 *
8d7a9d07 395 * @return bool
a6c01b45 396 * return the print value
6a488035 397 */
00be9182 398 public function getPrint() {
6a488035
TO
399 return $this->_print;
400 }
401
a0ee3941
EM
402 /**
403 * @return CRM_Core_Smarty
404 */
00be9182 405 public static function &getTemplate() {
6a488035
TO
406 return self::$_template;
407 }
408
a0ee3941 409 /**
100fef9d 410 * @param string $name
a0ee3941
EM
411 *
412 * @return null
413 */
00be9182 414 public function getVar($name) {
2e1f50d6 415 return $this->$name ?? NULL;
6a488035
TO
416 }
417
a0ee3941 418 /**
100fef9d 419 * @param string $name
a0ee3941
EM
420 * @param $value
421 */
00be9182 422 public function setVar($name, $value) {
6a488035
TO
423 $this->$name = $value;
424 }
96025800 425
ed0ca248 426 /**
427 * Assign metadata about fields to the template.
428 *
429 * In order to allow the template to format fields we assign information about them to the template.
430 *
431 * At this stage only date field metadata is assigned as that is the only use-case in play and
432 * we don't want to assign a lot of unneeded data.
433 *
434 * @param string $entity
435 * The entity being queried.
436 *
437 * @throws \CiviCRM_API3_Exception
438 */
439 protected function assignFieldMetadataToTemplate($entity) {
be2fb01f
CW
440 $fields = civicrm_api3($entity, 'getfields', ['action' => 'get']);
441 $dateFields = [];
ed0ca248 442 foreach ($fields['values'] as $fieldName => $fieldMetaData) {
443 if (isset($fieldMetaData['html']) && CRM_Utils_Array::value('type', $fieldMetaData['html']) == 'Select Date') {
444 $dateFields[$fieldName] = CRM_Utils_Date::addDateMetadataToField($fieldMetaData, $fieldMetaData);
445 }
446 }
447 $this->assign('fields', $dateFields);
448 }
449
f4388b57
AH
450 /**
451 * Handy helper to produce the standard markup for an icon with alternative
452 * text for a title and screen readers.
453 *
454 * See also the smarty block function `icon`
455 *
456 * @param string $icon
457 * The class name of the icon to display.
458 * @param string $text
459 * The translated text to display.
460 * @param bool $condition
461 * Whether to display anything at all. This helps simplify code when a
462 * checkmark should appear if something is true.
9de5aa1b
AH
463 * @param array $attribs
464 * Attributes to set or override on the icon element. Any standard
465 * attribute can be unset by setting the value to an empty string.
f4388b57
AH
466 *
467 * @return string
468 * The whole bit to drop in.
469 */
9de5aa1b 470 public static function crmIcon($icon, $text = NULL, $condition = TRUE, $attribs = []) {
f4388b57
AH
471 if (!$condition) {
472 return '';
473 }
9de5aa1b
AH
474
475 // Add icon classes to any that might exist in $attribs
476 $classes = array_key_exists('class', $attribs) ? explode(' ', $attribs['class']) : [];
477 $classes[] = 'crm-i';
478 $classes[] = $icon;
479 $attribs['class'] = implode(' ', array_unique($classes));
480
481 $standardAttribs = ['aria-hidden' => 'true'];
f4388b57
AH
482 if ($text === NULL || $text === '') {
483 $title = $sr = '';
484 }
485 else {
9de5aa1b 486 $standardAttribs['title'] = $text;
f4388b57
AH
487 $sr = "<span class=\"sr-only\">$text</span>";
488 }
9de5aa1b
AH
489
490 // Assemble attribs
491 $attribString = '';
492 // Strip out title if $attribs specifies a blank title
493 $attribs = array_merge($standardAttribs, $attribs);
494 foreach ($attribs as $attrib => $val) {
495 if (strlen($val)) {
496 $val = htmlspecialchars($val);
497 $attribString .= " $attrib=\"$val\"";
498 }
499 }
500
501 return "<i$attribString></i>$sr";
f4388b57
AH
502 }
503
6a488035 504}