<?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_album
{
	function __construct($album_id=null)
	{
		if ($album_id)
		{
			if ($info=$this->getAlbumInfo($album_id))
			{
				foreach($info as $key=>$item)
				{
					$this->$key=$item;
				}
			}
		}
	}

	public function getAlbumInfo($album_id)
	{
		$info=Db::i()->selectRow("SELECT album_id as id, album_id, name, `describe`, logo, sort FROM ?_lc_album_albums WHERE album_id=?", $album_id);
		$info['describe_html']=getbb($info['describe']);
		if ($info['logo']) $info['logo_file']=lc_album_main::$basepath.lc_album_main::$logo_path.'/'.$info['logo'];
		return $info;
	}
	
	public static function addAlbum($data)
	{
		$album_id=Db::i()->query("INSERT INTO ?_lc_album_albums (name, `describe`, updated) VALUES(?, ?, NOW())", $data['name'], $data['describe']);
		Db::i()->query("UPDATE ?_lc_album_albums SET sort=album_id WHERE album_id=?", $album_id);
		return $album_id;
	}
	
	public function editAlbum($data)
	{
		return Db::i()->query("UPDATE ?_lc_album_albums SET name=?, `describe`=? WHERE album_id=?", $data['name'], $data['describe'], $this->id);
	}
	
	public function deleteAlbum()
	{
		$files=Db::i()->selectCol("SELECT filename FROM ?_lc_album_images WHERE album_id=?", $this->id);
		if ($files)
		{
			foreach($files as $filename)
			{
				lc_album_image::deleteFiles($filename);
			}
			Db::i()->query("DELETE FROM ?_lc_album_images WHERE album_id=?", $this->id);
		}
		Db::i()->query("DELETE FROM ?_lc_album_albums WHERE album_id=?", $this->id);
	}
	
	public static function getAllAlbums()
	{
		return Db::i()->selectCol("SELECT album_id AS ARRAY_KEY, name FROM ?_lc_album_albums");
	}
	
	public static function setLogo($image_id)
	{
		if ($img=new lc_album_image($image_id))
		{
			$dir=LAB_PATH_ROOT.lc_album_main::$basepath.lc_album_main::$logo_path;
			if (!is_dir($dir))
				if (!mkdir($dir, 0777)) return LabCMS::i()->error(sprintf(_m("Не могу создать директорию %s"), $dir), true);
			Db::i()->query("UPDATE ?_lc_album_albums SET logo=? WHERE album_id=?", $img->filename, $img->album_id);
			copy(LAB_PATH_ROOT.$img->files['small'], $dir.'/'.$img->filename);
			return $img->album_id;
		}
		return LabCMS::i()->error("Изобрежение не найдено");
	}
	
	public function moveUp()
	{
		if ($prev=Db::i()->selectRow("SELECT album_id, sort FROM ?_lc_album_albums WHERE sort<? ORDER BY sort DESC LIMIT 1", $this->sort))
		{
			$this->_updateSort($prev['album_id'], $prev['sort']);
		}
	}
	
	public function moveDown()
	{
		if ($next=Db::i()->selectRow("SELECT album_id, sort FROM ?_lc_album_albums WHERE sort>? ORDER BY sort LIMIT 1", $this->sort))
		{
			$this->_updateSort($next['album_id'], $next['sort']);
		}
	}
	
	protected function _updateSort($album_id, $sort)
	{
		Db::i()->query("UPDATE ?_lc_album_albums SET sort=? WHERE album_id=?", $this->sort, $album_id);
		Db::i()->query("UPDATE ?_lc_album_albums SET sort=? WHERE album_id=?", $sort, $this->id);
	}
}
