<?php
/* DatabaseCall class is a generic called procedure library. It sets a database connection
and provides a library of SQL calls. SQL Calls return a structured array of data.
Although this class is writen for MySql databases it could be easly refactored to utilise 
any database system.

Instantiate the class with the database connection variables to connect.
Once connected this object will hold the db connect ready for SQL calls.
*/

class DatabaseCall extends MySqlDatabase {
	
	var $sHost;
	var $sUser;
	var $sPass;
	var $sName;
	var $iResultsCount;
	var $aDistinctFilters;
	var $bConnected = false;
	var $LOG;
	
	function __construct($oLog, $sHost, $sUser, $sPass, $sName) {
		
		$this->LOG = $oLog;
		
		$this->sHost = $sHost;
		$this->sUser = $sUser;
		$this->sPass = $sPass;
		$this->sName = $sName;
		$this->_Connect();
		return;
	}
	
	// Procedural Call libraries.
	// 
	// ***************************************************************************
	// ******* Loganc Procedural Call library. 
	// ******* Version 2.0 May 2014 LoganC.
	// ***************************************************************************
	// Updated Loganc 1-10-2018 (post site live)
	//
	// *********************************** BLOGS ****************************** 
	
	function GetBlog($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT			blogs.id,
						blogs.name,
						blogs.description, 
						blogs.blog_date,
						blogs.blog_year,
						blogs.blog_country,
						blogs.blog_region,
						blogs.set_image,
						blogs.set_background,
						blogs.url,
						blogs.auth
		FROM			blogs
		WHERE 			blogs.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aDb);
	}
	
	function GetBlogs() {
		
		$query = "
		SELECT			blogs.id,
						blogs.name,
						blogs.description, 
						blogs.blog_date,
						blogs.blog_year,
						blogs.blog_country,
						blogs.blog_region,
						blogs.set_image,
						blogs.set_background,
						blogs.url,
						blogs.auth
		FROM			blogs
		ORDER BY		blogs.blog_date DESC
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToListHash($aDb);
	}

	// Insert new blog. 
	function InsertBlog($name, $description, $blog_date, $blog_year, $blog_country, $blog_region, $set_image, $set_background, $url, $auth) {
		
		$name = $this->_Safe($name);
		$description = $this->_Safe($description);
		$blog_date = $this->_Safe($blog_date);
		$blog_year = $this->_Safe($blog_year);
		$blog_country = $this->_Safe($blog_country);
		$blog_region = $this->_Safe($blog_region);
		$set_image = $this->_Safe($set_image);
		$set_background = $this->_Safe($set_background);
		$url = $this->_Safe($url);
		$auth = $this->_Safe($auth);
		
		$query = "
		INSERT INTO 	blogs
		VALUES 			('', $name, $description, $blog_date, $blog_year, $blog_country, $blog_region, $set_image, $set_background, $url, $auth) 
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Update blog. 
	function UpdateBlog($id, $name = '', $description = '', $blog_date = '', $blog_year = '', $blog_country = '', $blog_region = '', $set_image = '', $set_background = '', $url = '', $auth = 'admin') {
		
		$id = $this->_Safe($id);
		$name = $this->_Safe($name);
		$description = $this->_Safe($description);
		$blog_date = $this->_Safe($blog_date);
		$blog_year = $this->_Safe($blog_year);
		$blog_country = $this->_Safe($blog_country);
		$blog_region = $this->_Safe($blog_region);
		$set_image = $this->_Safe($set_image);
		$set_background = $this->_Safe($set_background);
		$url = $this->_Safe($url);
		$auth = $this->_Safe($auth);
		
		$query = "
		UPDATE 			blogs
		SET				blogs.name = $name,
						blogs.description = $description,
						blogs.blog_date = $blog_date,
						blogs.blog_year = $blog_year,
						blogs.blog_country = $blog_country,
						blogs.blog_region = $blog_region,
						blogs.set_image = $set_image,
						blogs.set_background = $set_background,
						blogs.url = $url,
						blogs.auth = $auth
		WHERE 			blogs.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Delete comment
	function DeleteBlog($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		DELETE FROM		blogs
		WHERE 			blogs.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// *********************************** BLOGS ENTRIES ****************************** 
	
	function GetBlogEntry($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT			blog_entries.id,
						blog_entries.blog_id,
						blog_entries.entry_date, 
						blog_entries.name,
						blog_entries.description,
						blog_entries.set_image,
						blog_entries.comments_id,
						blog_entries.url,
						blog_entries.auth
		FROM			blog_entries
		WHERE 			blog_entries.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aDb);
	}
	
	function GetBlogEntryCommentId($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT			blog_entries.id,
						blog_entries.blog_id,
						blog_entries.entry_date, 
						blog_entries.name,
						blog_entries.description,
						blog_entries.set_image,
						blog_entries.comments_id,
						blog_entries.url,
						blog_entries.auth
		FROM			blog_entries
		WHERE 			blog_entries.comments_id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aDb);
	}
	
	function GetBlogEntryUrl($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT			blog_entries.url AS url
		FROM			blog_entries
		WHERE 			blog_entries.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['url'];
	}
	
	function GetBlogSearchLimited($id) {
	
		$id = $this->_Safe($id);
		
		$query = "
		SELECT			blogs.name AS blog_name,
						blog_entries.name AS entry_name,
						blog_entries.set_image AS set_image,
						blog_entries.entry_date AS entry_date,
						blog_entries.description As description,
						blog_entries.url AS url
		FROM			blog_entries
		LEFT JOIN		blogs
		ON				blog_entries.blog_id = blogs.id
		WHERE 			blog_entries.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aDb);	
	}
	
	function GetBlogEntries($blog_id) {
		
		$id = $this->_Safe($blog_id);
	
		$query = "
		SELECT			timestamp('blog_entries.entry_date') AS 'timestmap',
						blog_entries.id,
						blog_entries.blog_id,
						blog_entries.entry_date, 
						blog_entries.name,
						blog_entries.description,
						blog_entries.set_image,
						blog_entries.comments_id,
						blog_entries.url,
						blog_entries.auth
		FROM			blog_entries
		WHERE			blog_entries.blog_id = $blog_id
		ORDER BY 		1 ASC
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToListHash($aDb);
	}

	// Insert new blog entry. 
	function InsertBlogEntry($blog_id, $entry_date, $name, $description, $set_image, $comments_id, $url, $auth) {
		
		$blog_id = $this->_Safe($blog_id);
		$entry_date = $this->_Safe($entry_date);
		$name = $this->_Safe($name);
		$description = $this->_Safe($description);
		$set_image = $this->_Safe($set_image);
		$comments_id = $this->_Safe($comments_id);
		$url = $this->_Safe($url);
		$auth = $this->_Safe($auth);
		
		$query = "
		INSERT INTO 	blog_entries
		VALUES 			('', $blog_id, $entry_date, $name, $description, $set_image, $comments_id, $url, $auth) 
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Update auth for all blog entries.
	function UpdateBlogEntryAuth($blog_id, $auth) {
	
		$blog_id = $this->_Safe($blog_id);
		$auth = $this->_Safe($auth);
		
		$query = "
		UPDATE 			blog_entries
		SET				blog_entries.auth = $auth
		WHERE 			blog_entries.blog_id = $blog_id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Update blog entry. 
	function UpdateBlogEntry($id, $blog_id, $entry_date, $name, $description, $set_image, $comments_id, $url, $auth) {
		
		$id = $this->_Safe($id);
		$blog_id = $this->_Safe($blog_id);
		$entry_date = $this->_Safe($entry_date);
		$name = $this->_Safe($name);
		$description = $this->_Safe($description);
		$set_image = $this->_Safe($set_image);
		$comments_id = $this->_Safe($comments_id);
		$url = $this->_Safe($url);
		$auth = $this->_Safe($auth);
		
		$query = "
		UPDATE 			blog_entries
		SET				blog_entries.blog_id = $blog_id, 
						blog_entries.entry_date = $entry_date,
						blog_entries.name = $name,
						blog_entries.description = $description, 
						blog_entries.set_image = $set_image, 
						blog_entries.comments_id = $comments_id,
						blog_entries.url = $url,
						blog_entries.auth = $auth
		WHERE 			blog_entries.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Update blog entry set image. 
	function SetBlogEntryImage($id, $set_image) {
		
		$id = $this->_Safe($id);
		$set_image = $this->_Safe($set_image);
	
		$query = "
		UPDATE 			blog_entries
		SET				blog_entries.set_image = $set_image
		WHERE 			blog_entries.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Delete blog entry
	function DeleteBlogEntry($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		DELETE FROM		blog_entries
		WHERE 			blog_entries.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Delete all blog entries for blog_id
	function DeleteAllBlogEntry($blog_id) {
		
		$blog_id = $this->_Safe($blog_id);
		
		$query = "
		DELETE FROM		blog_entries
		WHERE 			blog_entries.blog_id = $blog_id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}	
		
	// *********************************** COMMENTS ****************************** 
	
	function GetComment($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT			comments.id,	
						comments.thread_id,
						comments.thread_type,
						comments.comment,
						comments.username,
						comments.name,
						comments.email,
						comments.created, 
						comments.modified
		FROM			comments
		WHERE 			comments.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aDb);
	}
	
	function GetComments($thread_id, $thread_type) {
		
		$thread_id = $this->_Safe($thread_id);
		$thread_type = $this->_Safe($thread_type);
		
		$query = "
		SELECT			comments.id,			
						comments.thread_id,
						comments.thread_type,
						comments.comment,
						comments.username,
						comments.name,
						comments.email,
						comments.created, 
						comments.modified
		FROM			comments
		WHERE 			comments.thread_id = $thread_id
		AND				comments.thread_type = $thread_type
		ORDER BY		comments.created
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToListHash($aDb);
	}
	
	function GetCommentsAllTypes($thread_id) {
		
		$thread_id = $this->_Safe($thread_id);
		
		$query = "
		SELECT			comments.id,			
						comments.thread_id,
						comments.thread_type,
						comments.comment,
						comments.username,
						comments.name,
						comments.email,
						comments.created, 
						comments.modified
		FROM			comments
		WHERE 			comments.thread_id = $thread_id
		ORDER BY		comments.created
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToListHash($aDb);
	}
	
	function GetAllThreads() {
		
		$query = "
		SELECT			DISTINCT comments.thread_id
		FROM 			comments
		ORDER BY 		comments.created DESC
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToList($aDb, 'thread_id');
	}
	
	function GetThreadType($thread_id) {
		
		$thread_id = $this->_Safe($thread_id);
		
		$query = "
		SELECT			comments.thread_type
		FROM 			comments
		WHERE 			comments.thread_id = $thread_id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['thread_type'];
	}	
	
	function GetThreadMessageCount($thread_id) {
		
		$thread_id = $this->_Safe($thread_id);
		
		$query = "
		SELECT			COUNT(comments.id) AS count
		FROM 			comments
		WHERE 			comments.thread_id = $thread_id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['count'];
	}	
	
	// Fetch highest thread_id from comments.
	function GetCommentsHighestId() {
		
		$query = "
		SELECT 			MAX(thread_id) AS thread_id 
		FROM 			comments
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['thread_id'];
	}

	// Insert comment. 
	function InsertComment($thread_id, $thread_type, $comment, $username, $name, $email) {
		
		$thread_id = $this->_Safe($thread_id);
		$thread_type = $this->_Safe($thread_type);
		$comment = $this->_Safe($comment);
		$username = $this->_Safe($username);
		$name = $this->_Safe($name);
		$email = $this->_Safe($email);
		
		$query = "
		INSERT INTO 	comments
		VALUES 			('', $thread_id, $thread_type, $comment, $username, $name, $email, now(), now()) 
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Update comment. 
	function UpdateComment($id, $comment) {
		
		$id = $this->_Safe($id);
		$comment = $this->_Safe($comment);
		
		$query = "
		UPDATE 			comments
		SET				comments.comment = $comment
		WHERE 			comments.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Delete comment
	function DeleteComment($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		DELETE FROM		comments
		WHERE 			comments.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}

	// Delete comment thread. 
	function DeleteCommentThread($thread_id, $thread_type) {
		
		$thread_id = $this->_Safe($thread_id);
		$thread_type = $this->_Safe($thread_type);
		
		$query = "
		DELETE FROM		comments
		WHERE			comments.thread_id = $thread_id 
		AND				comments.thread_type = $thread_type 			
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Delete reference to thread from Images and blog_entries tables;
	function DeleteAllThreadRef($thread_id) {
		
		$thread_id = $this->_Safe($thread_id);
		
		$query = "
		UPDATE			images
		SET				images.message_id = '0'
		WHERE			images.message_id = $thread_id  			
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		
		$query = "
		UPDATE			blog_entries
		SET				blog_entries.comments_id = '0'
		WHERE			blog_entries.comments_id = $thread_id  			
		";
		
		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		
		return true;
	}
	
	// *********************************** LINKS ***************************************
	// Get links. 
	function GetLinks($type, $type_id) {
		
		$type = $this->_Safe($type);
		$type_id = $this->_Safe($type_id);
		
		$query = "
		SELECT			links.id,			
						links.name,
						links.url
		FROM			links
		WHERE 			links.type = $type
		AND				links.type_id = $type_id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb;
	}

	// Insert link. 
	function InsertLink($type, $type_id, $name, $url) {
		
		$type = $this->_Safe($type);
		$type_id = $this->_Safe($type_id);
		$name = $this->_Safe($name);
		$url = $this->_Safe($url);
		
		$query = "
		INSERT INTO 	links
		VALUES 			('', $type,$type_id,$name,$url) 
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Update link. 
	function UpdateLink($id, $type, $type_id, $name, $url) {
		
		$type = $this->_Safe($type);
		$type_id = $this->_Safe($type_id);
		$name = $this->_Safe($name);
		$url = $this->_Safe($url);
		
		$query = "
		UPDATE 			links
		SET				links.type = $type,
						links.type_id = $type_id,
						links.name = $name,
						links.url = $url
		WHERE 			links.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Delete link. 
	function DeleteLink($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		DELETE FROM		links
		WHERE 			links.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}

	// Delete link. 
	function DeleteLinksForTypeId($type, $type_id) {
		
		$type = $this->_Safe($type);
		$type_id = $this->_Safe($type_id);
		
		$query = "
		DELETE FROM		links
		WHERE			links.type = $type 			
		AND				links.type_id = $type_id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// ***************************** Video_config *******************************
	
	// Get video configs 
	function GetVideoConfig() {
		
		$query = "
		SELECT			video_config.key,			
						video_config.value,
						video_config.type
		FROM			video_config
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToListHash($aDb);
	}

	// Insert video config
	function InsertVideoConfig($key, $value, $type) {
		
		$key = $this->_Safe($key);
		$value = $this->_Safe($value);
		$type = $this->_Safe($type);
		
		$query = "
		INSERT INTO 	video_config
		VALUES 			($key,$value,$type) 
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Update video config
	function UpdateVideoConfig($key, $value) {
		
		$key = $this->_Safe($key);
		$value = $this->_Safe($value);
		
		$query = "
		UPDATE 			video_config
		SET				video_config.value = $value
		WHERE 			video_config.key = $key
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Delete video config. 
	function DeleteVideoConfig($key) {
		
		$key = $this->_Safe($key);
		
		$query = "
		DELETE FROM		video_config
		WHERE 			video_config.key = $key
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	
	// ***************************** Keywords ***********************************
	
	// Fetch keyword set given set name. 
	function GetKeywordSet($name) {
		
		// Convert string to lowercase with capital first letters for each word.
		$name = $this->_ItemDisplay($name);
		
		$name = $this->_Safe($name);
		
		$query = "
		SELECT 			keyword_sets.name,
						keyword_sets.keywords
		FROM			keyword_sets
		WHERE 			keyword_sets.name = $name
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['keywords'];
	}
	
	// Insert new keyword set.  
	function InsertKeywordSet($name, $keywords) {
		
		// Convert string to lowercase with capital first letters for each word.
		$name = $this->_ItemDisplay($name);
		
		$name = $this->_Safe($name);
		$keywords = $this->_Safe($keywords);
		
		$query = "
		INSERT INTO		keyword_sets
		VALUES			($name, $keywords)
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Update keyword set
	function UpdateKeywordSet($name, $keywords) {
	
		// Convert string to lowercase with capital first letters for each word.
		$name = $this->_ItemDisplay($name);
		
		$name = $this->_Safe($name);
		$keywords = $this->_Safe($keywords);
		
		$query = "
		UPDATE			keyword_sets
		SET				name = $name, keywords = $keywords
		WHERE			keyword_sets.name = $name
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}

	// Delete keyword set
	function DeleteKeywordSet($name) {
	
		// Convert string to lowercase with capital first letters for each word.
		$name = $this->_ItemDisplay($name);
		
		$name = $this->_Safe($name);
		
		$query = "
		DELETE FROM		keyword_sets
		WHERE			keyword_sets.name = $name
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Fetch image_folders keywords.
	function GetKeywords($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT			image_folders.keywords
		FROM			image_folders
		WHERE			image_folders.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['keywords'];
	}
	
	// Update image_folders keywords.
	function UpdateKeywords($id, $keywords) {
		
		$id = $this->_Safe($id);
		$keywords = $this->_Safe($keywords);
		
		$query = "
		UPDATE			image_folders
		SET				keywords = $keywords
		WHERE			image_folders.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}

	// Fetch all keyword set names from keyword_sets. 
	function GetKeywordSetNames() {
	
		$query = "
		SELECT 			keyword_sets.name
		FROM			keyword_sets
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToList($aDb, 'name');
	}
	
	// ***************************** IMAGES  ***********************************
	// Get image metadata given image filepath only.
	function GetImageMetadata($filepath) {
		
		$filepath = $this->_Safe($filepath);
		
		$query = "
		SELECT 			images.id,
						images.filepath,
						images.modified,
						images.file_size, 
						images.folder_id, 
						images.message_id,
						images.image_year,
						images.image_date,
						images.country,
						images.region,
						images.title, 
						images.caption, 
						images.keywords,
						images.copyright, 
						images.creator, 
						images.location,
						images.longitude,
						images.latitude,
						images.altitude,
						images.capture_time,
						images.dimensions,
						images.exposure,
						images.focal_length,
						images.iso_speed_rating,
						images.flash,
						images.make,
						images.model,
						images.type,
						images.auth
		FROM			images
		WHERE 			images.filepath = $filepath
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aDb);
	}
	
	// Check if image exists in DB given filepath. 
	function ImageExists($filepath) {
		
		$filepath = $this->_Safe($filepath);
		
		$query = "
		SELECT 			images.id
		FROM			images
		WHERE			images.filepath = $filepath
		";
		
		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}

		return true;
	}
	
	// Fetch image filepath from iamge id.
	function GetImageFilepath($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT 			images.filepath AS filepath,
						images.title AS title,
						images.type AS type
		FROM			images
		WHERE 			images.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aDb);
	}
	
	// Fetch folder ID from image id. 
	function GetFolderID($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT 			images.folder_id AS folder_id
		FROM			images
		WHERE 			images.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['folder_id'];
	}
	
	// Fetch image message_id given id
	function GetImageMessageId($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT 			images.message_id
		FROM			images
		WHERE 			images.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['message_id'];
	}
	
	// Fetch limited image details given the message_id. 
	function GetImageLimMessageId($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT 			images.folder_id,
						images.title
		FROM			images
		WHERE 			images.message_id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aDb);
	}
	
	// Fetch blog entry message_id given blog entry ID
	function GetBlogMessageId($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT 			blog_entries.comments_id
		FROM			blog_entries
		WHERE 			blog_entries.id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['comments_id'];
	}
	
	// Fetch blog ID for folder, if exists. 
	function GetFolderBlogId($fid) {
		
		$fid = $this->_Safe($fid);
		
		$query = "
		SELECT 			image_folders.blog_id
		FROM			image_folders
		WHERE 			image_folders.id = $fid
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['blog_id'];
	}
	
	// Get image metadata given image ID.
	function GetImageMetadataFromID($id, $auth) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT 			images.id,
						images.filepath,
						images.modified,
						images.file_size,
						images.folder_id,
						images.message_id, 
						images.image_year,
						images.image_date,
						images.country,
						images.region,
						images.title, 
						images.caption, 
						images.keywords,
						images.copyright, 
						images.creator, 
						images.location,
						images.longitude,
						images.latitude,
						images.altitude,
						images.capture_time,
						images.dimensions,
						images.exposure,
						images.focal_length,
						images.iso_speed_rating,
						images.flash,
						images.make,
						images.model,
						images.type,
						images.auth
		FROM			images
		WHERE 			images.id = $id
		AND				images.auth IN ($auth)
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aDb);
	}
	
	// Get folder_year given folder ID.
	function GetFolderYear($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT 			image_folders.folder_year
		FROM			image_folders
		WHERE 			image.folders_id = $id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb;
	}
	
	// Fetch images in folder. Return minimal data set for image meta data.
	function GetFolderImages($folder_id, $auth) {
		
		$folder_id = $this->_Safe($folder_id);
		
		$query = "
		SELECT 			images.id,
						images.filepath,
						images.title,
						images.longitude,
						images.latitude,
						images.altitude,
						images.dimensions,
						images.type,
						images.auth
		FROM			images
		WHERE 			images.folder_id = $folder_id
		AND				images.auth IN ($auth)
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb;
	}
	
	// Fetch images in folder abd limit to first 20 images (excluding video).
	function GetFolderImagesLimited($folder_id, $auth) {
		
		$folder_id = $this->_Safe($folder_id);
		
		$query = "
		SELECT 			images.id,
						images.filepath,
						images.title,
						images.longitude,
						images.latitude,
						images.altitude,
						images.dimensions,
						images.type,
						images.auth
		FROM			images
		WHERE 			images.folder_id = $folder_id
		AND				images.type = 'img'
		AND				images.auth IN ($auth)
		LIMIT 			20
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb;
	}
	
	// Fetch image count for specific folder. Pass in folder ID. 
	function GetImageCount($folder_id, $auth) {
		
		$folder_id = $this->_Safe($folder_id);
		
		$query = "
		SELECT 			count(*) AS cnt
		FROM			images
		WHERE 			images.folder_id = $folder_id
		AND				images.auth IN ($auth)
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $aDb[0]['cnt'];
	}
	
	// Fetch unique regions from image_folders;
	function GetUniqueRegions($auth) {
		
		$query = "
		SELECT 			distinct(region) as region
		FROM			image_folders
		WHERE			image_folders.auth IN ($auth)
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToList($aDb, 'region');
	}
	
	// Fetch unique years from image_folders;
	function GetUniqueYears($auth) {
		
		$query = "
		SELECT 			distinct(folder_year) as year
		FROM			image_folders
		WHERE			image_folders.auth IN ($auth)
		ORDER BY		image_folders.folder_year
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToList($aDb, 'year');
	}
	
	// Fetch unique countries from image_folders;
	function GetUniqueCountry($auth) {
		
		$query = "
		SELECT 			distinct(country) as country
		FROM			image_folders
		WHERE			image_folders.auth IN ($auth)
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToList($aDb, 'country');
	}
	
	// Fetch all countries for given region in image_folders.
	function GetRegionCountries($region, $auth) {
		
		$region = $this->_Safe($region);
		
		$query = "
		SELECT 			distinct(country) as country
		FROM			image_folders
		WHERE 			region = $region
		AND				image_folders.auth IN ($auth)
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToList($aDb, 'country');
	}
	
	// Fetch filtered Folders. 
	function GetFilteredFolders($year = false, $region = false, $country = false, $keywords = false, $auth) {
		
		// Prepare SQL query.
		// Assuming all inputs are safe and clean. false input means search all '*'
		if(!$year) {
			$year = "'%'";
		} else {
			$year = $this->_Safe($year);
		}
		
		$query = "
			SELECT			image_folders.id,
							image_folders.folder_date,
							image_folders.title
			FROM 			image_folders 
			WHERE 			folder_year LIKE $year
		";
		
		// Prepare region search.
		if($region) {
			$region = $this->_Safe($region);
			$query .= " AND region = $region";
		}
		
		// Prepare country search.
		if($country) {
			$country = $this->_Safe($country);
			$query .= " AND country = $country";
		}
		
		$query .= " AND image_folders.auth IN ($auth)";
		$query .= " ORDER BY folder_date DESC;";
		
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		
		return $aData;
	}
		
	// Fetch images in folder. Return minimal data set with Geolocation information. 
	function GetFolderImagesGelocation($folder_id) {
		
		$folder_id = $this->_Safe($folder_id);
		
		$query = "
		SELECT 			images.id,
						images.filepath,
						images.location, 
						images.latitude,
						images.longitude,
						images.altitude,
						images.type
		FROM			images
		WHERE 			images.folder_id = $folder_id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToListHash($aDb);
	}
	
	// Fetch list of all image IDs for given folder ID. 
	// Used to sort and organise images for a folder.
	function GetImageListIDs($folder_id) {
		
		$folder_id = $this->_Safe($folder_id);
		
		$query = "
		SELECT 			images.id
		FROM			images
		WHERE 			images.folder_id = $folder_id
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		
		return $this->_ConvertToList($aDb, 'id');	
	}
	
	// Fetch folder SET image. Return filepath and title.
	function GetFolderSetImage($folder_id) {
		
		$folder_id = $this->_Safe($folder_id);
		$data = array();
		
		$query = "
		SELECT				images.id
		FROM				images
		LEFT JOIN			image_folders
		ON					(images.id = image_folders.set_image_id)
		WHERE				image_folders.id = $folder_id
		";

		if(!$aData = $this->_QueryExecute($query)) {
			
			$query = "
				SELECT			images.id,
								images.filepath, 
								images.title
				FROM 			images
				WHERE 			images.folder_id = $folder_id
				LIMIT			1
				";
		
			if(!$aData2 = $this->_QueryExecute($query)) {
				return false;
			}
			
			$img_id = $aData2[0]['id'];
			$data = $aData2[0];
			
			$query = "
				UPDATE			image_folders 
				SET				set_image_id = '$img_id'
				WHERE 			image_folders.id = $folder_id
				";
				
			if(!$aData3 = $this->_QueryExecute($query)) {
				return false;
			}
				
		} else {	
			$data = $aData[0];
		}

		return $data;
	}
	
	function UpdateFolderSetImage($folder_id, $set_image) {
		
		$folder_id = $this->_Safe($folder_id);
		$set_image = $this->_Safe($set_image);

		$query = "
			UPDATE			image_folders 
			SET				set_image_id = $set_image
			WHERE 			image_folders.id = $folder_id
		";
				
		if(!$aData3 = $this->_QueryExecute($query)) {
			return false;
		}
		return true; 
	}
	
	function UpdateFolderBlogId($fid, $bid) {
		
		$fid = $this->_Safe($fid);
	
		# Grab current blog_id string. 
		$query = "
			SELECT		image_folders.blog_id AS blogs
			FROM 		image_folders
			WHERE 		image_folders.id = $fid
			LIMIT		1
		";
		
		if(!$aData2 = $this->_QueryExecute($query)) {
			return false;
		}
		
		$current_blogs = $aData2[0]['blogs'];
		
		$blogs = explode(",", $current_blogs);
		
		if(!in_array($bid, $blogs)) {
			
			$new_bid = $current_blogs . $bid . ",";
			$new_bid = $this->_Safe($new_bid);
		
			$query = "
				UPDATE			image_folders 
				SET				blog_id = $new_bid
				WHERE 			image_folders.id = $fid
			";
				
			if(!$aData3 = $this->_QueryExecute($query)) {
				return false;
			}
		}
		
		return true; 
	}	
	
	function UpdateFolderGeolocation($folder_id, $loc_string, $lat, $lng, $alt, $country, $region) {
		
		$folder_id = $this->_Safe($folder_id);
		$loc_string = $this->_Safe($loc_string);
		$lat = $this->_Safe($lat);
		$lng = $this->_Safe($lng);
		$alt = $this->_Safe($alt);
		$country = $this->_Safe($country);
		$region = $this->_Safe($region);
		
		$query = "
			UPDATE			image_folders 
			SET				image_folders.location = $loc_string,
							image_folders.latitude = $lat,
							image_folders.longitude = $lng,
							image_folders.altitude = $alt,
							image_folders.country = $country,
							image_folders.region = $region
			WHERE 			image_folders.id = $folder_id
		";
				
		if(!$aData3 = $this->_QueryExecute($query)) {
			return false;
		}
		return true; 
	}
	
	function UpdateFolderMode($folder_id, $mode) {
		
		$folder_id = $this->_Safe($folder_id);
		$mode = $this->_Safe($mode);
		
		$query = "
			UPDATE			image_folders 
			SET				image_folders.mode = $mode
			WHERE 			image_folders.id = $folder_id
		";
				
		if(!$aData3 = $this->_QueryExecute($query)) {
			return false;
		}
		return true; 
	}

	function UpdateFolderDate($folder_id, $date) {
		
		$folder_id = $this->_Safe($folder_id);
		$date = $this->_Safe($date);
		
		$query = "
			UPDATE			image_folders 
			SET				image_folders.folder_date = $date
			WHERE 			image_folders.id = $folder_id
		";
					
		if(!$aData3 = $this->_QueryExecute($query)) {
			return false;
		}
		return true; 
	}

	function UpdateFolderImagesAuth($folder_id, $auth) {
		
		$folder_id = $this->_Safe($folder_id);
		$auth = $this->_Safe($auth);
		
		$query = "
			UPDATE			images 
			SET				images.auth = $auth
			WHERE 			images.folder_id = $folder_id
		";
				
		if(!$aData3 = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}

	function UpdateImageDates($folder_id, $date) {
		
		$folder_id = $this->_Safe($folder_id);
		$date = $this->_Safe($date);
		
		$query = "
			UPDATE			images 
			SET				images.image_date = $date
			WHERE 			images.folder_id = $folder_id
		";
				
		if(!$aData3 = $this->_QueryExecute($query)) {
			return false;
		}
		//return true; 
		
		
		// TEMP CALL
		$query = "
		SELECT image_folders.folder_year
		FROM image_folders 
		WHERE image_folders.id = $folder_id
		";
				
		$aData3 = $this->_QueryExecute($query);
		
		$temp = $aData3[0]['folder_year'];
		$query = "
		UPDATE images 
		SET images.image_year = '$temp'
		WHERE images.folder_id = $folder_id
		";
		
		$aData3 = $this->_QueryExecute($query);
		
		return;
		
	}	
	
	function UpdateImageGeolocation($id, $lat, $lng, $alt) {
		
		$id = $this->_Safe($id);
		$lat = $this->_Safe($lat);
		$lng = $this->_Safe($lng);
		$alt = $this->_Safe($alt);
		
		$query = "
			UPDATE			images 
			SET				latitude = $lat,
							longitude = $lng,
							altitude = $alt
			WHERE 			images.id = $id
		";
							
		if(!$aData3 = $this->_QueryExecute($query)) {
			return false;
		}
		return true; 
	}
	
	function GetFolderTitle($fid) {
		
		$fid = $this->_Safe($fid);

		$query = "
		SELECT			image_folders.title
		FROM			image_folders
		WHERE			image_folders.id = $fid
		";
		
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		return $aData[0]['title'];
	}
	
	function DeleteImage($filepath) {
		
		$filepath = $this->_lowerCase($filepath);
		$filepath = $this->_Safe($filepath);
		
		$query = "
		DELETE FROM 	images
		WHERE 			images.filepath = $filepath
		";
		
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	function DeleteFolderYear($year) {
		
		$year = $this->_Safe($year);
		
		// Fetch all folder IDs that have this year then delete all images that 
		// are attached to folders for this year.
		$query = "
		SELECT			image_folders.id
		FROM			image_folders
		WHERE			image_folders.folder_year = $year
		";
		
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		
		foreach($aData as $key) {
			if($key['id']) {
				
				$query = "
				DELETE FROM 	images
				WHERE 			images.folder_id = '$key[id]'
				";
				
				if(!$aData = $this->_QueryExecute($query)) {
					return false;
				}
					
				$query = "
				DELETE FROM 	image_folders
				WHERE 			image_folders.id = '$key[id]'
				";
				
				if(!$aData = $this->_QueryExecute($query)) {
					return false;
				}
			}
		}		
		return true;
	}


	function DeleteFolder($filepath) {
		
		$filepath = $this->_Safe($filepath);
		
		// Find folder in image_folders table and return ID
		$query = "
		SELECT			image_folders.id
		FROM			image_folders
		WHERE			image_folders.folder_path = $filepath
		";
		
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		
		foreach($aData as $key) {
			if($key['id']) {
				
				$query = "
				DELETE FROM 	images
				WHERE 			images.folder_id = '$key[id]'
				";
				
				if(!$aData = $this->_QueryExecute($query)) {
					return false;
				}
					
				$query = "
				DELETE FROM 	image_folders
				WHERE 			image_folders.id = '$key[id]'
				";
				
				if(!$aData = $this->_QueryExecute($query)) {
					return false;
				}
			}
		}		
		return true;
	}
	
	function GetImageFolder($filepath, $folder_name, $folder_year, $folder_name_display, $folder_created) {
		
		$folder_created_date = date( 'Y-m-d H:i:00', $folder_created);
		
		$filepath = $this->_Safe($filepath);
		$folder_name = $this->_Safe($folder_name);
		$folder_year = $this->_Safe($folder_year);
		$folder_name_display = $this->_Safe($folder_name_display);
		$folder_created_date = $this->_Safe($folder_created_date);
		
		// Find folder in image_folders table and return ID
		$query = "
		SELECT			image_folders.id AS id
		FROM			image_folders
		WHERE			image_folders.folder_path = $filepath
		";
		
		// If image folder doen't exist.
		if(!$aData = $this->_QueryExecute($query)) {
			
		
			$query = "
			INSERT INTO		image_folders 
			VALUES 			('',$filepath,$folder_name,$folder_year,$folder_created_date,$folder_created_date,'','',$folder_name_display,'','','','','','','','','','admin')         
			";
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}
			
			// Find folder in image_folders table and return ID
			$query = "
			SELECT			image_folders.id AS id
			FROM			image_folders
			WHERE			image_folders.folder_path = $filepath
			";
		
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}
		}
		return $aData[0]['id'];
	}
	
	function GetDistinctFolderYears($auth) {
				
		$query = "
		SELECT	DISTINCT	image_folders.folder_year AS years
		FROM				image_folders
		WHERE				image_folders.auth IN ($auth)
		";
		
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToList($aData, 'years');	 
	}
	
	function GetFolderData($id, $auth) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT				image_folders.id,
							image_folders.folder_path,
							image_folders.folder_name,
							image_folders.folder_year,
							image_folders.folder_modified,
							image_folders.folder_date,
							image_folders.set_image_id,
							image_folders.blog_id,
							image_folders.title,
							image_folders.location,
							image_folders.longitude,
							image_folders.latitude,
							image_folders.altitude,
							image_folders.caption,
							image_folders.country,
							image_folders.region,
							image_folders.mode,
							image_folders.keywords,
							image_folders.auth
		FROM				image_folders
		WHERE				image_folders.id = $id
		AND					image_folders.auth IN ($auth)
		";
		
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aData);
	}

	function GetFolderDataLimited($id) {
		
		$id = $this->_Safe($id);
		
		$query = "
		SELECT				image_folders.folder_path,
							image_folders.folder_date,
							image_folders.set_image_id,
							image_folders.title,
							image_folders.location,
							image_folders.caption,
							image_folders.country,
							image_folders.region,
							image_folders.auth
		FROM				image_folders
		WHERE				image_folders.id = $id
		";
		
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($aData);
	}
	
	function GetFoldersForYear($year, $auth) {
		
		$year = $this->_Safe($year);
		
		$query = "
		SELECT				image_folders.id,
							image_folders.folder_path,
							image_folders.folder_date,
							image_folders.title,
							image_folders.location,
							image_folders.longitude,
							image_folders.latitude,
							image_folders.altitude,
							image_folders.set_image_id,
							image_folders.country,
							image_folders.region,
							image_folders.auth
		FROM				image_folders
		WHERE				image_folders.folder_year = $year
		AND					image_folders.auth IN ($auth)
		";
		
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		
		// Loop through each folder and check for set_image_id. If not found retrieve top image for folder. 
		// If still not found then return blank as this will mean we haven't indexed the images into the 
		// DB yet - this will come once we load the view set page.  
		for($t=0;$t<count($aData);$t++) {
			$folder_id = $aData[$t]['id'];
			$set_image_id = $aData[$t]['set_image_id'];

			$query = "
				SELECT			images.id,
								images.filepath 
				FROM 			images
				WHERE 			images.id = '$set_image_id'
			";
					
			if(!$aData2 = $this->_QueryExecute($query)) {
				
				$query = "
				SELECT			images.id,
								images.filepath 
				FROM 			images
				WHERE 			images.folder_id = '$folder_id'
				LIMIT			1
				";
		
				if($aData3 = $this->_QueryExecute($query)) {
					$img_id = $aData2[0]['id'];
					$query = "
					UPDATE			image_folders 
					SET				set_image_id = '$img_id'
					WHERE 			id = '$folder_id'
					";
					$aData4 = $this->_QueryExecute($query);
				}
				
				$aData[$t]['filepath'] = $aData3[0]['filepath'];
			} else {	
				$aData[$t]['filepath'] = $aData2[0]['filepath'];
			}
			
			// Grab total count for all image inside this folder. 
			$query = "
			SELECT COUNT(*) AS cnt
			FROM 			images
			WHERE 			images.folder_id = '$folder_id'
			LIMIT			1
			";
			
			$aData2 = $this->_QueryExecute($query);
			$aData[$t]['image_count'] = $aData2[0]['cnt'];	
		}
			
		return $this->_ConvertToListHash($aData);
	}
	
	function InsertImage($img_data) {
			
			foreach($img_data as $key => $val) {
				$img_data[$key] = $this->_Safe($img_data[$key]);
			}
			
			$query = "
			INSERT INTO		images 
			VALUES 			('',
							$img_data[filepath],
							$img_data[modified],
							$img_data[file_size],
							$img_data[folder_id],
							$img_data[message_id],
							$img_data[image_year],
							$img_data[image_date],
							$img_data[country],
							$img_data[region],
							$img_data[title],
							$img_data[caption],
							$img_data[keywords],
							$img_data[copyright],
							$img_data[creator],
							$img_data[location],
							$img_data[longitude],
							$img_data[latitude],
							$img_data[altitude],
							$img_data[capture_time],
							$img_data[dimensions],
							$img_data[exposure],
							$img_data[focal_length],
							$img_data[iso_speed_rating],
							$img_data[flash],
							$img_data[make],
							$img_data[model],
							$img_data[type],
							$img_data[auth])
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	

			return true;
	}
	
	// Update new message_id for image.
	function UpdateImageMessageId($id, $message_id) {
			
			$id = $this->_Safe($id);
			$message_id = $this->_Safe($message_id);
			
			$query = "
			UPDATE			images
			SET				images.message_id = $message_id
			WHERE			images.id = $id
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	

			return true;
	}
	
	// Update new comments_id for blog.
	function UpdateBlogMessageId($id, $message_id) {
			
			$id = $this->_Safe($id);
			$message_id = $this->_Safe($message_id);
			
			$query = "
			UPDATE			blog_entries
			SET				blog_entries.comments_id = $message_id
			WHERE			blog_entries.id = $id
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	

			return true;
	}
	
	// Update image data.
	function UpdateImage($img_data) {
			
			//print "<BR><BR>FILEPATH:" . $img_data['filepath'];
			/*
			foreach($img_data as $key => $val) {
				//print "key" . $key . " - " . $val . "<br>";
				$img_data['$key'] = $this->_Safe($img_data['$key']);
			}
			*/
			$title = $this->_Safe($img_data[title]);
		
			$query = "
			UPDATE 			images 
			SET 			images.modified = '$img_data[modified]',
							images.folder_id = '$img_data[folder_id]',
							images.message_id = '$img_data[message_id]',
							images.file_size = '$img_data[file_size]',
							images.image_year = '$img_data[image_year]',
							images.image_date = '$img_data[image_date]',
							images.country = '$img_data[image_country]',
							images.region = '$img_data[image_region]',
							images.title = '$title',
							images.caption = '$img_data[caption]',
							images.keywords = '$img_data[keywords]',
							images.copyright = '$img_data[copyright]',
							images.creator = '$img_data[creator]',
							images.location = '$img_data[location]',
							images.longitude = '$img_data[longitude]',
							images.latitude = '$img_data[latitude]',
							images.altitude = '$img_data[altitude]',
							images.capture_time = '$img_data[capture_time]',
							images.dimensions = '$img_data[dimensions]',
							images.exposure = '$img_data[exposure]',
							images.focal_length = '$img_data[focal_length]',
							images.iso_speed_rating = '$img_data[iso_speed_rating]',
							images.flash = '$img_data[flash]',
							images.make = '$img_data[make]',
							images.model = '$img_data[model]',
							images.type = '$img_data[type]',
							images.auth = '$img_data[auth]'
			WHERE 			images.filepath = '$img_data[filepath]'
			";
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	

			return true;
	}

	// **** Image Folder AJAX calls.
	function UpdateImageFoldersTitle($id, $title) {
		
			$id = $this->_Safe($id);
			$title = $this->_Safe($title);
			
			//$title = addslashes(trim($title));
			
			$query = "
			UPDATE 			image_folders 
			SET 			image_folders.title = $title
			WHERE			image_folders.id = $id
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	
			return true;
	}
	
	function UpdateImageFolderAuth($id, $auth) {
		
			$id = $this->_Safe($id);
			$auth = $this->_Safe($auth);
			
			$query = "
			UPDATE 			image_folders 
			SET 			image_folders.auth = $auth
			WHERE			image_folders.id = $id
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	
			return true;
	}
	
	function UpdateImageFoldersLocation($id, $location) {
		
			$id = $this->_Safe($id);
			$location = $this->_Safe($location);
			
			//$location = addslashes(trim($location));
			$query = "
			UPDATE 			image_folders 
			SET 			image_folders.location = $location
			WHERE			image_folders.id = $id
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	
			return true;
	}
	
	function UpdateImageFoldersCaption($id, $caption) {
		
			$id = $this->_Safe($id);
			$caption = $this->_Safe($caption);
		
			//$caption = addslashes(trim($caption));
			$query = "
			UPDATE 			image_folders 
			SET 			image_folders.caption = $caption
			WHERE			image_folders.id = $id
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	
			return true;
	}
	
	function UpdateImageTitle($id, $title, $modified) {
		
			$id = $this->_Safe($id);
			
			$modified = $this->_Safe($modified);
			
			//$title = addslashes(trim($title));
			$title = $this->_Safe($title);
			
			$query = "
			UPDATE 			images 
			SET 			images.title = $title,
							images.modified = $modified
			WHERE			images.id = $id
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	
			return true;
	}
	
	function UpdateImageAuth($id, $auth) {
		
			$id = $this->_Safe($id);
			$auth = $this->_Safe($auth);
			
			$query = "
			UPDATE 			images 
			SET 			images.auth = $auth
			WHERE			images.id = $id
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	
			return true;
	}
	
	function UpdateImageLocation($id, $location) {
	
			$id = $this->_Safe($id);
			$location = $this->_Safe($location);
			
			//$location = addslashes(trim($location));
			$query = "
			UPDATE 			images 
			SET 			images.location = $location
			WHERE			images.id = $id
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	
			return true;
	}
	
	function UpdateImageCountryRegion($id, $country, $region) {
			
			$id = $this->_Safe($id);
			$country = $this->_Safe($country);
			$region = $this->_Safe($region);
			
			$query = "
			UPDATE 			images 
			SET 			images.country = $country,
							images.region = $region
			WHERE			images.id = $id
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	
			return true;
	}
	
	// TAG CLOUD queries. 
	// 
	// Fetch all content from following tables and fields. 
	// image_folders - title, caption, location, keywords.
	// images - title
	// blog_entries - name, description
	function TagCloudFetchAllContent($auth) {
		
		$query = "
			SELECT * FROM (
				SELECT 		'image_folders' AS 'table_name',
							CONCAT_WS(' ',image_folders.title,image_folders.caption,image_folders.location,image_folders.keywords) AS content,
							image_folders.auth AS auth
				FROM		image_folders
				UNION 
				SELECT		'images' AS 'table_name',
							images.title AS content,
							images.auth AS auth
				FROM		images
				UNION
				SELECT		'blogs' AS 'table_name',
							CONCAT_WS(' ',blog_entries.name,blog_entries.description) AS content,
							blog_entries.auth as auth
				FROM		blog_entries)
			AS sitewide
			WHERE auth IN ($auth)
			";
			
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}	
		return $aData;
	}
	
	// Fetch cloud tags for this auth
	function TagCloudFetchTags($auth) {
		
		$query = "
		SELECT		cloud_tags.weight,
					cloud_tags.tagname,
					cloud_tags.url,
					cloud_tags.auth
		FROM		cloud_tags
		WHERE 		cloud_tags.auth = '$auth'
		";
			
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}	
		return $aData;
	}
	
	// Delete all rows from cloud_tags table
	function TagCloudDeleteAll() {
		
		$query = "
		DELETE FROM 	cloud_tags
		";
		
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	// Insert words into cloud_tags
	function TagCloudInsertWord($weight, $tagname, $url, $auth) {
		
		$weight = $this->_Safe($weight);
		$tagname = $this->_Safe($tagname);
		$url = $this->_Safe($url);
		$auth = $this->_Safe($auth);
		$query = "
			INSERT INTO		cloud_tags 
			VALUES 			($weight,$tagname,$url,$auth)
			";
			
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}	
		return true;
	}
	
	
	
	
	// Search site including image sets, images and blogs.
	// Search paramaters are year, region, country and keywords. 
	// Use search_areas to specify db tables to search.
	// Using natural language FULLTEXT keyword search.
	// Include pagination (page num, first = 0). 
	// Use AuthGroup to determine set,image or blog permissions.
	function Search($search_areas, $keywords = false, $year = false, $country = false, $region = false, $limit_from, $limit_to, $auth) {
	
		// Build SQL Selects for each area (image_folders, images & blogs.
		$keywords_safe = $this->_Safe($keywords);
		$areas = explode(",", $search_areas);
		$query_select = array();
		$distinct_filters = array('year' => array(), 'country' => array(), 'region' => array());
		$sel_count = 0;

		if(in_array('image_folders', $areas)) {
			$query_select[$sel_count] = "
				SELECT 		'image_folders' AS 'table_name', 
							image_folders.id AS id, 
   							image_folders.folder_year AS year,
  							image_folders.country AS country,
  							image_folders.region AS region,
  							image_folders.folder_date AS date,
  							image_folders.auth AS auth";
			if($keywords) {
				$query_select[$sel_count] .= ", 
  							1 * (MATCH(title,caption,location,keywords) AGAINST ($keywords_safe)) AS relevance";
			}
			$query_select[$sel_count] .= "
   	 			FROM 		image_folders
	    	";
			$sel_count++;
		}
	
		if(in_array('images', $areas)) {
			$query_select[$sel_count] = "
 				SELECT 		'images' AS 'table_name',
	 						images.id AS id, 
   							images.image_year AS year,
	  						images.country AS country,
	  						images.region AS region,
	  						images.image_date AS date,
	  						images.auth AS auth";
			if($keywords) {
				$query_select[$sel_count] .= ", 
	  						1 * (MATCH(title) AGAINST ($keywords_safe)) AS relevance";	
			}
			$query_select[$sel_count] .= "
	    		FROM 		images
	    	";
			$sel_count++;
		}
	
		if(in_array('blogs', $areas)) {
			$query_select[$sel_count] = "
				SELECT 		'blogs' AS 'table_name',
							blog_entries.id AS id, 
	   	  					blogs.blog_year AS year, 
	  	  					blogs.blog_country AS country, 
	    					blogs.blog_region AS region,
	    					blog_entries.entry_date AS date,
	    					blog_entries.auth AS auth";
			if($keywords) {
				$query_select[$sel_count] .= ", 
  							1 * (MATCH(blog_entries.name,blog_entries.description) AGAINST ($keywords_safe)) AS relevance";
			}
			$query_select[$sel_count] .= "
				FROM 		blog_entries 
				LEFT JOIN 	blogs 
				ON 			blog_entries.blog_id = blogs.id
			";
			$sel_count++;
		}
	
		// Prepare query filter extensions for relevance, year, country, region and authgroups. 
		$query_ext = "WHERE			auth IN ($auth)";
		
		if($keywords || $year || $country || $region) {
			if($keywords) {
				$query_ext .= " AND relevance > 0.2";
			}	
			if($year) {
				$query_ext .= " AND year = " . $this->_Safe($year);
			}	
			if($country) {
				$query_ext .= " AND country = " . $this->_Safe($country);
			}
			if($region) {
				$query_ext .= " AND region = " . $this->_Safe($region);
			}
		}
	
		if($keywords) {
			$query_results_ext .= " 
			ORDER BY 		relevance DESC
			";
		} else {
			$query_results_ext .= " 
			ORDER BY 		date DESC
			";
		}
	
		$query_results_ext .= "LIMIT			$limit_from, $limit_to;
			";

		// Now prepare count(*) query and main results query.
		// First we run a total count of all filtered rows. 
		// Then we run a DISTICT year,country,region filtered query. Used to build search filters. 
		// Finally we run the results query to obtain filtered results data.
		// If we have more than one table to search then we construct a surrounding SELECT UNION statement.
		$query_count = "
			SELECT COUNT(*) AS count FROM (
		";
		$query_distinct = "
			SELECT DISTINCT year,country,region FROM (
		";
		$query_results = "
			SELECT * FROM (
		";
		
		if(count($query_select) > 1) {
			foreach($query_select as $key => $select) {
				if($key) {$query_main .= " UNION ";}
				$query_main .= $select;
			}
			$query_main .= ")
			AS sitewide
			";
		} else {
			$query_main .= $query_select[0];
			$query_main .= ")
			AS sitewide
			";
		}

		$query_count .= $query_main . $query_ext;
		$query_distinct .= $query_main . $query_ext;
		$query_results .= $query_main . $query_ext . $query_results_ext;
		
		// Run total count query. 
		if(!$aData = $this->_QueryExecute($query_count)) {
			return false;
		}
		$this->iResultsCount = $aData[0]['count'];
		
		// Run distinct filters query. 
		if(!$aData = $this->_QueryExecute($query_distinct)) {
			return false;
		}
		foreach($aData as $result) {
			if($result['year'] && !in_array($result['year'], $distinct_filters['year'])) {
				$distinct_filters['year'][] = $result['year'];
			}
			if($result['country'] && !in_array($result['country'], $distinct_filters['country'])) {
				$distinct_filters['country'][] = $result['country'];
			}
			if($result['region'] && !in_array($result['region'], $distinct_filters['region'])) {
				$distinct_filters['region'][] = $result['region'];
			}
		}
		$this->aDistinctFilters = $distinct_filters;

		// Run results query.
		if(!$aData = $this->_QueryExecute($query_results)) {
			return false;
		}
		return $aData;
	}
	
	// Home page - search latest content from image sets, images and blogs. 
	// Pass in tables to search as $search_areas.  
	// Use AuthGroup to determine set,image or blog permissions.
	function SearchLatest($search_areas, $limit_from, $limit_to, $auth) {
	
		$areas = explode(",", $search_areas);
		$query_select = array();
		$sel_count = 0;

		if(in_array('image_folders', $areas)) {
			$query_select[$sel_count] = "
				SELECT 		'image_folders' AS 'table_name', 
							image_folders.id AS id,
   							image_folders.title AS title,
  							image_folders.folder_date AS date,
  							image_folders.set_image_id AS set_image,
  							image_folders.caption AS description,
  							image_folders.location AS location,
  							image_folders.mode AS mode,
  							image_folders.folder_path AS folder_path,
  							image_folders.auth AS auth
				FROM 		image_folders
	    	";
			$sel_count++;
		}
	
		if(in_array('blogs', $areas)) {
			$query_select[$sel_count] = "
				SELECT 		'blogs' AS 'table_name',
							blog_entries.id AS id, 
   							blog_entries.name AS title,
  							blog_entries.entry_date AS date,
  							blog_entries.set_image AS set_image,
  							blog_entries.description AS description,
  							blog_entries.url AS url,
  							blogs.name AS blog_name,
  							blog_entries.blog_id AS blog_id,
  							blog_entries.auth AS auth
				FROM 		blog_entries 
				LEFT JOIN 	blogs 
				ON 			blog_entries.blog_id = blogs.id
			";
			$sel_count++;
		}
	
		// Prepare query filter extensions for relevance, year, country, region and authgroups. 
		$query_ext = "WHERE			auth IN ($auth)
			ORDER BY 		date DESC
			LIMIT			$limit_from, $limit_to;
			";

		$query_results = "
			SELECT * FROM (
		";
		
		if(count($query_select) > 1) {
			foreach($query_select as $key => $select) {
				if($key) {$query_main .= " UNION ";}
				$query_main .= $select;
			}
			$query_main .= ")
			AS sitewide
			";
		} else {
			$query_main .= $query_select[0];
			$query_main .= ")
			AS sitewide
			";
		}

		$query_results .= $query_main . $query_ext;
		
		// Run results query.
		if(!$aData = $this->_QueryExecute($query_results)) {
			return false;
		}
		return $aData;
	}

	// Build autocomplete table from Features, images and fulltext_index_words tables.
	function BuildAutocomplete($iAcMinWordLength, $iAcMaxWords) {
		
		// Delete all rows form current autocomplete table.
		$query = "
		DELETE FROM 	autocomplete
		";
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		
		// Select all features and load into temporary array. 
		$query = "
		SELECT			features.name 
		FROM			features
		";
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		foreach($aData as $key) {
			if($key['name']) {
				$aList[] = trim(ucfirst($key['name']));
			}
		}		

		// Get distinct types from images and insert into temp array checking for duplicates.
		$query = "
		SELECT DISTINCT	images.type
		FROM			images 
		";
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}	
		foreach($aData as $key) {
			$key['type'] = ucfirst($key['type']);
			if($key['type'] && !in_array($key['type'], $aList)) {
				$aList[] = trim($key['type']);
			}
		}

		// Get all keywords,name and location and split them by comma checking for duplicates and also restricting number of words in string. 
		$query = "
		SELECT			images.name,
						images.location,
						images.keywords
		FROM			images 
		";
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}	
		foreach($aData as $key) {
			$aKeywords = explode(',', $key['keywords']);
			foreach($aKeywords as $keywords) {
				if($keywords) {
					$pieces = explode(" ", $keywords);
					$trig = 0;
					$k3 = "";
					foreach($pieces as $p) {
						if(strlen($p) >= $iAcMinWordLength && $trig <= $iAcMaxWords) {
							$k3 .= $p . " ";
							$trig++;
						}
					}
					$k3 = trim($k3);
					$k3 = ucfirst($k3);
					if($k3 && !in_array($k3, $aList)) {
						$aList[] = $k3;
					}
				}
			}
			$aName = explode(',', $key['name']);
			foreach($aName as $name) {
				if($name) {
					$pieces = explode(" ", $name);
					$trig = 0;
					$k3 = "";
					foreach($pieces as $p) {
						if(strlen($p) >= $iAcMinWordLength && $trig <= $iAcMaxWords) {
							$k3 .= $p . " ";
							$trig++;
						}
					}
					$k3 = trim($k3);
					$k3 = ucfirst($k3);
					if($k3 && !in_array($k3, $aList)) {
						$aList[] = $k3;
					}
				}
			}
			$aLocation = explode(',', $key['location']);
			foreach($aLocation as $location) {
				if($location) {
					$pieces = explode(" ", $location);
					$trig = 0;
					$k3 = "";
					foreach($pieces as $p) {
						if(strlen($p) >= $iAcMinWordLength && $trig <= $iAcMaxWords) {
							$k3 .= $p . " ";
							$trig++;
						}
					}
					$k3 = trim($k3);
					$k3 = ucfirst($k3);
					if($k3 && !in_array($k3, $aList)) {
						$aList[] = $k3;
					}
				}
			}

		}

		// Insert and merge FULLTEXT raw words into array. 
		$query = "
		SELECT			fulltext_index_words.keyword 
		FROM			fulltext_index_words
		";
		
		if(!$aData = $this->_QueryExecute($query)) {
			//return false;
		}

		foreach($aData as $key) {
			$key['keyword'] = ucfirst($key['keyword']);
			if($key['keyword'] && !in_array($key['keyword'], $aList)) {
				$aList[] = trim($key['keyword']);
			}
		}
				
		// Insert final list of auto complete words into the autocomplete table.
		foreach($aList as $key) {
			$key = addslashes(trim($key));
			$query = "
			INSERT INTO 	autocomplete 
			VALUES 			('$key')
			";
			if(!$aData = $this->_QueryExecute($query)) {
				return false;
			}
		}

		return true;
	}
	
	// Returns the autocomplete list straight from table. 
	function GetAutocomplete() {
		$query = "
		SELECT			autocomplete.keyword
		FROM			autocomplete
		";
		if(!$aData = $this->_QueryExecute($query)) {
			return false;
		}
		foreach($aData as $key) {
			$aList[] = $key['keyword'];
		}
		return $aList;
	}
	
	// ******* User Authentication Procedural Calls ******************************
	
	function AddUser($username, $hash_password, $name, $role, $time, $comments) {
		
		$username = $this->_Safe($username);
		$hash_password = $this->_Safe($hash_password);
		$name = $this->_Safe($name);
		$role = $this->_Safe($role);
		$time = $this->_Safe($time);
		$comments = $this->_Safe($comments);

		$query = "
		INSERT INTO 	users
		VALUES 			($username, $hash_password, $name, $role, FROM_UNIXTIME($time), '', '', '', '', '0', $comments) 
		";
	
		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	function UpdatePassword($username, $hash_password, $time) {
		
		$username = $this->_Safe($username);
		$hash_password = $this->_Safe($hash_password);
		$time = $this->_Safe($time);	
	
		$query = "
		UPDATE 			users
		SET				users.password = $hash_password,
						users.created_time = FROM_UNIXTIME($time)
		WHERE 			users.username = $username
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;		
	}
	
	function UpdateUser($username, $name, $role, $last_login, $last_login_attempt, $login_attempts, $referrer, $active, $comments) {
		
		$username = $this->_Safe($username);
		$name = $this->_Safe($name);
		$role = $this->_Safe($role);
		$last_login = $this->_Safe($last_login);
		$last_login_attempt = $this->_Safe($last_login_attempt);
		$login_attempts = $this->_Safe($login_attempts);
		$referrer = $this->_Safe($referrer);	
		$active = $this->_Safe($active);
		$comments = $this->_Safe($comments);
		
		$query = "
		UPDATE 			users
		SET				users.name = $name,
						users.role = $role,
						users.last_login = FROM_UNIXTIME($last_login),
						users.last_login_attempt = FROM_UNIXTIME($last_login_attempt),
			 			users.login_attempts = $login_attempts,
			 			users.referrer = $referrer,
			 			users.active = $active,
			 			users.comments = $comments
		WHERE			users.username = $username
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;		
	}
	
	function UpdateUserActive($username, $role) {
		
		$username = $this->_Safe($username);
		$role = $this->_Safe($role);
		
		$query = "
		UPDATE 			users
		SET				users.role = $role,
						users.active = '1'
		WHERE			users.username = $username
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;		
	}
	
	function UpdateUserInactive($username) {
		
		$username = $this->_Safe($username);
		
		$query = "
		UPDATE 			users
		SET				users.active = '0'
		WHERE			users.username = $username
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;		
	}
	
	function UpdateUserRole($username, $role) {
		
		$username = $this->_Safe($username);
		$role = $this->_Safe($role);
		
		$query = "
		UPDATE 			users
		SET				users.role = $role
		WHERE			users.username = $username
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;		
	}
 
	function DeleteUser($username) {
		
		$username = $this->_Safe($username);
		
		$query = "
		DELETE FROM		users
		WHERE 			users.username = $username
		";

		if(!$aDb = $this->_QueryExecute($query)) {
			return false;
		}
		return true;
	}
	
	function GetUser($username) {

		$username = $this->_Safe($username);
		
		$query = "
		SELECT			users.username AS username,
						users.password AS password,
						users.name AS name,
						users.role AS role,
						UNIX_TIMESTAMP(users.created_time) AS time,
						UNIX_TIMESTAMP(users.last_login) AS last_login,
						UNIX_TIMESTAMP(users.last_login_attempt) AS last_login_attempt,
						users.login_attempts AS login_attempts,
						users.referrer AS referrer, 
						users.active AS active,
						users.comments AS comments
		FROM			users
		WHERE			users.username = $username
		";
		
		if(!$Db = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToHash($Db);
	}
	
	function GetActiveUsers() {

		$query = "
		SELECT			users.username AS username,
						users.password AS password,
						users.name AS name,
						users.role AS role,
						users.created_time AS time,
						users.last_login AS last_login,
						users.last_login_attempt AS last_login_attempt,
						users.login_attempts AS login_attempts,
						users.referrer AS referrer, 
						users.active AS active,
						users.comments AS comments
		FROM			users
		WHERE			users.active = '1'
		ORDER BY		users.created_time
		";
		
		if(!$Db = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToListHash($Db);
	}
	
	function GetInactiveUsers() {

		$query = "
		SELECT			users.username AS username,
						users.password AS password,
						users.name AS name,
						users.role AS role,
						users.created_time AS time,
						users.last_login AS last_login,
						users.last_login_attempt AS last_login_attempt,
						users.login_attempts AS login_attempts,
						users.referrer AS referrer, 
						users.active AS active,
						users.comments AS comments
		FROM			users
		WHERE			users.active = '0'
		ORDER BY		users.created_time
		";
		
		if(!$Db = $this->_QueryExecute($query)) {
			return false;
		}
		return $this->_ConvertToListHash($Db);
	}

	// *************************************************************************
	// ******* Base functions   ************************************************

	function IsConnected() {
		return $this->bConnected;
	}				
	function _QueryExecute($query) {
		
		// check if we are still connected to db. If not then reconnect.
		if(!$this->bConnected) {
			$this->_Connect();
		}
		$aData = $this->Query($query);
		return $aData;
	}
	
	function _Connect() {	
		if(!parent::__construct($this->LOG, $this->sHost, $this->sUser, $this->sPass, $this->sName)) {
			return false;
		}
		return true;
	}
	
	// prepare safe string for query build. Must be applied to all user inputs. 
	function _Safe($string) {
		return "'" . mysqli_real_escape_string($this->oDb, $string ) . "'";
	}


	// Format string to all lower and every first letter upper case.
	function _ItemDisplay($item) {
		return ucwords(strtolower($item));
	}
	
	// Format string to all lower.
	function _LowerCase($item) {
		return strtolower($item);
	}

	// Convert single record db hash to list (1 dimensional array) and strip slashes. 
	function _ConvertToList($aDb, $fieldname) {
		foreach($aDb as $val) {
			$aData[] = stripslashes($val[$fieldname]);
		}
		return $aData;
	}

	// Convert db record to key val hash. Key = field and val = value.
	function _ConvertToHash($aDb) {
		return $aDb[0];
	}

	// Convert db record to numerical list of key val hash. Key = field and val = value. For results with muliple rows. 
	function _ConvertToListHash($aDb) {
		$cnt = 0;
		foreach($aDb as $rows) {
			foreach($rows as $key => $val) {
				$aData[$cnt][$key] = stripslashes($val);
			}
			$cnt++;
		}
		return $aData;
	}
	
	// Convert db record to hash of key val hash with passed in field name as top key. 
	function _ConvertToHashHash($aDb, $fieldname) {
		foreach($aDb as $rows) {
			$aData[$rows[$fieldname]] = $rows;
		}
		return $aData;
	}
	
	// Convert db record to simple key => value array. DB table must have 2 columns named key and value.
	function _ConvertToSimpleKeyVal($aDb) {
		foreach($aDb as $rows) {
			$aData[$rows['key']] = stripslashes($rows['value']);
		}
		return $aData;
	}
}
?>