From eb930498971c039ecca453c76e048f7f5df7f539 Mon Sep 17 00:00:00 2001 From: franck Date: Wed, 7 Aug 2013 22:41:57 +0200 Subject: [PATCH] Added a new loop argument type to pass either true (1), false (0) or both (*). Useful for visible argument for example. --- .../Thelia/Core/Template/Loop/Category.php | 5 +- core/lib/Thelia/Type/BooleanOrBothType.php | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 core/lib/Thelia/Type/BooleanOrBothType.php diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php index 009ed1ed1..b3492a7ab 100755 --- a/core/lib/Thelia/Core/Template/Loop/Category.php +++ b/core/lib/Thelia/Core/Template/Loop/Category.php @@ -73,7 +73,7 @@ class Category extends BaseLoop Argument::createIntTypeArgument('parent'), Argument::createBooleanTypeArgument('current'), Argument::createBooleanTypeArgument('not_empty', 0), - Argument::createBooleanTypeArgument('visible', 1), + Argument::createBooleanOrBothTypeArgument('visible', 1), new Argument( 'order', new TypeCollection( @@ -122,7 +122,8 @@ class Category extends BaseLoop $search->filterById($exclude, Criteria::NOT_IN); } - $search->filterByVisible($this->getVisible() ? 1 : 0); + if ($this->getVisible() != '*') + $search->filterByVisible($this->getVisible() ? 1 : 0); $orders = $this->getOrder(); diff --git a/core/lib/Thelia/Type/BooleanOrBothType.php b/core/lib/Thelia/Type/BooleanOrBothType.php new file mode 100644 index 000000000..13c76e254 --- /dev/null +++ b/core/lib/Thelia/Type/BooleanOrBothType.php @@ -0,0 +1,53 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Type; + +/** + * This filter accepts either a boolean value, or '*' which means both, true and false + * + * @author Etienne Roudeix + * + */ + +class BooleanOrBothType implements TypeInterface +{ + + const ANY = '*'; + + public function getType() + { + return 'Boolean or both type'; + } + + public function isValid($value) + { + return $value === self::ANY || filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null; + } + + public function getFormattedValue($value) + { + if ($value === self::ANY) return $value; + + return $value === null ? null : filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + } +}