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