e14ef0fb72d1848dede76d52ae93eea272599735
[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 3313 2009-10-12 05:57:02Z sebastian $
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 $fieldNamePrefix = (string)$this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
62 if ($fieldNamePrefix === '') {
63 return $fieldName;
64 }
65 $fieldNameSegments = explode('[', $fieldName, 2);
66 $fieldName = $fieldNamePrefix . '[' . $fieldNameSegments[0] . ']';
67 if (count($fieldNameSegments) > 1) {
68 $fieldName .= '[' . $fieldNameSegments[1];
69 }
70 return $fieldName;
71 }
72
73 /**
74 * Renders a hidden form field containing the technical identity of the given object.
75 *
76 * @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
77 * @author Robert Lemke <robert@typo3.org>
78 * @author Karsten Dambekalns <karsten@typo3.org>
79 * @author Bastian Waidelich <bastian@typo3.org>
80 * @see Tx_Fluid_MVC_Controller_Argument::setValue()
81 */
82 protected function renderHiddenIdentityField($object, $name) {
83 if (!is_object($object)
84 || !($object instanceof Tx_Extbase_DomainObject_AbstractDomainObject)
85 || ($object->_isNew() && !$object->_isClone())
86 ){
87 return '';
88 }
89 // Intentionally NOT using PersistenceManager::getIdentifierByObject here!!
90 // Using that one breaks re-submission of data in forms in case of an error.
91 $identifier = $object->getUid();
92 if ($identifier === NULL) {
93 return chr(10) . '<!-- Object of type ' . get_class($object) . ' is without identity -->' . chr(10);
94 }
95 $name = $this->prefixFieldName($name) . '[__identity]';
96 $this->registerFieldNameForFormTokenGeneration($name);
97
98 return chr(10) . '<input type="hidden" name="'. $name . '" value="' . $identifier .'" />' . chr(10);
99 }
100
101 /**
102 * Register a field name for inclusion in the HMAC / Form Token generation
103 *
104 * @param string $fieldName name of the field to register
105 * @return void
106 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
107 */
108 protected function registerFieldNameForFormTokenGeneration($fieldName) {
109 $formFieldNames = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formFieldNames');
110 $formFieldNames[] = $fieldName;
111 $this->viewHelperVariableContainer->addOrUpdate('Tx_Fluid_ViewHelpers_FormViewHelper', 'formFieldNames', $formFieldNames);
112 }
113 }
114
115 ?>