<commit_before><?php 
/*
Image Database Controller

Author: <NAME>
Url: http://denbeke.be
Date: June 2014
*/

namespace Database;

require_once(__DIR__ . '/../model/album.php');


/**
Image Database Controller
*/
class Album {

	const ALBUMS = 'Albums';

		
	/**
	Check if the album with the given id exists
	
	@param id
	@return exists
	*/
	static public function albumNameExists($name) {
		
		$query = BUILDER::table(self::ALBUMS)->where('name', '=', $name);
		$count = $query->count();
		
		if($count >= 1) {
			return true;
		}
		else {
			return false;
		}
		
	}
	
	
	
	/**
	Add a new photo album to the database with the given name
	@param name
	@pre there is no album with the given name
	@pre album name cannot be empty
	*/
	static public function addAlbum($albumName) {
		
		if(self::ALBUMSNameExists($albumName)) {
			
			throw new Exception("There is already an album with the name '$albumName'");
		
		}
		if($albumName == '') {
			
			throw new Exception("Album name cannot be empty");
			
		}
		else {
		
			//TODO!!!
		
		}
		
	}
	
	
	/**
	Get a list of all albums
	
	@return albums (with only the latest image included)
	*/
	static public function getAllAlbums() {
		
		//Get all albums		
		$query = BUILDER::table(self::ALBUMS)->select('*')->orderBy('date', 'DESC');
		$result = $query->get();
		
		$albums = self::resultToAlbum($result);
		
		//Add the latest image to each album
		foreach ($albums as $album) {
			$album->addImage(Image::getLatestImage($album->getId()));
		}
		
		return $albums;
		
	}
	
	
	
	/**
	Get the album with the given id
	
	@param id
	@return album
	
	@pre id exists
	*/
	static public function getAlbum($id) {
		
		$query = BUILDER::table(self::ALBUMS)->where('id', '=', $id);
		$result = $query->get();
		
		if(sizeof($result) != 1) {
			throw new \exception("Could not find album with the given id($id)");
		}
		else {
			
			$album =  Album::resultToAlbum($result)[0];	
			$images = Image::getImagesFromAlbum($id);
			
			foreach ($images as $image) {
				$album->addImage($image);
			}
			
			return $album;
		}
		
	}
	
	
	
