Fluid: Added typolinkViewHelper and a basic actionViewHelper; fixed many argument...
authorSebastian Kurfürst <sebastian@typo3.org>
Fri, 27 Mar 2009 00:16:50 +0000 (00:16 +0000)
committerSebastian Kurfürst <sebastian@typo3.org>
Fri, 27 Mar 2009 00:16:50 +0000 (00:16 +0000)
typo3/sysext/fluid/Classes/Compatibility/ObjectFactory.php
typo3/sysext/fluid/Classes/Core/AbstractViewHelper.php
typo3/sysext/fluid/Classes/Core/ArgumentDefinition.php
typo3/sysext/fluid/Classes/Core/SyntaxTree/ViewHelperNode.php
typo3/sysext/fluid/Classes/Core/TagBasedViewHelper.php
typo3/sysext/fluid/Classes/ViewHelpers/ActionlinkViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/TypolinkViewHelper.php [new file with mode: 0644]

index 75db421..5789a95 100644 (file)
@@ -24,7 +24,7 @@ class Tx_Fluid_Compatibility_ObjectFactory implements t3lib_Singleton {
        protected $injectors = array(
                'Tx_Fluid_Core_AbstractViewHelper' => array(
                        'injectValidatorResolver' => 'Tx_Fluid_Compatibility_Validation_ValidatorResolver',
-                       'injectReflectionService' => 'Tx_Fluid_Compatibility_ReflectionService'
+                       'injectReflectionService' => 'Tx_ExtBase_Reflection_Service'
                ),
                'Tx_Fluid_Core_ParsingState' => array('injectVariableContainer' => 'Tx_Fluid_Core_VariableContainer'),
                'Tx_Fluid_Core_TemplateParser' => array('injectObjectFactory' => 'Tx_Fluid_Compatibility_ObjectFactory'),
index ac21746..8f3c505 100644 (file)
@@ -16,7 +16,7 @@
 /**
  * @package Fluid
  * @subpackage Core
- * @version $Id: AbstractViewHelper.php 2082 2009-03-26 14:24:59Z sebastian $
+ * @version $Id: AbstractViewHelper.php 2089 2009-03-26 17:01:48Z sebastian $
  */
 
 /**
  *
  * @package Fluid
  * @subpackage Core
- * @version $Id: AbstractViewHelper.php 2082 2009-03-26 14:24:59Z sebastian $
+ * @version $Id: AbstractViewHelper.php 2089 2009-03-26 17:01:48Z sebastian $
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
  * @scope prototype
  */
 abstract class Tx_Fluid_Core_AbstractViewHelper implements Tx_Fluid_Core_ViewHelperInterface {
 
+       /**
+        * TRUE if arguments have already been initialized
+        * @var boolean
+        */
+       private $argumentsInitialized = FALSE;
+
        /**
         * Stores all Tx_Fluid_ArgumentDefinition instances
         * @var array
@@ -62,7 +68,7 @@ abstract class Tx_Fluid_Core_AbstractViewHelper implements Tx_Fluid_Core_ViewHel
 
        /**
         * Reflection service
-        * @var Tx_Fluid_Compatibility_ReflectionService
+        * @var Tx_ExtBase_Reflection_Service
         */
        protected $reflectionService;
 
@@ -78,11 +84,11 @@ abstract class Tx_Fluid_Core_AbstractViewHelper implements Tx_Fluid_Core_ViewHel
 
        /**
         * Inject a Reflection service
-        * @param Tx_Fluid_Compatibility_ReflectionService $reflectionService Reflection service
+        * @param Tx_ExtBase_Reflection_Service $reflectionService Reflection service
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         * @internal
         */
-       public function injectReflectionService(Tx_Fluid_Compatibility_ReflectionService $reflectionService) {
+       public function injectReflectionService(Tx_ExtBase_Reflection_Service $reflectionService) {
                $this->reflectionService = $reflectionService;
        }
 
@@ -94,12 +100,13 @@ abstract class Tx_Fluid_Core_AbstractViewHelper implements Tx_Fluid_Core_ViewHel
         * @param string $type Type of the argument
         * @param string $description Description of the argument
         * @param boolean $required If TRUE, argument is required. Defaults to FALSE.
+        * @param mixed $defaultValue Default value of argument
         * @return Tx_Fluid_Core_AbstractViewHelper $this, to allow chaining.
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         * @todo Component manager usage!
         */
-       protected function registerArgument($name, $type, $description, $required = FALSE) {
-               $this->argumentDefinitions[$name] = new Tx_Fluid_Core_ArgumentDefinition($name, $type, $description, $required);
+       protected function registerArgument($name, $type, $description, $required = FALSE, $defaultValue = NULL) {
+               $this->argumentDefinitions[$name] = new Tx_Fluid_Core_ArgumentDefinition($name, $type, $description, $required, $defaultValue);
                return $this;
        }
 
@@ -135,8 +142,11 @@ abstract class Tx_Fluid_Core_AbstractViewHelper implements Tx_Fluid_Core_ViewHel
         * @internal
         */
        public function prepareArguments() {
-               $this->registerRenderMethodArguments();
-               $this->initializeArguments();
+               if (!$this->argumentsInitialized) {
+                       $this->registerRenderMethodArguments();
+                       $this->initializeArguments();
+                       $this->argumentsInitialized = TRUE;
+               }
                return $this->argumentDefinitions;
        }
 
@@ -147,6 +157,7 @@ abstract class Tx_Fluid_Core_AbstractViewHelper implements Tx_Fluid_Core_ViewHel
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         */
        private function registerRenderMethodArguments() {
+
                $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), 'render');
 
                $methodTags = $this->reflectionService->getMethodTagsValues(get_class($this), 'render');
@@ -157,10 +168,11 @@ abstract class Tx_Fluid_Core_AbstractViewHelper implements Tx_Fluid_Core_ViewHel
                }
 
                $i = 0;
-               if (!count($methodParameters)) return;
+               if (!count($methodParameters)) return array();
+
+               $output = array();
                foreach ($methodParameters as $parameterName => $parameterInfo) {
                        $dataType = 'Text';
-
                        if (isset($parameterInfo['type'])) {
                                $dataType = $parameterInfo['type'];
                        } elseif ($parameterInfo['array']) {
@@ -174,7 +186,11 @@ abstract class Tx_Fluid_Core_AbstractViewHelper implements Tx_Fluid_Core_ViewHel
                                array_shift($explodedAnnotation);
                                $description = implode(' ', $explodedAnnotation);
                        }
-                       $this->registerArgument($parameterName, $dataType, $description, ($parameterInfo['optional'] === FALSE));
+                       $defaultValue = NULL;
+                       if (isset($parameterInfo['defaultValue'])) {
+                               $defaultValue = $parameterInfo['defaultValue'];
+                       }
+                       $this->registerArgument($parameterName, $dataType, $description, ($parameterInfo['optional'] === FALSE), $defaultValue);
                        $i++;
                }
        }
@@ -199,12 +215,13 @@ abstract class Tx_Fluid_Core_AbstractViewHelper implements Tx_Fluid_Core_ViewHel
                                } else {
                                        $validator = $this->validatorResolver->getValidator($type);
                                        if (is_null($validator)) {
-                                               throw new Tx_Fluid_Core_RuntimeException('No validator found for argument name "' . $argumentName . '" with type "' . $type . '".', 1237900534);
+                                               throw new Tx_Fluid_Core_RuntimeException('No validator found for argument name "' . $argumentName . '" with type "' . $type . '" in view helper "' . get_class($this) . '".', 1237900534);
                                        }
                                        $errors = new Tx_Fluid_Compatibility_Validation_Errors();
 
                                        if (!$validator->isValid($this->arguments[$argumentName], $errors)) {
-                                               throw new Tx_Fluid_Core_RuntimeException('Validation for argument name "' . $argumentName . '" FAILED.', 1237900686);
+                                               var_dump($errors);
+                                               throw new Tx_Fluid_Core_RuntimeException('Validation for argument name "' . $argumentName . '" in view helper "' . get_class($this) . '" FAILED. Expected type: "' . $type . '"; Given: ' . gettype($this->arguments[$argumentName]), 1237900686);
                                        }
                                }
                        }
