<?php

	
    	
		
    
	/**
	 * \ingroup DAO
	 *
	 * Model class that manages the 'permissions' table
	 */ 
	class Permissions extends Model
	{
	
		function Permissions()
		{
			$this->Model();
			$this->table = $this->getPrefix()."permissions";
		}
	
		/**
		 * Add a new permission
		 *
		 * @param perm A Permission object
		 * @return True if successful or false otherwise
		 */	
		function addPermission( &$perm )
		{
			if( ($result = $this->add( $perm, Array( DaoCacheConstants::CACHE_PERMISSIONS => "getId" )))) {
				$this->_cache->removeData( "_all_", DaoCacheConstants::CACHE_PERMISSIONS_ALL );
			}
				
			return( $result );			
		}
		
		/**
		 * Update a permission
		 *
		 * @param perm
		 * @return True if successful or false otherwise
		 */
		function updatePermission( &$perm )
		{
			if( ($result = $this->update( $perm ))) {
				$this->_cache->removeData( $perm->getId(), DaoCacheConstants::CACHE_PERMISSIONS );
				$this->_cache->removeData( "_all_", DaoCacheConstants::CACHE_PERMISSIONS_ALL );				
			}
			
			return( $result );
		}
		
		/**
		 * Retrieve a permission given its id
		 *
		 * @param id The id of the permission
		 * @return A Permission object if successful or false otherwise
		 */
		function getPermission( $id )
		{
			return( $this->get( "id", $id, DaoCacheConstants::CACHE_PERMISSIONS ));			
		}
		
		/**
		 * Retrieve a permission given its name
		 *
		 * @param name The name of the permission
		 * @return A Permission object if successful or false otherwise
		 */		
		function getPermissionByName( $name )
		{
			$perms = $this->getAllPermissions();
			foreach( $perms as $perm ) {
				if( $perm->getName() == $name ) {
					return( $perm );	
				}	
			}
			
			return( false );
		}
		
		/**
		 * Deletes a permission given its id
		 *
		 * @param id The id of the permission
		 * @return True if successful or false otherwise
         * (note, only false on a SQL error, not if the value didn't exist)
		 */		 
		function deletePermission( $id )
		{
			if( ($result = $this->delete( "id", $id ))) {
				$this->_cache->removeData( $id, DaoCacheConstants::CACHE_PERMISSIONS );
				$this->_cache->removeData( "_all_", DaoCacheConstants::CACHE_PERMISSIONS_ALL );
			}
			
			return( $result );			
		}
		
		/**
		 * Loads all permissions from the database
		 *
		 * @return An array of Permission objects, or an empty array if no
		 * permissions were found
		 */
		function getAllPermissions()
		{
			$permissions = $this->getAll( "all", 
			                             DaoCacheConstants::CACHE_PERMISSIONS_ALL, 
			                             Array( DaoCacheConstants::CACHE_PERMISSIONS => "getId" ),
			                             Array( "permission" => "ASC" ));
			if( !$permissions )
				return( Array());				
			                             			                             
			return( $permissions );			
		}
		
		/**
		 * @private
		 */
		function mapRow( $row )
		{
			$perm = new Permission( $row["permission"], $row["description"], $row["id"] );
			$perm->setAdminOnlyPermission( $row["admin_only"] );
			$perm->setCorePermission( $row["core_perm"] );
			return( $perm  );	
		}
	}
?>