[REF][PHP8.1] Permit single quotes within html attributes
[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 *
2beb47b8
BT
62 * @var int|string
63 * Should match a CRM_Core_Smarty::PRINT_* constant,
64 * or equal 0 if not in print mode
6a488035
TO
65 */
66 protected $_print = FALSE;
67
68 /**
100fef9d 69 * Cache the smarty template for efficiency reasons
6a488035
TO
70 *
71 * @var CRM_Core_Smarty
6a488035
TO
72 */
73 static protected $_template;
74
75 /**
100fef9d 76 * Cache the session for efficiency reasons
6a488035
TO
77 *
78 * @var CRM_Core_Session
6a488035
TO
79 */
80 static protected $_session;
81
fc05b8da
CW
82 /**
83 * What to return to the client if in ajax mode (snippet=json)
84 *
85 * @var array
86 */
be2fb01f 87 public $ajaxResponse = [];
fc05b8da 88
7d93bcc4
CW
89 /**
90 * Url path used to reach this page
91 *
92 * @var array
93 */
be2fb01f 94 public $urlPath = [];
7d93bcc4 95
96f50de2
CW
96 /**
97 * Should crm.livePage.js be added to the page?
98 * @var bool
99 */
100 public $useLivePageJS;
101
40ad8f6a
EM
102 /**
103 * Variables smarty expects to have set.
104 *
105 * We ensure these are assigned (value = NULL) when Smarty is instantiated in
106 * order to avoid e-notices / having to use empty or isset in the template layer.
107 *
108 * @var string[]
109 */
ae9e89a0 110 public $expectedSmartyVariables = [
ae9e89a0
EM
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',
be6c2373
EM
124 // for CMSPrint.tpl
125 'urlIsPublic',
126 'breadcrumb',
127 'pageTitle',
128 'isDeleted',
46de5158
EM
129 // Required for footer.tpl,
130 // See ExampleHookTest:testPageOutput.
131 'footer_status_severity',
797adf7c
EM
132 // in 'body.tpl
133 'suppressForm',
134 'beginHookFormElements',
ae9e89a0 135 ];
40ad8f6a 136
6a488035 137 /**
fe482240 138 * Class constructor.
6a488035 139 *
6a0b768e
TO
140 * @param string $title
141 * Title of the page.
142 * @param int $mode
143 * Mode of the page.
6a488035
TO
144 *
145 * @return CRM_Core_Page
146 */
00be9182 147 public function __construct($title = NULL, $mode = NULL) {
353ffa53 148 $this->_name = CRM_Utils_System::getClassName($this);
6a488035 149 $this->_title = $title;
353ffa53 150 $this->_mode = $mode;
6a488035
TO
151
152 // let the constructor initialize this, should happen only once
153 if (!isset(self::$_template)) {
154 self::$_template = CRM_Core_Smarty::singleton();
155 self::$_session = CRM_Core_Session::singleton();
156 }
4ab7be1f 157 // Smarty $_template is a static var which persists between tests, so
158 // if something calls clearTemplateVars(), the static still exists but
159 // our ensured variables get blown away, so we need to set them even if
160 // it's already been initialized.
161 self::$_template->ensureVariablesAreAssigned($this->expectedSmartyVariables);
6a488035 162
0e017a41
CW
163 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
164 if (!empty($_REQUEST['snippet'])) {
03a7ec8f 165 if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) {
6a488035
TO
166 $this->_print = CRM_Core_Smarty::PRINT_PDF;
167 }
03a7ec8f 168 // FIXME - why does this number not match the constant?
0e017a41 169 elseif ($_REQUEST['snippet'] == 5) {
6a488035
TO
170 $this->_print = CRM_Core_Smarty::PRINT_NOFORM;
171 }
fc05b8da 172 // Support 'json' as well as legacy value '6'
be2fb01f 173 elseif (in_array($_REQUEST['snippet'], [CRM_Core_Smarty::PRINT_JSON, 6])) {
0e017a41
CW
174 $this->_print = CRM_Core_Smarty::PRINT_JSON;
175 }
6a488035
TO
176 else {
177 $this->_print = CRM_Core_Smarty::PRINT_SNIPPET;
178 }
179 }
180
181 // if the request has a reset value, initialize the controller session
a7488080 182 if (!empty($_REQUEST['reset'])) {
6a488035
TO
183 $this->reset();
184 }
185 }
186
187 /**
8eedd10a 188 * This function takes care of all the things common to all pages.
6a488035 189 *
8eedd10a 190 * This typically involves assigning the appropriate smarty variables :)
6a488035 191 */
00be9182 192 public function run() {
6a488035 193 if ($this->_embedded) {
8d7a9d07 194 return NULL;
6a488035
TO
195 }
196
197 self::$_template->assign('mode', $this->_mode);
198
8aac22c8 199 $pageTemplateFile = $this->getHookedTemplateFileName();
6a488035
TO
200 self::$_template->assign('tplFile', $pageTemplateFile);
201
42aa5813
BT
202 self::$_template->addExpectedTabHeaderKeys();
203
6a488035
TO
204 // invoke the pagRun hook, CRM-3906
205 CRM_Utils_Hook::pageRun($this);
206
207 if ($this->_print) {
be2fb01f 208 if (in_array($this->_print, [
353ffa53
TO
209 CRM_Core_Smarty::PRINT_SNIPPET,
210 CRM_Core_Smarty::PRINT_PDF,
211 CRM_Core_Smarty::PRINT_NOFORM,
8d7a9d07 212 CRM_Core_Smarty::PRINT_JSON,
be2fb01f 213 ])) {
6a488035
TO
214 $content = self::$_template->fetch('CRM/common/snippet.tpl');
215 }
216 else {
217 $content = self::$_template->fetch('CRM/common/print.tpl');
218 }
219
220 CRM_Utils_System::appendTPLFile($pageTemplateFile,
221 $content,
222 $this->overrideExtraTemplateFileName()
223 );
224
225 //its time to call the hook.
226 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
227
2beb47b8 228 if ($this->_print === CRM_Core_Smarty::PRINT_PDF) {
1097c394 229 CRM_Utils_PDF_Utils::html2pdf($content, "{$this->_name}.pdf", FALSE);
6a488035 230 }
2beb47b8 231 elseif ($this->_print === CRM_Core_Smarty::PRINT_JSON) {
fc05b8da
CW
232 $this->ajaxResponse['content'] = $content;
233 CRM_Core_Page_AJAX::returnJsonResponse($this->ajaxResponse);
0e017a41 234 }
6a488035
TO
235 else {
236 echo $content;
237 }
238 CRM_Utils_System::civiExit();
239 }
240
241 $config = CRM_Core_Config::singleton();
242
a5dfa653
MWMC
243 // @fixme this is probably the wrong place for this. It is required by jsortable.tpl which is inherited from many page templates.
244 // So we have to add it here to deprecate $config->defaultCurrencySymbol
245 $this->assign('defaultCurrencySymbol', CRM_Core_BAO_Country::defaultCurrencySymbol());
246
5c33ad28 247 // Intermittent alert to admins
c90a093a 248 CRM_Utils_Check::singleton()->showPeriodicAlerts();
8ef12e64 249
84fb7424 250 if ($this->useLivePageJS && Civi::settings()->get('ajaxPopupsEnabled')) {
96ed17aa 251 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header');
6a488035
TO
252 }
253
254 $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl');
255
256 // Render page header
9dc21423 257 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
258 CRM_Utils_System::addHTMLHead($region->render(''));
259 }
260 CRM_Utils_System::appendTPLFile($pageTemplateFile, $content);
261
262 //its time to call the hook.
263 CRM_Utils_Hook::alterContent($content, 'page', $pageTemplateFile, $this);
264
265 echo CRM_Utils_System::theme($content, $this->_print);
6a488035
TO
266 }
267
268 /**
fe482240 269 * Store the variable with the value in the form scope.
6a488035 270 *
6a0b768e
TO
271 * @param string|array $name name of the variable or an assoc array of name/value pairs
272 * @param mixed $value
273 * Value of the variable if string.
6a488035 274 */
00be9182 275 public function set($name, $value = NULL) {
6a488035
TO
276 self::$_session->set($name, $value, $this->_name);
277 }
278
279 /**
fe482240 280 * Get the variable from the form scope.
6a488035 281 *
8d7a9d07 282 * @param string $name name of the variable
6a488035
TO
283 *
284 * @return mixed
6a488035 285 */
00be9182 286 public function get($name) {
6a488035
TO
287 return self::$_session->get($name, $this->_name);
288 }
289
290 /**
fe482240 291 * Assign value to name in template.
6a488035 292 *
c490a46a 293 * @param string $var
6a0b768e 294 * @param mixed $value
8eedd10a 295 * Value of variable.
6a488035 296 */
00be9182 297 public function assign($var, $value = NULL) {
6a488035
TO
298 self::$_template->assign($var, $value);
299 }
300
301 /**
fe482240 302 * Assign value to name in template by reference.
6a488035 303 *
c490a46a 304 * @param string $var
6a0b768e 305 * @param mixed $value
8eedd10a 306 * (reference) value of variable.
6a488035 307 */
00be9182 308 public function assign_by_ref($var, &$value) {
6a488035
TO
309 self::$_template->assign_by_ref($var, $value);
310 }
311
4a9538ac 312 /**
fe482240 313 * Appends values to template variables.
4a9538ac
CW
314 *
315 * @param array|string $tpl_var the template variable name(s)
6a0b768e
TO
316 * @param mixed $value
317 * The value to append.
4a9538ac
CW
318 * @param bool $merge
319 */
2aa397bc 320 public function append($tpl_var, $value = NULL, $merge = FALSE) {
4a9538ac
CW
321 self::$_template->append($tpl_var, $value, $merge);
322 }
323
324 /**
fe482240 325 * Returns an array containing template variables.
4a9538ac
CW
326 *
327 * @param string $name
fd31fa4c 328 *
4a9538ac
CW
329 * @return array
330 */
2aa397bc 331 public function get_template_vars($name = NULL) {
4a9538ac
CW
332 return self::$_template->get_template_vars($name);
333 }
334
6a488035 335 /**
100fef9d 336 * Destroy all the session state of this page.
6a488035 337 */
00be9182 338 public function reset() {
6a488035
TO
339 self::$_session->resetScope($this->_name);
340 }
341
342 /**
fe482240 343 * Use the form name to create the tpl file name.
6a488035
TO
344 *
345 * @return string
6a488035 346 */
00be9182 347 public function getTemplateFileName() {
9b591d79
TO
348 return strtr(
349 CRM_Utils_System::getClassName($this),
be2fb01f 350 [
9b591d79
TO
351 '_' => DIRECTORY_SEPARATOR,
352 '\\' => DIRECTORY_SEPARATOR,
be2fb01f 353 ]
6a488035
TO
354 ) . '.tpl';
355 }
356
8aac22c8 357 /**
358 * A wrapper for getTemplateFileName that includes calling the hook to
359 * prevent us from having to copy & paste the logic of calling the hook
360 */
00be9182 361 public function getHookedTemplateFileName() {
8aac22c8 362 $pageTemplateFile = $this->getTemplateFileName();
363 CRM_Utils_Hook::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
364 return $pageTemplateFile;
365 }
366
6a488035
TO
367 /**
368 * Default extra tpl file basically just replaces .tpl with .extra.tpl
369 * i.e. we dont override
370 *
371 * @return string
6a488035 372 */
00be9182 373 public function overrideExtraTemplateFileName() {
6a488035
TO
374 return NULL;
375 }
376
377 /**
fe482240 378 * Setter for embedded.
6a488035 379 *
6a0b768e 380 * @param bool $embedded
6a488035 381 */
00be9182 382 public function setEmbedded($embedded) {
6a488035
TO
383 $this->_embedded = $embedded;
384 }
385
386 /**
fe482240 387 * Getter for embedded.
6a488035 388 *
8d7a9d07 389 * @return bool
a6c01b45 390 * return the embedded value
6a488035 391 */
00be9182 392 public function getEmbedded() {
6a488035
TO
393 return $this->_embedded;
394 }
395
396 /**
fe482240 397 * Setter for print.
6a488035 398 *
2beb47b8
BT
399 * @param int|string $print
400 * Should match a CRM_Core_Smarty::PRINT_* constant,
401 * or equal 0 if not in print mode
402 *
403 * @return void
6a488035 404 */
00be9182 405 public function setPrint($print) {
6a488035
TO
406 $this->_print = $print;
407 }
408
409 /**
fe482240 410 * Getter for print.
6a488035 411 *
2beb47b8
BT
412 * @return int|string
413 * Value matching a CRM_Core_Smarty::PRINT_* constant,
414 * or 0 if not in print mode
6a488035 415 */
00be9182 416 public function getPrint() {
6a488035
TO
417 return $this->_print;
418 }
419
a0ee3941
EM
420 /**
421 * @return CRM_Core_Smarty
422 */
00be9182 423 public static function &getTemplate() {
6a488035
TO
424 return self::$_template;
425 }
426
a0ee3941 427 /**
100fef9d 428 * @param string $name
a0ee3941
EM
429 *
430 * @return null
431 */
00be9182 432 public function getVar($name) {
2e1f50d6 433 return $this->$name ?? NULL;
6a488035
TO
434 }
435
a0ee3941 436 /**
100fef9d 437 * @param string $name
a0ee3941
EM
438 * @param $value
439 */
00be9182 440 public function setVar($name, $value) {
6a488035
TO
441 $this->$name = $value;
442 }
96025800 443
ed0ca248 444 /**
445 * Assign metadata about fields to the template.
446 *
447 * In order to allow the template to format fields we assign information about them to the template.
448 *
449 * At this stage only date field metadata is assigned as that is the only use-case in play and
450 * we don't want to assign a lot of unneeded data.
451 *
452 * @param string $entity
453 * The entity being queried.
454 *
455 * @throws \CiviCRM_API3_Exception
456 */
457 protected function assignFieldMetadataToTemplate($entity) {
be2fb01f
CW
458 $fields = civicrm_api3($entity, 'getfields', ['action' => 'get']);
459 $dateFields = [];
ed0ca248 460 foreach ($fields['values'] as $fieldName => $fieldMetaData) {
461 if (isset($fieldMetaData['html']) && CRM_Utils_Array::value('type', $fieldMetaData['html']) == 'Select Date') {
462 $dateFields[$fieldName] = CRM_Utils_Date::addDateMetadataToField($fieldMetaData, $fieldMetaData);
463 }
464 }
465 $this->assign('fields', $dateFields);
466 }
467
f4388b57
AH
468 /**
469 * Handy helper to produce the standard markup for an icon with alternative
470 * text for a title and screen readers.
471 *
472 * See also the smarty block function `icon`
473 *
474 * @param string $icon
475 * The class name of the icon to display.
476 * @param string $text
477 * The translated text to display.
478 * @param bool $condition
479 * Whether to display anything at all. This helps simplify code when a
480 * checkmark should appear if something is true.
9de5aa1b
AH
481 * @param array $attribs
482 * Attributes to set or override on the icon element. Any standard
483 * attribute can be unset by setting the value to an empty string.
f4388b57
AH
484 *
485 * @return string
486 * The whole bit to drop in.
487 */
9de5aa1b 488 public static function crmIcon($icon, $text = NULL, $condition = TRUE, $attribs = []) {
f4388b57
AH
489 if (!$condition) {
490 return '';
491 }
9de5aa1b
AH
492
493 // Add icon classes to any that might exist in $attribs
494 $classes = array_key_exists('class', $attribs) ? explode(' ', $attribs['class']) : [];
495 $classes[] = 'crm-i';
496 $classes[] = $icon;
497 $attribs['class'] = implode(' ', array_unique($classes));
498
499 $standardAttribs = ['aria-hidden' => 'true'];
f4388b57
AH
500 if ($text === NULL || $text === '') {
501 $title = $sr = '';
502 }
503 else {
9de5aa1b 504 $standardAttribs['title'] = $text;
f4388b57
AH
505 $sr = "<span class=\"sr-only\">$text</span>";
506 }
9de5aa1b
AH
507
508 // Assemble attribs
509 $attribString = '';
510 // Strip out title if $attribs specifies a blank title
511 $attribs = array_merge($standardAttribs, $attribs);
512 foreach ($attribs as $attrib => $val) {
513 if (strlen($val)) {
c77acc83 514 $val = htmlspecialchars($val, ENT_COMPAT);
9de5aa1b
AH
515 $attribString .= " $attrib=\"$val\"";
516 }
517 }
518
519 return "<i$attribString></i>$sr";
f4388b57
AH
520 }
521
5ac3fc42 522 /**
523 * Add an expected smarty variable to the array.
524 *
525 * @param string $elementName
526 */
527 public function addExpectedSmartyVariable(string $elementName): void {
528 $this->expectedSmartyVariables[] = $elementName;
529 }
530
531 /**
532 * Add an expected smarty variable to the array.
533 *
534 * @param array $elementNames
535 */
536 public function addExpectedSmartyVariables(array $elementNames): void {
537 foreach ($elementNames as $elementName) {
538 // Duplicates don't actually matter....
539 $this->addExpectedSmartyVariable($elementName);
540 }
541 }
542
6a488035 543}