index 93aaeee..658b2d2 100644 (file)
@@ -53,20 +53,27 @@ class Tx_Fluid_Core_ArgumentDefinition {
         */
        protected $required = FALSE;
 
+       /**
+        * Default value for argument
+        * @var null
+        */
+       protected $defaultValue = NULL;
+
        /**
         * Constructor for this argument definition.
         *
         * @param string $name Name of argument
         * @param string $type Type of argument
         * @param string $description Description of argument
-        * @param boolean $isOptional Optionality setting. If not set, defaults to FALSE
+        * @param boolean $required TRUE if argument is required
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         */
-       public function __construct($name, $type, $description, $required) {
+       public function __construct($name, $type, $description, $required, $defaultValue = NULL) {
                $this->name = $name;
                $this->type = $type;
                $this->description = $description;
                $this->required = $required;
+               $this->defaultValue = $defaultValue;
        }
 
        /**
@@ -108,6 +115,16 @@ class Tx_Fluid_Core_ArgumentDefinition {
        public function isRequired() {
                return $this->required;
        }
+
+       /**
+        * Get the default value, if set
+        *
+        * @return mixed Default value
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function getDefaultValue() {
+               return $this->defaultValue;
+       }
 }
 
 ?>
\ No newline at end of file
index ca9d5c7..be29ba4 100644 (file)
@@ -16,7 +16,7 @@
 /**
  * @package Fluid
  * @subpackage Core
- * @version $Id: ViewHelperNode.php 2082 2009-03-26 14:24:59Z sebastian $
+ * @version $Id: ViewHelperNode.php 2092 2009-03-26 22:21:47Z sebastian $
  */
 
 /**
@@ -24,7 +24,7 @@
  *
  * @package Fluid
  * @subpackage Core
- * @version $Id: ViewHelperNode.php 2082 2009-03-26 14:24:59Z sebastian $
+ * @version $Id: ViewHelperNode.php 2092 2009-03-26 22:21:47Z sebastian $
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
  * @scope prototype
  */
@@ -91,12 +91,20 @@ class Tx_Fluid_Core_SyntaxTree_ViewHelperNode extends Tx_Fluid_Core_SyntaxTree_A
 
                $objectFactory = $variableContainer->getObjectFactory();
                $viewHelper = $objectFactory->create($this->viewHelperClassName);
+               $argumentDefinitions = $viewHelper->prepareArguments();
 
                $contextVariables = $variableContainer->getAllIdentifiers();
 
                $evaluatedArguments = array();
-               foreach ($this->arguments as $argumentName => $argumentValue) {
-                       $evaluatedArguments[$argumentName] = $argumentValue->evaluate($variableContainer);
+               if (count($argumentDefinitions)) {
+                       foreach ($argumentDefinitions as $argumentName => $argumentDefinition) {
+                               if (isset($this->arguments[$argumentName])) {
+                                       $argumentValue = $this->arguments[$argumentName];
+                                       $evaluatedArguments[$argumentName] = $this->convertArgumentValue($argumentValue->evaluate($variableContainer), $argumentDefinition->getType());
+                               } else {
+                                       $evaluatedArguments[$argumentName] = $argumentDefinition->getDefaultValue();
+                               }
+                       }
                }
 
                $viewHelper->arguments = $objectFactory->create('Tx_Fluid_Core_ViewHelperArguments', $evaluatedArguments);
@@ -118,6 +126,13 @@ class Tx_Fluid_Core_SyntaxTree_ViewHelperNode extends Tx_Fluid_Core_SyntaxTree_A
                }
                return $out;
        }
+
+       protected function convertArgumentValue($value, $type) {
+               if ($type == 'boolean' && is_string($value)) {
+                       $value = (strtolower($value) === 'true' || $value === '1');
+               }
+               return $value;
+       }
 }
 
 
