3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * A Page is basically data in a nice pretty format.
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.
29 * The name of the page (auto generated from class name)
36 * The title associated with this page.
43 * A page can have multiple modes. (i.e. displays
44 * a different set of data based on the input
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)
56 protected $_embedded = FALSE;
59 * Are we in print mode? if so we need to modify the display
60 * functionality to do a minimal display :)
64 protected $_print = FALSE;
67 * Cache the smarty template for efficiency reasons
69 * @var CRM_Core_Smarty
71 static protected $_template;
74 * Cache the session for efficiency reasons
76 * @var CRM_Core_Session
78 static protected $_session;
81 * What to return to the client if in ajax mode (snippet=json)
85 public $ajaxResponse = [];
88 * Url path used to reach this page
95 * Should crm.livePage.js be added to the page?
98 public $useLivePageJS;
101 * Variables smarty expects to have set.
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.
108 public $expectedSmartyVariables = [
111 'hookContentPlacement',
112 // required for footer.tpl
114 // required for info.tpl
119 // required for Summary.tpl (contact summary) but seems
120 // likely to be used more broadly to warrant inclusion here.
132 * @param string $title
137 * @return CRM_Core_Page
139 public function __construct($title = NULL, $mode = NULL) {
140 $this->_name
= CRM_Utils_System
::getClassName($this);
141 $this->_title
= $title;
142 $this->_mode
= $mode;
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();
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
);
155 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
156 if (!empty($_REQUEST['snippet'])) {
157 if ($_REQUEST['snippet'] == CRM_Core_Smarty
::PRINT_PDF
) {
158 $this->_print
= CRM_Core_Smarty
::PRINT_PDF
;
160 // FIXME - why does this number not match the constant?
161 elseif ($_REQUEST['snippet'] == 5) {
162 $this->_print
= CRM_Core_Smarty
::PRINT_NOFORM
;
164 // Support 'json' as well as legacy value '6'
165 elseif (in_array($_REQUEST['snippet'], [CRM_Core_Smarty
::PRINT_JSON
, 6])) {
166 $this->_print
= CRM_Core_Smarty
::PRINT_JSON
;
169 $this->_print
= CRM_Core_Smarty
::PRINT_SNIPPET
;
173 // if the request has a reset value, initialize the controller session
174 if (!empty($_REQUEST['reset'])) {
180 * This function takes care of all the things common to all pages.
182 * This typically involves assigning the appropriate smarty variables :)
184 public function run() {
185 if ($this->_embedded
) {
189 self
::$_template->assign('mode', $this->_mode
);
191 $pageTemplateFile = $this->getHookedTemplateFileName();
192 self
::$_template->assign('tplFile', $pageTemplateFile);
194 // invoke the pagRun hook, CRM-3906
195 CRM_Utils_Hook
::pageRun($this);
198 if (in_array($this->_print
, [
199 CRM_Core_Smarty
::PRINT_SNIPPET
,
200 CRM_Core_Smarty
::PRINT_PDF
,
201 CRM_Core_Smarty
::PRINT_NOFORM
,
202 CRM_Core_Smarty
::PRINT_JSON
,
204 $content = self
::$_template->fetch('CRM/common/snippet.tpl');
207 $content = self
::$_template->fetch('CRM/common/print.tpl');
210 CRM_Utils_System
::appendTPLFile($pageTemplateFile,
212 $this->overrideExtraTemplateFileName()
215 //its time to call the hook.
216 CRM_Utils_Hook
::alterContent($content, 'page', $pageTemplateFile, $this);
218 if ($this->_print
== CRM_Core_Smarty
::PRINT_PDF
) {
219 CRM_Utils_PDF_Utils
::html2pdf($content, "{$this->_name}.pdf", FALSE);
221 elseif ($this->_print
== CRM_Core_Smarty
::PRINT_JSON
) {
222 $this->ajaxResponse
['content'] = $content;
223 CRM_Core_Page_AJAX
::returnJsonResponse($this->ajaxResponse
);
228 CRM_Utils_System
::civiExit();
231 $config = CRM_Core_Config
::singleton();
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());
237 // Intermittent alert to admins
238 CRM_Utils_Check
::singleton()->showPeriodicAlerts();
240 if ($this->useLivePageJS
&& Civi
::settings()->get('ajaxPopupsEnabled')) {
241 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header');
244 $content = self
::$_template->fetch('CRM/common/' . strtolower($config->userFramework
) . '.tpl');
246 // Render page header
247 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region
::instance('html-header', FALSE)) {
248 CRM_Utils_System
::addHTMLHead($region->render(''));
250 CRM_Utils_System
::appendTPLFile($pageTemplateFile, $content);
252 //its time to call the hook.
253 CRM_Utils_Hook
::alterContent($content, 'page', $pageTemplateFile, $this);
255 echo CRM_Utils_System
::theme($content, $this->_print
);
259 * Store the variable with the value in the form scope.
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.
265 public function set($name, $value = NULL) {
266 self
::$_session->set($name, $value, $this->_name
);
270 * Get the variable from the form scope.
272 * @param string $name name of the variable
276 public function get($name) {
277 return self
::$_session->get($name, $this->_name
);
281 * Assign value to name in template.
284 * @param mixed $value
287 public function assign($var, $value = NULL) {
288 self
::$_template->assign($var, $value);
292 * Assign value to name in template by reference.
295 * @param mixed $value
296 * (reference) value of variable.
298 public function assign_by_ref($var, &$value) {
299 self
::$_template->assign_by_ref($var, $value);
303 * Appends values to template variables.
305 * @param array|string $tpl_var the template variable name(s)
306 * @param mixed $value
307 * The value to append.
310 public function append($tpl_var, $value = NULL, $merge = FALSE) {
311 self
::$_template->append($tpl_var, $value, $merge);
315 * Returns an array containing template variables.
317 * @param string $name
321 public function get_template_vars($name = NULL) {
322 return self
::$_template->get_template_vars($name);
326 * Destroy all the session state of this page.
328 public function reset() {
329 self
::$_session->resetScope($this->_name
);
333 * Use the form name to create the tpl file name.
337 public function getTemplateFileName() {
339 CRM_Utils_System
::getClassName($this),
341 '_' => DIRECTORY_SEPARATOR
,
342 '\\' => DIRECTORY_SEPARATOR
,
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
351 public function getHookedTemplateFileName() {
352 $pageTemplateFile = $this->getTemplateFileName();
353 CRM_Utils_Hook
::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
354 return $pageTemplateFile;
358 * Default extra tpl file basically just replaces .tpl with .extra.tpl
359 * i.e. we dont override
363 public function overrideExtraTemplateFileName() {
368 * Setter for embedded.
370 * @param bool $embedded
372 public function setEmbedded($embedded) {
373 $this->_embedded
= $embedded;
377 * Getter for embedded.
380 * return the embedded value
382 public function getEmbedded() {
383 return $this->_embedded
;
391 public function setPrint($print) {
392 $this->_print
= $print;
399 * return the print value
401 public function getPrint() {
402 return $this->_print
;
406 * @return CRM_Core_Smarty
408 public static function &getTemplate() {
409 return self
::$_template;
413 * @param string $name
417 public function getVar($name) {
418 return $this->$name ??
NULL;
422 * @param string $name
425 public function setVar($name, $value) {
426 $this->$name = $value;
430 * Assign metadata about fields to the template.
432 * In order to allow the template to format fields we assign information about them to the template.
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.
437 * @param string $entity
438 * The entity being queried.
440 * @throws \CiviCRM_API3_Exception
442 protected function assignFieldMetadataToTemplate($entity) {
443 $fields = civicrm_api3($entity, 'getfields', ['action' => 'get']);
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);
450 $this->assign('fields', $dateFields);
454 * Handy helper to produce the standard markup for an icon with alternative
455 * text for a title and screen readers.
457 * See also the smarty block function `icon`
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.
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.
471 * The whole bit to drop in.
473 public static function crmIcon($icon, $text = NULL, $condition = TRUE, $attribs = []) {
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';
482 $attribs['class'] = implode(' ', array_unique($classes));
484 $standardAttribs = ['aria-hidden' => 'true'];
485 if ($text === NULL ||
$text === '') {
489 $standardAttribs['title'] = $text;
490 $sr = "<span class=\"sr-only\">$text</span>";
495 // Strip out title if $attribs specifies a blank title
496 $attribs = array_merge($standardAttribs, $attribs);
497 foreach ($attribs as $attrib => $val) {
499 $val = htmlspecialchars($val);
500 $attribString .= " $attrib=\"$val\"";
504 return "<i$attribString></i>$sr";