<?php
class Album extends DBObjectAutoCreate {
	public $Images=array();
	
	public static function GetAlbums($includeHidden=false, $includeEmpty=false) {
		self::CreateTable("albums");
		$albums=array();
		$hidden=$includeHidden?" AND album_hidden=1":"";
		$albumIds=self::$PDO->query("SELECT album_id FROM albums WHERE album_deleted=0".$hidden);
		while ($albumId=$albumIds->fetchColumn()){
			$album=new Album($albumId);
			if ($includeEmpty || count($album->Images)>0)
				$albums[]=$album;
		}
		return $albums;
	}
	
	public function __construct($id=0,$forceUrl=false) {
		$field="album_id";
		if ($forceUrl || !is_numeric($id))
			$field="album_url";
		parent::__construct("albums", $field, $id);
		
		$this->Images=Image::GetImagesByAlbum($this->AlbumId);
	}
	
	public function Save() {
		if (!isset($this->AlbumUrl) || $this->AlbumUrl==null || $this->AlbumUrl=="")
			$this->AlbumUrl=Utils::MakeUniqueURL("albums", "album_url", $this->AlbumTitle);
		parent::Save();
	}
}