	/**
	Delete the album with the given id
	
	Note: this function does not delete the album folder
	
	@param id   
	@pre album exists
	*/
	public function deleteAlbum($id) {
		if(!$this->albumExists($id)) {
			throw new Exception("There is no album with the id $id");
		}
		else {
			//Delete the images from the database
			Image::deleteImagesFromAlbum($id);
			
			//Delete the album from the database
			$query = BUILDER::table(self::ALBUMS)->where('id', '=', $id);
			$result = $query->delete();
		}
		
		//TODO NOT YET TESTED
		
	}
	
	
	
	
	/**
	Convert the database result to an instance of Album

	@param result
	@return Album
	*/
	static private function resultToAlbum($result) {

		$output = [];

		foreach ($result as $album) {
<commit_msg>Database: implemented getAlbumName<commit_after><?php 
/*
Image Database Controller

Author: <NAME>
Url: http://denbeke.be
Date: June 2014
*/

namespace Database;

require_once(__DIR__ . '/../model/album.php');


/**
Image Database Controller
*/
class Album {

	const ALBUMS = 'Albums';

		
	/**
	Check if the album with the given id exists
	
	@param id
	@return exists
	*/
	static public function albumNameExists($name) {
		
		$query = BUILDER::table(self::ALBUMS)->where('name', '=', $name);
		$count = $query->count();
		
		if($count >= 1) {
			return true;
		}
		else {
			return false;
		}
		
	}
	
	
	/**
	
	*/
	static public function albumExists($id) {
	
		$query = BUILDER::table(self::ALBUMS)->where('id', '=', $id);
		$count = $query->count();
		
		if($count >= 1) {
			return true;
		}
		else {
			return false;
		}
	
	}
	
	
	/**
	Add a new photo album to the database with the given name
	@param name
	@pre there is no album with the given name
	@pre album name cannot be empty
	*/
	static public function addAlbum($albumName) {
		
		if(self::ALBUMSNameExists($albumName)) {
			
			throw new Exception("There is already an album with the name '$albumName'");
		
		}
		if($albumName == '') {
			
			throw new Exception("Album name cannot be empty");
			
		}
		else {
		
			//TODO!!!
		
		}
		
	}
	
	
	/**
	Get a list of all albums
	
	@return albums (with only the latest image included)
	*/
	static public function getAllAlbums() {
		
		//Get all albums		
		$query = BUILDER::table(self::ALBUMS)->select('*')->orderBy('date', 'DESC');
		$result = $query->get();
		
		$albums = self::resultToAlbum($result);
		
		//Add the latest image to each album
		foreach ($albums as $album) {
			$album->addImage(Image::getLatestImage($album->getId()));
		}
		
		return $albums;
		
	}
	
	
	
	/**
	Get the album with the given id
	
	@param id
	@return album
	
	@pre id exists
	*/
	static public function getAlbum($id) {
		
		$query = BUILDER::table(self::ALBUMS)->where('id', '=', $id);
		$result = $query->get();
		
		if(sizeof($result) != 1) {
			throw new \exception("Could not find album with the given id($id)");
		}
		else {
			
			$album =  Album::resultToAlbum($result)[0];	
			$images = Image::getImagesFromAlbum($id);
			
			foreach ($images as $image) {
				$album->addImage($image);
			}
			
			return $album;
		}
		
	}
	
	
	
	/**
	Delete the album with the given id
	
	Note: this function does not delete the album folder
	
	@param id   
	@pre album exists
	*/
	public function deleteAlbum($id) {
		if(!$this->albumExists($id)) {
			throw new Exception("There is no album with the id $id");
		}
		else {
			//Delete the images from the database
			Image::deleteImagesFromAlbum($id);
			
			//Delete the album from the database
			$query = BUILDER::table(self::ALBUMS)->where('id', '=', $id);
			$result = $query->delete();
		}
		
		//TODO NOT YET TESTED
		
	}
	
	
	
	
	/**
	Change update the information of the given album in the database
	
	@param album
	@param new album name
	
	@pre album exists
	@pre there is no album with the new name
	@pre new album name not empty
	*/
	public function changeAlbumName($id, $albumName) {
		if(!self::albumExists($id)) {
			throw new Exception("There is no album with the id $id");
		}
		elseif(self::albumNameExists($albumName)) {
			throw new Exception("There is already an album with the new name '$albumName'");
		}
		elseif($albumName == '') {
			throw new Exception("Album name cannot be empty");
		}
		else {
			
			//Get the old album name
			$oldAlbum = self::getAlbumName($id);
			
			//Check if name is not the same, if so, we can return immediately
			if ($oldAlbum == $albumName) {
				return;
			}
		
		
			//TODO: update the album name in the database

			
		}
	}

	
	
	/**
	Get the name of the album with the given id
	
	@param id
	@return album  name
	@pre albume exists
	*/
	static public function getAlbumName($id) {
		
		if(!self::albumExists($id)) {
			throw new Exception("There is no album with the id $id");
		}
		else {
	
			$query = BUILDER::table(self::ALBUMS)->where('id', '=', $id);
			$result = $query->get();
			$name = $result[0]->name;
			
			return $name;
			
		}
		
	}
	
	
	
	
	/**
	Convert the database result to an instance of Album

	@param result
	@return Album
	*/
	static private function resultToAlbum($result) {

		$output = [];

		foreach ($result as $album) {

{'fraction_non_alphanumeric': 0.1447881828316611, 'fraction_numerical': 0.002229654403567447, 'mean_word_length': 2.924002186987425, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '776209', 'n_tokens_mistral': 2829, 'n_tokens_neox': 2325, 'n_words': 722}