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