<?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, $onlyShownGroups = false )
		{
        	$group = $this->get( "id", $id, DaoCacheConstants::CACHE_FRIEND_GROUPS );
        	if( !$group )
        		return false;
        	if( $userId > -1 && $group->getUserId() != $userId )
        		return false;
        	if( $onlyShownGroups && !$album->getShowGroup())
        		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, $onlyShownGroups = false, $searchTerms = "", $page = -1, $itemsPerPage = 15 )
        {
			$groups = $this->getMany( "user_id",
			                          $userId,
			                          DaoCacheConstants::CACHE_FRIEND_GROUPS_BY_USER,
			                          Array( DaoCacheConstants::CACHE_FRIEND_GROUPS => "getId" ),
			                          Array( "name" => "ASC" ),
			                          $searchTerms,
			                          $page,
			                          $itemsPerPage );

			if( !$groups )
				return Array();

        	$result = Array();
			if( $onlyShownGroups ) {
				foreach( $groups as $group ) {
					if( $group->getShowGroup())
						$result[] = $group;
				}
			}
			else
				$result = $groups;

            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( DaoCacheConstants::CACHE_FRIEND_GROUPS => "getId" )))) {
				$this->_cache->removeData( $group->getUserId(), DaoCacheConstants::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, DaoCacheConstants::CACHE_FRIEND_GROUPS );
				$this->_cache->removeData( $group->getUserId(), DaoCacheConstants::CACHE_FRIEND_GROUPS_BY_USER );
			}

			return( $result );
		}

        /**
         * Removes all the friend groups from the given user identifier
         *
         * @param userId The blog identifier
         */
        function deleteUserFriendGroups( $userId )
        {
        	$friendGroups = $this->getUserFriendGroups( $userId );

        	if( $this->delete( "user_id", $userId )) {
	        	foreach ( $friendGroups as $friendGroup ) {
					$this->_cache->removeData( $friendGroup->getId(), DaoCacheConstants::CACHE_FRIEND_GROUPS );
	        	}
	        	$this->_cache->removeData( $userId, DaoCacheConstants::CACHE_FRIEND_GROUPS_BY_USER );
	        	return true;
        	}
        	else
        		return false;
        }

		/**
		 * 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(), DaoCacheConstants::CACHE_FRIEND_GROUPS );
				$this->_cache->removeData( $group->getUserId(), DaoCacheConstants::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 = '".LtDb::qstr( $groupId )."'";

			if( $userAuthorizationStatus != USER_AUTHORIZATION_ALL )
				$cond .= " AND user_authorization_status = '".LtDb::qstr( $userAuthorizationStatus )."'";

			if( $friendAuthorizationStatus != FRIEND_AUTHORIZATION_ALL )
				$cond .= " AND friend_authorization_status = '".LtDb::qstr( $friendAuthorizationStatus )."'";

			return( $this->getNumItems( $this->getPrefix()."friends", $cond ));

		}

		/**
		 * @see Model::getSearchConditions()
		 */
		function getSearchConditions( $searchTerms )
		{
			return( "name LIKE '%".LtDb::qstr( $searchTerms )."%' OR description LIKE '%".LtDb::qstr( $searchTerms )."%'" );
		}

		/**
		 * @private
		 */
		function mapRow( $row )
		{
			$group = new FriendGroup( $row["user_id"],
			                          $row["name"],
			                          $row["description"],
			                          unserialize($row["properties"]),
			                          $row["show_group"],
			                          $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 );
		}
	}
?>