<?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 lc_album_images
{
	public $page=1;
	public $total=0;
	public $items_at_page=16;
	public $album_id;
	public $pager=array();
	
	function __construct($page=1)
	{
		if($page) $this->page=$page;
		if(intval(LabConfig::$mcfg['items_at_page'])) $this->items_at_page=intval(LabConfig::$mcfg['items_at_page']);
	}
	
	public function getList()
	{
		if (!$this->total) $this->total=Db::i()->selectCell("SELECT COUNT(*) FROM ?_lc_album_images WHERE album_id=?", $this->album_id);
		if ($this->total)
		{
			$start=$this->_getStart();
			$images=Db::i()->select("SELECT * FROM ?_lc_album_images WHERE album_id=? ORDER BY sort LIMIT ?d, ?d", $this->album_id, $start, $this->items_at_page);
			if ($images)
			{
				foreach($images as $key=>$image)
				{
					$images[$key]['counter']=$start;
					$start++;
				}
				
				return lc_album_image::setImagesPath($images);
			}
			
		}
	}
	
	protected function _getStart()
	{
		$pg=new Paginator($this->total, $this->items_at_page, $this->page);
		$this->pager=$pg->getPages();
		$this->page=$this->pager['current'];
		return $this->pager['sql_start'];
	}
	
	public static function setSort($files)
	{
		if ($files) foreach($files as $image_id=>$sort)
		{
				Db::i()->query("UPDATE ?_lc_album_images SET sort=? WHERE image_id=?", $sort, $image_id);
		}
	}

}