Merge pull request #15649 from JMAConsulting/core-1346
[civicrm-core.git] / CRM / Core / QuickForm / Action / Display.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 * Redefine the display action.
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
17 */
18class CRM_Core_QuickForm_Action_Display extends CRM_Core_QuickForm_Action {
19
20 /**
fe482240 21 * The template to display the required "red" asterick.
6a488035
TO
22 * @var string
23 */
518fa0ee 24 public static $_requiredTemplate = NULL;
6a488035
TO
25
26 /**
d09edf64 27 * The template to display error messages inline with the form element.
6a488035
TO
28 * @var string
29 */
518fa0ee 30 public static $_errorTemplate = NULL;
6a488035
TO
31
32 /**
d09edf64 33 * Class constructor.
6a488035 34 *
6a0b768e
TO
35 * @param object $stateMachine
36 * Reference to state machine object.
6a488035 37 *
77b97be7 38 * @return \CRM_Core_QuickForm_Action_Display
6a488035 39 */
00be9182 40 public function __construct(&$stateMachine) {
6a488035
TO
41 parent::__construct($stateMachine);
42 }
43
44 /**
45 * Processes the request.
46 *
6a0b768e
TO
47 * @param CRM_Core_Form $page
48 * CRM_Core_Form the current form-page.
49 * @param string $actionName
50 * Current action name, as one Action object can serve multiple actions.
6a488035 51 *
0b014509 52 * @return object|void
6a488035 53 */
00be9182 54 public function perform(&$page, $actionName) {
6a488035
TO
55 $pageName = $page->getAttribute('id');
56
57 // If the original action was 'display' and we have values in container then we load them
58 // BTW, if the page was invalid, we should later call validate() to get the errors
59 list(, $oldName) = $page->controller->getActionName();
60 if ('display' == $oldName) {
61 // If the controller is "modal" we should not allow direct access to a page
62 // unless all previous pages are valid (see also bug #2323)
63 if ($page->controller->isModal() && !$page->controller->isValid($page->getAttribute('id'))) {
64 $target = &$page->controller->getPage($page->controller->findInvalid());
65 return $target->handle('jump');
66 }
67 $data = &$page->controller->container();
68 if (!empty($data['values'][$pageName])) {
69 $page->loadValues($data['values'][$pageName]);
70 $validate = FALSE === $data['valid'][$pageName];
71 }
72 }
73
74 // set "common" defaults and constants
75 $page->controller->applyDefaults($pageName);
76 $page->isFormBuilt() or $page->buildForm();
77 // if we had errors we should show them again
78 if (isset($validate) && $validate) {
79 $page->validate();
80 }
81 //will this work generally as TRUE (i.e., return output)
82 //was default, i.e., FALSE
83 return $this->renderForm($page);
84 }
85
86 /**
d09edf64 87 * Render the page using a custom templating system.
6a488035 88 *
6a0b768e
TO
89 * @param CRM_Core_Form $page
90 * The CRM_Core_Form page.
6a488035 91 */
00be9182 92 public function renderForm(&$page) {
6a488035
TO
93 $this->_setRenderTemplates($page);
94 $template = CRM_Core_Smarty::singleton();
95 $form = $page->toSmarty();
96
03a7ec8f 97 // Deprecated - use snippet=6 instead of json=1
a3d827a7 98 $json = CRM_Utils_Request::retrieve('json', 'Boolean');
6a488035 99 if ($json) {
ecdef330 100 CRM_Utils_JSON::output($form);
6a488035
TO
101 }
102
103 $template->assign('form', $form);
104 $template->assign('isForm', 1);
105
106 $controller = &$page->controller;
baa2d85c
CW
107 // Stop here if we are in embedded mode. Exception: displaying form errors via ajax
108 if ($controller->getEmbedded() && !(!empty($form['errors']) && $controller->_QFResponseType == 'json')) {
6a488035
TO
109 return;
110 }
111
112 $template->assign('action', $page->getAction());
113
8aac22c8 114 $pageTemplateFile = $page->getHookedTemplateFileName();
6a488035
TO
115 $template->assign('tplFile', $pageTemplateFile);
116
117 $content = $template->fetch($controller->getTemplateFile());
118
9dc21423 119 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) {
6a488035
TO
120 CRM_Utils_System::addHTMLHead($region->render(''));
121 }
122 CRM_Utils_System::appendTPLFile($pageTemplateFile,
123 $content,
124 $page->overrideExtraTemplateFileName()
125 );
126
127 //its time to call the hook.
128 CRM_Utils_Hook::alterContent($content, 'form', $pageTemplateFile, $page);
129
130 $print = $controller->getPrint();
131 if ($print) {
132 $html = &$content;
133 }
134 else {
135 $html = CRM_Utils_System::theme($content, $print);
136 }
137
138 if ($controller->_QFResponseType == 'json') {
be2fb01f 139 $response = ['content' => $html];
5d92a7e7
CW
140 if (!empty($page->ajaxResponse)) {
141 $response += $page->ajaxResponse;
142 }
03a7ec8f
CW
143 if (!empty($form['errors'])) {
144 $response['status'] = 'form_error';
145 $response['errors'] = $form['errors'];
6a488035 146 }
03a7ec8f 147 CRM_Core_Page_AJAX::returnJsonResponse($response);
6a488035
TO
148 }
149
150 if ($print) {
151 if ($print == CRM_Core_Smarty::PRINT_PDF) {
152 CRM_Utils_PDF_Utils::html2pdf(
153 $content,
154 "{$page->_name}.pdf",
155 FALSE,
be2fb01f 156 ['paper_size' => 'a3', 'orientation' => 'landscape']
6a488035
TO
157 );
158 }
159 else {
160 echo $html;
161 }
162 CRM_Utils_System::civiExit();
163 }
164
165 print $html;
166 }
167
168 /**
d09edf64 169 * Set the various rendering templates.
6a488035 170 *
6a0b768e
TO
171 * @param CRM_Core_Form $page
172 * The CRM_Core_Form page.
6a488035 173 */
00be9182 174 public function _setRenderTemplates(&$page) {
6a488035
TO
175 if (self::$_requiredTemplate === NULL) {
176 $this->initializeTemplates();
177 }
178
179 $renderer = &$page->getRenderer();
180
181 $renderer->setRequiredTemplate(self::$_requiredTemplate);
182 $renderer->setErrorTemplate(self::$_errorTemplate);
183 }
184
185 /**
d09edf64 186 * Initialize the various templates.
6a488035 187 */
00be9182 188 public function initializeTemplates() {
6a488035
TO
189 if (self::$_requiredTemplate !== NULL) {
190 return;
191 }
192
193 $config = CRM_Core_Config::singleton();
194
195 $templateDir = $config->templateDir;
196 if (is_array($templateDir)) {
197 $templateDir = array_pop($templateDir);
198 }
199
200 self::$_requiredTemplate = file_get_contents($templateDir . '/CRM/Form/label.tpl');
201 self::$_errorTemplate = file_get_contents($templateDir . '/CRM/Form/error.tpl');
202 }
96025800 203
6a488035 204}