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