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