<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

class admin_pages_list
{
	protected $items_at_page=30;
	protected $page=1;
	protected $total;
	
	public $pager;
	
	public function __construct($page=1)
	{
		$this->setPage($page);
	}
	
	public function setPage($page)
	{
		$this->page=$page;
	}
	
	public function setItemsAtPage($count)
	{
		$this->items_at_page=$count;
	}
	
	public function setTotal($count)
	{
		$this->total=$count;
	}
	
	public function getList($filter=null)
	{
		if (!$this->total) {
			$this->total=Db::i()->selectCell(
				"SELECT COUNT(page_id) FROM ?_pages
				{WHERE title LIKE ?}", $filter?"%$filter%":DBSIMPLE_SKIP
			);
		}

		if (!$this->total) return;
		
		$this->_setPager();

		$result=Db::i()->select(
			"SELECT *
			FROM ?_pages p
			LEFT JOIN ?_tree t ON p.tree_id=t.tree_id
			{WHERE title LIKE ?}
			ORDER BY path
			LIMIT ?d, ?d",
			$filter?"%$filter%":DBSIMPLE_SKIP, $this->pager['sql_start'], $this->pager['sql_limit']
		);
		
		if ($result) {
			foreach ($result as $item) {
				$obj=Factory::create('admin_pages.page');
				$obj->set($item);
				$news[$obj->id]=$obj;
			}
		}
		
		return $news;
	}
	
	protected function _setPager()
	{
		if (!$this->items_at_page) $this->items_at_page=config::get('news_at_page');
		$pg=Factory::create('Paginator', $this->total, $this->items_at_page, $this->page);
		$this->pager=$pg->getPages();
	}
}