<?php

/**
 *--------------------------------------
 * 链接
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-18
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class FlinkModl extends Modl
{
	public function get_flinkList($where = '', $order = '', $limit = 200)
	{
		$_FL = M('Flink')->join('__FLINK_CATEGORY__ AS fc ON fc.flink_category_id = __FLINK__.flink_category_id')
			->where($where)->order($order)->limit($limit)->select();
		return $_FL;
	}

	public function get_flinkInfo($flinkId)
	{
		$_FI = $this->join('__FLINK_CATEGORY__ AS fc ON fc.flink_category_id = __FLINK__.flink_category_id')
			->where('flink_id = \''.$flinkId.'\'')->find();
		return $_FI;
	}

	public function add_flink($data)
	{
		$result = array('data' => '', 'error' => '');

		$_t_id = $this->insert($data);
		if(false === $_t_id)
		{
			$result['error'] = L('ADD_FAILED');
			return $result;
		}
		$result['data'] = $_t_id;

		/* 更新附件 */
		M('Upload')->update_upload($_t_id, ASession::get('member_id'));

		return $result;
	}

	public function edit_flink($data)
	{
		$result = array('data' => '', 'error' => '');

		if(false === $this->update($data))
		{
			$result['error'] = L('EDIT_FAILED');
			return $result;
		}

		/* 更新附件 */
		M('Upload')->update_upload($data['flink_id'], ASession::get('member_id'));

		return $result;
	}

	public function delete_flink($flinkId)
	{
		$result = array('data' => '', 'error' => '');

		if(false === $this->delete($flinkId))
		{
			$result['error'] = L('DELETE_FAILED');
			return $result;
		}

		/* 删除上传文件 */
		$_AUL = M('Upload')->where("`u_item_type` = 'flink' and `u_item_id` = '".$flinkId."'")->select();
		if(!empty($_AUL))
		{
			foreach($_AUL as $au)
			{
				/* 删除文件 */
				@unlink(realpath($_SERVER['DOCUMENT_ROOT'].$a['u_src']));
			}
		}
		/* 删除上传表信息 */
		M('Upload')->where("`u_item_type` = 'flink' and `u_item_id` = '".$flinkId."'")->delete();

		return $result;
	}

}

?>