From 80380878f598ffcfdd821533b106982a4ddb8355 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Wed, 26 Jun 2013 14:45:19 +0200 Subject: [PATCH 1/4] changes tests create argument helpers start loop auto pagination --- .../Thelia/Core/Template/Element/BaseLoop.php | 16 +++- .../Core/Template/Loop/Argument/Argument.php | 42 ++++++++++ .../Loop/Argument/ArgumentCollection.php | 18 +++- .../Thelia/Core/Template/Loop/Category.php | 84 ++++--------------- .../Template/Smarty/Plugins/TheliaLoop.php | 12 +-- ...entTest.php => ArgumentCollectionTest.php} | 10 +-- core/lib/Thelia/Tests/Type/AnyTypeTest.php | 40 +++++++++ core/lib/Thelia/Tests/Type/EnumTypeTest.php | 43 ++++++++++ core/lib/Thelia/Tests/Type/FloatTypeTest.php | 42 ++++++++++ .../lib/Thelia/Tests/Type/IntListTypeTest.php | 42 ++++++++++ core/lib/Thelia/Tests/Type/IntTypeTest.php | 42 ++++++++++ core/lib/Thelia/Tests/Type/JsonTypeTest.php | 41 +++++++++ .../{TypeTest.php => TypeCollectionTest.php} | 53 ++---------- 13 files changed, 352 insertions(+), 133 deletions(-) rename core/lib/Thelia/Tests/Core/Template/Loop/Argument/{ArgumentTest.php => ArgumentCollectionTest.php} (95%) create mode 100755 core/lib/Thelia/Tests/Type/AnyTypeTest.php create mode 100755 core/lib/Thelia/Tests/Type/EnumTypeTest.php create mode 100755 core/lib/Thelia/Tests/Type/FloatTypeTest.php create mode 100755 core/lib/Thelia/Tests/Type/IntListTypeTest.php create mode 100755 core/lib/Thelia/Tests/Type/IntTypeTest.php create mode 100755 core/lib/Thelia/Tests/Type/JsonTypeTest.php rename core/lib/Thelia/Tests/Type/{TypeTest.php => TypeCollectionTest.php} (62%) diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index 519aae4bd..821055204 100755 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -26,6 +26,7 @@ namespace Thelia\Core\Template\Element; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Thelia\Core\Template\Loop\Argument\ArgumentCollection; +use Thelia\Core\Template\Loop\Argument\Argument; /** * @@ -44,6 +45,14 @@ abstract class BaseLoop */ public $dispatcher; + protected function getDefaultArgs() + { + return array( + Argument::createIntTypeArgument('offset'), + Argument::createIntTypeArgument('limit'), + ); + } + /** * @param \Symfony\Component\HttpFoundation\Request $request * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher @@ -54,6 +63,11 @@ abstract class BaseLoop $this->dispatcher = $dispatcher; } + public function getArgs() + { + return $this->defineArgs()->addArguments($this->getDefaultArgs()); + } + /** * * this function have to be implement in your own loop class. @@ -99,6 +113,6 @@ abstract class BaseLoop * * @return ArgumentCollection */ - abstract public function defineArgs(); + abstract protected function defineArgs(); } diff --git a/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php b/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php index d2b6cdbb2..9fb72bff0 100755 --- a/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php +++ b/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php @@ -22,6 +22,9 @@ /*************************************************************************************/ namespace Thelia\Core\Template\Loop\Argument; +use Thelia\Type\TypeCollection; +use Thelia\Type; + /** * * @author Etienne Roudeix @@ -42,4 +45,43 @@ class Argument $this->mandatory = $mandatory ? true : false; $this->default = $default; } + + public static function createAnyTypeArgument($name, $default=null, $mandatory=false, $empty=true) + { + return new Argument( + $name, + new TypeCollection( + new Type\AnyType() + ), + $default, + $mandatory, + $empty + ); + } + + public static function createIntTypeArgument($name, $default=null, $mandatory=false, $empty=true) + { + return new Argument( + $name, + new TypeCollection( + new Type\IntType() + ), + $default, + $mandatory, + $empty + ); + } + + public static function createIntListTypeArgument($name, $default=null, $mandatory=false, $empty=true) + { + return new Argument( + $name, + new TypeCollection( + new Type\IntListType() + ), + $default, + $mandatory, + $empty + ); + } } diff --git a/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php b/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php index d3422c1e9..6aec1fd9b 100755 --- a/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php +++ b/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php @@ -35,9 +35,7 @@ class ArgumentCollection implements \Iterator public function __construct() { - foreach(func_get_args() as $argument) { - $this->addArgument($argument); - } + $this->addArguments(func_get_args()); } public function isEmpty() @@ -45,6 +43,20 @@ class ArgumentCollection implements \Iterator return count($this->arguments) == 0; } + /** + * @param array $argumentList + * + * @return ArgumentCollection + */ + public function addArguments(array $argumentList) + { + foreach($argumentList as $argument) { + $this->addArgument($argument); + } + + return $this; + } + /** * @param Argument $argument * diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php index dadb69505..0af81e563 100755 --- a/core/lib/Thelia/Core/Template/Loop/Category.php +++ b/core/lib/Thelia/Core/Template/Loop/Category.php @@ -79,80 +79,23 @@ class Category extends BaseLoop { public $limit; public $offset; - public function defineArgs() + protected function defineArgs() { return new ArgumentCollection( - new Argument( - 'id', - new TypeCollection( - new Type\IntListType() - ) - ), - new Argument( - 'parent', - new TypeCollection( - new Type\IntType() - ) - ), - new Argument( - 'current', - new TypeCollection( - new Type\IntType() - ) - ), - new Argument( - 'not_empty', - new TypeCollection( - new Type\IntType() - ), - 0 - ), - new Argument( - 'visible', - new TypeCollection( - new Type\IntType() - ), - 1 - ), - new Argument( - 'link', - new TypeCollection( - new Type\AnyType() - ) - ), + Argument::createIntListTypeArgument('id'), + Argument::createIntTypeArgument('parent'), + Argument::createIntTypeArgument('current'), + Argument::createIntTypeArgument('not_empty', 0), + Argument::createIntTypeArgument('visible', 1), + Argument::createAnyTypeArgument('link'), new Argument( 'order', new TypeCollection( new Type\EnumType('alpha', 'alpha_reverse', 'reverse') ) ), - new Argument( - 'random', - new TypeCollection( - new Type\AnyType() - ), - 0 - ), - new Argument( - 'exclude', - new TypeCollection( - new Type\IntListType() - ) - ), - new Argument( - 'limit', - new TypeCollection( - new Type\IntType() - ), - 10 - ), - new Argument( - 'offset', - new TypeCollection( - new Type\IntType() - ), - 0 - ) + Argument::createIntTypeArgument('random', 0), + Argument::createIntListTypeArgument('exclude') ); } @@ -187,11 +130,11 @@ class Category extends BaseLoop { $search->filterByLink($this->link); } - if($this->limit > -1) { + /*if($this->limit > -1) { $search->limit($this->limit); - } + }*/ $search->filterByVisible($this->visible); - $search->offset($this->offset); + //$search->offset($this->offset); switch($this->order) { @@ -221,7 +164,8 @@ class Category extends BaseLoop { */ $search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN); - $categories = $search->find(); + //$categories = $search->find(); + $categories = $search->paginate($page = 2, $maxPerPage = 2); $loopResult = new LoopResult(); diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php index 67ccfcf98..a26471e4c 100755 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php @@ -177,8 +177,8 @@ class TheliaLoop implements SmartyPluginInterface * * @param string $name * @return \Thelia\Core\Template\Element\BaseLoop - * @throws \Thelia\Tpex\Exception\InvalidElementException - * @throws \Thelia\Tpex\Exception\ElementNotFoundException + * @throws InvalidElementException + * @throws ElementNotFoundException */ protected function createLoopInstance($name) { @@ -204,8 +204,8 @@ class TheliaLoop implements SmartyPluginInterface /** * Returns the value of a loop argument. * - * @param unknown $loop a BaseLoop instance - * @param unknown $smartyParam + * @param BaseLoop $loop a BaseLoop instance + * @param $smartyParam * @throws \InvalidArgumentException */ protected function getLoopArgument(BaseLoop $loop, $smartyParam) @@ -218,7 +218,7 @@ class TheliaLoop implements SmartyPluginInterface $faultActor = array(); $faultDetails = array(); - $argumentsCollection = $loop->defineArgs(); + $argumentsCollection = $loop->getArgs(); foreach( $argumentsCollection as $argument ) { $value = isset($smartyParam[$argument->name]) ? $smartyParam[$argument->name] : null; @@ -275,7 +275,7 @@ class TheliaLoop implements SmartyPluginInterface * "myLoop" => "My\Own\Loop" * ); * - * @param array $loops + * @param array $loopDefinition * @throws \InvalidArgumentException if loop name already exists */ public function setLoopList(array $loopDefinition) diff --git a/core/lib/Thelia/Tests/Core/Template/Loop/Argument/ArgumentTest.php b/core/lib/Thelia/Tests/Core/Template/Loop/Argument/ArgumentCollectionTest.php similarity index 95% rename from core/lib/Thelia/Tests/Core/Template/Loop/Argument/ArgumentTest.php rename to core/lib/Thelia/Tests/Core/Template/Loop/Argument/ArgumentCollectionTest.php index 7c5ce5d16..b7556c10c 100755 --- a/core/lib/Thelia/Tests/Core/Template/Loop/Argument/ArgumentTest.php +++ b/core/lib/Thelia/Tests/Core/Template/Loop/Argument/ArgumentCollectionTest.php @@ -113,17 +113,11 @@ class ArgumentTest extends \PHPUnit_Framework_TestCase $arguments = \PHPUnit_Framework_Assert::readAttribute($collection, 'arguments'); - $collection->rewind(); - while ($collection->valid()) { - - $argument = $collection->current(); - + foreach($collection as $key => $argument) { $this->assertEquals( $argument, - $arguments[$collection->key()] + $arguments[$key] ); - - $collection->next(); } } } diff --git a/core/lib/Thelia/Tests/Type/AnyTypeTest.php b/core/lib/Thelia/Tests/Type/AnyTypeTest.php new file mode 100755 index 000000000..8e959d154 --- /dev/null +++ b/core/lib/Thelia/Tests/Type/AnyTypeTest.php @@ -0,0 +1,40 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Type; + +use Thelia\Type\AnyType; + +/** + * + * @author Etienne Roudeix + * + */ +class AnyTypeTest extends \PHPUnit_Framework_TestCase +{ + public function testAnyType() + { + $anyType = new AnyType(); + $this->assertTrue($anyType->isValid(md5(rand(1000, 10000)))); + } +} diff --git a/core/lib/Thelia/Tests/Type/EnumTypeTest.php b/core/lib/Thelia/Tests/Type/EnumTypeTest.php new file mode 100755 index 000000000..5ed4637e9 --- /dev/null +++ b/core/lib/Thelia/Tests/Type/EnumTypeTest.php @@ -0,0 +1,43 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Type; + +use Thelia\Type\EnumType; + +/** + * + * @author Etienne Roudeix + * + */ +class EnumTypeTest extends \PHPUnit_Framework_TestCase +{ + public function testEnumType() + { + $enumType = new EnumType(array("cat", "dog")); + $this->assertTrue($enumType->isValid('cat')); + $this->assertTrue($enumType->isValid('dog')); + $this->assertFalse($enumType->isValid('monkey')); + $this->assertFalse($enumType->isValid('catdog')); + } +} diff --git a/core/lib/Thelia/Tests/Type/FloatTypeTest.php b/core/lib/Thelia/Tests/Type/FloatTypeTest.php new file mode 100755 index 000000000..82bf9bfe2 --- /dev/null +++ b/core/lib/Thelia/Tests/Type/FloatTypeTest.php @@ -0,0 +1,42 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Type; + +use Thelia\Type\FloatType; + +/** + * + * @author Etienne Roudeix + * + */ +class FloatTypeTest extends \PHPUnit_Framework_TestCase +{ + public function testFloatType() + { + $floatType = new FloatType(); + $this->assertTrue($floatType->isValid('1.1')); + $this->assertTrue($floatType->isValid(2.2)); + $this->assertFalse($floatType->isValid('foo')); + } +} diff --git a/core/lib/Thelia/Tests/Type/IntListTypeTest.php b/core/lib/Thelia/Tests/Type/IntListTypeTest.php new file mode 100755 index 000000000..68b628a5c --- /dev/null +++ b/core/lib/Thelia/Tests/Type/IntListTypeTest.php @@ -0,0 +1,42 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Type; + +use Thelia\Type\IntListType; + +/** + * + * @author Etienne Roudeix + * + */ +class IntListTypeTest extends \PHPUnit_Framework_TestCase +{ + public function testIntListType() + { + $intListType = new IntListType(); + $this->assertTrue($intListType->isValid('1')); + $this->assertTrue($intListType->isValid('1,2,3')); + $this->assertFalse($intListType->isValid('1,2,3.3')); + } +} diff --git a/core/lib/Thelia/Tests/Type/IntTypeTest.php b/core/lib/Thelia/Tests/Type/IntTypeTest.php new file mode 100755 index 000000000..2f9ce8a40 --- /dev/null +++ b/core/lib/Thelia/Tests/Type/IntTypeTest.php @@ -0,0 +1,42 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Type; + +use Thelia\Type\IntType; + +/** + * + * @author Etienne Roudeix + * + */ +class IntTypeTest extends \PHPUnit_Framework_TestCase +{ + public function testIntType() + { + $intType = new IntType(); + $this->assertTrue($intType->isValid('1')); + $this->assertTrue($intType->isValid(2)); + $this->assertFalse($intType->isValid('3.3')); + } +} diff --git a/core/lib/Thelia/Tests/Type/JsonTypeTest.php b/core/lib/Thelia/Tests/Type/JsonTypeTest.php new file mode 100755 index 000000000..276a867d2 --- /dev/null +++ b/core/lib/Thelia/Tests/Type/JsonTypeTest.php @@ -0,0 +1,41 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Type; + +use Thelia\Type\JsonType; + +/** + * + * @author Etienne Roudeix + * + */ +class JsonTypeTest extends \PHPUnit_Framework_TestCase +{ + public function testJsonType() + { + $jsonType = new JsonType(); + $this->assertTrue($jsonType->isValid('{"k0":"v0","k1":"v1","k2":"v2"}')); + $this->assertFalse($jsonType->isValid('1,2,3')); + } +} diff --git a/core/lib/Thelia/Tests/Type/TypeTest.php b/core/lib/Thelia/Tests/Type/TypeCollectionTest.php similarity index 62% rename from core/lib/Thelia/Tests/Type/TypeTest.php rename to core/lib/Thelia/Tests/Type/TypeCollectionTest.php index 4b7c65df8..5cef59215 100755 --- a/core/lib/Thelia/Tests/Type/TypeTest.php +++ b/core/lib/Thelia/Tests/Type/TypeCollectionTest.php @@ -37,18 +37,18 @@ class TypeTest extends \PHPUnit_Framework_TestCase { $collection = new TypeCollection( new Type\AnyType(), - new Type\AnyType() + new Type\IntType() ); $collection->addType( - new Type\AnyType() + new Type\FloatType() ); $this->assertAttributeEquals( array( new Type\AnyType(), - new Type\AnyType(), - new Type\AnyType(), + new Type\IntType(), + new Type\FloatType(), ), 'types', $collection @@ -59,55 +59,18 @@ class TypeTest extends \PHPUnit_Framework_TestCase { $collection = new TypeCollection( new Type\AnyType(), - new Type\AnyType(), - new Type\AnyType() + new Type\IntType(), + new Type\FloatType() ); $types = \PHPUnit_Framework_Assert::readAttribute($collection, 'types'); - $collection->rewind(); - while ($collection->valid()) { - - $type = $collection->current(); - + foreach($collection as $key => $type) { $this->assertEquals( $type, - $types[$collection->key()] + $types[$key] ); - - $collection->next(); } } - - public function testTypes() - { - $anyType = new Type\AnyType(); - $this->assertTrue($anyType->isValid(md5(rand(1000, 10000)))); - - $intType = new Type\IntType(); - $this->assertTrue($intType->isValid('1')); - $this->assertTrue($intType->isValid(2)); - $this->assertFalse($intType->isValid('3.3')); - - $floatType = new Type\FloatType(); - $this->assertTrue($floatType->isValid('1.1')); - $this->assertTrue($floatType->isValid(2.2)); - $this->assertFalse($floatType->isValid('foo')); - - $enumType = new Type\EnumType(array("cat", "dog")); - $this->assertTrue($enumType->isValid('cat')); - $this->assertTrue($enumType->isValid('dog')); - $this->assertFalse($enumType->isValid('monkey')); - $this->assertFalse($enumType->isValid('catdog')); - - $intListType = new Type\IntListType(); - $this->assertTrue($intListType->isValid('1')); - $this->assertTrue($intListType->isValid('1,2,3')); - $this->assertFalse($intListType->isValid('1,2,3.3')); - - $jsonType = new Type\JsonType(); - $this->assertTrue($jsonType->isValid('{"k0":"v0","k1":"v1","k2":"v2"}')); - $this->assertFalse($jsonType->isValid('1,2,3')); - } } From 3aca4bfb2528267552214fb8207545d4ae1988a8 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Wed, 26 Jun 2013 16:09:46 +0200 Subject: [PATCH 2/4] pagination --- .../Thelia/Core/Template/Element/BaseLoop.php | 33 +++++++++++++++++-- .../Thelia/Core/Template/Loop/Category.php | 11 +------ templates/smarty-sample/index.html | 16 +++++++++ 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index 821055204..adc406d7a 100755 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -45,11 +45,16 @@ abstract class BaseLoop */ public $dispatcher; + public $limit; + public $page; + public $offset; + protected function getDefaultArgs() { return array( - Argument::createIntTypeArgument('offset'), - Argument::createIntTypeArgument('limit'), + Argument::createIntTypeArgument('offset', 0), + Argument::createIntTypeArgument('page'), + Argument::createIntTypeArgument('limit', 10), ); } @@ -68,6 +73,30 @@ abstract class BaseLoop return $this->defineArgs()->addArguments($this->getDefaultArgs()); } + public function search(\ModelCriteria $search) + { + if($this->page !== null) { + return $this->searchWithPagination($search); + } else { + return $this->searchWithOffset($search); + } + } + + public function searchWithOffset(\ModelCriteria $search) + { + if($this->limit >= 0) { + $search->limit($this->limit); + } + $search->offset($this->offset); + + return $search->find(); + } + + public function searchWithPagination(\ModelCriteria $search) + { + return $search->paginate($this->page, $this->limit); + } + /** * * this function have to be implement in your own loop class. diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php index 0af81e563..2a47ac835 100755 --- a/core/lib/Thelia/Core/Template/Loop/Category.php +++ b/core/lib/Thelia/Core/Template/Loop/Category.php @@ -51,8 +51,6 @@ use Thelia\Type; * * by default results are sorting by position ascending * - random : if 1, random results. Default value is 0 * - exclude : all category id you want to exclude (as for id, an integer or a "string list" can be used) - * - limit : number of results. Default value is 10 - * - offset : at witch id start the search * * example : * @@ -76,8 +74,6 @@ class Category extends BaseLoop { public $order; public $random; public $exclude; - public $limit; - public $offset; protected function defineArgs() { @@ -130,11 +126,7 @@ class Category extends BaseLoop { $search->filterByLink($this->link); } - /*if($this->limit > -1) { - $search->limit($this->limit); - }*/ $search->filterByVisible($this->visible); - //$search->offset($this->offset); switch($this->order) { @@ -164,8 +156,7 @@ class Category extends BaseLoop { */ $search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN); - //$categories = $search->find(); - $categories = $search->paginate($page = 2, $maxPerPage = 2); + $categories = $this->search($search); $loopResult = new LoopResult(); diff --git a/templates/smarty-sample/index.html b/templates/smarty-sample/index.html index 9135b8867..c9d4e6842 100755 --- a/templates/smarty-sample/index.html +++ b/templates/smarty-sample/index.html @@ -84,4 +84,20 @@ An image from asset directory : {/loop} +
+

