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

mark at devel.lifetype.net mark at devel.lifetype.net
Fri Jun 8 09:28:20 EDT 2007


Author: mark
Date: 2007-06-08 09:28:20 -0400 (Fri, 08 Jun 2007)
New Revision: 5512

Added:
   plog/trunk/class/dao/friend.class.php
   plog/trunk/class/dao/friendauthorizationstatus.class.php
   plog/trunk/class/dao/friendgroup.class.php
   plog/trunk/class/dao/friendgroups.class.php
   plog/trunk/class/dao/friends.class.php
   plog/trunk/class/dao/userauthorizationstatus.class.php
Modified:
   plog/trunk/class/dao/daocacheconstants.properties.php
   plog/trunk/install/dbschemas.properties.php
Log:
Commit the DAOs of friend and friend groups.

In this new friend system, the user can invite another user as his friend, and can categorize the friend into different friend category.

The system is the two-way friend system as MSN or Skype, that means one ask to add you as his friend, he must wait for your approval. If you reject, then he can not add you as his friend.

But, when you delete one friend, you only delete him from your friend list. But, you still exist in his friend list.

So, I consider implement block_friend_list, too.

Modified: plog/trunk/class/dao/daocacheconstants.properties.php
===================================================================
--- plog/trunk/class/dao/daocacheconstants.properties.php	2007-06-07 20:32:30 UTC (rev 5511)
+++ plog/trunk/class/dao/daocacheconstants.properties.php	2007-06-08 13:28:20 UTC (rev 5512)
@@ -113,4 +113,15 @@
      */
     define( "CACHE_PERMISSIONS_ALL", "permissions_all" );
     define( "CACHE_PERMISSIONS", "permissions" );
+
+    /**
+     * friend groups
+     */
+    define( "CACHE_FRIEND_GROUPS", "friend_groups" );
+    define( "CACHE_FRIEND_GROUPS_BY_USER", "friend_groups_by_user" );
+
+    /**
+     * friends
+     */
+    define( "CACHE_FRIENDS", "friends" );
 ?>
\ No newline at end of file

