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

	/*
	*	Messageclass 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 SiteMessage {
		
		/**
		* @access private         
		* @var object SQLite Database Handle
		*/		
		private $dbHandle;		
		
		/**
		* @access private         
		* @var string subject
		*/		
		private $Subject;
		
		/**
		* @access private         
		* @var string message
		*/		
		private $Text;
		
		/**
		* @access private         
		* @var string date
		*/		
		private $Date;
		
		/**
		* @access private         
		* @var Integer SenderID
		*/		
		private $Sender;
		
		/**
		* @access private         
		* @var Integer ReceiverID
		*/		
		private $Receiver;
		
		/**
		* @access private         
		* @var integer messageID
		*/		
		private $messageID;
		
		/**
		* @access private         
		* @var String MessageReadDate
		*/		
		private $MessageRead;					


		/**
		*	Constructor
		*
		*	@access public
		*	@param	object	databaseHandle
		*	@param	integer	messageID
		*/		
		public function __construct($db, $sm_message_id=false) {
			$this->dbHandle		=	$db;
			if($sm_message_id) {
				$this->messageID	=	$sm_message_id;
				$this->open($this->messageID);
			}			
		}
		
		
		/**
		*	Destructor
		*
		*	@access public
		*/		
		public function __destruct() {}
		
		
		/**
		*	open
		*
		*	opens a message
		*	@access public
		*	@param	integer MessageID
		*	@param	boolean	openToRead (sets messageReadDate)
		*/
		public function open($messageID, $openToRead=true) {
			$query		=	"SELECT * FROM libSiteMessage WHERE sm_id = ".$messageID.";";
			$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->messageID	=	$result[0]['sm_id'];
					$this->Subject		=	$result[0]['sm_subject_data'];
					$this->Date			=	$result[0]['sm_message_date'];					
					$this->Text			=	$result[0]['sm_message_data'];
					$this->MessageRead	=	$result[0]['sm_message_read'];
					$this->Sender		=	$result[0]['sm_sender_id'];
					$this->Receiver		=	$result[0]['sm_receiver_id'];
					$this->Folder		=	$result[0]['sm_folder_id'];
					
					if($openToRead) {
						$query	=	"UPDATE libSiteMessage set sm_message_read='".time()."' WHERE sm_id = '".$this->messageID."';";
						$sqlResult	=	sqlite_query($this->dbHandle, $query);
						if(!$sqlResult) {
							throw new SQLiteException( sqlite_error_string(sqlite_last_error($this->dbHandle)) );
						} else {
							return true;
						}						
					} else {
						return true;
					}
				}
			}
		}		
		

		/**
		*	getSubject
		*
		*	returns the messages' subject
		*	@access public
		*	@return string MessageSubject
		*/		
		public function getSubject() {
			return $this->Subject;
		}
		
		
		/**
		*	setSubject
		*
		*	set the messages' subject
		*	@access public
		*	@param	string	new subject
		*/		
		public function setSubject($newSubject) {
			$query	=	"UPDATE libSiteMessage set sm_subject_data='".$newSubject."' WHERE sm_id = '".$this->messageID."';";
			$sqlResult	=	sqlite_query($this->dbHandle, $query);
			if(!$sqlResult) {
				throw new SQLiteException( sqlite_error_string(sqlite_last_error($this->dbHandle)) );
			} else {
				$this->open($this->messageID,false);
				return true;
			}			
		}
		
		
		/**
		*	getText
		*
		*	returns the messages' text
		*	@access public
		*	@return string	MessageText		
		*/		
		public function getText() {
			return $this->Text;
		}
		
		
		/**
		*	setText
		*
		*	set the messages' text
		*	@access public
		*	@param	string	new Text
		*/		
		public function setText($newText) {
			$query	=	"UPDATE libSiteMessage set sm_message_data='".$newText."' WHERE sm_id = '".$this->messageID."';";
			$sqlResult	=	sqlite_query($this->dbHandle, $query);
			if(!$sqlResult) {
				throw new SQLiteException( sqlite_error_string(sqlite_last_error($this->dbHandle)) );
			} else {
				$this->open($this->messageID,false);
				return true;
			}			
		}
		
		
		/**
		*	getDate
		*
		*	get the messages' date
		*	@access public
		*	@return	string	MessageDate
		*/		
		public function getDate() {
			return $this->Date;
		}
		
		/**
		*	getReadDate
		*
		*	get the Read-Date of the message
		*	@access public
		*	@return	string	MessageReadDate
		*/		
		public function getReadDate() {
			return $this->MessageRead;
		}		
		
		/**
		*	getSender
		*
		*	get the senderID
		*	@access public
		*	@return	string	messageSenderID
		*/		
		public function getSender() {
			return $this->Sender;
		}
		
		/**
		*	getReceiver
		*
		*	get the receiverID
		*	@access public
		*	@return	string	messageReceiverID
		*/		
		public function getReceiver() {
			return $this->Receiver;
		}	
		
		/**
		*	getFolder
		*
		*	get the FolderID
		*	@access public
		*	@return	integer	Folderid
		*/		
		public function getFolder() {
			return $this->Folder;
		}				
		
		/**
		*	setFolder
		*
		*	set the folder id of this message
		*	@access public
		*	@param	integer	folderID
		*/		
		public function setFolder($id) {
			$query	=	"UPDATE libSiteMessage set sm_folder_id='".$id."' WHERE sm_id = '".$this->messageID."';";
			$sqlResult	=	sqlite_query($this->dbHandle, $query);
			if(!$sqlResult) {
				throw new SQLiteException( sqlite_error_string(sqlite_last_error($this->dbHandle)) );
			} else {
				$this->open($this->messageID,false);
				return true;
			}			
		}		
		
		
		/**
		*	create
		*
		*	create a Message
		*	@access public
		*	@return boolean message created
		*	@param	integer	messageSenderID
		*	@param	integer	messageReceiverID
		*	@param	string	MessageSubject
		*	@param	string	MessageText
		*/
		public function create($sender, $receiver, $subject, $text) {
			$query		=	"INSERT INTO libSiteMessage (sm_id, sm_sender_id, sm_receiver_id, sm_subject_data, sm_message_data, sm_message_date, sm_message_read, sm_folder_id)
							 VALUES (NULL, '".$sender."','".$receiver."','".$subject."','".$text."','".time()."',NULL,NULL);";
			$sqlResult	=	sqlite_query($this->dbHandle, $query);

			if(!$sqlResult) {
				throw new SQLiteException( sqlite_error_string(sqlite_last_error($this->dbHandle)) );
			} else {
				$this->open(sqlite_last_insert_rowid($this->dbHandle), false);
				return true;
			}
		}

		/**
		*	delete
		*
		*	delets the message
		*	@access public
		*	@return	boolean message deleted
		*/		
		public function delete() {
			$query		=	"DELETE FROM libSiteMessage where sm_id = '".$this->messageID."';";
			$sqlResult	=	sqlite_query($this->dbHandle, $query);
			if(!$sqlResult)
				throw new SQLiteException( sqlite_error_string(sqlite_last_error($this->dbHandle)) );
			else
				return true;
		}
		
	}
	
?>
Return current item: libSiteMessage