<?php

/**
 *--------------------------------------
 * 上传文件
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-11-2
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class UploadCtrlr extends ManageCtrlr
{
	public function list_upload()
	{
		$where = array();
		/* 筛选文件名 */
		$uFilename = ARequest::get('u_filename');
		if(!empty($uFilename))
		{
			$where['u_filename'] = array('LIKE', '%'.$uFilename.'%');
		}

		/* 筛选文件类型 */
		$uType = ARequest::get('u_type');
		if(!empty($auType))
		{
			$where['u_type'] = array('EQ', $uType);
		}

		/* 筛选项目类型 */
		$uItemType = ARequest::get('u_item_type');
		if(!empty($uItemType))
		{
			$where['u_item_type'] = array('EQ', $uItemType);
		}
		/* 筛选项目编号 */
		$uItemId = ARequest::get('u_item_id') ? ARequest::get('u_item_id') : 0;
		if($uItemId > 0)
		{
			$where['u_item_id'] = array('EQ', $uItemId);
		}

		/* 筛选会员 */
		$memberId = ARequest::get('member_id') ? ARequest::get('member_id') : 0;
		if($memberId > 0)
		{
			$where['member_id'] = array('EQ', $memberId);
		}

		/* 列表排序 */
		$orderBy = ARequest::get('order_by') ? ARequest::get('order_by') : 'upload_id';
		$orderTurn = ARequest::get('order_turn') ? ARequest::get('order_turn') : 'desc';
		$order = "`{$orderBy}` {$orderTurn}";

		/* 获取分页信息 */
		$pageSize = (ARequest::get('page_size') > 0 ? ARequest::get('page_size') : 20);
		$_GET[C('VAR.PAGE')] = ARequest::get(C('VAR.PAGE')) ? ARequest::get(C('VAR.PAGE')) : 1;
		$rowsNum = M('Upload')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('upload/list_upload?member_id='.$memberId
			.'&u_filename='.$uFilename
			.'&u_type='.$uType
			.'&u_item_type='.$uItemType
			.'&u_item_id='.$uItemId
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 档案列表 */
		$_UL = M('Upload')->get_uploadList($where, $order, $limit);
		$this->assign('_UL', $_UL);

		$this->display();
	}

	public function edit_upload()
	{
		$uploadId = ARequest::get('upload_id');
		$_UI = M('Upload')->get_uploadInfo($uploadId);
		if(empty($_UI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('upload/list_upload'));
		}
		$this->assign('_UI', $_UI);
		$this->display();
	}
	public function edit_upload_do()
	{
		$data = ARequest::get();
		$result = M('Upload')->edit_upload($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('UPLOAD').': UPLOAD_ID['.$data['upload_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('upload/list_upload'));
		}
		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('UPLOAD').': UPLOAD_ID['.$data['upload_id'].']');
		$this->success(L('EDIT_SUCCESS'), Url::U('upload/list_upload'));
	}

	public function delete_upload_do()
	{
		$uploadId = ARequest::get('upload_id');
		$_L_ID = is_array($uploadId) ? implode(',', $uploadId) : $uploadId; /* 日志记录ID */

		if(is_array($uploadId))
		{
			foreach($uploadId as $uploadId)
			{
				$result = M('Upload')->delete_upload($uploadId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('UPLOAD').': UPLOAD_ID['.$uploadId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('upload/list_upload'));
				}
			}
		}
		else
		{
			$result = M('Upload')->delete_upload($uploadId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('UPLOAD').': UPLOAD_ID['.$uploadId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('upload/list_upload'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('UPLOAD').': UPLOAD_ID['.$_L_ID.']');
		$this->success(L('DELETE_SUCCESS'), Url::U('upload/list_upload'));
	}

	/* 上传文件 */
	public function upload_file()
	{
		/* file field name */
		$field = 'upload';
		/* 文件类型, 如image, file, all */
		$typeSet = in_array(ARequest::get('type_set'), array('image', 'file', 'all')) ? ARequest::get('type_set') : 'image';
		/* 是否缩略图 */
		$thumb = ('yes' == ARequest::get('thumb')) ? true : false;
		/* 是否水印 */
		$watermark = ('yes' == ARequest::get('watermark')) ? true : false;
		/* 返回类型 normal, editor */
		$returnType = in_array(ARequest::get('return_type'), array('editor', 'normal')) ? ARequest::get('return_type') :'normal';
		/* 上传类型, 如archive, flink, ad */
		$uploadType = AFilter::is_word(ARequest::get('upload_type')) ? ARequest::get('upload_type') : 'archive';
		/* 编辑器函数编号 */
		$fn = intval(ARequest::get('CKEditorFuncNum'));

		/* 上传文件 */
		$result = M('Upload')->upload_file($field, $typeSet, $thumb, $watermark, $returnType, $uploadType, $fn);
		if(!empty($result['error']))
		{
			exit($result['error']);
		}
		exit($result['data']);
	}

}


?>