[pLog-svn] r5613 - in plog/trunk: class/dao install

mark at devel.lifetype.net mark at devel.lifetype.net
Thu Jul 5 03:34:35 EDT 2007


Author: mark
Date: 2007-07-05 03:34:35 -0400 (Thu, 05 Jul 2007)
New Revision: 5613

Added:
   plog/trunk/class/dao/privatemessage.php
   plog/trunk/class/dao/privatemessages.php
   plog/trunk/class/dao/privatemessagestatus.php
Modified:
   plog/trunk/install/dbschemas.properties.php
Log:
Commit the private messages DAO class.

Added: plog/trunk/class/dao/privatemessage.php
===================================================================
--- plog/trunk/class/dao/privatemessage.php	                        (rev 0)
+++ plog/trunk/class/dao/privatemessage.php	2007-07-05 07:34:35 UTC (rev 5613)
@@ -0,0 +1,218 @@
+<?php
+
+	lt_include( PLOG_CLASS_PATH."class/database/dbobject.class.php" );
+	lt_include( PLOG_CLASS_PATH."class/dao/privatemessagestatus.class.php" );
+	
+	/**
+	 * \ingroup DAO
+	 *
+     * This is the representation of a private message. It contains all the information we need to know,
+     * such as the senderId, receiverId, subject, message, date created and whether it's been read by the user yet.
+     */
+    class PrivateMessage extends DbObject
+    {
+
+        // the keys representing a message in the db.
+		var $_id;
+        var $_receiverId;
+        var $_senderId;
+        var $_subject;
+        var $_message;
+		var $_date;
+		var $_timestamp;
+		var $_status;
+
+        function PrivateMessage( $receiverId, $senderId, $subject, $message, $date = "", 
+                                 $status = PRIVATE_MESSAGE_UNREAD, $id = -1 ) {
+      
+        	$this->DbObject();
+			lt_include( PLOG_CLASS_PATH.'class/data/timestamp.class.php' );
+        
+			//filling up the object-variables
+			$this->_receiverId = $receiverId;
+			$this->_senderId = $senderId;
+			$this->_subject = $subject;
+			$this->_message = $message;
+			$this->_date = $date;
+			$this->_status = $status;
+			$this->_id = $id;
+			
+			// if we're creating a new Message, then we're inserting the current date.
+			if( $date == "" ) {
+				lt_include( PLOG_CLASS_PATH."class/data/Date.class.php" );				
+				$dateObject = new Date();
+				$date = $dateObject->getDate();
+			}
+            $this->setDate( $date );
+
+			$this->_pk = "id";
+			$this->_fields = Array(
+				"receiver_id" => "getReceiverId",
+				"sender_id" => "getSenderId",
+				"subject" => "getSubject",
+				"message" => "getMessage",
+				"date" => "getDate",
+				"status" => "getStatus"
+			);
+        }
+
+        /**
+         * Returns the identifier of this message.
+         *
+         * @return An integer value with the identifier of this message.
+         */
+        function getId()
+        {
+            return $this->_id;
+        }
+
+        /**
+         * Returns the receiver id of this message
+         *
+         * @return An integer representing the id of the receiver
+         */
+        function getReceiverId()
+        {
+            return $this->_receiverId;
+        }
+
+        /**
+         * Returns the receiver info of this message
+         *
+         * @return An integer representing the id of the receiver
+         */		
+		function getReceiverInfo() {
+			lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
+			
+			$users = new Users();
+			$user = $users->getUserInfoFromId($this->_receiver);
+			return $user;
+		}
+		
+		/**
+         * Returns the sender id of this message
+         *
+         * @return An integer value representing the identifier of the user who is sending the message.
+         */
+        function getSenderId()
+        {
+            return $this->_senderId;
+        }
+
+		function getSenderInfo() {
+			lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
+			
+			$users = new Users();
+			$user = $users->getUserInfoFromId($this->_sender);
+			return $user;
+		}
+		
+		/**
+         * Returns the subject of this message
+         *
+         * @return A string containing the subject of this message.
+         */
+        function getSubject()
+        {
+            return $this->_subject;
+        }
+		
+		/**
+         * Returns the message in this message
+         *
+         * @return A string containing the text in this message.
+         */
+        function getMessage()
+        {
+            return $this->_message;
+        }
+		
+		
+		/**
+         * Returns the create_date for this message.
+         *
+         * @return Returns the create_date..
+         */
+        function getDate()
+        {
+            return $this->_date;
+        }
+
+		/**
+         * Returns the create date object for this message.
+         *
+         * @return Returns the create date object..
+         */
+        function getDateObject()
+        {
+            return $this->_timestamp;
+        }
+		
+        /**
+         * @private
+         */
+        function getStatus()
+        {
+            return $this->_status;
+        }
+        
+        /**
+         * @private
+         */
+        function setId( $id )
+        {
+            $this->_id = $id;
+        }
+
+		/**
+         * @private
+         */
+        function setReceiverId( $newReceiverId )
+        {
+            $this->_receiverId = $newReceiverId;
+        }
+
+		/**
+         * @private
+         */
+        function setSenderId( $newSenderId )
+        {
+            $this->_senderId = $newSenderId;
+        }
+
+		/**
+         * @private
+         */
+        function setSubject( $newSubject )
+        {
+            $this->_subject = $newSubject;
+        }
+
+		/**
+         * @private
+         */
+        function setMessasge( $newMessage )
+        {
+            $this->_message = $newMessage;
+        }
+
+		/**
+         * @private
+         */
+        function setDate( $newDate )
+        {
+            lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
+
+            $this->_date = $newDate;
+            $this->_timestamp = new Timestamp( $newDate );
+        }
+
+		/**
+         * @private
+         */
+        function setStatus( $status )
+        {
+            $this->_status = $status;
+        }
+	}
+?>
\ No newline at end of file

