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