Some pagination

+

PAGE 1

+
    + {loop type="category" name="catloopwithpagination" limit="2" page="1"} +
  • {$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}
  • + {/loop} +
+

PAGE 2

+
    + {loop type="category" name="catloopwithpagination" limit="2" page="2"} +
  • {$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}
  • + {/loop} +
+
+ {include file="includes/footer.html"} \ No newline at end of file From 3fb5466ef311e7988053a020ffc1034a699e1154 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Thu, 27 Jun 2013 13:18:29 +0200 Subject: [PATCH 3/4] pagination --- .../Thelia/Core/Template/Element/BaseLoop.php | 23 +++-- .../Thelia/Core/Template/Loop/Category.php | 4 +- .../Template/Smarty/Plugins/TheliaLoop.php | 83 ++++++++++++++++--- templates/smarty-sample/index.html | 45 +++++++++- 4 files changed, 134 insertions(+), 21 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index adc406d7a..1293d96a6 100755 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -25,7 +25,6 @@ namespace Thelia\Core\Template\Element; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Thelia\Core\Template\Loop\Argument\ArgumentCollection; use Thelia\Core\Template\Loop\Argument\Argument; /** @@ -73,10 +72,10 @@ abstract class BaseLoop return $this->defineArgs()->addArguments($this->getDefaultArgs()); } - public function search(\ModelCriteria $search) + public function search(\ModelCriteria $search, &$pagination = null) { if($this->page !== null) { - return $this->searchWithPagination($search); + return $this->searchWithPagination($search, $pagination); } else { return $this->searchWithOffset($search); } @@ -92,9 +91,19 @@ abstract class BaseLoop return $search->find(); } - public function searchWithPagination(\ModelCriteria $search) + public function searchWithPagination(\ModelCriteria $search, &$pagination) { - return $search->paginate($this->page, $this->limit); + $pagination = $search->paginate($this->page, $this->limit); + + //$toto = $pagination->haveToPaginate(); + //$toto = $pagination->getNbResults(); + //$toto2 = $pagination->count(); + + if($this->page > $pagination->getLastPage()) { + return array(); + } else { + return $pagination; + } } /** @@ -121,7 +130,7 @@ abstract class BaseLoop * * @return mixed */ - abstract public function exec(); + abstract public function exec(&$pagination); /** * @@ -140,7 +149,7 @@ abstract class BaseLoop * ) * ); * - * @return ArgumentCollection + * @return \Thelia\Core\Template\Element\LoopResult */ abstract protected function defineArgs(); diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php index 2a47ac835..f229d9340 100755 --- a/core/lib/Thelia/Core/Template/Loop/Category.php +++ b/core/lib/Thelia/Core/Template/Loop/Category.php @@ -100,7 +100,7 @@ class Category extends BaseLoop { * * @return \Thelia\Core\Template\Element\LoopResult */ - public function exec() + public function exec(&$pagination) { $search = CategoryQuery::create(); @@ -156,7 +156,7 @@ class Category extends BaseLoop { */ $search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN); - $categories = $this->search($search); + $categories = $this->search($search, $pagination); $loopResult = new LoopResult(); diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php index a26471e4c..a7947215f 100755 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php @@ -35,6 +35,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; class TheliaLoop implements SmartyPluginInterface { + protected static $pagination = null; protected $loopDefinition = array(); @@ -48,6 +49,20 @@ class TheliaLoop implements SmartyPluginInterface $this->dispatcher = $dispatcher; } + /** + * @param $loopId + * + * @return \PropelModelPager + */ + public static function getPagination($loopId) + { + if(!empty(self::$pagination[$loopId])) { + return self::$pagination[$loopId]; + } else { + return null; + } + } + /** * Process {loop name="loop name" type="loop type" ... } ... {/loop} block * @@ -75,11 +90,13 @@ class TheliaLoop implements SmartyPluginInterface $this->getLoopArgument($loop, $params); - $loopResults = $loop->exec(); + self::$pagination[$name] = null; + + $loopResults = $loop->exec(self::$pagination[$name]); $template->assignByRef($name, $loopResults); - } - else { + + } else { $loopResults = $template->getTemplateVars($name); @@ -97,7 +114,8 @@ class TheliaLoop implements SmartyPluginInterface $template->assign('__COUNT__', 1 + $loopResults->key()); $template->assign('__TOTAL__', $loopResults->getCount()); - $repeat = $loopResults->valid(); + //$repeat = $loopResults->valid(); + $repeat = true; } if ($content !== null) { @@ -139,13 +157,62 @@ class TheliaLoop implements SmartyPluginInterface */ public function theliaIfLoop($params, $content, $template, &$repeat) { - // When encountering close tag, check if loop has results. if ($repeat === false) { return $this->checkEmptyLoop($params, $template) ? '' : $content; } } + /** + * Process {pageloop rel="loopname"} ... {/pageloop} block + * + * @param $params + * @param $content + * @param $template + * @param $repeat + * + * @return string + * @throws \InvalidArgumentException + */ + public function theliaPageLoop($params, $content, $template, &$repeat) + { + if (empty($params['rel'])) + throw new \InvalidArgumentException("Missing 'rel' parameter in page loop"); + + $loopName = $params['rel']; + + // Find loop results in the current template vars + $loopResults = $template->getTemplateVars($loopName); + if (empty($loopResults)) { + throw new \InvalidArgumentException("Loop $loopName is not defined."); + } + + // Find pagination + $pagination = self::getPagination($loopName); + if ($pagination === null) { + throw new \InvalidArgumentException("Loop $loopName : no pagination found."); + } + + if ($content === null) { + $page = 1; + } else { + $page = $template->getTemplateVars('PAGE'); + $page++; + } + + if ($page <= $pagination->getLastPage()) { + $template->assign('PAGE', $page); + $template->assign('CURRENT', $pagination->getPage()); + $template->assign('LAST', $pagination->getLastPage()); + + $repeat = true; + } + + if ($content !== null) { + return $content; + } + } + /** * Check if a loop has returned results. The loop shoud have been executed before, or an * InvalidArgumentException is thrown @@ -210,11 +277,6 @@ class TheliaLoop implements SmartyPluginInterface */ protected function getLoopArgument(BaseLoop $loop, $smartyParam) { - $defaultItemsParams = array('required' => true); - - $shortcutItemParams = array('optional' => array('required' => false)); - - $errorCode = 0; $faultActor = array(); $faultDetails = array(); @@ -300,6 +362,7 @@ class TheliaLoop implements SmartyPluginInterface new SmartyPluginDescriptor('block', 'loop' , $this, 'theliaLoop'), new SmartyPluginDescriptor('block', 'elseloop' , $this, 'theliaElseloop'), new SmartyPluginDescriptor('block', 'ifloop' , $this, 'theliaIfLoop'), + new SmartyPluginDescriptor('block', 'pageloop' , $this, 'theliaPageLoop'), ); } } \ No newline at end of file diff --git a/templates/smarty-sample/index.html b/templates/smarty-sample/index.html index c9d4e6842..aea81cd3d 100755 --- a/templates/smarty-sample/index.html +++ b/templates/smarty-sample/index.html @@ -88,16 +88,57 @@ An image from asset directory :

Some pagination

PAGE 1

    - {loop type="category" name="catloopwithpagination" limit="2" page="1"} + {loop type="category" name="catloopwithpagination1" limit="2" page="1"}
  • {$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}
  • {/loop}

PAGE 2

    - {loop type="category" name="catloopwithpagination" limit="2" page="2"} + {loop type="category" name="catloopwithpagination2" limit="2" page="2"}
  • {$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}
  • {/loop}
+

PAGE 1000

+
    + + {loop type="category" name="catloopwithpagination1000" limit="2" page="1000"} +
  • {$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}
  • + {/loop} + + {elseloop rel="catloopwithpagination1000"} + NO RESULTS + {/elseloop} +
+ + +
+

Some pagination with page choice

+ {assign var=current_page value=2} +

PAGE {$current_page} :

+
    + {loop type="category" name="catloopwithpaginationchoice" limit="2" page="{$current_page}"} +
  • {$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}
  • + {/loop} +
+

page choice

+ {pageloop rel="catloopwithpaginationchoice"} + {if ${PAGE} != {$current_page}} + {if {$PAGE} > {$current_page}-10 AND {$PAGE} < {$current_page}+10} + {$PAGE} + {/if} + {if {$PAGE} == {$current_page}-10 OR {$PAGE} == {$current_page}+10} + ... + {/if} + {if ({$PAGE} < {$current_page}-10 OR {$PAGE} > {$current_page}+10) AND ({$PAGE}%10 == 0 OR ${PAGE} == {$LAST} OR ${PAGE} == 1)} + {$PAGE} + {/if} + {else} + { {$PAGE} } + {/if} + {if {$PAGE} != {$LAST}} + - + {/if} + {/pageloop}
{include file="includes/footer.html"} \ No newline at end of file From fc4a0734dbaadb5231537e706ec2b6cc80653c14 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Thu, 27 Jun 2013 16:08:36 +0200 Subject: [PATCH 4/4] no more test loops --- .../Thelia/Core/Template/TestLoop/Equal.php | 51 ------------------- 1 file changed, 51 deletions(-) delete mode 100755 core/lib/Thelia/Core/Template/TestLoop/Equal.php diff --git a/core/lib/Thelia/Core/Template/TestLoop/Equal.php b/core/lib/Thelia/Core/Template/TestLoop/Equal.php deleted file mode 100755 index 5e812a7a4..000000000 --- a/core/lib/Thelia/Core/Template/TestLoop/Equal.php +++ /dev/null @@ -1,51 +0,0 @@ -. */ -/* */ -/*************************************************************************************/ - -namespace Thelia\Core\Template\TestLoop; - -use Thelia\Tpex\Element\TestLoop\BaseTestLoop; - -/** - * - * TestLoop equal, test if value and variable are equal - * - * example : - * - * - * Result display here if variable and value are equal - * - * Result display here if variable and value are not equal - * - * - * Class Equal - * @package Thelia\Core\Template\TestLoop - * @author Manuel Raynaud - */ -class Equal extends BaseTestLoop -{ - - public function exec($variable, $value) - { - return $variable == $value; - } -} \ No newline at end of file