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;
103 * @param string $title
108 * @return CRM_Core_Page
110 public function __construct($title = NULL, $mode = NULL) {
111 $this->_name
= CRM_Utils_System
::getClassName($this);
112 $this->_title
= $title;
113 $this->_mode
= $mode;
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();
121 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
122 if (!empty($_REQUEST['snippet'])) {
123 if ($_REQUEST['snippet'] == CRM_Core_Smarty
::PRINT_PDF
) {
124 $this->_print
= CRM_Core_Smarty
::PRINT_PDF
;
126 // FIXME - why does this number not match the constant?
127 elseif ($_REQUEST['snippet'] == 5) {
128 $this->_print
= CRM_Core_Smarty
::PRINT_NOFORM
;
130 // Support 'json' as well as legacy value '6'
131 elseif (in_array($_REQUEST['snippet'], [CRM_Core_Smarty
::PRINT_JSON
, 6])) {
132 $this->_print
= CRM_Core_Smarty
::PRINT_JSON
;
135 $this->_print
= CRM_Core_Smarty
::PRINT_SNIPPET
;
139 // if the request has a reset value, initialize the controller session
140 if (!empty($_REQUEST['reset'])) {
146 * This function takes care of all the things common to all pages.
148 * This typically involves assigning the appropriate smarty variables :)
150 public function run() {
151 if ($this->_embedded
) {
155 self
::$_template->assign('mode', $this->_mode
);
157 $pageTemplateFile = $this->getHookedTemplateFileName();
158 self
::$_template->assign('tplFile', $pageTemplateFile);
160 // invoke the pagRun hook, CRM-3906
161 CRM_Utils_Hook
::pageRun($this);
164 if (in_array($this->_print
, [
165 CRM_Core_Smarty
::PRINT_SNIPPET
,
166 CRM_Core_Smarty
::PRINT_PDF
,
167 CRM_Core_Smarty
::PRINT_NOFORM
,
168 CRM_Core_Smarty
::PRINT_JSON
,
170 $content = self
::$_template->fetch('CRM/common/snippet.tpl');
173 $content = self
::$_template->fetch('CRM/common/print.tpl');
176 CRM_Utils_System
::appendTPLFile($pageTemplateFile,
178 $this->overrideExtraTemplateFileName()
181 //its time to call the hook.
182 CRM_Utils_Hook
::alterContent($content, 'page', $pageTemplateFile, $this);
184 if ($this->_print
== CRM_Core_Smarty
::PRINT_PDF
) {
185 CRM_Utils_PDF_Utils
::html2pdf($content, "{$this->_name}.pdf", FALSE,
186 ['paper_size' => 'a3', 'orientation' => 'landscape']
189 elseif ($this->_print
== CRM_Core_Smarty
::PRINT_JSON
) {
190 $this->ajaxResponse
['content'] = $content;
191 CRM_Core_Page_AJAX
::returnJsonResponse($this->ajaxResponse
);
196 CRM_Utils_System
::civiExit();
199 $config = CRM_Core_Config
::singleton();
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());
205 // Intermittent alert to admins
206 CRM_Utils_Check
::singleton()->showPeriodicAlerts();
208 if ($this->useLivePageJS
&& Civi
::settings()->get('ajaxPopupsEnabled')) {
209 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header');
212 $content = self
::$_template->fetch('CRM/common/' . strtolower($config->userFramework
) . '.tpl');
214 // Render page header
215 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region
::instance('html-header', FALSE)) {
216 CRM_Utils_System
::addHTMLHead($region->render(''));
218 CRM_Utils_System
::appendTPLFile($pageTemplateFile, $content);
220 //its time to call the hook.
221 CRM_Utils_Hook
::alterContent($content, 'page', $pageTemplateFile, $this);
223 echo CRM_Utils_System
::theme($content, $this->_print
);
227 * Store the variable with the value in the form scope.
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.
233 public function set($name, $value = NULL) {
234 self
::$_session->set($name, $value, $this->_name
);
238 * Get the variable from the form scope.
240 * @param string $name name of the variable
244 public function get($name) {
245 return self
::$_session->get($name, $this->_name
);
249 * Assign value to name in template.
252 * @param mixed $value
255 public function assign($var, $value = NULL) {
256 self
::$_template->assign($var, $value);
260 * Assign value to name in template by reference.
263 * @param mixed $value
264 * (reference) value of variable.
266 public function assign_by_ref($var, &$value) {
267 self
::$_template->assign_by_ref($var, $value);
271 * Appends values to template variables.
273 * @param array|string $tpl_var the template variable name(s)
274 * @param mixed $value
275 * The value to append.
278 public function append($tpl_var, $value = NULL, $merge = FALSE) {
279 self
::$_template->append($tpl_var, $value, $merge);
283 * Returns an array containing template variables.
285 * @param string $name
289 public function get_template_vars($name = NULL) {
290 return self
::$_template->get_template_vars($name);
294 * Destroy all the session state of this page.
296 public function reset() {
297 self
::$_session->resetScope($this->_name
);
301 * Use the form name to create the tpl file name.
305 public function getTemplateFileName() {
307 CRM_Utils_System
::getClassName($this),
309 '_' => DIRECTORY_SEPARATOR
,
310 '\\' => DIRECTORY_SEPARATOR
,
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
319 public function getHookedTemplateFileName() {
320 $pageTemplateFile = $this->getTemplateFileName();
321 CRM_Utils_Hook
::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
322 return $pageTemplateFile;
326 * Default extra tpl file basically just replaces .tpl with .extra.tpl
327 * i.e. we dont override
331 public function overrideExtraTemplateFileName() {
336 * Setter for embedded.
338 * @param bool $embedded
340 public function setEmbedded($embedded) {
341 $this->_embedded
= $embedded;
345 * Getter for embedded.
348 * return the embedded value
350 public function getEmbedded() {
351 return $this->_embedded
;
359 public function setPrint($print) {
360 $this->_print
= $print;
367 * return the print value
369 public function getPrint() {
370 return $this->_print
;
374 * @return CRM_Core_Smarty
376 public static function &getTemplate() {
377 return self
::$_template;
381 * @param string $name
385 public function getVar($name) {
386 return $this->$name ??
NULL;
390 * @param string $name
393 public function setVar($name, $value) {
394 $this->$name = $value;
398 * Assign metadata about fields to the template.
400 * In order to allow the template to format fields we assign information about them to the template.
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.
405 * @param string $entity
406 * The entity being queried.
408 * @throws \CiviCRM_API3_Exception
410 protected function assignFieldMetadataToTemplate($entity) {
411 $fields = civicrm_api3($entity, 'getfields', ['action' => 'get']);
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);
418 $this->assign('fields', $dateFields);
422 * Handy helper to produce the standard markup for an icon with alternative
423 * text for a title and screen readers.
425 * See also the smarty block function `icon`
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.
436 * The whole bit to drop in.
438 public static function crmIcon($icon, $text = NULL, $condition = TRUE) {
442 if ($text === NULL ||
$text === '') {
446 $text = htmlspecialchars($text);
447 $title = " title=\"$text\"";
448 $sr = "<span class=\"sr-only\">$text</span>";
450 return "<i class=\"crm-i $icon\"$title></i>$sr";