3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * A Page is basically data in a nice pretty format.
37 * Pages should not have any form actions / elements in them. If they
38 * do, make sure you use CRM_Core_Form and the related structures. You can
39 * embed simple forms in Page and do your own form handling.
45 * The name of the page (auto generated from class name)
52 * The title associated with this page.
59 * A page can have multiple modes. (i.e. displays
60 * a different set of data based on the input
66 * Is this object being embedded in another object. If
67 * so the display routine needs to not do any work. (The
68 * parent object takes care of the display)
72 protected $_embedded = FALSE;
75 * Are we in print mode? if so we need to modify the display
76 * functionality to do a minimal display :)
80 protected $_print = FALSE;
83 * Cache the smarty template for efficiency reasons
85 * @var CRM_Core_Smarty
87 static protected $_template;
90 * Cache the session for efficiency reasons
92 * @var CRM_Core_Session
94 static protected $_session;
97 * What to return to the client if in ajax mode (snippet=json)
101 public $ajaxResponse = [];
104 * Url path used to reach this page
108 public $urlPath = [];
111 * Should crm.livePage.js be added to the page?
114 public $useLivePageJS;
119 * @param string $title
124 * @return CRM_Core_Page
126 public function __construct($title = NULL, $mode = NULL) {
127 $this->_name
= CRM_Utils_System
::getClassName($this);
128 $this->_title
= $title;
129 $this->_mode
= $mode;
131 // let the constructor initialize this, should happen only once
132 if (!isset(self
::$_template)) {
133 self
::$_template = CRM_Core_Smarty
::singleton();
134 self
::$_session = CRM_Core_Session
::singleton();
137 // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print?
138 if (!empty($_REQUEST['snippet'])) {
139 if ($_REQUEST['snippet'] == CRM_Core_Smarty
::PRINT_PDF
) {
140 $this->_print
= CRM_Core_Smarty
::PRINT_PDF
;
142 // FIXME - why does this number not match the constant?
143 elseif ($_REQUEST['snippet'] == 5) {
144 $this->_print
= CRM_Core_Smarty
::PRINT_NOFORM
;
146 // Support 'json' as well as legacy value '6'
147 elseif (in_array($_REQUEST['snippet'], [CRM_Core_Smarty
::PRINT_JSON
, 6])) {
148 $this->_print
= CRM_Core_Smarty
::PRINT_JSON
;
151 $this->_print
= CRM_Core_Smarty
::PRINT_SNIPPET
;
155 // if the request has a reset value, initialize the controller session
156 if (!empty($_REQUEST['reset'])) {
162 * This function takes care of all the things common to all pages.
164 * This typically involves assigning the appropriate smarty variables :)
166 public function run() {
167 if ($this->_embedded
) {
171 self
::$_template->assign('mode', $this->_mode
);
173 $pageTemplateFile = $this->getHookedTemplateFileName();
174 self
::$_template->assign('tplFile', $pageTemplateFile);
176 // invoke the pagRun hook, CRM-3906
177 CRM_Utils_Hook
::pageRun($this);
180 if (in_array($this->_print
, [
181 CRM_Core_Smarty
::PRINT_SNIPPET
,
182 CRM_Core_Smarty
::PRINT_PDF
,
183 CRM_Core_Smarty
::PRINT_NOFORM
,
184 CRM_Core_Smarty
::PRINT_JSON
,
186 $content = self
::$_template->fetch('CRM/common/snippet.tpl');
189 $content = self
::$_template->fetch('CRM/common/print.tpl');
192 CRM_Utils_System
::appendTPLFile($pageTemplateFile,
194 $this->overrideExtraTemplateFileName()
197 //its time to call the hook.
198 CRM_Utils_Hook
::alterContent($content, 'page', $pageTemplateFile, $this);
200 if ($this->_print
== CRM_Core_Smarty
::PRINT_PDF
) {
201 CRM_Utils_PDF_Utils
::html2pdf($content, "{$this->_name}.pdf", FALSE,
202 ['paper_size' => 'a3', 'orientation' => 'landscape']
205 elseif ($this->_print
== CRM_Core_Smarty
::PRINT_JSON
) {
206 $this->ajaxResponse
['content'] = $content;
207 CRM_Core_Page_AJAX
::returnJsonResponse($this->ajaxResponse
);
212 CRM_Utils_System
::civiExit();
215 $config = CRM_Core_Config
::singleton();
217 // @fixme this is probably the wrong place for this. It is required by jsortable.tpl which is inherited from many page templates.
218 // So we have to add it here to deprecate $config->defaultCurrencySymbol
219 $this->assign('defaultCurrencySymbol', CRM_Core_BAO_Country
::defaultCurrencySymbol());
221 // Intermittent alert to admins
222 CRM_Utils_Check
::singleton()->showPeriodicAlerts();
224 if ($this->useLivePageJS
&& Civi
::settings()->get('ajaxPopupsEnabled')) {
225 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header');
228 $content = self
::$_template->fetch('CRM/common/' . strtolower($config->userFramework
) . '.tpl');
230 // Render page header
231 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region
::instance('html-header', FALSE)) {
232 CRM_Utils_System
::addHTMLHead($region->render(''));
234 CRM_Utils_System
::appendTPLFile($pageTemplateFile, $content);
236 //its time to call the hook.
237 CRM_Utils_Hook
::alterContent($content, 'page', $pageTemplateFile, $this);
239 echo CRM_Utils_System
::theme($content, $this->_print
);
243 * Store the variable with the value in the form scope.
245 * @param string|array $name name of the variable or an assoc array of name/value pairs
246 * @param mixed $value
247 * Value of the variable if string.
249 public function set($name, $value = NULL) {
250 self
::$_session->set($name, $value, $this->_name
);
254 * Get the variable from the form scope.
256 * @param string $name name of the variable
260 public function get($name) {
261 return self
::$_session->get($name, $this->_name
);
265 * Assign value to name in template.
268 * @param mixed $value
271 public function assign($var, $value = NULL) {
272 self
::$_template->assign($var, $value);
276 * Assign value to name in template by reference.
279 * @param mixed $value
280 * (reference) value of variable.
282 public function assign_by_ref($var, &$value) {
283 self
::$_template->assign_by_ref($var, $value);
287 * Appends values to template variables.
289 * @param array|string $tpl_var the template variable name(s)
290 * @param mixed $value
291 * The value to append.
294 public function append($tpl_var, $value = NULL, $merge = FALSE) {
295 self
::$_template->append($tpl_var, $value, $merge);
299 * Returns an array containing template variables.
301 * @param string $name
305 public function get_template_vars($name = NULL) {
306 return self
::$_template->get_template_vars($name);
310 * Destroy all the session state of this page.
312 public function reset() {
313 self
::$_session->resetScope($this->_name
);
317 * Use the form name to create the tpl file name.
321 public function getTemplateFileName() {
323 CRM_Utils_System
::getClassName($this),
325 '_' => DIRECTORY_SEPARATOR
,
326 '\\' => DIRECTORY_SEPARATOR
,
332 * A wrapper for getTemplateFileName that includes calling the hook to
333 * prevent us from having to copy & paste the logic of calling the hook
335 public function getHookedTemplateFileName() {
336 $pageTemplateFile = $this->getTemplateFileName();
337 CRM_Utils_Hook
::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
338 return $pageTemplateFile;
342 * Default extra tpl file basically just replaces .tpl with .extra.tpl
343 * i.e. we dont override
347 public function overrideExtraTemplateFileName() {
352 * Setter for embedded.
354 * @param bool $embedded
356 public function setEmbedded($embedded) {
357 $this->_embedded
= $embedded;
361 * Getter for embedded.
364 * return the embedded value
366 public function getEmbedded() {
367 return $this->_embedded
;
375 public function setPrint($print) {
376 $this->_print
= $print;
383 * return the print value
385 public function getPrint() {
386 return $this->_print
;
390 * @return CRM_Core_Smarty
392 public static function &getTemplate() {
393 return self
::$_template;
397 * @param string $name
401 public function getVar($name) {
402 return isset($this->$name) ?
$this->$name : NULL;
406 * @param string $name
409 public function setVar($name, $value) {
410 $this->$name = $value;
414 * Assign metadata about fields to the template.
416 * In order to allow the template to format fields we assign information about them to the template.
418 * At this stage only date field metadata is assigned as that is the only use-case in play and
419 * we don't want to assign a lot of unneeded data.
421 * @param string $entity
422 * The entity being queried.
424 * @throws \CiviCRM_API3_Exception
426 protected function assignFieldMetadataToTemplate($entity) {
427 $fields = civicrm_api3($entity, 'getfields', ['action' => 'get']);
429 foreach ($fields['values'] as $fieldName => $fieldMetaData) {
430 if (isset($fieldMetaData['html']) && CRM_Utils_Array
::value('type', $fieldMetaData['html']) == 'Select Date') {
431 $dateFields[$fieldName] = CRM_Utils_Date
::addDateMetadataToField($fieldMetaData, $fieldMetaData);
434 $this->assign('fields', $dateFields);