index 212c6f0..2dc6c4c 100644 (file)
@@ -16,7 +16,7 @@
 /**
  * @package Fluid
  * @subpackage Core
- * @version $Id: TagBasedViewHelper.php 1962 2009-03-03 12:10:41Z k-fish $
+ * @version $Id: TagBasedViewHelper.php 2095 2009-03-26 22:34:13Z bwaidelich $
  */
 
 /**
@@ -26,7 +26,7 @@
  *
  * @package Fluid
  * @subpackage Core
- * @version $Id: TagBasedViewHelper.php 1962 2009-03-03 12:10:41Z k-fish $
+ * @version $Id: TagBasedViewHelper.php 2095 2009-03-26 22:34:13Z bwaidelich $
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
  * @scope prototype
  */
@@ -53,13 +53,14 @@ abstract class Tx_Fluid_Core_TagBasedViewHelper extends Tx_Fluid_Core_AbstractVi
         * The tag attributes registered here are rendered with the $this->renderTagAttributes() method.
         *
         * @param string $name Name of tag attribute
+        * @param strgin $type Type of the tag attribute
         * @param string $description Description of tag attribute
         * @param boolean $required set to TRUE if tag attribute is required. Defaults to FALSE.
         * @return void
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         */
-       protected function registerTagAttribute($name, $description, $required = FALSE) {
-               $this->registerArgument($name, 'string', $description, $required);
+       protected function registerTagAttribute($name, $type, $description, $required = FALSE) {
+               $this->registerArgument($name, $type, $description, $required);
                $this->tagAttributes[] = $name;
        }
 
@@ -79,12 +80,14 @@ abstract class Tx_Fluid_Core_TagBasedViewHelper extends Tx_Fluid_Core_AbstractVi
         * @author Sebastian Kurfürst <sebastian@typo3.org>
         */
        protected function registerUniversalTagAttributes() {
-               $this->registerTagAttribute('class', 'CSS class(es) for this element');
-               $this->registerTagAttribute('dir', 'Text direction for this HTML element. Allowed strings: "ltr" (left to right), "rtl" (right to left)');
-               $this->registerTagAttribute('id', 'Unique (in this file) identifier for this HTML element.');
-               $this->registerTagAttribute('lang', 'Language for this element. Use short names specified in RFC 1766');
-               $this->registerTagAttribute('style', 'Individual CSS styles for this element');
-               $this->registerTagAttribute('title', 'Tooltip text of element');
+               $this->registerTagAttribute('class', 'string', 'CSS class(es) for this element');
+               $this->registerTagAttribute('dir', 'string', 'Text direction for this HTML element. Allowed strings: "ltr" (left to right), "rtl" (right to left)');
+               $this->registerTagAttribute('id', 'string', 'Unique (in this file) identifier for this HTML element.');
+               $this->registerTagAttribute('lang', 'string', 'Language for this element. Use short names specified in RFC 1766');
+               $this->registerTagAttribute('style', 'string', 'Individual CSS styles for this element');
+               $this->registerTagAttribute('title', 'string', 'Tooltip text of element');
+               $this->registerTagAttribute('accesskey', 'string', 'Keyboard shortcut to access this element');
+               $this->registerTagAttribute('tabindex', 'integer', 'Specifies the tab order of this element');
        }
 
        /**
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/ActionlinkViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/ActionlinkViewHelper.php
new file mode 100644 (file)
index 0000000..e6ec162
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+/**
+ * @package
+ * @subpackage
+ * @version $Id:$
+ */
+class Tx_Fluid_ViewHelpers_ActionlinkViewHelper extends Tx_Fluid_ViewHelpers_TypolinkViewHelper {
+
+       /**
+        * Render.
+        *
+        * @param string $page Target page
+        * @param string $action Target action
+        * @param string $controller Target controller
+        * @param string $extensionKey Target Extension Key
+        * @param string $anchor Anchor
+        * @param array $arguments Arguments
+        * @return string Rendered string
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function render($page = '', $action = '', $controller = '', $extensionKey = '', $anchor = '', $arguments = array()) {
+               //tx_blubb_controller[argumentName] = argumentValue
+
+               // $prefixedExtensionKey = 'tx_' . strtolower($this->request->getExtensionName()) . '_' . strtolower($this->request->getControllerName());
+
+               $prefixedArguments = array();
+               foreach ($arguments as $argumentName => $argumentValue) {
+
+               }
+
+               parent::render($page, $anchor, TRUE, args);
+       }
+}
+
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/TypolinkViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/TypolinkViewHelper.php
new file mode 100644 (file)
index 0000000..6f57477
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+/**
+ *
+ *
+ * @package
+ * @subpackage
+ * @version $Id:$
+ */
+class Tx_Fluid_ViewHelpers_TypolinkViewHelper extends Tx_Fluid_Core_TagBasedViewHelper {
+       /**
+        * an instance of tslib_cObj
+        *
+        * @var tslib_cObj
+        */
+       protected $contentObject = null;
+
+       /**
+        * constructor for class tx_community_viewhelper_Link
+        */
+       public function __construct(array $arguments = array()) {
+               if (is_null($this->contentObject)) {
+                       $this->contentObject = t3lib_div::makeInstance('tslib_cObj');
+               }
+       }
+       /**
+        * Arguments initialization
+        *
+        * @return void
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function initializeArguments() {
+               $this->registerUniversalTagAttributes();
+               $this->registerTagAttribute('target', 'Target of link', FALSE);
+               $this->registerTagAttribute('rel', 'Specifies the relationship between the current document and the linked document', FALSE);
+       }
+
+       /**
+        * Render.
+        *
+        * @param string $page Target page. See TypoLink destination
+        * @param string $anchor Anchor
+        * @param boolean $useCacheHash If true, cHash is appended to URL
+        * @param array $arguments Arguments
+        * @return string Rendered string
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function render($page = '', $anchor = '', $useCacheHash = TRUE, $arguments = array()) {
+
+               if ($page === '') {
+                       $page = $GLOBALS['TSFE']->id;
+               }
+
+               $typolinkConfiguration = array(
+                       'parameter' => $page,
+                       'ATagParams' => $this->renderTagAttributes()
+               );
+
+               if (count($arguments)) {
+                        $typolinkConfiguration['additionalParams'] = '&' . http_build_query($arguments);
+               }
+               if ($anchor) {
+                       $typolinkConfiguration['section'] = $anchor;
+               }
+               if ($useCacheHash) {
+                       $typolinkConfiguration['useCacheHash'] = 1;
+               } else {
+                       $typolinkConfiguration['useCacheHash'] = 0;
+               }
+
+               $link = $this->contentObject->typoLink(
+                       $this->renderChildren(),
+                       $typolinkConfiguration
+               );
+
+               return $link;
+       }
+}
+
+
+?>
\ No newline at end of file