<?php
/**
 * 链接列表数据层
 *
 * @author Wzb
 *        
 */
class DAL_Link extends DAL_Base {
	/**
	 * 添加一个链接
	 *
	 * @param array $theArray
	 * @return int
	 */
	public static function add($theArray) {
		return parent::_add ( $theArray, 8 );
	}
	
	/**
	 * 修改一个链接
	 *
	 * @param array $theArray
	 * @return int
	 */
	public static function edit($theArray) {
		return parent::_edit ( $theArray, 8 );
	}
	
	/**
	 * 删除链接
	 *
	 * @param string $idList 多个必须用-分割开
	 * @param int $type 0表示$idList为链接ID 1表示$idList为链接类别 默认值：0
	 * @return bool
	 */
	public static function delete($idList, $type = 0) {
		if (intval ( $type ) === 0) {
			return parent::_delete ( $idList, 8 );
		} else {
			global $conn;
			$idList = preg_replace ( '/[^\d^-]/', '', $idList );
			$sql = "delete from " . self::_TableName ( 8 ) . " where ";
			if (count ( explode ( '-', $idList ) ) > 0) {
				$sql .= 'sort in (' . str_replace ( '-', ',', $idList ) . ')';
			} else {
				$sql .= "sort = $idList";
			}
			if ($conn->Execute ( $sql )) {
				return true;
			} else {
				return false;
			}
		}
	}
	
	/**
	 * 获取一个链接
	 *
	 * @param int $id
	 * @param string $code 需要的字段 默认值：*
	 * @return array
	 */
	public static function getOne($id, $code = '*') {
		return parent::_getOne ( $id, 8, $code );
	}
	
	/**
	 * 获取链接列表
	 *
	 * @param string $sortIdList 类别ID，多个用逗号分割
	 * @param string $code 需要的字段 默认值：*
	 * @param array $limitArr 包含两项值limit数组 默认值：array('begin'=>0,'num'=>0)
	 * @return array
	 */
	public static function getList($sortIdList, $code = '*', $limitArr = array('begin'=>0,'num'=>0)) {
		$sql = "select $code from " . parent::_tableName ( 8 ) . " where sort";
		if (strpos ( $sortIdList, ',' ) !== false) {
			$sql .= " in ($sortIdList)";
		} else {
			$sql .= "=$sortIdList";
		}
		$sql .= " order by serialNum desc";
		return parent::_getDataList ( $sql, $limitArr );
	}
	
	/**
	 * 搜索列表
	 *
	 * @param string $sortIdList 类别ID，多个用逗号分割
	 * @param array $whereArray 搜索条件(type,name,value) type:likeString|=String|=Int|>Int|<Int
	 * @param string $code 需要的字段 默认值：*
	 * @param array $limitArr 包含两项值limit数组 默认值array('begin'=>0,'num'=>0)
	 * @return array
	 */
	public static function getSerachList($sortIdList, $whereArray, $code = '*', $limitArr = array('begin'=>0,'num'=>0)) {
		$sql = "select $code from " . parent::_tableName ( 8 ) . " where sort";
		if (strpos ( $sortIdList, ',' ) !== false) {
			$sql .= " in ($sortIdList)";
		} else {
			$sql .= "=$sortIdList";
		}
		if (count ( $whereArray ) > 0) {
			foreach ( $whereArray as $row ) {
				if ($row ['type'] == "likeString") {
					$sql .= " and $row[name] like '%$row[value]%' ";
				} elseif ($row ['type'] == "=String") {
					$sql .= " and $row[name] = '$row[value]' ";
				} elseif ($row ['type'] == "=Int") {
					$sql .= " and $row[name] = $row[value] ";
				} elseif ($row ['type'] == ">Int") {
					$sql .= " and $row[name] > $row[value] ";
				} elseif ($row ['type'] == "<Int") {
					$sql .= " and $row[name] < $row[value] ";
				} else {
				}
			}
		}
		$sql .= " order by serialNum desc";
		return parent::_getDataList ( $sql, $limitArr );
	}
	
	/**
	 * 获取某类别下的链接数量
	 *
	 * @param int $id 类别ID
	 * @return int
	 */
	public static function getCount($id) {
		global $conn;
		$sql = "select count(*) from " . parent::_tableName ( 8 ) . " where sort=$id";
		return $conn->GetOne ( $sql );
	}
	
	/**
	 * 获取某个链接的上一个或者下一个
	 *
	 * @param int $sort
	 * @param int $serialNum
	 * @param string $type
	 * @param string $code 需要的字段 默认值：*
	 */
	public static function getOnePreNext($sort, $serialNum, $type, $code = '*') {
		global $conn;
		if ($type == "pre") {
			$sql = "select $code from " . parent::_tableName ( 8 ) . " where sort=$sort and serialNum>$serialNum order by serialNum asc";
		} else {
			$sql = "select $code from " . parent::_tableName ( 8 ) . " where sort=$sort and serialNum<$serialNum order by serialNum desc";
		}
		$datalist = parent::_getDataList ( $sql, array ('begin' => 0,'num' => 1 ) );
		if (count ( $datalist ) > 0) {
			return $datalist [0];
		} else {
			return array ();
		}
	}
}
?>