Added: plog/trunk/class/dao/privatemessages.php
===================================================================
--- plog/trunk/class/dao/privatemessages.php	                        (rev 0)
+++ plog/trunk/class/dao/privatemessages.php	2007-07-05 07:34:35 UTC (rev 5613)
@@ -0,0 +1,215 @@
+<?php
+
+	lt_include( PLOG_CLASS_PATH."class/dao/model.class.php" );
+	lt_include( PLOG_CLASS_PATH."class/dao/privatemessagestatus.class.php" );
+
+    /**
+	 * \ingroup DAO
+	 *
+     * Model for the private message feature
+     */
+    class PrivateMessages extends Model 
+	{
+	
+		function PrivateMessages()
+		{
+			$this->Model();
+		
+			$this->table = $this->getPrefix()."private_messages";
+		}
+
+		/**
+         * Adds a private message to the database
+         *
+         * @param privateMessage a PrivateMessage Object
+         * @return True if successful or false otherwise.
+         */
+        function addPrivateMessage( $privateMessage )
+        {
+        	$result = $this->add( $privateMessage );
+
+			if( $result ) {
+				// clean the cache
+                $this->_cache->removeData( $privateMessage->getId(), CACHE_PRIVATE_MESSAGES );
+                $this->_cache->removeData( $privateMessage->getReceiverId(), CACHE_PRIVATE_MESSAGES_BY_RECEIVER_ID );
+                $this->_cache->removeData( $privateMessage->getSenderId(), CACHE_PRIVATE_MESSAGES_BY_SENDER_ID );
+				// update corresponding counters
+				$receiver = $privateMessage->getReceiverInfo();
+				$receiver->setNumPrivateMessages( $receiver->getNumPrivateMessages() + 1 );
+				$receiver->setNumUnreadPrivateMessages( $receiver->getNumUnreadPrivateMessages() + 1 );
+				lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
+				$users = new Users();
+				$users->updateUser( $receiver );
+			}
+
+            return $result;
+        }
+		
+		/**
+         * Updates a private message to the database
+         *
+         * @param $privateMessage a PrivateMessage object
+         * @return True if successful or false otherwise.
+         */
+        function updatePrivateMessage( &$privateMessage )
+        {
+			// get the previous version of this private message before saving it
+			$prevVersion = $this->getPrivateMessage( $privateMessage->getId());
+		
+        	$result = $this->update( $privateMessage );
+
+            if( !$result ) {
+            	return false;
+            }
+
+            // clean the cache
+            $this->_cache->removeData( $privateMessage->getId(), CACHE_PRIVATE_MESSAGES );
+            $this->_cache->removeData( $privateMessage->getReceiverId(), CACHE_PRIVATE_MESSAGES_BY_RECEIVER_ID );
+            $this->_cache->removeData( $privateMessage->getSenderId(), CACHE_PRIVATE_MESSAGES_BY_SENDER_ID );
+			// update corresponding counters
+			$receiver = $prevVersion->getReceiverInfo();
+			if( $prevVersion->getStatus() == PRIVATE_MESSAGE_UNREAD &&
+				$privateMessage->getStatus() == PRIVATE_MESSAGE_READ ) 
+			{
+				$receiver->setNumUnreadPrivateMessages( $receiver->getNumUnreadPrivateMessages() - 1 );
+				lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
+				$users = new Users();
+				$users->updateUser( $receiver );
+			} elseif ( $prevVersion->getStatus() == PRIVATE_MESSAGE_READ &&
+					   $privateMessage->getStatus() == PRIVATE_MESSAGE_UNREAD ) 
+			{
+				$receiver->setNumUnreadPrivateMessages( $receiver->getNumUnreadPrivateMessages() + 1 );
+				lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
+				$users = new Users();
+				$users->updateUser( $receiver );
+			}
+			
+          	return true;
+        }
+
+		/**
+         * Mark private message as read, a shortcut to updatePrivateMessage
+         *
+         * @param $privateMessage a PrivateMessage object
+         * @return True if successful or false otherwise.
+         */
+        function markPrivateMessasgeAsRead( &$privateMessage )
+        {
+			$privateMessage->setStatus( PRIVATE_MESSAGE_READ );
+			$result = $this->updatePrivateMessage( $privateMessage );
+			
+			return $result;
+        }
+
+		/**
+         * Mark private message as unread, a shortcut to updatePrivateMessage
+         *
+         * @param $privateMessage a PrivateMessage object
+         * @return True if successful or false otherwise.
+         */
+        function markPrivateMessasgeAsUnread( &$privateMessage )
+        {
+			$privateMessage->setStatus( PRIVATE_MESSAGE_UNREAD );
+			$result = $this->updatePrivateMessage( $privateMessage );
+			
+			return $result;
+        }
+		
+		/**
+         * Get all incoming private messages from a receiverId from the db.
+		 *
+         * @param receiverId Receiver identifier of the message
+		 * @param searchTerms
+		 * @param page
+		 * @param itemsPerPage
+		 * @return Returns an array of Private Messages for that specific Receiver.
+         */
+		function getAllIncomingMessagesFromReceiverId( $receiverId, $searchTerms = "", $page = -1, $itemsPerPage = 15 )			
+		{
+			$privateMessages = $this->getMany( "receiver_id", 
+			                                   $receiverId, 
+			                                   CACHE_PRIVATE_MESSAGES_BY_RECEIVER_ID,
+			                                   Array( CACHE_PRIVATE_MESSAGES => "getId" ),
+			                                   Array( "date" => "DESC" ),
+			                                   $searchTerms,
+			                                   $page,
+			                                   $itemsPerPage );			                             
+			    
+            if( !$privateMessages )
+            	return Array();
+            else
+            	return( $privateMessages );
+		}
+
+		/**
+         * Get all outgoing private messages for a senderId from the db.
+		 *
+         * @param senderId Receiver identifier of the message
+		 * @param searchTerms
+		 * @param page
+		 * @param itemsPerPage
+		 * @return Returns an array of Private Messages for that specific Receiver.
+         */
+		function getAllOutgoingMessagesFromSenderId( $senderId, $searchTerms = "", $page = -1, $itemsPerPage = 15 )			
+		{
+			$privateMessages = $this->getMany( "sender_id", 
+			                                   $senderId, 
+			                                   CACHE_PRIVATE_MESSAGES_BY_SENDER_ID,
+			                                   Array( CACHE_PRIVATE_MESSAGES => "getId" ),
+			                                   Array( "date" => "DESC" ),
+			                                   $searchTerms,
+			                                   $page,
+			                                   $itemsPerPage );			                             
+			    
+            if( !$privateMessages )
+            	return Array();
+            else
+            	return( $privateMessages );
+		}
+
+		/**
+         * get a specific private message from the db.
+		 *
+		 * @param $messageId The id of the private message.
+		 * @param $receiverId The user identifier. Used as a securityfunction, so that you
+		 * can only see your own private messages.
+		 * @return Returns the specific private message as a PrivateMessage object.
+         */
+		function getPrivateMessage( $messageId, $receiverId = -1 )
+        {
+        	$privateMessage = $this->get( "id", $messageId, CACHE_PRIVATE_MESSAGES );
+        	if( !$privateMessage )
+        		return false;
+
+			if( $privateMessage->getReceiverId() == $receiverId )
+				return $privateMessage;
+			else
+				return false;
+		}
+
+		/**
+		 * @see Model::getSearchConditions
+		 */
+		function getSearchConditions( $searchTerms )
+		{
+			return( "(normalized_subject LIKE '%".Db::qstr( $searchTerms )."%' OR normalized_message LIKE '%".Db::qstr( $searchTerms )."%')" );
+		}
+        
+		/**
+		 * @private
+		 */
+        function mapRow( $row )
+        {
+            $privateMessage = new PrivateMessage( $row["sender_id"], 
+			                                      $row["receiver_id"], 
+			                                      $row["subject"], 
+			                                      $row["message"], 
+			                                      $row["date"], 
+			                                      $row["status"],
+			                                      $row["id"] );
+
+			return $privateMessage;
+
+        }
+	}
+?>
\ No newline at end of file

