pagination
This commit is contained in:
@@ -25,7 +25,6 @@ namespace Thelia\Core\Template\Element;
|
|||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
|
|
||||||
use Thelia\Core\Template\Loop\Argument\Argument;
|
use Thelia\Core\Template\Loop\Argument\Argument;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -73,10 +72,10 @@ abstract class BaseLoop
|
|||||||
return $this->defineArgs()->addArguments($this->getDefaultArgs());
|
return $this->defineArgs()->addArguments($this->getDefaultArgs());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function search(\ModelCriteria $search)
|
public function search(\ModelCriteria $search, &$pagination = null)
|
||||||
{
|
{
|
||||||
if($this->page !== null) {
|
if($this->page !== null) {
|
||||||
return $this->searchWithPagination($search);
|
return $this->searchWithPagination($search, $pagination);
|
||||||
} else {
|
} else {
|
||||||
return $this->searchWithOffset($search);
|
return $this->searchWithOffset($search);
|
||||||
}
|
}
|
||||||
@@ -92,9 +91,19 @@ abstract class BaseLoop
|
|||||||
return $search->find();
|
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
|
* @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();
|
abstract protected function defineArgs();
|
||||||
|
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ class Category extends BaseLoop {
|
|||||||
*
|
*
|
||||||
* @return \Thelia\Core\Template\Element\LoopResult
|
* @return \Thelia\Core\Template\Element\LoopResult
|
||||||
*/
|
*/
|
||||||
public function exec()
|
public function exec(&$pagination)
|
||||||
{
|
{
|
||||||
$search = CategoryQuery::create();
|
$search = CategoryQuery::create();
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ class Category extends BaseLoop {
|
|||||||
*/
|
*/
|
||||||
$search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN);
|
$search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN);
|
||||||
|
|
||||||
$categories = $this->search($search);
|
$categories = $this->search($search, $pagination);
|
||||||
|
|
||||||
$loopResult = new LoopResult();
|
$loopResult = new LoopResult();
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|||||||
|
|
||||||
class TheliaLoop implements SmartyPluginInterface
|
class TheliaLoop implements SmartyPluginInterface
|
||||||
{
|
{
|
||||||
|
protected static $pagination = null;
|
||||||
|
|
||||||
protected $loopDefinition = array();
|
protected $loopDefinition = array();
|
||||||
|
|
||||||
@@ -48,6 +49,20 @@ class TheliaLoop implements SmartyPluginInterface
|
|||||||
$this->dispatcher = $dispatcher;
|
$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
|
* Process {loop name="loop name" type="loop type" ... } ... {/loop} block
|
||||||
*
|
*
|
||||||
@@ -75,11 +90,13 @@ class TheliaLoop implements SmartyPluginInterface
|
|||||||
|
|
||||||
$this->getLoopArgument($loop, $params);
|
$this->getLoopArgument($loop, $params);
|
||||||
|
|
||||||
$loopResults = $loop->exec();
|
self::$pagination[$name] = null;
|
||||||
|
|
||||||
|
$loopResults = $loop->exec(self::$pagination[$name]);
|
||||||
|
|
||||||
$template->assignByRef($name, $loopResults);
|
$template->assignByRef($name, $loopResults);
|
||||||
}
|
|
||||||
else {
|
} else {
|
||||||
|
|
||||||
$loopResults = $template->getTemplateVars($name);
|
$loopResults = $template->getTemplateVars($name);
|
||||||
|
|
||||||
@@ -97,7 +114,8 @@ class TheliaLoop implements SmartyPluginInterface
|
|||||||
$template->assign('__COUNT__', 1 + $loopResults->key());
|
$template->assign('__COUNT__', 1 + $loopResults->key());
|
||||||
$template->assign('__TOTAL__', $loopResults->getCount());
|
$template->assign('__TOTAL__', $loopResults->getCount());
|
||||||
|
|
||||||
$repeat = $loopResults->valid();
|
//$repeat = $loopResults->valid();
|
||||||
|
$repeat = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($content !== null) {
|
if ($content !== null) {
|
||||||
@@ -139,13 +157,62 @@ class TheliaLoop implements SmartyPluginInterface
|
|||||||
*/
|
*/
|
||||||
public function theliaIfLoop($params, $content, $template, &$repeat)
|
public function theliaIfLoop($params, $content, $template, &$repeat)
|
||||||
{
|
{
|
||||||
|
|
||||||
// When encountering close tag, check if loop has results.
|
// When encountering close tag, check if loop has results.
|
||||||
if ($repeat === false) {
|
if ($repeat === false) {
|
||||||
return $this->checkEmptyLoop($params, $template) ? '' : $content;
|
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
|
* Check if a loop has returned results. The loop shoud have been executed before, or an
|
||||||
* InvalidArgumentException is thrown
|
* InvalidArgumentException is thrown
|
||||||
@@ -210,11 +277,6 @@ class TheliaLoop implements SmartyPluginInterface
|
|||||||
*/
|
*/
|
||||||
protected function getLoopArgument(BaseLoop $loop, $smartyParam)
|
protected function getLoopArgument(BaseLoop $loop, $smartyParam)
|
||||||
{
|
{
|
||||||
$defaultItemsParams = array('required' => true);
|
|
||||||
|
|
||||||
$shortcutItemParams = array('optional' => array('required' => false));
|
|
||||||
|
|
||||||
$errorCode = 0;
|
|
||||||
$faultActor = array();
|
$faultActor = array();
|
||||||
$faultDetails = array();
|
$faultDetails = array();
|
||||||
|
|
||||||
@@ -300,6 +362,7 @@ class TheliaLoop implements SmartyPluginInterface
|
|||||||
new SmartyPluginDescriptor('block', 'loop' , $this, 'theliaLoop'),
|
new SmartyPluginDescriptor('block', 'loop' , $this, 'theliaLoop'),
|
||||||
new SmartyPluginDescriptor('block', 'elseloop' , $this, 'theliaElseloop'),
|
new SmartyPluginDescriptor('block', 'elseloop' , $this, 'theliaElseloop'),
|
||||||
new SmartyPluginDescriptor('block', 'ifloop' , $this, 'theliaIfLoop'),
|
new SmartyPluginDescriptor('block', 'ifloop' , $this, 'theliaIfLoop'),
|
||||||
|
new SmartyPluginDescriptor('block', 'pageloop' , $this, 'theliaPageLoop'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,16 +88,57 @@ An image from asset directory :
|
|||||||
<p>Some pagination</p>
|
<p>Some pagination</p>
|
||||||
<p>PAGE 1</p>
|
<p>PAGE 1</p>
|
||||||
<ul>
|
<ul>
|
||||||
{loop type="category" name="catloopwithpagination" limit="2" page="1"}
|
{loop type="category" name="catloopwithpagination1" limit="2" page="1"}
|
||||||
<li>{$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}</li>
|
<li>{$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}</li>
|
||||||
{/loop}
|
{/loop}
|
||||||
</ul>
|
</ul>
|
||||||
<p>PAGE 2</p>
|
<p>PAGE 2</p>
|
||||||
<ul>
|
<ul>
|
||||||
{loop type="category" name="catloopwithpagination" limit="2" page="2"}
|
{loop type="category" name="catloopwithpagination2" limit="2" page="2"}
|
||||||
<li>{$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}</li>
|
<li>{$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}</li>
|
||||||
{/loop}
|
{/loop}
|
||||||
</ul>
|
</ul>
|
||||||
|
<p>PAGE 1000</p>
|
||||||
|
<ul>
|
||||||
|
|
||||||
|
{loop type="category" name="catloopwithpagination1000" limit="2" page="1000"}
|
||||||
|
<li>{$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}</li>
|
||||||
|
{/loop}
|
||||||
|
|
||||||
|
{elseloop rel="catloopwithpagination1000"}
|
||||||
|
NO RESULTS
|
||||||
|
{/elseloop}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<p>Some pagination with page choice</p>
|
||||||
|
{assign var=current_page value=2}
|
||||||
|
<p>PAGE {$current_page} :</p>
|
||||||
|
<ul>
|
||||||
|
{loop type="category" name="catloopwithpaginationchoice" limit="2" page="{$current_page}"}
|
||||||
|
<li>{$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}</li>
|
||||||
|
{/loop}
|
||||||
|
</ul>
|
||||||
|
<p>page choice</p>
|
||||||
|
{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}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{include file="includes/footer.html"}
|
{include file="includes/footer.html"}
|
||||||
Reference in New Issue
Block a user