Added: plog/trunk/class/dao/friend.class.php
===================================================================
--- plog/trunk/class/dao/friend.class.php	                        (rev 0)
+++ plog/trunk/class/dao/friend.class.php	2007-06-08 13:28:20 UTC (rev 5512)
@@ -0,0 +1,214 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/database/dbobject.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/friendgroups.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/userauthorizationstatus.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/friendauthorizationstatus.class.php" );
+
+    /**
+	 * \ingroup DAO
+	 *
+     * Represents one of the friends that can be created for each blog.
+     */
+	class Friend extends DbObject
+	{
+
+		var $_id;
+		var $_userId;
+		var $_friendId;
+        var $_groupId;
+		var $_description;
+        var $_userAuthorizationStatus;
+        var $_friendAuthorizationStatus;
+        var $_friendInfo;
+
+        /**
+         * Constructor.
+         *
+         * @param id Internal idenfitier of the friend
+         * @param userId Identifier of the user
+         * @param friendId User identifier of the friend
+         * @param groupId Group identifier of the friend
+         * @param description Description of the friend
+         * @param userAuthorizationStatus
+         * @param friendAuthorizationStatus
+         */
+		function Friend( $userId, $friendId, $groupId = 0 , $description = "", 
+						 $userAuthorizationStatus = USER_AUTHORIZATION_APPROVED, $friendAuthorizationStatus = FRIEND_AUTHORIZATION_APPROVED, $id = -1)
+		{
+			$this->DbObject();
+
+			$this->_userId = $userId;
+			$this->_friendId = $friendId;
+			$this->_groupId  = $groupId;
+			$this->_description = $description;
+			$this->_userAuthorizationStatus = $userAuthorizationStatus;
+			$this->_friendAuthorizationStatus = $friendAuthorizationStatus;
+            $this->_id = $id;
+
+            $this->_pk = "id";
+			$this->_fields = Array( "id" => "getId",
+									"user_id" => "getUserId",
+			   						"friend_id" => "getFriendId",
+			   						"group_id" => "getGroupId",
+			   						"description" => "getDescription",
+			   						"user_authorization_status" => "getUserAuthorizationStatus",
+			   						"friend_authorization_status" => "getFriendAuthorizationStatus");
+		}
+
+        /**
+         * @private
+         */
+		function setId( $newId )
+		{
+			$this->_id = $newId;
+		}
+
+		/**
+         * @private
+         */
+        function setUserId( $newUserId )
+        {
+        	$this->_userId = $newUserId;
+        }
+
+        /**
+         * @private
+         */
+		function setFriendId( $newFriendId )
+		{
+			$this->_friendId = $newFriendId;
+		}
+
+		/**
+         * @private
+         */
+		function setGroupId( $newGroupId )
+		{
+			$this->_groupId = $newGroupId;
+		}
+
+		/**
+         * @private
+         */
+		function setDescription( $newDescription )
+		{
+			$this->_description = $newDescription;
+		}
+
+        /**
+         * @private
+         */
+        function setUserAuthorizationStatus( $newUserAuthorizationStatus )
+        {
+        	$this->_userAuthorizationStatus = $newUserAuthorizationStatus;
+        }
+
+        /**
+         * @private
+         */
+        function setFriendAuthorizationStatus( $newFriendAuthorizationStatus )
+        {
+        	$this->_friendAuthorizationStatus = $newFriendAuthorizationStatus;
+        }
+
+        /**
+         * Returns the identifier that this friend has in the database.
+         *
+         * @return An integer value.
+         */
+		function getId()
+		{
+			return $this->_id;
+		}
+
+        /**
+         * Returns the user identifier that was given to this friend.
+         *
+         * @return An integer value.
+         */
+        function getUserId()
+        {
+        	return $this->_userId;
+        }
+
+        /**
+         * Returns the friend identifier that was given to this friend.
+         *
+         * @return An integer value.
+         */
+        function getFriendId()
+        {
+        	return $this->_friendId;
+        }
+
+        /**
+         * Returns the group identifier that was given to this friend.
+         *
+         * @return An integer value.
+         */
+        function getGroupId()
+        {
+        	return $this->_groupId;
+        }
+
+        /**
+         * Returns the group object that was given to this friend.
+         *
+         * @return An integer value.
+         */
+        function getGroup()
+        {
+        	$groups = new FriendGroups();
+        	$group = $groups->getFriendGroup( $this->_groupId );
+        	return $group;
+        }
+
+        /**
+         * Returns the description assigned to this friend.
+         *
+         * @return An string with the description.
+         */
+        function getDescription()
+		{
+			return $this->_description;
+		}
+
+        /**
+         * Returns the user authorization status assigned to this friend.
+         *
+         * @return An integer value.
+         */
+        function getUserAuthorizationStatus()
+		{
+			return $this->_userAuthorizationStatus;
+		}
+		
+        /**
+         * Returns the friend authorization status assigned to this friend.
+         *
+         * @return An integer value.
+         */
+        function getFriendAuthorizationStatus()
+		{
+			return $this->_friendAuthorizationStatus;
+		}
+		
+		function getFriendInfo()
+		{
+			if( $this->_friendInfo )
+				return $this->_friendInfo;
+			else {
+				include_once( PLOG_CLASS_PATH."class/dao/users.class.php" );
+				$users =& new Users();
+				$friendInfo = $users->getUserInfoFromId( $this->_friendId );
+				if( $friendInfo ) {
+					$this->_friendInfo = $friendInfo;
+					return $friendInfo;
+				}
+				else
+					return false;
+			}
+		}
+}
+?>
\ No newline at end of file

Added: plog/trunk/class/dao/friendauthorizationstatus.class.php
===================================================================
--- plog/trunk/class/dao/friendauthorizationstatus.class.php	                        (rev 0)
+++ plog/trunk/class/dao/friendauthorizationstatus.class.php	2007-06-08 13:28:20 UTC (rev 5512)
@@ -0,0 +1,53 @@
+<?php
+
+    include_once( PLOG_CLASS_PATH."class/dao/status/genericstatuslist.class.php" );
+	
+	define( "FRIEND_AUTHORIZATION_ALL", -1, true ); 
+    define( "FRIEND_AUTHORIZATION_APPROVED", 1, true );
+    define( "FRIEND_AUTHORIZATION_WAITING", 2, true );
+    define( "FRIEND_AUTHORIZATION_REJECT", 3, true );
+
+    /**
+     * This class keeps track of all the possible status that a friend can have. If plugins dynamically 
+	 * register new friend statuses, this class will still be able to handle them
+	 * 
+	 * \ingroup DAO
+     */    
+    class FriendAuthorizationStatus extends GenericStatusList
+    {
+    
+        /**
+         * returns a list with all the user 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( "FRIEND_AUTHORIZATION_", "FRIEND_AUTHORIZATION_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 = FriendAuthorizationStatus::getStatusList( true );
+	    	return( in_array( $status, $statusList ));
+        }
+        
+        /**
+         * returns the default status code for this class
+         *
+         * @return The default status
+         */
+        function getDefaultStatus()
+        {
+	     	return( FRIEND_AUTHORIZATION_ALL );   
+        }        
+    }
+?>
\ No newline at end of file