Added: plog/trunk/class/dao/privatemessagestatus.php
===================================================================
--- plog/trunk/class/dao/privatemessagestatus.php	                        (rev 0)
+++ plog/trunk/class/dao/privatemessagestatus.php	2007-07-05 07:34:35 UTC (rev 5613)
@@ -0,0 +1,52 @@
+<?php
+
+    lt_include( PLOG_CLASS_PATH."class/dao/status/genericstatuslist.class.php" );
+	
+	define( "PRIVATE_MESSAGE_ALL", -1, true );
+    define( "PRIVATE_MESSAGE_UNREAD", 1, true );
+    define( "PRIVATE_MESSAGE_READ", 2, true );
+
+    /**
+     * This class keeps track of all the possible status that a private message can have. If plugins dynamically 
+	 * register new private message statuses, this class will still be able to handle them
+	 * 
+	 * \ingroup DAO
+     */    
+    class PrivateMessageStatus extends GenericStatusList
+    {
+    
+        /**
+         * returns a list with all the private message statuses that have been defined
+         * so far in the code.
+         *
+         * @return Returns an array where every position is an array with two
+         * keys: "constant" and "value", where "constant" is the name of the constant
+         * that defines this status and "value" is the value assigned to it
+         */
+        function getStatusList( $includeStatusAll = false )
+        {
+            return( GenericStatusList::getStatusList( "PRIVATE_MESSAGE_", "PRIVATE_MESSAGE_ALL", $includeStatusAll ));
+        }
+        
+        /**
+         * @param status The status code we'd like to check
+         * 
+         * @return Returns true if the status is valid or false otherwise
+         */
+        function isValidStatus( $status )
+        {
+	    	$statusList = PrivateMessageStatus::getStatusList( true );
+	    	return( in_array( $status, $statusList ));
+        }
+        
+        /**
+         * returns the default status code for this class
+         *
+         * @return The default status
+         */
+        function getDefaultStatus()
+        {
+	     	return( PRIVATE_MESSAGE_ALL );   
+        }        
+    }
+?>
\ No newline at end of file

Modified: plog/trunk/install/dbschemas.properties.php
===================================================================
--- plog/trunk/install/dbschemas.properties.php	2007-07-04 21:21:32 UTC (rev 5612)
+++ plog/trunk/install/dbschemas.properties.php	2007-07-05 07:34:35 UTC (rev 5613)
@@ -435,7 +435,6 @@
   message TEXT NOTNULL,
   normalized_subject TEXT NOTNULL DEFAULT '',
   normalized_message TEXT NOTNULL DEFAULT '',
-  already_read I(1) NOTNULL DEFAULT 0,
   date DATETIME NOTNULL,
   status I(1) NOTNULL DEFAULT 1,
   INDEX sender_id (sender_id),



More information about the pLog-svn mailing list