TYPO3 CMS  TYPO3_6-2
TypoScriptFactory.php
Go to the documentation of this file.
1 <?php
3 
26 
30  const PROPERTY_DisableContentElement = 'disableContentElement';
31 
36 
40  protected $disableContentElement = FALSE;
41 
48  public function buildModelFromTyposcript(array $typoscript) {
49  if (isset($typoscript[self::PROPERTY_DisableContentElement])) {
50  $this->setDisableContentElement($typoscript[self::PROPERTY_DisableContentElement]);
51  }
52  $this->setLayoutHandler($typoscript);
53  $form = $this->createElement('form', $typoscript);
54  return $form;
55  }
56 
64  $this->disableContentElement = (bool) $disableContentElement;
65  }
66 
76  public function getChildElementsByIntegerKey(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $parentElement, array $typoscript) {
77  if (is_array($typoscript)) {
79  foreach ($keys as $key) {
80  $class = $typoscript[$key];
81  if ((int)$key && strpos($key, '.') === FALSE) {
82  if (isset($typoscript[$key . '.'])) {
83  $elementArguments = $typoscript[$key . '.'];
84  } else {
85  $elementArguments = array();
86  }
87  $this->setElementType($parentElement, $class, $elementArguments);
88  }
89  }
90  } else {
91  throw new \InvalidArgumentException('Container element with id=' . $parentElement->getElementId() . ' has no configuration which means no children.', 1333754854);
92  }
93  }
94 
105  public function setElementType(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $parentElement, $class, array $arguments) {
106  if (in_array($class, \TYPO3\CMS\Form\Utility\FormUtility::getInstance()->getFormObjects())) {
107  $this->addElement($parentElement, $class, $arguments);
108  } elseif ($this->disableContentElement === FALSE) {
109  if ($class[0] === '<') {
110  $key = trim(substr($class, 1));
112  $typoscriptParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser');
113  $oldArguments = $arguments;
114  list($class, $arguments) = $typoscriptParser->getVal($key, $GLOBALS['TSFE']->tmpl->setup);
115  if (is_array($oldArguments) && count($oldArguments)) {
116  $arguments = array_replace_recursive($arguments, $oldArguments);
117  }
118  $GLOBALS['TT']->incStackPointer();
119  $contentObject = array(
120  'cObj' => $class,
121  'cObj.' => $arguments
122  );
123  $this->addElement($parentElement, 'content', $contentObject);
124  $GLOBALS['TT']->decStackPointer();
125  } else {
126  $contentObject = array(
127  'cObj' => $class,
128  'cObj.' => $arguments
129  );
130  $this->addElement($parentElement, 'content', $contentObject);
131  }
132  }
133  }
134 
143  public function addElement(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $parentElement, $class, array $arguments = array()) {
144  $element = $this->createElement($class, $arguments);
145  if (method_exists($parentElement, 'addElement')) {
146  $parentElement->addElement($element);
147  }
148  }
149 
159  public function createElement($class, array $arguments = array()) {
160  $class = strtolower((string) $class);
161  if ($class === 'form') {
162  $className = 'TYPO3\\CMS\\Form\\Domain\\Model\\' . ucfirst($class);
163  } else {
164  $className = 'TYPO3\\CMS\\Form\\Domain\\Model\\Element\\' . ucfirst($class) . 'Element';
165  }
169  $object->setData($arguments['cObj'], $arguments['cObj.']);
170  } elseif ($object->getElementType() === \TYPO3\CMS\Form\Domain\Model\Element\AbstractElement::ELEMENT_TYPE_PLAIN) {
171  $object->setProperties($arguments);
172  } elseif ($object->getElementType() === \TYPO3\CMS\Form\Domain\Model\Element\AbstractElement::ELEMENT_TYPE_FORM) {
173  $object->setData($arguments['data']);
174  $this->reconstituteElement($object, $arguments);
175  } else {
176  throw new \InvalidArgumentException('Element type "' . $object->getElementType() . '" is not supported.', 1333754878);
177  }
178  return $object;
179  }
180 
188  protected function reconstituteElement(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $element, array $arguments = array()) {
189  if (isset($arguments['value.'])) {
190  $cObj = $this->getLocalConentObject();
191  $arguments['value'] = $cObj->stdWrap($arguments['value'], $arguments['value.']);
192  }
193 
194  $this->setAttributes($element, $arguments);
195  $this->setAdditionals($element, $arguments);
196  if (isset($arguments['filters.'])) {
197  $this->setFilters($element, $arguments['filters.']);
198  }
199  $element->setLayout($arguments['layout']);
200  $element->setValue($arguments['value']);
201  $element->setName($arguments['name']);
202  $element->setMessagesFromValidation();
203  $element->setErrorsFromValidation();
204  $element->checkFilterAndSetIncomingDataFromRequest();
205  $this->getChildElementsByIntegerKey($element, $arguments);
206  }
207 
217  public function setAttributes(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $element, array $arguments) {
218  if ($element->hasAllowedAttributes()) {
219  $attributes = $element->getAllowedAttributes();
220  $mandatoryAttributes = $element->getMandatoryAttributes();
221  foreach ($attributes as $attribute => $value) {
222  if (isset($arguments[$attribute]) || isset($arguments[$attribute . '.']) || in_array($attribute, $mandatoryAttributes) || !empty($value)) {
223  if ((string)$arguments[$attribute] !== '') {
224  $value = $arguments[$attribute];
225  } elseif (!empty($arguments[($attribute . '.')])) {
226  $value = $arguments[$attribute . '.'];
227  }
228  try {
229  $element->setAttribute($attribute, $value);
230  } catch (\Exception $exception) {
231  throw new \RuntimeException('Cannot call user function for attribute ' . ucfirst($attribute), 1333754904);
232  }
233  }
234  }
235  } else {
236  throw new \InvalidArgumentException('The element with id=' . $element->getElementId() . ' has no default attributes set.', 1333754925);
237  }
238  }
239 
249  public function setAdditionals(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $element, array $arguments) {
250  if (!empty($arguments)) {
251  if ($element->hasAllowedAdditionals()) {
252  $additionals = $element->getAllowedAdditionals();
253  foreach ($additionals as $additional) {
254  if (isset($arguments[$additional . '.']) || isset($arguments[$additional])) {
255  if (isset($arguments[$additional]) && isset($arguments[$additional . '.'])) {
256  $value = $arguments[$additional . '.'];
257  $type = $arguments[$additional];
258  } elseif (isset($arguments[$additional . '.'])) {
259  $value = $arguments[$additional . '.'];
260  $type = 'TEXT';
261  } else {
262  $value['value'] = $arguments[$additional];
263  $type = 'TEXT';
264  }
265  try {
266  $element->setAdditional($additional, $type, $value);
267  } catch (\Exception $exception) {
268  throw new \RuntimeException('Cannot call user function for additional ' . ucfirst($additional), 1333754941);
269  }
270  }
271  if (isset($arguments['layout.'][$additional]) && $element->additionalIsSet($additional)) {
272  $layout = $arguments['layout.'][$additional];
273  $element->setAdditionalLayout($additional, $layout);
274  }
275  }
276  } else {
277  throw new \InvalidArgumentException('The element with id=' . $element->getElementId() . ' has no additionals set.', 1333754962);
278  }
279  }
280  }
281 
289  protected function setFilters(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $element, array $arguments) {
291  foreach ($keys as $key) {
292  $class = $arguments[$key];
293  if ((int)$key && strpos($key, '.') === FALSE) {
294  $filterArguments = $arguments[$key . '.'];
295  $filter = $element->makeFilter($class, $filterArguments);
296  $element->addFilter($filter);
297  }
298  }
299  }
300 
307  public function setLayoutHandler(array $typoscript) {
309  $layoutHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Layout');
310  $layoutHandler->setLayout($this->getLayoutFromTypoScript($typoscript));
311  return $layoutHandler;
312  }
313 
321  public function getLayoutFromTypoScript($typoscript) {
322  return !empty($typoscript['layout.']) ? $typoscript['layout.'] : array();
323  }
324 
331  public function setRequestHandler($typoscript) {
332  $prefix = isset($typoscript['prefix']) ? $typoscript['prefix'] : '';
333  $method = isset($typoscript['method']) ? $typoscript['method'] : '';
335  $requestHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Request');
336  // singleton
337  $requestHandler->setPrefix($prefix);
338  $requestHandler->setMethod($method);
339  $requestHandler->storeFiles();
340  return $requestHandler;
341  }
342 
351  public function setRules(array $typoscript) {
352  $rulesTyposcript = isset($typoscript['rules.']) ? $typoscript['rules.'] : NULL;
354  $rulesClass = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Utility\\ValidatorUtility', $rulesTyposcript);
355  // singleton
356  if (is_array($rulesTyposcript)) {
358  foreach ($keys as $key) {
359  $class = $rulesTyposcript[$key];
360  if ((int)$key && strpos($key, '.') === FALSE) {
361  $elementArguments = $rulesTyposcript[$key . '.'];
362  $rule = $rulesClass->createRule($class, $elementArguments);
363  $rule->setFieldName($elementArguments['element']);
364  $breakOnError = isset($elementArguments['breakOnError']) ? $elementArguments['breakOnError'] : FALSE;
365  $rulesClass->addRule($rule, $elementArguments['element'], $breakOnError);
366  }
367  }
368  }
369  return $rulesClass;
370  }
371 
377  protected function getLocalConentObject() {
378  if (!isset($this->localContentObject)) {
379  $this->localContentObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
380  }
382  }
383 
384 }
setAttributes(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $element, array $arguments)
reconstituteElement(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $element, array $arguments=array())
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
setFilters(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $element, array $arguments)
addElement(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $parentElement, $class, array $arguments=array())
getChildElementsByIntegerKey(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $parentElement, array $typoscript)
setAdditionals(\TYPO3\CMS\Form\Domain\Model\Element\AbstractElement $element, array $arguments)
static sortedKeyList($setupArr, $acceptOnlyProperties=FALSE)