Added: plog/trunk/class/dao/friendgroup.class.php
===================================================================
--- plog/trunk/class/dao/friendgroup.class.php	                        (rev 0)
+++ plog/trunk/class/dao/friendgroup.class.php	2007-06-08 13:28:20 UTC (rev 5512)
@@ -0,0 +1,228 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/database/dbobject.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/userauthorizationstatus.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/friendauthorizationstatus.class.php" );
+
+    /**
+	 * \ingroup DAO
+	 *
+     * Represents a friend group
+     */
+	class FriendGroup extends DbObject
+	{
+
+		var $_id;
+		var $_userId;
+		var $_name;
+		var $_description;
+		var $_properties;
+		var $_mangledName;
+		var $_numFriends;
+		var $_numAuthorizedFriends;
+		var $_numWaitingFriends;
+		var $_numPendingFriends;
+
+		function FriendGroup( $userId, $name, $description = "", $properties = Array(), $id = -1 )
+		{
+			$this->DbObject();
+
+			$this->_id = $id;
+			$this->_userId = $userId;
+			$this->_name = $name;
+			$this->_description = $description;
+			$this->setProperties( $properties );
+			$this->_numFriends = 0;
+			$this->_numAuthorizedFriends = 0;
+			$this->_numWaitingFriends = 0;
+			$this->_numPendingFriends = 0;
+
+			$this->_pk = "id";
+			$this->_fields = Array( "id" => "getId",
+			                        "user_id" => "getUserId",
+			                        "name" => "getName",
+			                        "description" => "getDescription",
+			                        "properties" => "getProperties",
+			                        "num_friends" => "getNumFriends",
+			                        "num_authorized_friends" => "getNumAuthorizedFriends",
+			                        "num_waiting_friends" => "getNumWaitingFriends",
+			                        "num_pending_friends" => "getNumPendingFriends",
+			                        "mangled_name" => "getMangledName" );
+		}
+
+        /**
+         * @private
+         */
+		function setId( $newId )
+		{
+			$this->_id = $newId;
+		}
+
+        /**
+         * @private
+         */
+		function setUserId( $newUserId )
+		{
+			$this->_userId = $newUserId;
+		}
+
+        /**
+         * @private
+         */
+		function setName( $newName )
+		{
+			$this->_name = $newName;
+		}
+
+		/**
+		 * sets the description
+		 *
+		 * @param description
+		 */
+		function setDescription( $desc )
+		{
+			$this->_description = $desc;
+		}
+
+        /**
+         * Returns the identifier assigned to this category.
+         *
+         * @return An integer value with the category number.
+         */
+		function getId()
+		{
+			return $this->_id;
+		}
+
+        /**
+         * Returns the user identifier assigned to the category.
+         *
+         * @return An integer value with the category number.
+         */
+		function getUserId()
+		{
+			return $this->_userId;
+		}
+
+        /**
+         * Returns the name assigned to the category.
+         *
+         * @return A string value with the name assigned to the category.
+         */
+		function getName()
+		{
+			return $this->_name;
+		}
+
+        /**
+         * Returns how many articles have been categorized under this category.
+         *
+		 * @param status A valid post status
+         * @return An integer value
+         */
+        function getNumFriends( $userAuthorizationStatus = USER_AUTHORIZATION_APPROVED, $friendAuthorizationStatus = FRIEND_AUTHORIZATION_APPROVED )
+        {
+        	if( $userAuthorizationStatus == USER_AUTHORIZATION_ALL && $friendAuthorizationStatus == FRIEND_AUTHORIZATION_ALL )
+        		return( $this->_numFriends );
+        	elseif( $userAuthorizationStatus == USER_AUTHORIZATION_WAITING && $friendAuthorizationStatus == FRIEND_AUTHORIZATION_APPROVED )
+        		return( $this->_numWaitingFriends );
+        	elseif( $userAuthorizationStatus == USER_AUTHORIZATION_APPROVED && $friendAuthorizationStatus == FRIEND_AUTHORIZATION_WAITING )
+        		return( $this->_numPendingFriends );
+        	elseif( $userAuthorizationStatus == USER_AUTHORIZATION_APPROVED && $friendAuthorizationStatus == FRIEND_AUTHORIZATION_APPROVED )
+        		return( $this->_numAuthorizedFriends );
+        	else {
+	        	include_once( PLOG_CLASS_PATH."class/dao/friendgroups.class.php" );
+    	    	$groups = new FriendGroups();
+        		return( $groups->getNumFriendsGroup( $this->getId(), $userAuthorizationStatus, $friendAuthorizationStatus ) );
+        	}
+        }
+
+        function setNumFriends( $numFriends )
+        {
+        	$this->_numFriends = $numFriends;
+			if( $this->_numFriends < 0 )
+				$this->_numFriends = 0;
+        }
+
+        function getNumAuthorizedFriends()
+        {
+        	return( $this->getNumFriends( USER_AUTHORIZATION_APPROVED, FRIEND_AUTHORIZATION_APPROVED ));
+        }
+
+        function setNumAuthorizedFriends( $numAuthorizedFriends )
+        {
+        	$this->_numAuthorizedFriends = $numAuthorizedFriends;
+			if( $this->_numAuthorizedFriends < 0 )
+				$this->_numAuthorizedFriends = 0;
+        }
+        
+        function getNumWaitingFriends()
+        {
+        	return( $this->getNumFriends( USER_AUTHORIZATION_WAITING, FRIEND_AUTHORIZATION_APPROVED ));
+        }
+
+        function setNumWaitingFriends( $numWaitingFriends )
+        {
+        	$this->_numWaitingFriends = $numWaitingFriends;
+			if( $this->_numWaitingFriends < 0 )
+				$this->_numAWaitingFriends = 0;
+        }
+        
+        function getNumPendingFriends()
+        {
+        	return( $this->getNumFriends( USER_AUTHORIZATION_APPROVED, FRIEND_AUTHORIZATION_WAITING ));
+        }
+
+        function setNumPendingFriends( $numPendingFriends )
+        {
+        	$this->_numPendingFriends = $numPendingFriends;
+			if( $this->_numPendingFriends < 0 )
+				$this->_numPendingFriends = 0;
+        }
+
+		/**
+		 * returns a list with all the friends that have been categorized under this group
+		 *
+		 * @return An Array of Friend objects
+		 */
+		function getFriends( $userAuthorizationStatus = -1, $friendAuthorizationStatus = -1, $searchTerms = "", $page = -1, $itemsPerPage = 15 )
+		{
+			include_once( PLOG_CLASS_PATH."class/dao/friens.class.php" );
+			$friends = Friends::getFriends( $this->_userId, $this->_id, $userAuthorizationStatus, $friendAuthorizationStatus, $searchTerms, $page, $itemsPerPage );
+		}
+
+		/**
+		 * returns the description
+		 *
+		 * @return The description
+		 */
+		function getDescription()
+		{
+			return $this->_description;
+		}
+
+		/**
+		 * @private
+		 * For future use
+		 */
+		function getProperties()
+		{
+			return( $this->_properties );
+		}
+
+		function setMangledName( $mangledName )
+		{
+			$this->_mangledName = $mangledName;
+		}
+
+		function getMangledName()
+		{
+			if( $this->_mangledName == "" ) {
+				include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
+				$this->_mangledName = Textfilter::urlize( $this->getName() );
+			}
+
+			return( $this->_mangledName );
+		}
+	}
+?>

