<?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 Users
 */

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

/**
 * User Class
 *
 * @author     cedseven <cedseven@users.sourceforge.net>
 * @package    Core
 * @subpackage Users
 */
class User extends CoreObject {
	/**#@+
	 * @access private
	 */	 
	/**
	 * @var integer
	 */
	var $id;
	
	/**
	 * @var string
	 */
	var $login;
	
	/**
	 * @var string md5 encrypted string
	 */
	var $password;
	

	/**
	 * @var string user's firstname
	 */
	var $firstname;

	/**
	 * @var string user's lastname
	 */
	var $lastname;

	/**
	 * @var string e-mail address
	 */
	var $email;

	/**
	 * @var integer user default language
	 */
	var $langId;

	/**
	 * @var integer user homepage
	 */
	var $homePageId;

	/**
	 * @var integer user status
	 */
	var $disabled;

	/**
	 * @var array indexed array of groups id
	 */
	var $groupsIdList;
	/**#@-*/
	
	/**#@+
	 * @access public
	 */
	/**
	 * Class Constructor
	 *
	 * @param integer $id 
	 * @param string  $login
	 * @param string  $password
	 * @param integer $langId
	 * @param array   $groupsIdList
	 */
  function User($id=0, $login='', $password='', $langId=0) {
		$this->setUserId($id);
		$this->setUserLogin($login);
		$this->setUserPassword($password);
		$this->setUserLangId($langId);
		$this->groupsIdList = array();
  } 
	
	/**
	 * Gets User Id
	 *
	 * @return integer
	 */
	function getUserId() {
		return $this->id;
	}
	
	/**
	 * Sets User Id
	 *
	 * @param integer $id User Id
	 */
	function setUserId($id) {
		$this->id = $id;
	}
	
	/**
	 * Gets User Login
	 *
	 * @return string
	 */
	function getUserLogin() {
		return $this->login;
	}
	
	/**
	 * Sets User Login
	 *
	 * @param string $login User Login
	 */
	function setUserLogin($login) {
		$this->login = $login;
	}
	
	/**
	 * Gets User Password
	 *
	 * @return string md5 string
	 */
	function getUserPassword() {
		return $this->password;
	}
	
	/**
	 * Sets User Password
	 *
	 * @param string $password
	 */
	function setUserPassword($password) {
    // these manipulations should not alter the password
    $password = str_replace(' ', '', substr($password,0,32));
		$this->password = $password;
	}
	
	/**
	 * Gets User Lang Id
	 *
	 * @return integer
	 */
	function getUserLangId() {
		return $this->langId;
	}
	
	/**
	 * Sets User Lang Id
	 *
	 * @param integer $langId
	 */
	function setUserLangId($langId) {
		$this->langId = $langId;
	}

  function getUserFirstname() {
    return $this->firstname;
  }

  function setUserFirstname($firstname) {
    $this->firstname = $firstname;
  }

  function getUserLastname() {
    return $this->lastname;
  }

  function setUserLastname($lastname) {
    $this->lastname = $lastname;
  }

  function getUserEmail() {
    return $this->email;
  }

  function setUserEmail($email) {
    $this->email = $email;
  }

  function getUserHomePageId() {
    return $this->homePageId;
  }

  function setUserHomePageId($pageId) {
    $this->homePageId = $pageId;
  }

	/**
	 * Gets User status
	 *
	 * @return integer
	 */
	function getUserDisabled() {
		return $this->disabled;
	}

	/**
	 * Sets User status
	 *
	 * @param integer $disabled User Disabled
	 */
	function setUserDisabled($disabled) {
		$this->disabled = $disabled;
	}
	
	/**
	 * Adds Group Membership
	 *
	 * @param integer $groupId Group Id
	 */
	function addGroupMembership($groupId) {
		$this->groupsIdList[] = $groupId;
	}
	
	/**
	 * Deletes Group Membership
	 *
	 * @param integer $groupId
	 */
	function delGroupMembership($groupId) {
		$pos = array_search($groupId, $this->groupsIdList);
		if ($pos !== FALSE && $pos !== NULL) {
			unset($this->groupsIdList[$pos]);
		}
		return true;
	}
	
	/**
	 * Checks Group Membership
	 *
	 * @param  integer $groupId Group Id
	 * @return boolean
	 */
	function checkGroupMembership($groupId) {
		if($groupId == GROUP_ID_EVERYONE) {
			return true;
		}
		foreach($this->groupsIdList as $key => $gid) {
			if($groupId == $gid) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Gets Groups Membership List
	 *
	 * @return array
	 */
	function &getGroupsMembershipList() {
		return $this->groupsIdList;
	}
	
	/**
	 * Gets Groups Membership Count
	 *
	 * @return integer
	 */
	function getGroupsMembershipCount() {
		return count($this->groupsIdList);
	}
	
	/**
	 * Sets User Values
	 *
	 * @param array $array
	 */
	function setUserValues($array) {
		if(!is_array($array)) {
			return;
		}

		if(isset($array['id'])) {
			$this->setUserId($array['id']);
		}
		if(isset($array['login'])) {
			$this->setUserLogin($array['login']);
		}
		if(isset($array['password']) && strlen($array['password']) > 0) {
			$this->setUserPassword($array['password']);
		}
		if(isset($array['firstname'])) {
			$this->setUserFirstname($array['firstname']);
		}
		if(isset($array['lastname'])) {
			$this->setUserLastname($array['lastname']);
		}
		if(isset($array['email'])) {
			$this->setUserEmail($array['email']);
		}
		if(isset($array['lang_id'])) {
			$this->setUserLangId($array['lang_id']);
		}
		if(isset($array['home_page_id'])) {
			$this->setUserHomePageId($array['home_page_id']);
		}
		if(isset($array['disabled'])) {
			$this->setUserDisabled($array['disabled']);
		}
	}

	/**
	 * Gets User Controls
	 *
	 * User controls pop-up menu
	 *
	 * @param  object $user User Object
	 * @param  integer $menuId Menu Id
	 * @param  string $mode
	 * @return string html
	 */
	function getControls($menuId=0, $mode=VIEW_MODE) {
		$userId  = $this->getUserId();

		$html = '';

    if ($mode == EDIT_MODE) {
      $menu = &new egContextMenu("menu_user_".$menuId."_".$userId, 0, 0, 150, 350);
			// user properties
	    $menu->addListItem(new egMenuItem("mnu_usr_".$menuId."_".$userId."_5", LANG_USER_MENU_EDIT, "/interface/icons/edit.png", "launchUserEditionWindow(\'user_id=".$userId."\');"));

      $main = &new egContainer("eg_mnu_usr_".$menuId."_".$userId, 0, 0, 0, 0);

      $main->addObject($menu);

      $manager = &egManager::getInstance();
    	$manager->addContainer($main);

 			$html .= "<a class=\"easy-CMS_UI_propertiesIcon\" href=\"javascript:egShowContextMenu(contextMenu_"."menu_user_".$menuId."_".$userId.");\"><img src=\"/interface/icons/properties.png\" /></a>";
    }
		return $html;
	}

	/**
	 * Exports User Properties Into CSV String
	 *
	 * @return string
	 */
	function toString() {
		$csv  = 'id : '.$this->getUserId();
		$csv .= ', login : '.$this->getUserLogin();
		$csv .= ', lang : '.$this->getUserLangId();
		return $csv;
	}
	/**#@-*/
	
}

?>