Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 32 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
33 | */ |
34 | class CRM_Core_QuickForm_Action_Display extends CRM_Core_QuickForm_Action { | |
35 | ||
36 | /** | |
fe482240 | 37 | * The template to display the required "red" asterick. |
6a488035 TO |
38 | * @var string |
39 | */ | |
40 | static $_requiredTemplate = NULL; | |
41 | ||
42 | /** | |
d09edf64 | 43 | * The template to display error messages inline with the form element. |
6a488035 TO |
44 | * @var string |
45 | */ | |
46 | static $_errorTemplate = NULL; | |
47 | ||
48 | /** | |
d09edf64 | 49 | * Class constructor. |
6a488035 | 50 | * |
6a0b768e TO |
51 | * @param object $stateMachine |
52 | * Reference to state machine object. | |
6a488035 | 53 | * |
77b97be7 | 54 | * @return \CRM_Core_QuickForm_Action_Display |
6a488035 | 55 | */ |
00be9182 | 56 | public function __construct(&$stateMachine) { |
6a488035 TO |
57 | parent::__construct($stateMachine); |
58 | } | |
59 | ||
60 | /** | |
61 | * Processes the request. | |
62 | * | |
6a0b768e TO |
63 | * @param CRM_Core_Form $page |
64 | * CRM_Core_Form the current form-page. | |
65 | * @param string $actionName | |
66 | * Current action name, as one Action object can serve multiple actions. | |
6a488035 | 67 | * |
0b014509 | 68 | * @return object|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 | /** | |
d09edf64 | 103 | * Render the page using a custom templating system. |
6a488035 | 104 | * |
6a0b768e TO |
105 | * @param CRM_Core_Form $page |
106 | * The CRM_Core_Form page. | |
6a488035 | 107 | */ |
00be9182 | 108 | public function renderForm(&$page) { |
6a488035 TO |
109 | $this->_setRenderTemplates($page); |
110 | $template = CRM_Core_Smarty::singleton(); | |
111 | $form = $page->toSmarty(); | |
112 | ||
03a7ec8f | 113 | // Deprecated - use snippet=6 instead of json=1 |
6a488035 | 114 | $json = CRM_Utils_Request::retrieve('json', 'Boolean', CRM_Core_DAO::$_nullObject); |
6a488035 | 115 | if ($json) { |
ecdef330 | 116 | CRM_Utils_JSON::output($form); |
6a488035 TO |
117 | } |
118 | ||
119 | $template->assign('form', $form); | |
120 | $template->assign('isForm', 1); | |
121 | ||
122 | $controller = &$page->controller; | |
baa2d85c CW |
123 | // Stop here if we are in embedded mode. Exception: displaying form errors via ajax |
124 | if ($controller->getEmbedded() && !(!empty($form['errors']) && $controller->_QFResponseType == 'json')) { | |
6a488035 TO |
125 | return; |
126 | } | |
127 | ||
128 | $template->assign('action', $page->getAction()); | |
129 | ||
8aac22c8 | 130 | $pageTemplateFile = $page->getHookedTemplateFileName(); |
6a488035 TO |
131 | $template->assign('tplFile', $pageTemplateFile); |
132 | ||
133 | $content = $template->fetch($controller->getTemplateFile()); | |
134 | ||
9dc21423 | 135 | if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) { |
6a488035 TO |
136 | CRM_Utils_System::addHTMLHead($region->render('')); |
137 | } | |
138 | CRM_Utils_System::appendTPLFile($pageTemplateFile, | |
139 | $content, | |
140 | $page->overrideExtraTemplateFileName() | |
141 | ); | |
142 | ||
143 | //its time to call the hook. | |
144 | CRM_Utils_Hook::alterContent($content, 'form', $pageTemplateFile, $page); | |
145 | ||
146 | $print = $controller->getPrint(); | |
147 | if ($print) { | |
148 | $html = &$content; | |
149 | } | |
150 | else { | |
151 | $html = CRM_Utils_System::theme($content, $print); | |
152 | } | |
153 | ||
154 | if ($controller->_QFResponseType == 'json') { | |
5d92a7e7 CW |
155 | $response = array('content' => $html); |
156 | if (!empty($page->ajaxResponse)) { | |
157 | $response += $page->ajaxResponse; | |
158 | } | |
03a7ec8f CW |
159 | if (!empty($form['errors'])) { |
160 | $response['status'] = 'form_error'; | |
161 | $response['errors'] = $form['errors']; | |
6a488035 | 162 | } |
03a7ec8f | 163 | CRM_Core_Page_AJAX::returnJsonResponse($response); |
6a488035 TO |
164 | } |
165 | ||
166 | if ($print) { | |
167 | if ($print == CRM_Core_Smarty::PRINT_PDF) { | |
168 | CRM_Utils_PDF_Utils::html2pdf( | |
169 | $content, | |
170 | "{$page->_name}.pdf", | |
171 | FALSE, | |
172 | array('paper_size' => 'a3', 'orientation' => 'landscape') | |
173 | ); | |
174 | } | |
175 | else { | |
176 | echo $html; | |
177 | } | |
178 | CRM_Utils_System::civiExit(); | |
179 | } | |
180 | ||
181 | print $html; | |
182 | } | |
183 | ||
184 | /** | |
d09edf64 | 185 | * Set the various rendering templates. |
6a488035 | 186 | * |
6a0b768e TO |
187 | * @param CRM_Core_Form $page |
188 | * The CRM_Core_Form page. | |
6a488035 | 189 | */ |
00be9182 | 190 | public function _setRenderTemplates(&$page) { |
6a488035 TO |
191 | if (self::$_requiredTemplate === NULL) { |
192 | $this->initializeTemplates(); | |
193 | } | |
194 | ||
195 | $renderer = &$page->getRenderer(); | |
196 | ||
197 | $renderer->setRequiredTemplate(self::$_requiredTemplate); | |
198 | $renderer->setErrorTemplate(self::$_errorTemplate); | |
199 | } | |
200 | ||
201 | /** | |
d09edf64 | 202 | * Initialize the various templates. |
6a488035 | 203 | */ |
00be9182 | 204 | public function initializeTemplates() { |
6a488035 TO |
205 | if (self::$_requiredTemplate !== NULL) { |
206 | return; | |
207 | } | |
208 | ||
209 | $config = CRM_Core_Config::singleton(); | |
210 | ||
211 | $templateDir = $config->templateDir; | |
212 | if (is_array($templateDir)) { | |
213 | $templateDir = array_pop($templateDir); | |
214 | } | |
215 | ||
216 | self::$_requiredTemplate = file_get_contents($templateDir . '/CRM/Form/label.tpl'); | |
217 | self::$_errorTemplate = file_get_contents($templateDir . '/CRM/Form/error.tpl'); | |
218 | } | |
96025800 | 219 | |
6a488035 | 220 | } |