Added: plog/trunk/class/dao/friendgroups.class.php
===================================================================
--- plog/trunk/class/dao/friendgroups.class.php	                        (rev 0)
+++ plog/trunk/class/dao/friendgroups.class.php	2007-06-08 13:28:20 UTC (rev 5512)
@@ -0,0 +1,171 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
+	include_once( PLOG_CLASS_PATH."class/dao/friendgroup.class.php" );
+	include_once( PLOG_CLASS_PATH.'class/dao/daocacheconstants.properties.php' );
+
+	class FriendGroups extends Model
+	{
+
+		function FriendGroups()
+		{
+			$this->Model();
+			$this->table = $this->getPrefix()."friend_groups";
+		}
+
+		/**
+		 * loads the given friend group
+		 *
+		 * @param id
+		 * @return false if there was an error loading or a FriendGroup object if it was found
+		 */
+		function getFriendGroup( $id, $userId = -1 )
+		{
+        	$group = $this->get( "id", $id, CACHE_FRIEND_GROUPS );
+        	if( !$group )
+        		return false;
+        	if( $userId > -1 && $group->getUserId() != $userId )
+        		return false;
+
+        	return( $group );
+		}
+
+        /**
+         * Returns the FriendGroups that belong to a given user
+         *
+         * @param userId The identifer of the user from which we want to fetch
+		 * @param searchTerms
+		 * @param page
+		 * @param itemsPerPage
+         * @return An array containing all the categories from a given blog.
+         */
+        function getUserFriendGroups( $userId, $searchTerms = "", $page = -1, $itemsPerPage = 15 )
+        {
+			$groups = $this->getMany( "user_id",
+			                          $userId,
+			                          CACHE_FRIEND_GROUPS_BY_USER,
+			                          Array( CACHE_FRIEND_GROUPS => "getId" ),
+			                          Array( "name" => "ASC" ),
+			                          $searchTerms,
+			                          $page,
+			                          $itemsPerPage );
+
+			if( !$groups )
+				return Array();
+
+            return( $groups );
+		}
+
+		/**
+		 * adds a new blog category
+		 *
+		 * @param category A FriendGroup object
+		 * @return true if successful or false otherwise. Upon success, $group->getId() will
+		 * return the new id assigned to the object in the db.
+		 */
+		function addFriendGroup( &$group )
+		{
+			if( ($result = $this->add( $group, Array( CACHE_FRIEND_GROUPS => "getId" )))) {
+				$this->_cache->removeData( $group->getUserId(), CACHE_FRIEND_GROUPS_BY_USER );
+			}
+
+			return( $result );
+		}
+
+		/**
+		 * deletes a FriendGroup. Warning: the upper layers must have already made sure that there
+		 * are no blogs that point to this blog categories *before* removing it, or else we could have
+		 * problems with data integrity.
+		 *
+		 * @param id
+		 * @return True if successful, false otherwise
+		 */
+		function deleteFriendGroup( $id )
+		{
+			$group = $this->getFriendGroup( $id );
+			if( !$group )
+				return false;
+
+			if( ($result = $this->delete( "id", $id ))) {
+				$this->_cache->removeData( $id, CACHE_FRIEND_GROUPS );
+				$this->_cache->removeData( $group->getUserId(), CACHE_FRIEND_GROUPS_BY_USER );
+			}
+
+			return( $result );
+		}
+
+		/**
+		 * update a given blog category
+		 *
+		 * @param category A FriendGroup object
+		 * @return True if successful or false otherwise
+		 */
+		function updateFriendGroup( $group )
+		{
+			if( ($result = $this->update( $group ))) {
+				$this->_cache->removeData( $group->getId(), CACHE_FRIEND_GROUPS );
+				$this->_cache->removeData( $group->getUserId(), CACHE_FRIEND_GROUPS_BY_USER );
+			}
+
+			return( $result );
+		}
+
+		/**
+		 * returns the total amount of blog categories in the database
+		 *
+		 * @return an integer
+		 */
+		function getNumUserFriendGroups( $userId, $searchTerms = "" )
+		{
+			return( count( $this->getUserFriendGroups( $userId, $searchTerms ) ) );
+		}
+
+		/**
+		 * returns how many friends have been categorized under this group
+		 *
+		 * @param groupId
+		 * @param userAuthorizationStatus
+		 * @param friendAuthorizationStatus
+		 * @return an integer
+		 */
+		function getNumFriendsGroup( $groupId, $userAuthorizationStatus = USER_AUTHORIZATION_ALL, $friendAuthorizationStatus = FRIEND_AUTHORIZATION_ALL )
+		{
+			$cond = "b.group_id = '".Db::qstr( $groupId )."'";
+			
+			if( $userAuthorizationStatus != USER_AUTHORIZATION_ALL )
+				$cond .= " AND user_authorization_status = '".Db::qstr( $userAuthorizationStatus )."'";
+				
+			if( $friendAuthorizationStatus != FRIEND_AUTHORIZATION_ALL )
+				$cond .= " AND friend_authorization_status = '".Db::qstr( $friendAuthorizationStatus )."'";
+
+			return( $this->getNumItems( $this->getPrefix()."friends", $cond ));
+
+		}
+
+		/**
+		 * @see Model::getSearchConditions()
+		 */
+		function getSearchConditions( $searchTerms )
+		{
+			return( "name LIKE '%".Db::qstr( $searchTerms )."%' OR description LIKE '%".Db::qstr( $searchTerms )."%'" );
+		}
+
+		/**
+		 * @private
+		 */
+		function mapRow( $row )
+		{
+			$group = new FriendGroup( $row["user_id"],
+			                          $row["name"],
+			                          $row["description"],
+			                          unserialize($row["properties"]),
+			                          $row["id"] );
+			$group->setNumFriends( $row["num_friends"] );
+			$group->setNumAuthorizedFriends( $row["num_authorized_friends"] );
+			$group->setNumWaitingFriends( $row["num_waiting_friends"] );
+			$group->setNumPendingFriends( $row["num_pending_friends"] );
+
+			return( $group );
+		}
+	}
+?>
\ No newline at end of file

