[~FEATURE] Fluid (ViewHelpers): Adjusted AbstractFormViewHelper to be able to namespa...
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / FormViewHelper.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License as published by the Free *
8 * Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14 * Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with the script. *
18 * If not, see http://www.gnu.org/licenses/gpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * @package Fluid
25 * @subpackage ViewHelpers
26 * @version $Id: FormViewHelper.php 2177 2009-04-22 22:52:02Z bwaidelich $
27 */
28
29 /**
30 * Form view helper. Generates a <form> Tag.
31 *
32 * = Basic usage =
33 *
34 * Use <f:form> to output an HTML <form> tag which is targeted at the specified action, in the current controller and package.
35 * It will submit the form data via a POST request. If you want to change this, use method="get" as an argument.
36 * <code title="Example">
37 * <f:form action="...">...</f:form>
38 * </code>
39 *
40 * = A complex form with a specified encoding type =
41 *
42 * <code title="Form with enctype set">
43 * <f:form action=".." controller="..." package="..." enctype="multipart/form-data">...</f:form>
44 * </code>
45 *
46 * = A Form which should render a domain object =
47 *
48 * <code title="Binding a domain object to a form">
49 * <f:form action="..." name="customer" object="{customer}">
50 * <f:form.hidden property="id" />
51 * <f:form.textbox property="name" />
52 * </f:form>
53 * </code>
54 * This automatically inserts the value of {customer.name} inside the textbox and adjusts the name of the textbox accordingly.
55 *
56 * @package Fluid
57 * @subpackage ViewHelpers
58 * @version $Id: FormViewHelper.php 2177 2009-04-22 22:52:02Z bwaidelich $
59 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
60 * @scope prototype
61 */
62 class Tx_Fluid_ViewHelpers_FormViewHelper extends Tx_Fluid_ViewHelpers_Form_AbstractFormViewHelper {
63
64 /**
65 * @var string
66 */
67 protected $tagName = 'form';
68
69 /**
70 * Initialize arguments.
71 *
72 * @return void
73 */
74 public function initializeArguments() {
75 $this->registerTagAttribute('enctype', 'string', 'MIME type with which the form is submitted');
76 $this->registerTagAttribute('method', 'string', 'Transfer type (GET or POST)');
77 $this->registerTagAttribute('name', 'string', 'Name of form');
78 $this->registerTagAttribute('onreset', 'string', 'JavaScript: On reset of the form');
79 $this->registerTagAttribute('onsubmit', 'string', 'JavaScript: On submit of the form');
80
81 $this->registerUniversalTagAttributes();
82 }
83
84 /**
85 * Render the form.
86 *
87 * @param string $action Target action
88 * @param array $arguments Arguments
89 * @param string $controller Target controller
90 * @param string $extensionName Target Extension Name (without "tx_" prefix and no underscores). If NULL the current extension name is used
91 * @param string $pluginName Target plugin. If empty, the current plugin name is used
92 * @param integer $pageUid Target page uid
93 * @param array $options typolink options
94 * @param mixed $object Object to use for the form. Use in conjunction with the "property" attribute on the sub tags
95 * @param integer $pageType Target page type
96 * @param string $fieldNamePrefix Prefix that will be added to all field names within this form. If not set the prefix will be tx_yourExtension_plugin
97 * @return string rendered form
98 */
99 public function render($action = NULL, array $arguments = array(), $controller = NULL, $extensionName = NULL, $pluginName = NULL, $pageUid = NULL, array $options = array(), $object = NULL, $pageType = 0, $prefix = NULL) {
100 if ($pageUid === NULL) {
101 $pageUid = $GLOBALS['TSFE']->id;
102 }
103 $URIBuilder = $this->controllerContext->getURIBuilder();
104 $formActionUrl = $URIBuilder->URIFor($pageUid, $action, $arguments, $controller, $extensionName, $pluginName, $options, $pageType);
105 $this->tag->addAttribute('action', $formActionUrl);
106
107 if (strtolower($this->arguments['method']) === 'get') {
108 $this->tag->addAttribute('method', 'get');
109 } else {
110 $this->tag->addAttribute('method', 'post');
111 }
112
113 $this->addFormNameToViewHelperVariableContainer();
114 $this->addFormObjectToViewHelperVariableContainer();
115 $this->addFieldNamePrefixToViewHelperVariableContainer();
116
117 $content = $this->renderHiddenIdentityField();
118 $content .= $this->renderHiddenReferrerFields();
119 $content .= $this->renderChildren();
120 $this->tag->setContent($content);
121
122 $this->removeFieldNamePrefixFromViewHelperVariableContainer();
123 $this->removeFormObjectFromViewHelperVariableContainer();
124 $this->removeFormNameFromViewHelperVariableContainer();
125
126 return $this->tag->render();
127 }
128
129 /**
130 * Renders a hidden form field containing the technical identity of the given object.
131 *
132 * @return string A hidden field containing the Identity (UUID in FLOW3, uid in Extbase) of the given object or NULL if the object is unknown to the persistence framework
133 */
134 protected function renderHiddenIdentityField() {
135 $object = $this->arguments['object'];
136 if (!is_object($object)
137 || !($object instanceof Tx_Extbase_DomainObject_AbstractDomainObject)
138 || ($object->_isNew() && !$object->_isClone())) {
139 return '';
140 }
141 // Intentionally NOT using PersistenceManager::getIdentifierByObject here!!
142 // Using that one breaks re-submission of data in forms in case of an error.
143 $identifier = $object->getUid();
144 if ($identifier === NULL) {
145 return chr(10) . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . chr(10);
146 }
147 return chr(10) . '<input type="hidden" name="'. $this->prefixFieldName($this->arguments['name']) . '[uid]" value="' . $identifier .'" />' . chr(10);
148 }
149
150 /**
151 * Renders hidden form fields for referrer information about
152 * the current controller and action.
153 *
154 * @return string Hidden fields with referrer information
155 * @todo filter out referrer information that is equal to the target (e.g. same packageKey)
156 */
157 protected function renderHiddenReferrerFields() {
158 $request = $this->controllerContext->getRequest();
159 $extensionName = $request->getControllerExtensionName();
160 $controllerName = $request->getControllerName();
161 $actionName = $request->getControllerActionName();
162
163 $result = chr(10);
164 $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[extensionName]') . '" value="' . $extensionName . '" />' . chr(10);
165 $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[controllerName]') . '" value="' . $controllerName . '" />' . chr(10);
166 $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[actionName]') . '" value="' . $actionName . '" />' . chr(10);
167 return $result;
168 }
169
170 /**
171 * Adds the form name to the ViewHelperVariableContainer if the name attribute is specified.
172 *
173 * @return void
174 */
175 protected function addFormNameToViewHelperVariableContainer() {
176 if ($this->arguments->hasArgument('name')) {
177 $this->viewHelperVariableContainer->add('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName', $this->arguments['name']);
178 }
179 }
180
181 /**
182 * Removes the form name from the ViewHelperVariableContainer.
183 *
184 * @return void
185 */
186 protected function removeFormNameFromViewHelperVariableContainer() {
187 if ($this->arguments->hasArgument('name')) {
188 $this->viewHelperVariableContainer->remove('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
189 }
190 }
191
192 /**
193 * Adds the object that is bound to this form to the ViewHelperVariableContainer if the formObject attribute is specified.
194 *
195 * @return void
196 */
197 protected function addFormObjectToViewHelperVariableContainer() {
198 if ($this->arguments->hasArgument('object')) {
199 $this->viewHelperVariableContainer->add('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject', $this->arguments['object']);
200 }
201 }
202
203 /**
204 * Removes the form object from the ViewHelperVariableContainer.
205 *
206 * @return void
207 */
208 protected function removeFormObjectFromViewHelperVariableContainer() {
209 if ($this->arguments->hasArgument('object')) {
210 $this->viewHelperVariableContainer->remove('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject');
211 }
212 }
213
214 /**
215 * Adds the field name prefix to the ViewHelperVariableContainer
216 *
217 * @return void
218 */
219 protected function addFieldNamePrefixToViewHelperVariableContainer() {
220 if ($this->arguments->hasArgument('fieldNamePrefix')) {
221 $fieldNamePrefix = $this->arguments['fieldNamePrefix'];
222 } else {
223 $fieldNamePrefix = $this->getDefaultFieldNamePrefix();
224 }
225 $this->viewHelperVariableContainer->add('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $fieldNamePrefix);
226 }
227
228 /**
229 * Removes field name prefix from the ViewHelperVariableContainer
230 *
231 * @return void
232 */
233 protected function removeFieldNamePrefixFromViewHelperVariableContainer() {
234 $this->viewHelperVariableContainer->remove('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
235 }
236
237 /**
238 * Retrieves the default field name prefix for this form
239 *
240 * @return string default field name prefix
241 */
242 protected function getDefaultFieldNamePrefix() {
243 $request = $this->controllerContext->getRequest();
244 if ($this->arguments->hasArgument('extensionName')) {
245 $extensionName = $this->arguments['extensionName'];
246 } else {
247 $extensionName = $request->getControllerExtensionName();
248 }
249 if ($this->arguments->hasArgument('pluginName')) {
250 $pluginName = $this->arguments['pluginName'];
251 } else {
252 $pluginName = $request->getPluginName();
253 }
254
255 return 'tx_' . strtolower($extensionName) . '_' . strtolower($pluginName);
256 }
257 }
258
259 ?>