<?php


class UserStorageBase {

	private $_errors = array();

	/**
	 * @returns object $usr
	 *   $usr->primaryKey
	 *   $usr->login
	 *   $usr->password					optional (required in createUser)
	 *   $usr->name
	 *   $usr->email
	 *   $usr->phone
	 *   $usr->homeEmail
	 *   $usr->homePhone
	 *   $usr->employeeType			'Pracownik','Kandydat','Partner','Anonymous'
	 *   $usr->isDisabled				1, 0 or null if not set
	 */
	public function getUser($usrLogin) {}

	/**
	 * @returns bool
	 */
	public function isDisabled($usr) {}
	/**
	 * @returns bool
	 */
	public function setDisabled($usrLogin, $isDisabled) {}

	/**
	 * Create user by data.
	 * @see getUser for data fields
	 * 
	 * @returns bool
	 */
	public function createUser($userData) {}

	/**
	 * Update user by data.
	 * @see getUser for data fields
	 * 
	 * @returns bool
	 */
	public function updateUser($usrLogin, $updateData) {}

	public function setSyncUserDate($usrLogin) {}
	public function setSyncGroupDate($groupID) {}

	/**
	 * User group list by id.
	 * 
	 * @returns assoc array with group names
	 *    ['ID_ZASOB'] = 'GROUP_NAME';
	 *    ['workgroup'] = 'workgroup';
	 *    ['com.apple.access_mail'] = 'com.apple.access_mail';
	 *    ['com.apple.access_addressbook'] = 'com.apple.access_addressbook';
	 *    ['com.apple.access_calendar'] = 'com.apple.access_calendar';
	 *    ['com.apple.access_smb'] = 'com.apple.access_smb';
	 *    ['com.apple.access_afp'] = 'com.apple.access_afp';
	 *    ['com.apple.access_vpn'] = 'com.apple.access_vpn';
	 *    ['com.apple.access_chat'] = 'com.apple.access_chat';
	 */
	public function getUserGroups($usrLogin) {}
	public function createGroup($group) {}
	public function addUserGroup($usrLogin, $group) {}
	public function removeUserGroup($usrLogin, $group) {}
	public function updateGroup($group, $updateData) {}
	public function addNestedGroup($groupID, $nestedGroupID) {}
	public function removeNestedGroup($groupID, $nestedGroupID) {}

	public function addParentGroup($groupID, $parentGroupID) {}
	public function removeParentGroup($groupID, $parentGroupID) {}

	/**
	 * Group.
	 * 
	 * @returns object $group
	 *   $group->primaryKey
	 *   $group->realName
	 *   $group->nestedGroups
	 *   $group->type			'STANOWISKO','PODMIOT','DZIAL','local','network'
	 *     'local' - access groups like com.apple.access_mail
	 *     'network' - network group: workgroup
	 *   (optional) $group->zasobID
	 *   (optional) $group->zasobDESC
	 */
	public function getGroup($groupID) {}
	public function getParentGroups(ObjectGroup $group) {}

	/**
	 * Build local group object.
	 * 
	 * @returns object $group @see getGroup
	 * 
	 * Example: _buildGroupLocal('com.apple.access_mail')
	 */
	protected function _buildGroupLocal($groupName) {
		$group = new stdClass();
		$group->primaryKey = $groupName;
		$group->realName = $groupName;
		$group->groupID = null;
		$group->type = 'local';
		$group->nestedGroups = null;
		return $group;
	}

	/**
	 * Build network group object.
	 * 
	 * @returns object $group @see getGroup
	 * 
	 * Example: _buildGroupNetwork('workgroup')
	 */
	protected function _buildGroupNetwork($groupName) {
		$group = new stdClass();
		$group->primaryKey = $groupName;
		$group->realName = $groupName;
		$group->groupID = null;
		$group->type = 'network';
		$group->nestedGroups = null;
		return $group;
	}

	/**
	 * Clean string to use as uid, ldap cn, etc.
	 * @returns string
	 */
	protected function _cleanUid($str) {
		// TODO: clean 
		$str = preg_replace('/[^a-zA-Z0-9._-]+/', '_', $str);
		return $str;
	}

	protected function _cleanText($str) {
		// TODO: clean text from bad signs
		$str = preg_replace('/[^a-zA-Z0-9._-]+/', '_', $str);
		return $str;
	}

	protected function setError($code, $msg, $dbgMsg) {
		$this->_errors[] = (object)array('code'=>$code, 'msg'=>$msg, 'dbgMsg'=>$dbgMsg);
	}

	public function hasErrors() {
		return !empty($this->_errors);
	}

	public function getErrorsMsgList() {
		$msgList = array();
		foreach ($this->_errors as $vErr) {
			$msgList[] = "Error {$vErr->code}: {$vErr->msg}";
		}
		return $msgList;
	}

	public function getRawErrorsList() {
		return $this->_errors;
	}

	public function getErrorsMsgListWithDbg() {
		$msgList = array();
		foreach ($this->_errors as $vErr) {
			$msgList[] = "Error {$vErr->code}: {$vErr->msg} (DBG:{$vErr->dbgMsg})";
		}
		return $msgList;
	}

	public function isPasswordChanged($usrLogin) {
		return 0;
	}

}
