<?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 extends DefaultObject
{
	protected $allowed_keys=array('name', 'describe', 'logo', 'sort');
	
	public function __construct($album_id=null)
	{
		$this->data['id']=&$this->data['album_id'];
		if ($album_id) {
			$this->loadDataById($album_id);
		}
	}
	
	public function __set($key, $value)
	{
		parent::__set($key, $value);
		if ('describe'==$key) {
			$this->data['describe_html']=getbb($value);
		} elseif ('logo'==$key && $value) {
			$this->data['logo_file']=config::get('basepath').config::get('logo_path').'/'.$value;
		}
	}
	
	public function loadDataById($album_id)
	{
		$info=Db::i()->selectRow("SELECT * FROM ".config::get('table_albums')." WHERE album_id=?", $album_id);
		if (!$info) return false;
		$this->set($info);
		unset($info);
		return $this;
	}

	public function addAlbum($data)
	{
		$data['album_id']=Db::i()->query(
			"INSERT INTO ".config::get('table_albums')."
			(name, `describe`)
			VALUES(?, ?)",
			$data['name'],
			$data['describe'],
			time()
		);
		if (!$data['album_id']) return false;
		$album=Factory::create('lc_album.album');
		$album->set($data);
		$album->update(array('sort'=>$album->id));
		return $album;
	}
	
	public function delete()
	{
		//TODO тут сделать вызов списка изображений и удаление каждого изображения (из-за комментариев и рейтинга)
		$files=Db::i()->selectCol("SELECT filename FROM ".config::get('table_images')." WHERE album_id=?", $this->id);
		if ($files) {
			foreach ($files as $filename) {
				lc_album_image::deleteFiles($filename);
			}
			Db::i()->query("DELETE FROM ".config::get('table_images')." WHERE album_id=?", $this->id);
		}
		Db::i()->query("DELETE FROM ".config::get('table_albums')." WHERE album_id=?", $this->id);
	}
	
	public function setLogo($img)
	{
		$dir=LAB_PATH_ROOT.config::get('basepath').config::get('logo_path');
		if (!is_dir($dir))
			if (!mkdir($dir, 0777)) return LabCMS::i()->error(sprintf(_m("Не могу создать директорию %s"), $dir));
		$this->update(array('logo'=>$img->filename));
		copy(LAB_PATH_ROOT.$img->files['small'], $dir.'/'.$img->filename);
		return true;
	}
	
	public function moveUp()
	{
		if (
			$prev_id=Db::i()->selectCell(
				"SELECT album_id
				FROM ".config::get('table_albums')."
				WHERE sort<?
				ORDER BY sort DESC
				LIMIT 1", $this->sort
			)
		) {
			$this->_swapSort($prev_id);
		}
	}
	
	public function moveDown()
	{
		if (
			$next_id=Db::i()->selectCell(
				"SELECT album_id, sort
				FROM ".config::get('table_albums')."
				WHERE sort>?
				ORDER BY sort LIMIT 1",
				$this->sort
			)
		) {
			$this->_swapSort($next_id);
		}
	}
	
	public function getArray()
	{
		return $this->data;
	}
	
	protected function _swapSort($id)
	{
		$album=Factory::create('lc_album.album', $id);
		$old_sort=$this->sort;
		$this->update(array('sort'=>$album->sort));
		$album->update(array('sort'=>$old_sort));
	}
	
	protected function _updateDB(&$data)
	{
		return Db::i()->query(
			"UPDATE ".config::get('table_albums')." SET ?a, `updated`=?d WHERE album_id=?", $data, time(), $this->id
		);
	}
	
	protected function _resetItemCache()
	{
	}

}