Added: plog/trunk/class/dao/friends.class.php
===================================================================
--- plog/trunk/class/dao/friends.class.php	                        (rev 0)
+++ plog/trunk/class/dao/friends.class.php	2007-06-08 13:28:20 UTC (rev 5512)
@@ -0,0 +1,296 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/friend.class.php" );
+	include_once( PLOG_CLASS_PATH.'class/dao/daocacheconstants.properties.php' );
+
+    /**
+	 * \ingroup DAO
+	 *
+     * Model for the friends feature
+     */
+    class Friends extends Model
+	{
+
+		function Friends()
+		{
+			$this->Model();
+
+			$this->table = $this->getPrefix()."friends";
+		}
+
+        /**
+		 * Retrieves the friends of the given user.
+         *
+         * @param userId Identifier of the user
+         * @param groupId Group identifier of the friends
+         * @param userAuthorizationStatus User authorization status
+         * @param friendAuthorizationStatus Friend authorization status
+		 * @param searchTerms
+		 * @param page
+		 * @param itemsPerPage
+		 */
+		function getFriends( $userId, $groupId = 0, $userAuthorizationStatus = USER_AUTHORIZATION_ALL, $friendAuthorizationStatus = FRIEND_AUTHORIZATION_ALL, $searchTerms = "", $page = -1, $itemsPerPage = 15 )
+		{
+			$friends = Array();
+			$query = "SELECT id FROM ".$this->getPrefix()."friends WHERE user_id = '".$userId."' AND"; 
+			if( $groupId != 0 )
+				$query .= " group_id = '".Db::qstr($groupId)."' AND";
+			if( $userAuthorizationStatus != USER_AUTHORIZATION_ALL )
+				$query .= " user_authorization_status = '".Db::qstr($userAuthorizationStatus)."' AND";
+			if( $friendAuthorizationStatus != FRIEND_AUTHORIZATION_ALL )
+				$query .= " friend_authorization_status = '".Db::qstr($friendAuthorizationStatus)."' AND";
+			if( $searchTerms != "" )
+				$query .= " (".$this->getSearchConditions( $searchTerms ).")";
+
+			// just in case if for any reason the string ends with "AND"
+			$query = trim( $query );
+			if( substr( $query, -3, 3 ) == "AND" )
+				$query = substr( $query, 0, strlen( $query ) - 3 );
+
+			$result = $this->Execute( $query, $page, $itemsPerPage );
+			if( !$result )
+				return $friends;
+
+			while( $row = $result->FetchRow()) {
+				// use the primary key to retrieve the items via the cache
+				$friends[] = $friend = $this->get( "id", $row["id"], CACHE_FRIENDS );
+			}
+
+            return $friends;
+		}
+
+		/**
+		 * returns how many friend a user has
+		 *
+		 * @param userId
+		 * @param groupId
+		 * @param searchTerms
+		 * @return an integer
+		 */
+		function getNumFriends( $userId, $groupId = 0, $userAuthorizationStatus = USER_AUTHORIZATION_ALL, $friendAuthorizationStatus = FRIEND_AUTHORIZATION_ALL, $searchTerms = "" )
+		{
+			return( count( $this->getFriends( $userId, $groupId, $userAuthorizationStatus, $friendAuthorizationStatus, $searchTerms )));
+		}
+		
+		function isFriend( &$friend )
+		{
+			return $this->getNumItems( $this->getPrefix().'friends', 'user_id = '.$friend->getUserId().' AND friend_id = '.$friend->getFriendId() );	
+		}
+
+		/**
+		 * Updates friend group counters
+		 *
+		 * @private
+		 */
+		function updateFriendGroupCounters( $newFriend, $oldFriend = null )
+		{
+			include_once( PLOG_CLASS_PATH."class/dao/friendgroups.class.php" );
+			$groups = new FriendGroups();
+			$newGroup = $groups->getFriendGroup( $newFriend->getFriendGroupId() );
+
+			if( $newGroup ) {
+				$newGroup->setNumFriends($this->getNumItems( $this->getPrefix().'friends', 'group_id = '.$newGroup->getId()));
+				$newGroup->setNumAuthorizedFriends($this->getNumItems( $this->getPrefix().'friends', 'group_id = '.$newGroup->getId().' AND user_authorization_status = '.USER_AUTHORIZATION_APPROVED.' AND friend_authorization_status = '.FRIEND_AUTHORIZATION_APPROVED ));
+				$newGroup->setNumWaitingFriends($this->getNumItems( $this->getPrefix().'friends', 'group_id = '.$newGroup->getId().' AND user_authorization_status = '.USER_AUTHORIZATION_WAITING.' AND friend_authorization_status = '.FRIEND_AUTHORIZATION_APPROVED ));
+				$newGroup->setNumPendingFriends($this->getNumItems( $this->getPrefix().'friends', 'group_id = '.$newGroup->getId().' AND user_authorization_status = '.USER_AUTHORIZATION_APPROVED.' AND friend_authorization_status = '.FRIEND_AUTHORIZATION_WAITING ));
+				$friendGroups->updateFriendGroup( $newGroup );
+			}
+
+			if( $oldFriend ) {
+				$oldGroup = $friendGroups->getFriendGroup( $oldFriend->getGroupId() );
+				if ($oldGroup)
+				{
+					$oldGroup->setNumFriends($this->getNumItems( $this->getPrefix().'friends', 'group_id = '.$oldGroup->getId()));
+					$oldGroup->setNumAuthorizedFriends($this->getNumItems( $this->getPrefix().'friends', 'group_id = '.$oldGroup->getId().' AND user_authorization_status = '.USER_AUTHORIZATION_APPROVED.' AND friend_authorization_status = '.FRIEND_AUTHORIZATION_APPROVED ));
+					$oldGroup->setNumWaitingFriends($this->getNumItems( $this->getPrefix().'friends', 'group_id = '.$oldGroup->getId().' AND user_authorization_status = '.USER_AUTHORIZATION_WAITING.' AND friend_authorization_status = '.FRIEND_AUTHORIZATION_APPROVED ));
+					$oldGroup->setNumPendingFriends($this->getNumItems( $this->getPrefix().'friends', 'group_id = '.$oldGroup->getId().' AND user_authorization_status = '.USER_AUTHORIZATION_APPROVED.' AND friend_authorization_status = '.FRIEND_AUTHORIZATION_WAITING ));
+					$friendGroups->updateFriendGroup( $oldGroup );
+				}
+			}
+
+			return( true );
+		}
+
+        /**
+         * Adds a friend to the database
+         *
+         * @param friend a Friend object
+         * @return True if successful or false otherwise.
+         */
+        function addFriend( &$friend )
+        {
+        	if( $this->isFriend( $friend ) )
+        		return false;
+        	
+        	$result = $this->add( $friend );
+
+			if( $result ) {
+				// clean the cache
+                $this->_cache->removeData( $friend->getId(), CACHE_FRIENDS );
+				// update the friend group counters
+				$this->updateFriendGroupCounters( $friend );
+			}
+
+            return $result;
+        }
+
+        /**
+         * Removes a Friend object from the database.
+         *
+         * @param friendId The friend identifier.
+         * @param userId The user identifier.
+         * @return True if successful or false otherwise.
+         */
+        function deleteFriend( $id, $userId )
+        {
+        	$friend = $this->getFriend( $id, $userId );
+        	if( $friend ) {
+        		$this->delete( "id", $id );
+	            $this->_cache->removeData( $id, CACHE_FRIENDS );
+				// update friend realtionship counters
+				$this->updateFriendGroupCounters( $friend );
+
+				return( true );
+        	}
+        	else
+        		return false;
+        }
+        
+        /**
+         * Removes a Friend object from the database.
+         *
+         * @param friendId The friend identifier.
+         * @param userId The user identifier.
+         * @return True if successful or false otherwise.
+         */
+        function deleteFriendByUserIdAndFriendId( $userId, $friendId )
+        {
+        	$friends = $this->getFriends( $userId );
+        	foreach( $friends as $friend ) {
+	        	if( $friend->getFriendId() == $friendId ) {
+	        		$id = $friend->getId();
+	        		$this->delete( "id", $id );
+		            $this->_cache->removeData( $id, CACHE_FRIENDS );
+					// update friend realtionship counters
+					$this->updateFriendGroupCounters( $friend );
+	
+					return( true );
+	        	}
+        	}
+        	
+       		return false;
+        }
+
+        /**
+         * Removes all the friends from the given user identifier
+         *
+         * @param userId The blog identifier
+         */
+        function deleteUserFriends( $userId )
+        {
+        	$friends = $this->getFriends( $userId );
+
+        	if( $this->delete( "user_id", $userId )) {
+	        	foreach ( $friends as $friend ) {
+					$this->deleteFriend( $friend->getId(), $friend->getUserId() );
+	        	}
+	        	return true;
+        	}
+        	else
+        		return false;
+        }
+
+        /**
+         * Retrieves a single friend object from db.
+         *
+         * @param id The identifier
+         * @param userId The user identifier
+         * @return The Friend object containing information or false if it didn't exist
+         */
+        function getFriend( $id, $userId = -1 )
+        {
+        	$friend = $this->get( "id", $id, CACHE_FRIENDS );
+        	if( !$friend )
+        		return false;
+
+			if( $userId == -1 )
+				return $friend;
+
+			if( $friend->getUserId() == $userId )
+				return $friend;
+			else
+				return false;
+        }
+        
+        /**
+         * Removes a Friend object from the database.
+         *
+         * @param friendId The friend identifier.
+         * @param userId The user identifier.
+         * @return True if successful or false otherwise.
+         */
+        function getFriendByUserIdAndFriendId( $userId, $friendId )
+        {
+        	$friends = $this->getFriends( $userId );
+        	foreach( $friends as $friend ) {
+	        	if( $friend->getFriendId() == $friendId ) {
+					return $friend;
+	        	}
+        	}
+        	
+       		return false;
+        }
+
+        /**
+         * Updates a friend in the database.
+         *
+         * @param friend A Friend object with the information we'd like to update.
+         * @return True if successful or false otherwise.
+         */
+        function updateFriend( &$friend )
+        {
+			// get the previous version of this friend before saving it
+			$prevVersion = $this->getFriend( $friend->getId());
+
+        	$result = $this->update( $friend );
+
+            if( !$result ) {
+            	return false;
+            }
+
+			// clean the cache
+            $this->_cache->removeData( $friend->getId(), CACHE_FRIENDS );
+            
+			// update friend realtionship counters
+			$this->updateFriendGroupCounters( $friend, $prevVersion );
+
+          	return true;
+        }
+
+		/**
+		 * @see Model::getSearchConditions
+		 */
+		function getSearchConditions( $searchTerms )
+		{
+			return( "(description LIKE'%".Db::qstr( $searchTerms )."%')" );
+		}
+
+		/**
+		 * @private
+		 */
+        function mapRow( $row )
+        {
+            $friend = new Friend( $row["user_id"],
+                                  $row["friend_id"],
+			                      $row["group_id"],
+			                      $row["description"],
+			                      $row["user_authorization_status"],
+			                      $row["friend_authorization_status"],
+								  $row["id"] );
+
+			return $friend;
+
+        }
+    }
+?>
\ No newline at end of file

