<?php
/*
 * This file is part of the Easy-CMS project.
 * Easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * @package    Core
 * @subpackage ACL
 */
 

/**
 */
include_once('config.inc.php');
include_once('classes/core/CoreObject.class.php');

/**
 * An ACLEntry holds infos about permissions of a group/user.
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	   ACLEntry.class, Sun Feb 13 12:32:49 CET 2005
 * @package	   Core
 * @subpackage ACL
 */
class ACLEntry extends CoreObject {
	/**#@+
	 * @access private
	 */
  /**
   * @var integer Group/User Id
   */
	var $id;
	
  /**
   * @var integer
   */
	var $viewRight;
  
  /**
   * @var integer
   */
	var $editRight;
	
  /**
   * @var integer
   */
	var $publishRight;
	
  /**
   * @var integer
   */
	var $removeRight;
  
  /**
   * @var integer
   */  
	var $adminRight;
  /**#@-*/
    
  /**#@+
   * @access public
   */
  /**
   * Class Constructor
   *
   * @param integer $id
   * @param integer $viewRight
   * @param integer $editRight
   * @param integer $publishRight
   * @param integer $removeRight
   * @param integer $adminRight
   */
	function ACLEntry($id=0, $viewRight=0, $editRight=0, $publishRight=0, $removeRight=0, $adminRight=0) {
    $this->setId($id);
    $this->setViewRight($viewRight);
    $this->setEditRight($editRight);
    $this->setPublishRight($publishRight);
    $this->setRemoveRight($removeRight);
    $this->setAdminRight($adminRight);
	}
	
	/**
	 * Gets Group or User ID
	 *
	 * @return integer
	 */
	function getId() {
		return $this->id;
	}
  
  /**
	 * Set ACL Id
	 *
	 * @param integer $id
	 */
	function setId($id) {
		$this->id = $id;
	}

	/**
	 * Gets View Right
	 *
	 * @return integer
	 */
	function getViewRight() {
		return $this->viewRight;
	}
  
  /**
	 * Set View Right
	 *
	 * @param integer $viewRight
	 */
	function setViewRight($viewRight) {
		$this->viewRight = $viewRight;
	}

	/**
	 * Gets Edit Right
	 *
	 * @return integer
	 */
	function getEditRight() {
		return $this->editRight;
	}
  
  /**
	 * Set Edit Right
	 *
	 * @param integer $editRight
	 */
	function setEditRight($editRight) {
		$this->editRight = $editRight;
	}
	
	/**
	 * Gets Publish Right
	 *
	 * @return integer
	 */
	function getPublishRight() {
		return $this->publishRight;
	}
  
  /**
	 * Set Publish Right
	 *
	 * @param integer $publishRight
	 */
	function setPublishRight($publishRight) {
		$this->publishRight = $publishRight;
	}

	/**
	 * Gets Remove Right
	 *
	 * @return integer
	 */
	function getRemoveRight() {
		return $this->removeRight;
	}
  
  /**
	 * Set Remove Right
	 *
	 * @param integer $removeRight
	 */
	function setRemoveRight($removeRight) {
		$this->removeRight = $removeRight;
	}

	/**
	 * Gets Admin Right
	 *
	 * @return integer
	 */
	function getAdminRight() {
		return $this->adminRight;
	}
  
  /**
	 * Set Admin Right
	 *
	 * @param integer $adminRight
	 */
	function setAdminRight($adminRight) {
		$this->adminRight = $adminRight;
	}

	/**
	 * Get ACL Property Value
	 *
	 * @param  string $fieldName property name
	 * @return integer
	 */
	function getValue($fieldName) {
		if($fieldName == 'id')
			return $this->getId();
		if($fieldName == ACLENTRY_VIEW)
			return $this->getViewRight();		
		if($fieldName == ACLENTRY_EDIT)
			return $this->getEditRight();
		if($fieldName == ACLENTRY_PUBLISH)
			return $this->getPublishRight();
		if($fieldName == ACLENTRY_REMOVE)
			return $this->getRemoveRight();
		if($fieldName == ACLENTRY_ADMIN)
			return $this->getAdminRight();
		return -1;
	}

	
	/*
	function setACLEntryId($id) {
		$this->id = $id;
	}
  */

	
	/**
	 * Set Rights From Array
	 *
	 * @param array $array associative array right=>value
	 */
	function setValues($array) {
		if (isset($array['group_id'])) {
			$this->setId($array['group_id']);
		}
		elseif (isset($array['user_id'])) {
			$this->setId($array['user_id']);
		}
    
		if(isset($array[ACLENTRY_VIEW])) {
			$this->setViewRight($array[ACLENTRY_VIEW]);
		}
    
		if(isset($array[ACLENTRY_EDIT])) {
			$this->setEditRight($array[ACLENTRY_EDIT]);
		}
    
		if(isset($array[ACLENTRY_PUBLISH])) {
			$this->setPublishRight($array[ACLENTRY_PUBLISH]);
		}
    
		if(isset($array[ACLENTRY_REMOVE])) {
			$this->setRemoveRight($array[ACLENTRY_REMOVE]);
		}
    
		if(isset($array[ACLENTRY_ADMIN])) {
			$this->setAdminRight($array[ACLENTRY_ADMIN]);
		}
    
		return 1;
	}
	
