<?php
class Gallery extends Controller {
	public function Index(IAlbumRepository $repo) {
		$albums=$repo->GetAlbums();
		return new View("Gallery/index.view",array("albums"=>$albums));
	}

	public function View($url) {
		$album=new Album($url,true);
		Breadcrumbs::Add($album->AlbumTitle, "");
		return new View("Gallery/view.view",array("album"=>$album));
	}

	public function Manage(IAlbumRepository $albumRepo, IImageRepository $imageRepo, $errors) {
		$vars=array();
		Breadcrumbs::Add("Manage", "");
		$vars["albums"]=$albumRepo->GetAlbums(true,true,true);
		if (isset($errors))
			$vars["errors"]=$errors;
		$vars['images']=$imageRepo->GetImagesByAlbum(0);
		if(isset($image))
			$vars['image']=$image;
		else
			$vars['image']=new Image();
		return new View("Gallery/manage.view",$vars);
	}

	public function Move(IAlbumRepository $albumRepo, $selectedImages, $targetAlbumId){
		if ($selectedImages!=null){
			foreach ($selectedImages as $imgId){
				$image=new Image($imgId);
				$image->AlbumId=$targetAlbumId;
				$image->Save();
			}
		}
		$albums=$albumRepo->GetAlbums(true,true,true);
		return json_encode($albums);
	}

	public function DeleteImages(IAlbumRepository $albumRepo, IImageRepository $imageRepo, $selectedImages){
		$imageRepo->DeleteImages($selectedImages);
		$albums=$albumRepo->GetAlbums(true,true,true);
		return json_encode($albums);
	}

	public function CreateAlbum($title,$description) {
		Breadcrumbs::Add("Manage", "/gallery/manage/");
		Breadcrumbs::Add("Create Album", "");
		$album=new Album();
		if (isset($title))
			$album->AlbumTitle=$title;
		if (isset($description))
			$album->AlbumDescription=$description;
		if ($album->AlbumTitle!="" && $album->AlbumDescription!=""){
			$album->Save();
			header("location:/gallery/manage");
			return;
		}
		return new View("Gallery/create_album.view",array("album"=>$album));
	}

	public function Upload(){
		Breadcrumbs::Add("Manage", "/gallery/manage/");
		Breadcrumbs::Add("Upload", "");
		return new View("Gallery/upload.view", array("maxUploadSize"=>Utils::GetMaxUploadSize()));
	}

	public function UploadImages($title, $description){
		if (count($title)==0)
			return;

		$allErrors=array();
		$uploadedImages=array();
		$files=$_FILES['files'];
		foreach ($files['tmp_name'] as $index=>$tempFile) {
			$filename=$files['name'][$index];
			$imageTitle=$title[$index];
			$errors=array();

			if ($imageTitle=="")
				$errors[]="The image $filename doesn't have a title";
			if ($files['error'][$index])
				$errors[]=Utils::FileUploadErrorToMessage($files['error'][$index]);

			if (count($errors)==0) {
				if(!Image::IsValidType(pathinfo($filename,PATHINFO_EXTENSION)))
					$errors[]="File is of an invalid type";
				if (getimagesize($tempFile)===false)
					$errors[]="File is not an image";
			}

			if (count($errors)>0){
				$allErrors=array_merge($allErrors, $errors);
				continue;
			}
			$uploadedImages[]=$index;

			$image=new Image($filename,$tempFile);
			$image->ImageTitle=$imageTitle;
			$image->ImageDescription=$description[$index];
			$image->Save();
		}

		return json_encode(array("errors"=>$allErrors, "uploaded"=>$uploadedImages));
	}
}