Added: plog/trunk/class/dao/userauthorizationstatus.class.php
===================================================================
--- plog/trunk/class/dao/userauthorizationstatus.class.php	                        (rev 0)
+++ plog/trunk/class/dao/userauthorizationstatus.class.php	2007-06-08 13:28:20 UTC (rev 5512)
@@ -0,0 +1,53 @@
+<?php
+
+    include_once( PLOG_CLASS_PATH."class/dao/status/genericstatuslist.class.php" );
+	
+	define( "USER_AUTHORIZATION_ALL", -1, true ); 
+    define( "USER_AUTHORIZATION_APPROVED", 1, true );
+    define( "USER_AUTHORIZATION_WAITING", 2, true );
+    define( "USER_AUTHORIZATION_REJECT", 3, true );
+
+    /**
+     * This class keeps track of all the possible status that a user can have. If plugins dynamically 
+	 * register new friend statuses, this class will still be able to handle them
+	 * 
+	 * \ingroup DAO
+     */    
+    class UserAuthorizationStatus extends GenericStatusList
+    {
+    
+        /**
+         * returns a list with all the user 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( "USER_AUTHORIZATION_", "USER_AUTHORIZATION_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 = UserAuthorizationStatus::getStatusList( true );
+	    	return( in_array( $status, $statusList ));
+        }
+        
+        /**
+         * returns the default status code for this class
+         *
+         * @return The default status
+         */
+        function getDefaultStatus()
+        {
+	     	return( USER_AUTHORIZATION_ALL );   
+        }        
+    }
+?>
\ No newline at end of file

Modified: plog/trunk/install/dbschemas.properties.php
===================================================================
--- plog/trunk/install/dbschemas.properties.php	2007-06-07 20:32:30 UTC (rev 5511)
+++ plog/trunk/install/dbschemas.properties.php	2007-06-08 13:28:20 UTC (rev 5512)
@@ -415,12 +415,12 @@
   friend_id I(10) UNSIGNED NOTNULL DEFAULT '0',
   group_id I(10) UNSIGNED NOTNULL DEFAULT '0',
   description C(255) NOTNULL DEFAULT '',
-  user_authorized I(10) NOTNULL DEFAULT '0',
-  friend_authorized I(10) NOTNULL DEFAULT '0',
+  user_authorization_status I(10) NOTNULL DEFAULT '0',
+  friend_authorization_status I(10) NOTNULL DEFAULT '0',
   INDEX user_id (user_id),
   INDEX friend_id (friend_id),
   INDEX user_id_friend_id (user_id,friend_id),
   INDEX user_id_group_id (user_id,group_id)
 ";
 $Tables["friends"]["options"]["mysql"] = "TYPE=MyISAM";
-?>
+?>
\ No newline at end of file



More information about the pLog-svn mailing list