<?php

	
	
	

    /**
	 * \ingroup DAO
	 *
     * Represents a friend group
     */
	class FriendGroup extends DbObject
	{

		var $_id;
		var $_userId;
		var $_name;
		var $_description;
		var $_properties;
		var $_showGroup;
		var $_mangledName;
		var $_numFriends;
		var $_numAuthorizedFriends;
		var $_numWaitingFriends;
		var $_numPendingFriends;

		function FriendGroup( $userId, $name, $description = "", $properties = Array(), $showGroup = 1, $id = -1 )
		{
			$this->DbObject();

			$this->_id = $id;
			$this->_userId = $userId;
			$this->_name = $name;
			$this->_description = $description;
			$this->setProperties( $properties );
			$this->_showGroup = $showGroup;
			$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",
			                        "show_group" => "getShowGroup",
			                        "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;
		}

		/**
		 * Sets whether this group has to be shown in the main page or not.
		 *
		 * @param showGroup A boolean value
		 * @return Always true.
		 */
        function setShowGroup( $showGroup )
        {
        	$this->_showGroup = $showGroup;

			return true;
        }

        /**
         * 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_CONFIRMED, $friendAuthorizationStatus = FRIEND_AUTHORIZATION_CONFIRMED )
        {
        	if( $userAuthorizationStatus == USER_AUTHORIZATION_ALL && $friendAuthorizationStatus == FRIEND_AUTHORIZATION_ALL )
        		return( $this->_numFriends );
        	elseif( $userAuthorizationStatus == USER_AUTHORIZATION_WAITING && $friendAuthorizationStatus == FRIEND_AUTHORIZATION_CONFIRMED )
        		return( $this->_numWaitingFriends );
        	elseif( $userAuthorizationStatus == USER_AUTHORIZATION_CONFIRMED && $friendAuthorizationStatus == FRIEND_AUTHORIZATION_WAITING )
        		return( $this->_numPendingFriends );
        	elseif( $userAuthorizationStatus == USER_AUTHORIZATION_CONFIRMED && $friendAuthorizationStatus == FRIEND_AUTHORIZATION_CONFIRMED )
        		return( $this->_numAuthorizedFriends );
        	else {
	        	
    	    	$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_CONFIRMED, FRIEND_AUTHORIZATION_CONFIRMED ));
        }

        function setNumAuthorizedFriends( $numAuthorizedFriends )
        {
        	$this->_numAuthorizedFriends = $numAuthorizedFriends;
			if( $this->_numAuthorizedFriends < 0 )
				$this->_numAuthorizedFriends = 0;
        }

        function getNumWaitingFriends()
        {
        	return( $this->getNumFriends( USER_AUTHORIZATION_WAITING, FRIEND_AUTHORIZATION_CONFIRMED ));
        }

        function setNumWaitingFriends( $numWaitingFriends )
        {
        	$this->_numWaitingFriends = $numWaitingFriends;
			if( $this->_numWaitingFriends < 0 )
				$this->_numAWaitingFriends = 0;
        }

        function getNumPendingFriends()
        {
        	return( $this->getNumFriends( USER_AUTHORIZATION_CONFIRMED, 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 )
		{
			$friends = new Friends();
			$myFriends = $friends->getFriends( $this->_userId, $this->_id, $userAuthorizationStatus, $friendAuthorizationStatus, $searchTerms, $page, $itemsPerPage );
			
			return $myFriends;
		}

		/**
		 * returns the description
		 *
		 * @return The description
		 */
		function getDescription()
		{
			return $this->_description;
		}

		/**
		 * @private
		 * For future use
		 */
		function getProperties()
		{
			return( $this->_properties );
		}

		/**
		 * Returns whether this group has to be shown in the main page or not.
		 *
		 * @return A boolean value.
		 */
        function getShowGroup()
        {
        	return $this->_showGroup;
        }

		function setMangledName( $mangledName )
		{
			$this->_mangledName = $mangledName;
		}

		function getMangledName()
		{
			if( $this->_mangledName == "" ) {
				
				$this->_mangledName = Textfilter::urlize( $this->getName() );
			}

			return( $this->_mangledName );
		}
	}
?>
