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

PAGE 2

+

PAGE 1000

+ + + +
+

Some pagination with page choice

+ {assign var=current_page value=2} +

PAGE {$current_page} :

+ +

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