Location: PHPKode > projects > libSiteMessage > libsitemessage/lib.SiteMessageFolder.php
<?php

	/*
	*	Folderclass for libSiteMessage
	*
	*	@author 	$Author: Cornelius Bolten $
	*	@version	$Revision: 1.2 $
	*	@package	SiteMessage	
	*	@example 	example.SiteMessage.php	example on how to get it work!
	*	@link 		latest available version @ phpclasses.org
	**/
	class SiteMessageFolder {

		/**
		* @access private         
		* @var object SQLite Database Handle
		*/		
		private $dbHandle;

		/**
		* @access private         
		* @var string folderName
		*/		
		private $folderName;
		
		/**
		* @access private         
		* @var string folderDate
		*/		
		private $folderDate;		
		
		/**
		* @access private         
		* @var integer hasMessages
		*/		
		private $hasMessages;
		
		/**
		* @access private         
		* @var integer folderid
		*/		
		private $folderID;
		
		/**
		* @access private         
		* @var array subfolders
		*/		
		private $subfolders;

	
		/**
		*	Constructor
		*
		*	@access public
		*/		
		public function __construct($dbhandle, $sm_folder_id=false) {
			$this->dbHandle		=	$dbhandle;
			if($sm_folder_id) {
				$this->folderID	=	$sm_folder_id;
				$this->open($this->folderID);
			}
		}
		
		
		/**
		*	Destructor
		*
		*	@access public
		*/		
		public function __destruct() {}
		
		
		/**
		*	open
		*
		*	opens a folder
		*	@access public
		*/
		public function open($folderID) {
			$query		=	"SELECT * FROM libSiteMessageFolders WHERE sm_folder_id = ".$folderID.";";
			$sqlResult	=	sqlite_query($this->dbHandle, $query);		
			if(!$sqlResult) {
				throw new SQLiteException( sqlite_error_string(sqlite_last_error($this->dbHandle)) );
			} else {
				$result				=	sqlite_fetch_all($sqlResult);
				if(sizeof($result) <= 0) {
					throw new FolderNotAvailableException();
				} else {
					$this->folderID		=	$result[0]['sm_folder_id'];
					$this->folderName	=	$result[0]['sm_folder_name'];
					$this->folderDate	=	$result[0]['sm_folder_date'];					
				}
			}
		}	
		
		
		/**
		*	getDate
		*
		*	returns the folders' date
		*	@access public
		*	@return string unixTimestamp
		*/			
		public function getDate() {
			return $this->folderDate;
		}
		
		
		/**
		*	getName
		*
		*	returns the folders' name
		*	@access public
		*	@return string Foldername
		*/			
		public function getName() {
			return $this->folderName;
		}
		
		
		/**
		*	setName
		*
		*	sets the folders' name
		*	@access public
		*/		
		public function setName($folderName) {
			$query	=	"UPDATE libSiteMessageFolders set sm_folder_name = '".$folderName."' WHERE sm_folder_id = ".$this->folderID.";";
			if(!$sqlResult	=	sqlite_query($this->dbHandle, $query)) {
				throw new SQLiteException( sqlite_error_string(sqlite_last_error($this->dbHandle)) );
			} else {
				$this->open($this->folderID);
			}	
		}


		/**
		*	hasSubfolder
		*
		*	returns number of Subfolders of this folder
		*	@access public
		*	@return integer number of subfolders
		*/		
		public function hasSubfolder() {
			$query				=	"SELECT sm_folder_id FROM libSiteMessageFolders WHERE sm_parent_id = '".$this->folderID."';";
			$subfolders			=	sqlite_array_query($this->dbHandle, $query);
			$this->subfolders	=	$subfolders;
			return(sizeof($subfolders));
		}


		/**
		*	getSubfolder
		*
		*	returns a Subfolder of this folder
		*	@access public
		*	@return object folder
		*	@param	integer idToGet
		*/		
		public function getSubfolder($idToGet) {
			$subfolder	=	new SiteMessageFolder($this->dbHandle, $this->subfolders[($idToGet-1)]['sm_folder_id']);
			return $subfolder;
		}				


		/**
		*	hasMessage
		*
		*	returns number of Messages saved within folder
		*	@access public
		*	@return integer number of messages
		*/		
		public function hasMessage() {
			$query				=	"SELECT sm_id FROM libSiteMessage WHERE sm_folder_id = '".$this->folderID."';";
			$messages			=	sqlite_array_query($this->dbHandle, $query);
			$this->subfolders	=	$messages;
			return(sizeof($messages));		
		}
		

		/**
		*	getMessage
		*
		*	returns a message
		*	@access public
		*	@return object 	siteMessage
		*	@param	integer	id
		*/		
		public function getMessage($idToGet) {}


		/**
		*	delete
		*
		*	delets a folder with all its' messages
		*	@access public
		*	@return boolean folder deleted
		*/		
		public function delete() {
			$query	=	"DELETE FROM libSiteMessageFolders where sm_parent_id = '".$this->folderID."';";
			$sqlResult1	=	sqlite_query($this->dbHandle, $query);			
			$query	=	"DELETE FROM libSiteMessage where sm_folder_id = '".$this->folderID."';";
			$sqlResult2	=	sqlite_query($this->dbHandle, $query);
			$query	=	"DELETE FROM libSiteMessageFolders where sm_folder_id = '".$this->folderID."';";
			$sqlResult3	=	sqlite_query($this->dbHandle, $query);
			if(!$sqlResult2 and $sqlResult3)
				throw new SQLiteException( sqlite_error_string(sqlite_last_error($this->dbHandle)) );
			else
				return true;
		}
		
		/**
		*	create
		*
		*	create a folder
		*	@access public
		*	@return boolean folder created
		*/		
		public function create($folderName, $userId, $parent_id = "") {
			$query			=	"INSERT INTO libSiteMessageFolders (sm_folder_id, sm_user_id, sm_folder_name, sm_parent_id, sm_folder_date)
						 		 VALUES (NULL, '".$userId."','".$folderName."','".$parent_id."','".time()."');";
			if(!$sqlResult	=	sqlite_query($this->dbHandle, $query)) {
				$query		=	"SELECT sm_folder_id FROM libSiteMessageFolders WHERE sm_user_id = '".$userId."' and sm_folder_name = '".$folderName."';";
				$sqlResult	=	sqlite_query($this->dbHandle, $query);
				$result		=	sqlite_fetch_all($sqlResult);
				$this->open($result[0]['sm_folder_id']);
				return false;
			} else {
				$this->open(sqlite_last_insert_rowid($this->dbHandle));
				return true;
			}
		}		
	}
	
?>
Return current item: libSiteMessage