	/**
	 * @return array
	 */
	function getValues() {
		$rights = array(
			 ACLENTRY_VIEW    => $this->getViewRight()
			,ACLENTRY_EDIT    => $this->getEditRight()
			,ACLENTRY_PUBLISH => $this->getPublishRight()
			,ACLENTRY_REMOVE  => $this->getRemoveRight()
			,ACLENTRY_ADMIN   => $this->getAdminRight()
		);
		return $rights;
	}
    
    /**
     * @author  vinadelmar <vinadelmar@users.sourceforge.net>
     * @since   2006-03-07
     * @version 0.1.0d
     * @param   array $rights
	 * @deprecated
     */
	function setRights($rights) {
    $this->viewRight    = $rights[ACLENTRY_VIEW];
    $this->editRight    = $rights[ACLENTRY_EDIT];
    $this->publishRight = $rights[ACLENTRY_PUBLISH];
    $this->removeRight  = $rights[ACLENTRY_REMOVE];
    $this->adminRight   = $rights[ACLENTRY_ADMIN];
	}

	/**
	 * Export ACL To XML String
	 *
	 * @return string XML
	 * @todo   finish method
	 */
	function toXML() {
		$xml = "<aclEntry>\n";
		/* TODO */
		$xml .= "</aclEntry>\n";		
		return $xml;
	}
	
	/**
	 * Compare To Another ACLEntry Object
	 *
	 * @author  vinadelmar <vinadelmar@users.sourceforge.net>
	 * @since   2006-02-22
	 * @version 0.1.0
	 * @param   object $aclEntry instance of ACLEntry
	 * @return  boolean true if both objects' properties are equal
	 */
  //returns < 0 if the current entry has less rights than acl;
  //				> 0 if current entry has more rights than acl;
  //					0 if entries have equals rights
	function compareTo(&$aclEntry) {
		if ($this->getViewRight() != $aclEntry->getViewRight()) {
			return $this->getViewRight()-$aclEntry->getViewRight();
		}
		if ($this->getEditRight() != $aclEntry->getEditRight()) {
			return $this->getEditRight()-$aclEntry->getEditRight();
		}
		if ($this->getPublishRight() != $aclEntry->getPublishRight()) {
			return $this->getPublishRight()-$aclEntry->getPublishRight();
		}
		if ($this->getRemoveRight() != $aclEntry->getRemoveRight()) {
			return $this->getRemoveRight()-$aclEntry->getRemoveRight();
		}
		if ($this->getAdminRight() != $aclEntry->getAdminRight()) {
			return $this->getAdminRight()-$aclEntry->getAdminRight();
		}
		return 0;
	}
	
	/**
	 * Compare Rights With Another ACLEntry
	 *
	 * @author  vinadelmar <vinadelmar@users.sourceforge.net>
	 * @since   2006-03-07
	 * @version 0.1.0
	 * @param   object $aclEntry instanceof `ACLEntry`
	 * @return  boolean true if both ACLEntry are equal in terms of rights
	 */
	function compareRights($aclEntry) {
		if ($this->getViewRight() != $aclEntry->getViewRight())
			return false;
		if ($this->getEditRight() != $aclEntry->getEditRight())
			return false;
		if ($this->getPublishRight() != $aclEntry->getPublishRight())
			return false;
		if ($this->getRemoveRight() != $aclEntry->getRemoveRight())
			return false;
		if ($this->getAdminRight() != $aclEntry->getAdminRight()) 
			return false;
		return true;
	}

	/**
	 * @return boolean true if all rights are denied
	 */
	function isEmpty() {
		if (!($this->viewRight || $this->editRight || $this->publishRight || $this->removeRight || $this->adminRight))
			return true;
			
		return false;
	}
	/**#@-*/
} 

?>
