<?php
/**
 * 管理员逻辑层
 *
 * @author Wzb
 *        
 */
class BLL_Admin {
	
	/**
	 * 添加管理员
	 *
	 * @param array $theArray
	 * @return int
	 */
	public static function add($theArray) {
		$newArray = COM_OftenFunction::makeArray ( $theArray );
		$newArray ['username'] = COM_OftenFunction::delSpecialChar ( $theArray ['user'] );
		$newArray ['password'] = COM_OftenFunction::delSpecialChar ( $theArray ['pass1'] );
		$newArray ['nickname'] = COM_OftenFunction::delSpecialChar ( $theArray ['nickname'] );
		
		if (empty ( $newArray ['username'] ) || empty ( $newArray ['password'] )) {
			die ( "数据篡改" );
		}
		$temp = self::getOne ( array ("username" => $newArray ['username'] ) );
		if (count ( $temp ) > 0) {
			die ( "该账号已经存在" );
		}
		$newArray ['password'] = md5 ( $newArray ['password'] );
		$newArray ['loginNum'] = 0;
		$newArray ['lastLoginTime'] = date ( "Y-m-d H:i:s" );
		return DAL_Admin::add ( $newArray );
	}
	
	/**
	 * 修改管理员
	 *
	 * @param array $theArray
	 * @return int
	 */
	public static function edit($theArray) {
		$newArray = COM_OftenFunction::makeArray ( $theArray );
		$newArray ['id'] = intval ( $theArray ['id'] );
		if (array_key_exists ( 'password', $newArray )) {
			if (empty ( $newArray ['password'] )) {
				die ( "数据篡改" );
			}
			$newArray ['password'] = md5 ( $newArray ['password'] );
		}
		return DAL_Admin::edit ( $newArray );
	}
	
	/**
	 * 删除指定个管理员
	 *
	 * @param string $idList 不能为空 多个必须用-分割开
	 * @return string error1 id字符串为空|error2 删除失败|ok 删除成功
	 */
	public static function delete($idList) {
		if (empty ( $idList )) {
			return 'error1';
		}
		if (DAL_Admin::delete ( $idList )) {
			return 'ok';
		} else {
			return 'error2';
		}
	}
	
	/**
	 * 获取一个管理员
	 *
	 * @param array $theArray id|username|username、password
	 * @param string $code 需要的字段 默认值：*
	 * @return array
	 */
	public static function getOne($theArray, $code = '*') {
		if (array_key_exists ( 'id', $theArray )) {
			$newArray ['id'] = intval ( $theArray ['id'] );
		} elseif (array_key_exists ( 'password', $theArray )) {
			$newArray ['username'] = COM_OftenFunction::delSpecialChar ( $theArray ['username'] );
			$newArray ['password'] = COM_OftenFunction::delSpecialChar ( $theArray ['password'] );
			if (empty ( $newArray ['username'] ) || empty ( $newArray ['password'] )) {
				die ( "数据篡改" );
			}
			$newArray ['password'] = md5 ( $theArray ['password'] );
		} else {
			$newArray ['username'] = COM_OftenFunction::delSpecialChar ( $theArray ['username'] );
			if (empty ( $newArray ['username'] )) {
				die ( "数据篡改" );
			}
		}
		return DAL_Admin::getOne ( $newArray, $code );
	}
	
	/**
	 * 获取所有管理员列表
	 *
	 * @param array $limitArr 默认值：array('begin'=>0,'num'=>0)
	 * @return array()
	 */
	public static function getAllList($limitArr = array('begin'=>0,'num'=>0)) {
		return DAL_Admin::getAllList ( $limitArr );
	}
	
	/**
	 * 获取当前登录的管理员
	 *
	 * @param string $code 需要的字段 默认值：id
	 * @return array
	 */
	public static function getAdminNow($code = 'id') {
		$theId = self::isLogin ();
		return self::getOne ( array ("id" => $theId ), $code );
	}
	
	/**
	 * 检测当前是否有管理员登录
	 *
	 * @return int 管理员ID
	 */
	public static function isLogin() {
		if (! array_key_exists ( 'adminLoginInfo', $_SESSION )) {
			return 0;
		}
		$newArray = explode ( "|", $_SESSION ['adminLoginInfo'] );
		if (count ( $newArray ) != 2 || intval ( $newArray [0] ) === 0 || strlen ( $newArray [1] ) != 27) {
			return 0;
		} else {
			return intval ( $newArray [0] );
		}
	}
	
	/**
	 * 管理员登录
	 *
	 * @param array $theArray
	 * @return bool
	 */
	public static function login($theArray) {
		$theObj = self::getOne ( $theArray, 'id,password,loginNum' );
		if (count ( $theObj ) > 0) {
			$_SESSION ['adminLoginInfo'] = $theObj ['id'] . '|' . substr ( md5 ( $theObj ['password'] ), 5 );
			$newArray = array ();
			$newArray ['id'] = $theObj ['id'];
			$newArray ['loginNum'] = $theObj ['loginNum'] + 1;
			$newArray ['lastLoginTime'] = date ( "Y-m-d H:i:s" );
			if (intval ( DAL_Admin::edit ( $newArray ) ) > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	/**
	 * 管理员退出登录
	 */
	public static function outLogin() {
		if (array_key_exists ( 'adminLoginInfo', $_SESSION )) {
			unset ( $_SESSION ['adminLoginInfo'] );
		}
	}
}
?>