2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
namespace Icinga\Web\Paginator\Adapter;
|
2013-06-11 11:09:28 +02:00
|
|
|
|
2013-08-21 01:07:01 +02:00
|
|
|
use Zend_Paginator_Adapter_Interface;
|
2015-05-07 14:45:47 +02:00
|
|
|
use Icinga\Data\QueryInterface;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-08-21 01:07:01 +02:00
|
|
|
class QueryAdapter implements Zend_Paginator_Adapter_Interface
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
/**
|
2015-05-07 14:45:47 +02:00
|
|
|
* The query being paginated
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2015-05-07 14:45:47 +02:00
|
|
|
* @var QueryInterface
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2015-05-07 14:45:47 +02:00
|
|
|
protected $query;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Item count
|
|
|
|
*
|
2015-05-07 14:45:47 +02:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $count;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new QueryAdapter
|
|
|
|
*
|
|
|
|
* @param QueryInterface $query The query to paginate
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2015-05-07 14:45:47 +02:00
|
|
|
public function __construct(QueryInterface $query)
|
|
|
|
{
|
|
|
|
$this->setQuery($query);
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
/**
|
2015-05-07 14:45:47 +02:00
|
|
|
* Set the query to paginate
|
|
|
|
*
|
|
|
|
* @param QueryInterface $query
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2015-05-07 14:45:47 +02:00
|
|
|
* @return $this
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2015-05-07 14:45:47 +02:00
|
|
|
public function setQuery(QueryInterface $query)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
$this->query = $query;
|
2015-05-07 14:45:47 +02:00
|
|
|
return $this;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-07 14:45:47 +02:00
|
|
|
* Return the query being paginated
|
2013-06-07 11:44:37 +02:00
|
|
|
*
|
2015-05-07 14:45:47 +02:00
|
|
|
* @return QueryInterface
|
|
|
|
*/
|
|
|
|
public function getQuery()
|
|
|
|
{
|
|
|
|
return $this->query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and return the rows in the given range of the query result
|
|
|
|
*
|
|
|
|
* @param int $offset Page offset
|
|
|
|
* @param int $itemCountPerPage Number of items per page
|
|
|
|
*
|
|
|
|
* @return array
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
|
|
|
public function getItems($offset, $itemCountPerPage)
|
|
|
|
{
|
|
|
|
return $this->query->limit($itemCountPerPage, $offset)->fetchAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-07 14:45:47 +02:00
|
|
|
* Return the total number of items in the query result
|
2013-06-07 11:44:37 +02:00
|
|
|
*
|
2015-05-07 14:45:47 +02:00
|
|
|
* @return int
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
if ($this->count === null) {
|
|
|
|
$this->count = $this->query->count();
|
|
|
|
}
|
2015-05-07 14:45:47 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
return $this->count;
|
|
|
|
}
|
|
|
|
}
|