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