<?php

/**
 *--------------------------------------
 * 档案
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-4
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class ArchiveCtrlr extends ManageCtrlr
{
	public function list_archive()
	{
		/* 模型列表 */
		$_AML = M('ArchiveModel')->get_modelList(1, true);
		$this->assign('_AML', $_AML);

		/* 属性列表 */
		$_AFL = M('ArchiveFlag')->get_flagList();
		$this->assign('_AFL', $_AFL);

		$archiveChannelId = ARequest::get('archive_channel_id') ? ARequest::get('archive_channel_id') : 0;
		if(0 < $archiveChannelId)
		{
			/* 检查授权 */
			$_t_AI = M('Admin')->get_adminInfo(ASession::get('admin_id'));
			$myChannel = $_t_AI['a_ac_id'];
			if(!in_array('_all', $myChannel) && !in_array($archiveChannelId, $myChannel))
			{
				$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
			}
			$archiveModelId = M('ArchiveChannel')->field('archive_model_id')->find($archiveChannelId);
			$archiveModelId = $archiveModelId['archive_model_id'];
		}
		else
		{
			$archiveModelId = ARequest::get('archive_model_id') ? ARequest::get('archive_model_id') : 0;
		}

		$_AMI = '';
		if(0 < $archiveModelId)
		{
			$_AMI = M('ArchiveModel')->get_modelInfo($archiveModelId);
			$this->assign('_AMI', $_AMI);
		}

		/* 模型全部频道 */
		$_ACL = M('ArchiveChannel')->get_myChannelList($archiveModelId);
		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'));
		$_ACLStr = $act->get_leafStr(0, "<option value='\$archive_channel_id'>\$spacer\$ac_name</option>\r\n", ARequest::get('archive_channel_id'), "<option value='\$archive_channel_id' selected='selected'>\$spacer\$ac_name</option>\r\n");
		$this->assign('_ACLStr', $_ACLStr);

		$where = array();
		/* 筛选频道 */
		if(0 == $archiveChannelId && !empty($_ACL))
		{
			$archiveChannelId = '';
			foreach($_ACL as $c)
			{
				$archiveChannelId .= $c['archive_channel_id'].',';
				if(!empty($c['ac_sub_channel']))
				{
					foreach($c['ac_sub_channel'] as $cs)
					{
						$archiveChannelId .= $cs['archive_channel_id'].',';
					}
				}
			}
			$archiveChannelId = rtrim($archiveChannelId, ',');
		}
		$where['__ARCHIVE__.archive_channel_id'] = array('IN', $archiveChannelId);

		/* 筛选属性 */
		$afAlias = ARequest::get('af_alias') ? ARequest::get('af_alias') : '';
		if(!empty($afAlias))
		{
			$where['__ARCHIVE__.af_alias'] = array('INSET', $afAlias);
		}

		/* 筛选状态 */
		$aStatus = ARequest::get('a_status') ? ARequest::get('a_status') : '';
		if('n' == $aStatus)
		{
			$where['__ARCHIVE__.a_status'] = array('EQ', 0);
		}
		elseif('p' == $aStatus)
		{
			$where['__ARCHIVE__.a_status'] = array('EQ', 1);
		}
		elseif('r' == $aStatus)
		{
			$where['__ARCHIVE__.a_status'] = array('EQ', 2);
		}

		/* 筛选标题 */
		$aTitle = ARequest::get('a_title');
		if(!empty($aTitle))
		{
			$where['__ARCHIVE__.a_title'] = array('LIKE', '%'.$aTitle.'%');
		}

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

		/* 列表排序 */
		$orderBy = ARequest::get('order_by') ? ARequest::get('order_by') : 'archive_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('Archive')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('archive/list_archive?archive_model_id='.$archiveModelId
			.'&archive_channel_id='.$archiveChannelId
			.'&a_status='.$aStatus
			.'&a_title='.$aTitle
			.'&member_id='.$memberId
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 档案列表 */
		$_AL = M('Archive')->get_archiveList($where, $order, $limit);
		$this->assign('_AL', $_AL);

		if(!empty($_AMI))
		{
			$this->display('admin/'.$_AMI['am_tpl_list']);
		}
		else
		{
			$this->display();
		}
	}

	/* 添加档案 */
	public function add_archive()
	{
		$archiveModelId = ARequest::get('archive_model_id');
		$archiveChannelId = ARequest::get('archive_channel_id');

		if(!$archiveModelId)
		{
			$_ACI = M('ArchiveChannel')->get_channelInfo($archiveChannelId);
			$archiveModelId = $_ACI['archive_model_id'];
		}

		$_AMI = M('ArchiveModel')->get_modelInfo($archiveModelId);
		if(empty($_AMI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('archive/list_archive'));
		}
		if(0 == $_AMI['am_status'])
		{
			$this->error(L('MODEL_IS_NOT_ACTIVE'), Url::U('archive/list_archive'));
		}
		$this->assign('_AI', $_AMI);

		$_ACL = M('ArchiveChannel')->get_myChannelList($_AMI['archive_model_id']);
		if(empty($_ACL))
		{
			$this->error(L('NO_CHANNEL_AVAILABLE'), Url::U('archive_channel/add_channel'));
		}
		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'));
		$_ACLStr = $act->get_leafStr(0, "<option value='\$archive_channel_id'>\$spacer\$ac_name</option>\r\n");
		$_ACLStr = str_replace("<option value='{$archiveChannelId}'>", "<option value='{$archiveChannelId}'selected='selected'>", $_ACLStr);
		$this->assign('_ACLStr', $_ACLStr);

		$_FI = '';
		load('field#func');
		foreach($_AMI['am_field'] as $tag => $params)
		{
			if(1 == $params['f_is_auto'])
			{
				$_FI .= get_formItem($tag, $params);
			}
		}
		$this->assign('_FI', $_FI);

		/* 属性列表 */
		$_AFL = M('ArchiveFlag')->get_flagList();
		$this->assign('_AFL', $_AFL);

		$this->display('admin/'.$_AMI['am_tpl_add']);
	}
	public function add_archive_do()
	{
		$data = ARequest::get();

		if(!M('ArchiveChannel')->check_permission($data['archive_channel_id']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE').': '.L('PERMISSION_LIMIT'), 0);
			$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
		}

		$data['af_alias'] = (!empty($data['af_alias']) ? implode(',', $data['af_alias']) : '');
		$data['a_add_time'] = time();
		$data['a_edit_time'] = !empty($data['a_edit_time']) ? strtotime($data['a_edit_time']) : time();
		$data['a_add_ip'] = AServer::get_ip();
		$data['a_edit_ip'] = $data['a_add_ip'];
		$data['member_id'] = ASession::get('member_id');
		$data['m_username'] = ASession::get('m_username');

		/* 提取缩略图 */
		if(isset($data['first_img_as_thumb']) and !empty($data['first_img_as_thumb']))
		{
			$field = $data['first_img_as_thumb'][0];
			$data['a_thumb'] = M('Upload')->get_thumb($data[$field], $data['am_alias']);
		}

		/* 提取摘要 */
		if(isset($data['get_abstract']) and !empty($data['get_abstract']))
		{
			$field = $data['get_abstract'][0];
			$data['a_description'] = AFilter::plain_text($data[$field], 200);
		}

		/* 处理远程文件 */
		if(isset($data['save_remote_source']) and !empty($data['save_remote_source']))
		{
			foreach($data['save_remote_source'] as $field)
			{
				$waterMark = false;
				if(isset($data['watermark_remote_img']) and in_array($field, $data['watermark_remote_img']))
				{
					$waterMark = true;
				}
				$data[$field] = M('Upload')->deal_reomote_file($data[$field], $waterMark, $data['am_alias']);
			}
		}

		/* 插入主表 */
		$result = M('Archive')->add_archive($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('archive/list_archive?archive_channel_id='.$data['archive_channel_id']));
		}

		/* 插入附加表 */
		$data['archive_id'] = $result['data'];
		$result = M('Archive')->add_archive_addon($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('archive/list_archive?archive_channel_id='.$data['archive_channel_id']));
		}

		/* 更新上传文件 */
		M('Upload')->update_upload($data['archive_id'], ASession::get('member_id'));

		/* 更新会员经验, 点券, 积分 */
		if(1 == $data['a_status'])
		{
			M('Member')->update_credit($data['member_id'], 'publish');
		}

		/* 立即更新 */
		if(isset($data['update_now']) and 1 == $data['update_now'])
		{
			M('Archive')->update_url($data['archive_id']);
			ARequest::set('archive_id', $data['archive_id']);
			$this->update_html_do();
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE').': ARCHIVE_ID['.$data['archive_id'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('archive/list_archive?archive_channel_id='.$data['archive_channel_id']));
	}

	/* 编辑档案 */
	public function edit_archive()
	{
		$archiveId = ARequest::get('archive_id');
		$_AI = M('Archive')->get_archiveInfo($archiveId);
		if(empty($_AI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('archive/list_archive'));
		}
		if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id']))
		{
			$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
		}
		$this->assign('_AI', $_AI);

		$_ACL = M('ArchiveChannel')->get_myChannelList($_AI['archive_model_id']);
		if(empty($_ACL))
		{
			$this->error(L('NO_CHANNEL_AVAILABLE'), Url::U('archive_channel/add_channel'));
		}
		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'));
		$_ACLStr = $act->get_leafStr(0, "<option value='\$archive_channel_id'>\$spacer\$ac_name</option>\r\n", $_AI['archive_channel_id'], "<option value='\$archive_channel_id' selected='selected'>\$spacer\$ac_name</option>\r\n");
		$this->assign('_ACLStr', $_ACLStr);

		$_FI = '';
		load('field#func');
		foreach($_AI['am_field'] as $tag => $params)
		{
			if(1 == $params['f_is_auto'])
			{
				$_FI .= get_formItem($tag, $params, $_AI);
			}
		}
		$this->assign('_FI', $_FI);

		/* 属性列表 */
		$_AFL = M('ArchiveFlag')->get_flagList();
		$this->assign('_AFL', $_AFL);

		$this->display('admin/'.$_AI['am_tpl_edit']);
	}
	public function edit_archive_do()
	{
		$data = ARequest::get();
		unset($data['member_id']); unset($data['m_username']);

		$_AI = M('Archive')->field('archive_channel_id,member_id,a_status')
			->where('archive_id = \''.ARequest::get('archive_id').'\'')->find();
		if(empty($_AI))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE').': ARCHIVE_ID['.$data['archive_id'].']'.L('ITEM_NOT_EXIST'), 0);
			$this->error(L('ITEM_NOT_EXIST'), Url::U('archive/list_archive'));
		}
		if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id'])
			or !M('ArchiveChannel')->check_permission($data['archive_channel_id']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE').': ARCHIVE_ID['.$data['archive_id'].']'.L('PERMISSION_LIMIT'), 0);
			$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
		}

		$data['member_id'] = $_AI['member_id'];
		$data['af_alias'] = (!empty($data['af_alias']) ? implode(',', $data['af_alias']) : '');
		$data['a_edit_time'] = !empty($data['a_edit_time']) ? strtotime($data['a_edit_time']) : time();
		$data['a_edit_ip'] = AServer::get_ip();

		/* 提取缩略图 */
		if(isset($data['first_img_as_thumb']) and !empty($data['first_img_as_thumb']))
		{
			$field = $data['first_img_as_thumb'][0];
			$data['a_thumb'] = M('Upload')->get_thumb($data[$field], $data['am_alias']);
		}

		/* 提取摘要 */
		if(isset($data['get_abstract']) and !empty($data['get_abstract']))
		{
			$field = $data['get_abstract'][0];
			$data['a_description'] = AFilter::plain_text($data[$field], 200);
		}

		/* 处理远程文件 */
		if(isset($data['save_remote_source']) and !empty($data['save_remote_source']))
		{
			foreach($data['save_remote_source'] as $field)
			{
				$waterMark = false;
				if(isset($data['watermark_remote_img']) and in_array($field, $data['watermark_remote_img']))
				{
					$waterMark = true;
				}
				$data[$field] = M('Upload')->deal_reomote_file($data[$field], $waterMark, $data['am_alias']);
			}
		}

		/* 编辑主表 */
		$result = M('Archive')->edit_archive($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE').': ARCHIVE_ID['.$data['archive_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('archive/list_archive?archive_channel_id='.$data['archive_channel_id']));
		}

		/* 编辑附加表 */
		$result = M('Archive')->edit_archive_addon($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE').': ARCHIVE_ID['.$data['archive_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('archive/list_archive?archive_channel_id='.$data['archive_channel_id']));
		}

		/* 更新上传文件 */
		M('Upload')->update_upload($data['archive_id'], $data['member_id']);

		/* 更新会员经验, 点券, 积分 */
		if(1 == $data['a_status'] and 1 != $_AI['a_status'])
		{
			M('Member')->update_credit($_AI['member_id'], 'publish');
		}
		elseif(1 != $data['a_status'] and 1 == $_AI['a_status'])
		{
			M('Member')->update_credit($_AI['member_id'], 'publish', false);
		}

		/* 发送通知 */
		if($_AI['a_status'] != $data['a_status'] and isset($data['send_notify']) and 'y' == strtolower($data['send_notify']))
		{
			switch($data['a_status'])
			{
				case 0:
					$_t_status = L('NOT_PASSED');
					break;
				case 1:
					$_t_status = L('PASSED');
					break;
				case 2:
					$_t_status = L('REFUNDED');
					break;
				default:
					$_t_status = '';
					break;
			}
			$mnContent = L('ARCHIVE').'[<a href="'.Url::U('home@archive/show_archive?archive_id='.$data['archive_id']).'" target="_blank">'.$data['a_title'].'</a>] '.$_t_status;
			M('MemberNotify')->send_notify($data['member_id'], L('ARCHIVE_STATUS_IS_CHANGED'), $mnContent);
		}

		/* 立即更新 */
		if(isset($data['update_now']) and 1 == $data['update_now'])
		{
			M('Archive')->update_url($data['archive_id']);
			ARequest::set('archive_id', $data['archive_id']);
			$this->update_html_do();
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE').': ARCHIVE_ID['.$data['archive_id'].']');
		$this->success(L('EDIT_SUCCESS'), Url::U('archive/list_archive?archive_channel_id='.$data['archive_channel_id']));
	}

	/* 删除档案 */
	public function delete_archive_do()
	{
		$archiveId = ARequest::get('archive_id');
		$_L_ID = is_array($archiveId) ? implode(',', $archiveId) : $archiveId; /* 日志记录ID */

		if(is_array($archiveId))
		{
			foreach($archiveId as $archiveId)
			{
				$_AI = M('Archive')->field('archive_channel_id,member_id,a_status,a_title')->where('archive_id = \''.$archiveId.'\'')->find();
				if(empty($_AI))
				{
					continue;
				}
				if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('PERMISSION_LIMIT'), 0);
					$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
				}

				$result = M('Archive')->delete_archive($archiveId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('archive/list_archive'));
				}

				/* 更新会员经验, 点券, 积分 */
				if(1 == $_AI['a_status'])
				{
					M('Member')->update_credit($_AI['member_id'], 'publish', false);
				}

				/* 发送通知 */
				if('y' == strtolower(ARequest::get('send_notify')))
				{
					M('MemberNotify')->send_notify($_AI['member_id'], L('ARCHIVE_IS_DELETED'), L('ARCHIVE').'['.$_AI['a_title'].']');
				}
			}
		}
		else
		{
			$_AI = M('Archive')->field('archive_channel_id,member_id,a_status,a_title')->where('archive_id = \''.$archiveId.'\'')->find();
			if(empty($_AI))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('ITEM_NOT_EXIST'), 0);
				$this->error(L('ITEM_NOT_EXIST'), Url::U('archive/list_archive'));
			}
			if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('PERMISSION_LIMIT'), 0);
				$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
			}

			$result = M('Archive')->delete_archive($archiveId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('archive/list_archive'));
			}

			/* 更新会员经验, 点券, 积分 */
			if(1 == $_AI['a_status'])
			{
				M('Member')->update_credit($_AI['member_id'], 'publish', false);
			}

			/* 发送通知 */
			if('y' == strtolower(ARequest::get('send_notify')))
			{
				M('MemberNotify')->send_notify($_AI['member_id'], L('ARCHIVE_IS_DELETED'), L('ARCHIVE').'['.$_AI['a_title'].']');
			}
		}

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

	/* 审核档案 */
	public function pass_archive_do()
	{
		$archiveId = ARequest::get('archive_id');
		$_L_ID = is_array($archiveId) ? implode(',', $archiveId) : $archiveId; /* 日志记录ID */

		if(is_array($archiveId))
		{
			foreach($archiveId as $archiveId)
			{
				$_AI = M('Archive')->field('archive_channel_id,member_id,a_status,a_title')->where('archive_id = \''.$archiveId.'\'')->find();
				if(empty($_AI))
				{
					continue;
				}
				if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('PASS').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('PERMISSION_LIMIT'), 0);
					$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
				}

				$result = M('Archive')->pass_archive($archiveId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('PASS').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('archive/list_archive'));
				}

				/* 更新会员经验, 点券, 积分 */
				if(1 != $_AI['a_status'])
				{
					M('Member')->update_credit($_AI['member_id'], 'publish');

					/* 发送通知 */
					if('y' == strtolower(ARequest::get('send_notify')))
					{
						$mnContent = L('ARCHIVE').'[<a href="'.Url::U('home@archive/show_archive?archive_id='.$archiveId).'" target="_blank">'.$_AI['a_title'].'</a>] '.L('PASSED');
						M('MemberNotify')->send_notify($_AI['member_id'], L('ARCHIVE_STATUS_IS_CHANGED'), $mnContent);;
					}
				}

			}
		}
		else
		{
			$_AI = M('Archive')->field('archive_channel_id,member_id,a_status,a_title')->where('archive_id = \''.$archiveId.'\'')->find();
			if(empty($_AI))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('PASS').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('ITEM_NOT_EXIST'), 0);
				$this->error(L('ITEM_NOT_EXIST'), Url::U('archive/list_archive'));
			}
			if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('PASS').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('PERMISSION_LIMIT'), 0);
				$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
			}

			$result = M('Archive')->pass_archive($archiveId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('PASS').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('archive/list_archive'));
			}

			/* 更新会员经验, 点券, 积分 */
			if(1 != $_AI['a_status'])
			{
				M('Member')->update_credit($_AI['member_id'], 'publish');

				/* 发送通知 */
				if('y' == strtolower(ARequest::get('send_notify')))
				{
					$mnContent = L('ARCHIVE').'[<a href="'.Url::U('home@archive/show_archive?archive_id='.$archiveId).'" target="_blank">'.$_AI['a_title'].'</a>] '.L('PASSED');
					M('MemberNotify')->send_notify($_AI['member_id'], L('ARCHIVE_STATUS_IS_CHANGED'), $mnContent);;
				}
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('PASS').L('ARCHIVE').': ARCHIVE_ID['.$_L_ID.']');
		$this->success(L('PASS_SUCCESS'), Url::U('archive/list_archive'));
	}

	/* 退回档案 */
	public function refund_archive_do()
	{
		$archiveId = ARequest::get('archive_id');
		$_L_ID = is_array($archiveId) ? implode(',', $archiveId) : $archiveId; /* 日志记录ID */

		if(is_array($archiveId))
		{
			foreach($archiveId as $archiveId)
			{
				$_AI = M('Archive')->field('archive_channel_id,member_id,a_status,a_title')->where('archive_id = \''.$archiveId.'\'')->find();
				if(empty($_AI))
				{
					continue;
				}
				if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('REFUND').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('PERMISSION_LIMIT'), 0);
					$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
				}

				$result = M('Archive')->refund_archive($archiveId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('REFUND').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('archive/list_archive'));
				}

				/* 更新会员经验, 点券, 积分 */
				if(1 == $_AI['a_status'])
				{
					M('Member')->update_credit($_AI['member_id'], 'publish', false);

					/* 发送通知 */
					if('y' == strtolower(ARequest::get('send_notify')))
					{
						$mnContent = L('ARCHIVE').'[<a href="'.Url::U('home@archive/show_archive?archive_id='.$archiveId).'" target="_blank">'.$_AI['a_title'].'</a>] '.L('REFUNDED');
						M('MemberNotify')->send_notify($_AI['member_id'], L('ARCHIVE_STATUS_IS_CHANGED'), $mnContent);;
					}
				}
			}
		}
		else
		{
			$_AI = M('Archive')->field('archive_channel_id,member_id,a_status,a_title')->where('archive_id = \''.$archiveId.'\'')->find();
			if(empty($_AI))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('REFUND').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('ITEM_NOT_EXIST'), 0);
				$this->error(L('ITEM_NOT_EXIST'), Url::U('archive/list_archive'));
			}
			if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('REFUND').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('PERMISSION_LIMIT'), 0);
				$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
			}

			$result = M('Archive')->refund_archive($archiveId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('REFUND').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('archive/list_archive'));
			}

			/* 更新会员经验, 点券, 积分 */
			if(1 == $_AI['a_status'])
			{
				M('Member')->update_credit($_AI['member_id'], 'publish', false);

				/* 发送通知 */
				if('y' == strtolower(ARequest::get('send_notify')))
				{
					$mnContent = L('ARCHIVE').'[<a href="'.Url::U('home@archive/show_archive?archive_id='.$archiveId).'" target="_blank">'.$_AI['a_title'].'</a>] '.L('REFUNDED');
					M('MemberNotify')->send_notify($_AI['member_id'], L('ARCHIVE_STATUS_IS_CHANGED'), $mnContent);;
				}
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('REFUND').L('ARCHIVE').': ARCHIVE_ID['.$_L_ID.']');
		$this->success(L('REFUND_SUCCESS'), Url::U('archive/list_archive'));
	}

	/* 更改频道 */
	public function change_channel_do()
	{
		$archiveId = ARequest::get('archive_id');
		$archiveChannelId = ARequest::get('archive_channel_id');
		$_L_ID = is_array($archiveId) ? implode(',', $archiveId) : $archiveId; /* 日志记录ID */

		if(empty($archiveChannelId) or empty($archiveId))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('PARAMS_ERROR'), 0);
			$this->error(L('PARAMS_ERROR'), Url::U('archive/list_archive'));
		}

		$archiveId = explode(',', $archiveId);
		foreach($archiveId as $archiveId)
		{
			$_AI = M('Archive')->field('archive_channel_id,member_id,a_title')->where('archive_id = \''.$archiveId.'\'')->find();
			if(empty($_AI))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('ITEM_NOT_EXIST'), 0);
				$this->error(L('ITEM_NOT_EXIST'), Url::U('archive/list_archive'));
			}
			if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id'].','.$data['archive_channel_id']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.L('PERMISSION_LIMIT'), 0);
				$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
			}

			$data['archive_channel_id'] = $archiveChannelId;
			$data['archive_id'] = $archiveId;
			$result = M('Archive')->edit_archive($data);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE').': ARCHIVE_ID['.$archiveId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('archive/list_archive'));
			}

			/* 发送通知 */
			if('y' == strtolower(ARequest::get('send_notify')))
			{
				$mnContent = L('ARCHIVE').'[<a href="'.Url::U('home@archive/show_archive?archive_id='.$archiveId).'" target="_blank">'.$_AI['a_title'].'</a>] '.L('MOVED');
				M('MemberNotify')->send_notify($_AI['member_id'], L('ARCHIVE_CHANNEL_IS_CHANGED'), $mnContent);;
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE').': ARCHIVE_ID['.$_L_ID.']');
		$this->success(L('EDIT_SUCCESS'), Url::U('archive/list_archive'));
	}

	/* 添加属性 */
	public function add_flag_do()
	{
		$archiveId = ARequest::get('archive_id');
		$afALias = ARequest::get('af_alias');
		$_L_ID = is_array($archiveId) ? implode(',', $archiveId) : $archiveId; /* 日志记录ID */

		if(empty($afALias) or empty($archiveId))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE_FLAG').': ARCHIVE_ID['.$archiveId.']'.L('PARAMS_ERROR'), 0);
			$this->error(L('PARAMS_ERROR'), Url::U('archive/list_archive'));
		}

		$archiveId = explode(',', $archiveId);
		foreach($archiveId as $archiveId)
		{
			$_AI = M('Archive')->field('archive_channel_id,member_id,a_title')->where('archive_id = \''.$archiveId.'\'')->find();
			if(empty($_AI))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE_FLAG').': ARCHIVE_ID['.$archiveId.']'.L('ITEM_NOT_EXIST'), 0);
				$this->error(L('ITEM_NOT_EXIST'), Url::U('archive/list_archive'));
			}
			if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE_FLAG').': ARCHIVE_ID['.$archiveId.']'.L('PERMISSION_LIMIT'), 0);
				$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
			}

			$result = M('Archive')->add_flag($archiveId, $afALias);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE_FLAG').': ARCHIVE_ID['.$archiveId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('archive/list_archive'));
			}

			/* 发送通知 */
			if('y' == strtolower(ARequest::get('send_notify')))
			{
				$mnContent = L('ARCHIVE').'[<a href="'.Url::U('home@archive/show_archive?archive_id='.$archiveId).'" target="_blank">'.$_AI['a_title'].'</a>] '.L('ADD').L('FLAG').'['.$afALias.']';
				M('MemberNotify')->send_notify($_AI['member_id'], L('ARCHIVE_FLAG_IS_CHANGED'), $mnContent);;
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE_FLAG').': ARCHIVE_ID['.$_L_ID.']');
		$this->success(L('ADD_FLAG_SUCCESS'), Url::U('archive/list_archive'));
	}

	/* 删除属性 */
	public function delete_flag_do()
	{
		$archiveId = ARequest::get('archive_id');
		$afALias = ARequest::get('af_alias');
		$_L_ID = is_array($archiveId) ? implode(',', $archiveId) : $archiveId; /* 日志记录ID */

		if(empty($afALias) or empty($archiveId))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE_FLAG').': ARCHIVE_ID['.$archiveId.']'.L('PARAMS_ERROR'), 0);
			$this->error(L('PARAMS_ERROR'), Url::U('archive/list_archive'));
		}

		$archiveId = explode(',', $archiveId);
		foreach($archiveId as $archiveId)
		{
			$_AI = M('Archive')->field('archive_channel_id,member_id,a_title')->where('archive_id = \''.$archiveId.'\'')->find();
			if(empty($_AI))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE_FLAG').': ARCHIVE_ID['.$archiveId.']'.L('ITEM_NOT_EXIST'), 0);
				$this->error(L('ITEM_NOT_EXIST'), Url::U('archive/list_archive'));
			}
			if(!M('ArchiveChannel')->check_permission($_AI['archive_channel_id']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE_FLAG').': ARCHIVE_ID['.$archiveId.']'.L('PERMISSION_LIMIT'), 0);
				$this->error(L('PERMISSION_LIMIT'), Url::U('archive/list_archive'));
			}

			$result = M('Archive')->delete_flag($archiveId, $afALias);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE_FLAG').': ARCHIVE_ID['.$archiveId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('archive/list_archive'));
			}

			/* 发送通知 */
			if('y' == strtolower(ARequest::get('send_notify')))
			{
				$mnContent = L('ARCHIVE').'[<a href="'.Url::U('home@archive/show_archive?archive_id='.$archiveId).'" target="_blank">'.$_AI['a_title'].'</a>] '.L('DELETE').L('FLAG').'['.$afALias.']';
				M('MemberNotify')->send_notify($_AI['member_id'], L('ARCHIVE_FLAG_IS_CHANGED'), $mnContent);;
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE_FLAG').': ARCHIVE_ID['.$_L_ID.']');
		$this->success(L('DELETE_FLAG_SUCCESS'), Url::U('archive/list_archive'));
	}

	public function update_html_do()
	{
		$_o = M('Option')->get_option('core');
		if(!$_o['html_switch'])
		{
			return;
		}

		$archiveId = ARequest::get('archive_id');
		$_AI = M('Archive')->get_archiveInfo($archiveId);
		if(empty($_AI)
			or !$_AI['ac_is_html']
			or 0 != $_AI['ac_view_ml_rank']
			or !$_AI['a_is_html']
			or 1 != $_AI['a_status']
			or 0 != $_AI['a_cost_points'])
		{
			return;
		}

		/* 站点信息 */
		$_SITE = M('Option')->get_option('site');
		unset($_SITE['theme']);
		$this->assign('_SITE', $_SITE);

		/* 设置 */
		$_G = M('Option')->get_option(array(
			'upload',
			'interaction',
			'member'
		));
		$this->assign('_G', $_G);

		/* 定义当前频道ID */
		$this->assign('AC_ID', $_AI['archive_channel_id']);
		/* 定义当前档案ID */
		$this->assign('A_ID', $_AI['archive_id']);
		/* 频道信息 */
		$_ACI = M('ArchiveChannel')->get_channelInfo($_AI['archive_channel_id']);
		$_AI['ac_sibling'] = $_ACI['ac_sibling']; /* 获取同级分类 */
		/* 定义当前位置 */
		$this->assign('POSITION', $_ACI['ac_position'].' &raquo; '.$_AI['a_title']);
		/* 计划任务 */
		$this->assign('TASK', 'update_html_archive&archive_id='.$archiveId);

		/* 判断文件路径 */
		$_dir = '';
		if(0 == $_AI['a_html_path'])
		{
			$_dir = trim(str_replace('{uwa_path}', '', $_AI['ac_html_dir']), '/');
		}

		$pyc = get_instance('Pinyin');

		/* 判断文件名 */
		if(!empty($_AI['a_html_naming']))
		{
			$naming = $_AI['a_html_naming'];
		}
		else
		{
			$naming = $_AI['ac_html_naming_archive'];
		}
		$naming = str_replace(
			array(
				'{ac_py}',
				'{ac_id}',
				'{Y}',
				'{M}',
				'{D}',
				'{a_py}',
				'{a_id}'),
			array(
				$pyc->get_pinyin($_AI['ac_name'], 'utf-8'),
				$_AI['archive_channel_id'],
				date('Y', $_AI['a_add_time']),
				date('m', $_AI['a_add_time']),
				date('d', $_AI['a_add_time']),
				$pyc->get_pinyin($_AI['a_title'], 'utf-8'),
				$_AI['archive_id']),
			$naming
		);
		$file = $_dir.'/'.trim($naming, '/');

		/* 获取模板 */
		if(!empty($_AI['a_tpl']))
		{
			$tpl = 'home/'.$_AI['a_tpl'];
		}
		else
		{
			$tpl = 'home/'.$_AI['ac_tpl_archive'];
		}

		/* 处理自动分页字段 */
		foreach($_ACI['am_field'] as $field => $params)
		{
			if(1 == $params['f_is_paging'])
			{
				$pagingField = $field;
				break;
			}
		}
		if(isset($pagingField) and false !== strpos($_AI[$pagingField], '#uwa_paging#'))
		{
			$_title = $_AI['a_title'];
			$_content = explode('#uwa_paging#', $_AI[$pagingField]);

			$rowsNum = count($_content);
			/* 创建分页 */
			foreach($_content as $key => $_c)
			{
				$_GET[C('VAR.PAGE')] = $key + 1;
				$p = new APage($rowsNum, 1, __APP__.'/'.$file.'-_page_'.C('HTML.FILE_SUFFIX'));
				$this->assign('PAGE_LIST', $p->show());
				$_AI[$pagingField] = ltrim($_content[ARequest::get(C('VAR.PAGE')) - 1], '</p>');
				$_AI['a_title'] = $_title.'('.ARequest::get(C('VAR.PAGE')).')';
				$this->assign('_V', $_AI);

				$_C = require(CFG_PATH.D_S.'comm.php');
				$this->te->tplTheme = $_C['TE']['TPL_THEME'];
				$this->build_html($file.'-'.ARequest::get(C('VAR.PAGE')).C('HTML.FILE_SUFFIX'), APP_PATH, $tpl);
				/* 创建默认页 */
				if(1 == ARequest::get(C('VAR.PAGE')))
				{
					$this->build_html($file.C('HTML.FILE_SUFFIX'), APP_PATH, $tpl);
				}
				$this->te->tplTheme = 'default';
			}
		}
		else
		{
			$this->assign('PAGE_LIST', '');

			$this->assign('_V', $_AI);

			$_C = require(CFG_PATH.D_S.'comm.php');
			$this->te->tplTheme = $_C['TE']['TPL_THEME'];
			$this->build_html($file.C('HTML.FILE_SUFFIX'), APP_PATH, $tpl);
			$this->te->tplTheme = 'default';
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('UPDATE_ARCHIVE_HTML').': ARCHIVE_ID['.$archiveId.']');
	}
}

?>