pagination

This commit is contained in:
Etienne Roudeix
2013-06-27 13:18:29 +02:00
parent 3aca4bfb25
commit 3fb5466ef3
4 changed files with 134 additions and 21 deletions

View File

@@ -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();

View File

@@ -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();

View File

@@ -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'),
);
}
}