<?php

	/**
	 * whether we'd like to fetch a inbox, outbox or anything
	 */
	define( "BOX_TYPE_INBOX", 1 );
	define( "BOX_TYPE_OUTBOX", 2 );
	define( "BOX_TYPE_ANY", -1 );

	/**
	 * \ingroup DAO
	 *
	 * Provides an interface for working with PrivateMessageBoxes objects
	 */
	class PrivateMessageBoxes extends Model
	{

		function PrivateMessageBoxes()
		{
			$this->Model();
			$this->table = $this->getPrefix()."private_message_boxes";
		}

		/**
		 * loads the given private message box identifier
		 *
		 * @param id
		 * @return false if there was an error loading or a PrivateMessageBox object if it was found
		 */
		function getPrivateMessageBox( $id )
		{
			return( $this->get( "id", $id, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_BOXES ));
		}

		/**
		 * loads the private message inbox from user identifier
		 *
		 * @param user identifier
		 * @return false if there was an error loading or a PrivateMessageBox object if it was found
		 */
		function getPrivateMessageInboxFromUserId( $userId )
		{
			$inbox = $this->_cache->getData( $userId, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_INBOX_BY_USERID );
			if( empty( $inbox ) )
			{
				$query = "SELECT * FROM ".$this->table." WHERE user_id = ".$userId;
				$query .= " AND box_type = ".BOX_TYPE_INBOX;

				$result = $this->Execute( $query );

				if( !$result )
					return( Array());

				$row = $result->FetchRow();
				$inbox = $this->mapRow( $row );
				$this->_cache->setData( $userId, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_INBOX_BY_USERID, $inbox );
			}

			return( $inbox );
		}

		/**
		 * loads the private message outbox from user identifier
		 *
		 * @param user identifier
		 * @return false if there was an error loading or a PrivateMessageBox object if it was found
		 */
		function getPrivateMessageOutboxFromUserId( $userId )
		{
			$outbox = $this->_cache->getData( $userId, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_OUTBOX_BY_USERID );
			if( empty( $outbox ) )
			{
				$query = "SELECT * FROM ".$this->table." WHERE user_id = ".$userId;
				$query .= " AND box_type = ".BOX_TYPE_OUTBOX;

				$result = $this->Execute( $query );

				if( !$result )
					return( Array());

				$row = $result->FetchRow();
				$outbox = $this->mapRow( $row );
				$this->_cache->setData( $userId, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_OUTBOX_BY_USERID, $outbox );
			}

			return( $outbox );
		}

		/**
		 * adds a new private message outbox
		 *
		 * @param privateMessageBox A PrivateMessageBox object
		 * @return true if successful or false otherwise. Upon success, $privateMessageBox->getId() will
		 * return the new id assigned to the object in the db.
		 */
		function addPrivateMessageBox( &$privateMessageBox )
		{
			$result = $this->add( $privateMessageBox, Array( DaoCacheConstants::CACHE_PRIVATE_MESSAGE_BOXES => "getId" ));

			return( $result );
		}

		/**
		 * deletes a private message outbox. Warning: the upper layers must have already made sure that there
		 * are no private messages that point to this private message box *before* removing it, or else we could
		 * have problems with data integrity.
		 *
		 * @param id
		 * @return True if successful, false otherwise
		 */
		function deletePrivateMessageBox( $id )
		{
			$box = $this->getPrivateMessageBox( $id );
			if(!$box)
				return flase;

			if( $box->getNumPrivateMessages() > 0 )
				return flase;

			$userId = $box->getUserId();
			$boxType = $box->getBoxType();

			if( ($result = $this->delete( "id", $id ))) {
				$this->_cache->removeData( $id, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_BOXES );
				if( $boxType == BOX_TYPE_INBOX )
					$this->_cache->removeData( $userId, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_INBOX_BY_USERID );
				elseif( $boxType == BOX_TYPE_OUTBOX )
					$this->_cache->removeData( $userId, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_OUTBOX_BY_USERID );
			}

			return( $result );
		}

		/**
		 * update a given private message outbox
		 *
		 * @param privateMessageBox A PrivateMessageBox object
		 * @return True if successful or false otherwise
		 */
		function updatePrivateMessageBox( $privateMessageBox )
		{
		    $id = $privateMessageBox->getId();
		    $userId = $privateMessageBox->getUserId();
			$boxType = $privateMessageBox->getBoxType();

			if( ($result = $this->update( $privateMessageBox ))) {
				$this->_cache->removeData( $id, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_BOXES );
				if( $boxType == BOX_TYPE_INBOX )
					$this->_cache->removeData( $userId, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_INBOX_BY_USERID );
				elseif( $boxType == BOX_TYPE_OUTBOX )
					$this->_cache->removeData( $userId, DaoCacheConstants::CACHE_PRIVATE_MESSAGE_OUTBOX_BY_USERID );
			}

			return( $result );
		}

		/**
		 * @private
		 */
		function mapRow( $row )
		{
			$privateMessageBox = new PrivateMessageBox( $row["user_id"],
			                                            $row["box_type"],
							    		                unserialize($row["properties"]),
								    	                $row["id"] );
			$privateMessageBox->setNumUnreadPrivateMessages( $row["num_unread_messages"] );
			$privateMessageBox->setNumPrivateMessages( $row["num_messages"] );

			return( $privateMessageBox );
		}
	}
?>
