[~TASK] Fluid (ViewHelpers): Added some checks to make form view helper usable withou...
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / AbstractFormViewHelper.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 Lesser General Public License as published by the *
8 * Free 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 Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * Abstract Form View Helper. Bundles functionality related to direct property access of objects in other Form ViewHelpers.
25 *
26 * If you set the "property" attribute to the name of the property to resolve from the object, this class will
27 * automatically set the name and value of a form element.
28 *
29 * @version $Id: AbstractFormViewHelper.php 3336 2009-10-21 16:52:50Z bwaidelich $
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 * @scope prototype
32 */
33 abstract class Tx_Fluid_ViewHelpers_Form_AbstractFormViewHelper extends Tx_Fluid_Core_ViewHelper_TagBasedViewHelper {
34
35 /**
36 * @var \Tx_Extbase_Persistence_ManagerInterface
37 */
38 protected $persistenceManager;
39
40 /**
41 * Injects the FLOW3 Persistence Manager
42 *
43 * @param Tx_Extbase_Persistence_ManagerInterface $persistenceManager
44 * @return void
45 * @author Robert Lemke <robert@typo3.org>
46 */
47 public function injectPersistenceManager(Tx_Extbase_Persistence_ManagerInterface $persistenceManager) {
48 $this->persistenceManager = $persistenceManager;
49 }
50
51 /**
52 * Prefixes / namespaces the given name with the form field prefix
53 *
54 * @param string $fieldName field name to be prefixed
55 * @return string namespaced field name
56 */
57 protected function prefixFieldName($fieldName) {
58 if ($fieldName === NULL || $fieldName === '') {
59 return '';
60 }
61 if (!$this->viewHelperVariableContainer->exists('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')) {
62 return $fieldName;
63 }
64 $fieldNamePrefix = (string)$this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
65 if ($fieldNamePrefix === '') {
66 return $fieldName;
67 }
68 $fieldNameSegments = explode('[', $fieldName, 2);
69 $fieldName = $fieldNamePrefix . '[' . $fieldNameSegments[0] . ']';
70 if (count($fieldNameSegments) > 1) {
71 $fieldName .= '[' . $fieldNameSegments[1];
72 }
73 return $fieldName;
74 }
75
76 /**
77 * Renders a hidden form field containing the technical identity of the given object.
78 *
79 * @return string A hidden field containing the Identity (UID in FLOW3, uid in Extbase) of the given object or NULL if the object is unknown to the persistence framework
80 * @author Robert Lemke <robert@typo3.org>
81 * @author Karsten Dambekalns <karsten@typo3.org>
82 * @author Bastian Waidelich <bastian@typo3.org>
83 * @see Tx_Fluid_MVC_Controller_Argument::setValue()
84 */
85 protected function renderHiddenIdentityField($object, $name) {
86 if (!is_object($object)
87 || !($object instanceof Tx_Extbase_DomainObject_AbstractDomainObject)
88 || ($object->_isNew() && !$object->_isClone())
89 ){
90 return '';
91 }
92 // Intentionally NOT using PersistenceManager::getIdentifierByObject here!!
93 // Using that one breaks re-submission of data in forms in case of an error.
94 $identifier = $object->getUid();
95 if ($identifier === NULL) {
96 return chr(10) . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . chr(10);
97 }
98 $name = $this->prefixFieldName($name) . '[__identity]';
99 $this->registerFieldNameForFormTokenGeneration($name);
100
101 return chr(10) . '<input type="hidden" name="'. $name . '" value="' . $identifier .'" />' . chr(10);
102 }
103
104 /**
105 * Register a field name for inclusion in the HMAC / Form Token generation
106 *
107 * @param string $fieldName name of the field to register
108 * @return void
109 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
110 * @author Bastian Waidelich <bastian@typo3.org>
111 */
112 protected function registerFieldNameForFormTokenGeneration($fieldName) {
113 if ($this->viewHelperVariableContainer->exists('Tx_Fluid_ViewHelpers_FormViewHelper', 'formFieldNames')) {
114 $formFieldNames = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formFieldNames');
115 } else {
116 $formFieldNames = array();
117 }
118 $formFieldNames[] = $fieldName;
119 $this->viewHelperVariableContainer->addOrUpdate('Tx_Fluid_ViewHelpers_FormViewHelper', 'formFieldNames', $formFieldNames);
120 }
121 }
122
123 ?>