<?php

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

class ArchiveCtrlr extends IndexCtrlr
{
	public function show_channel()
	{
		$archiveChannelId = intval(ARequest::get('archive_channel_id'));

		$_ACI = M('ArchiveChannel')->get_channelInfo($archiveChannelId);
		if(empty($_ACI))
		{
			halt();
		}
		$this->assign('_V', $_ACI);

		/* 定义当前频道ID */
		$this->assign('AC_ID', $archiveChannelId);
		/* 定义当前位置 */
		$this->assign('POSITION', $_ACI['ac_position']);

		if(1 == $_ACI['ac_type'])
		{
			/* 计划任务 */
			$this->assign('TASK', 'update_html_channel_index&archive_channel_id='.$archiveChannelId);

			$this->display('home/'.$_ACI['ac_tpl_index']);
		}
		else
		{
			$where = array();
			$where['__ARCHIVE__.a_status'] = array('EQ', 1);

			$_ACL = M('ArchiveChannel')->get_channelList(0, $archiveChannelId);
			$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'), $archiveChannelId);

			$where['__ARCHIVE__.archive_channel_id'] = array('IN', implode(',', $act->get_leafid($archiveChannelId)));

			$order = '`a_rank` DESC, `a_edit_time` DESC';

			/* 获取分页信息 */
			$_GET[C('VAR.PAGE')] = intval(ARequest::get(C('VAR.PAGE'))) ? intval(ARequest::get(C('VAR.PAGE'))) : 1;
			$rowsNum = M('Archive')->where($where)->count();
			$p = new APage($rowsNum, $_ACI['ac_page_size'], Url::U('home@archive/show_channel?archive_channel_id='.$archiveChannelId.'&'.C('VAR.PAGE').'=_page_'));
			$this->assign('PAGE_LIST', $p->show());
			$limit = $p->get_limit();

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

			/* 计划任务 */
			$this->assign('TASK', 'update_html_channel_list&archive_channel_id='.$archiveChannelId.'&'.C('VAR.PAGE').'='.ARequest::get(C('VAR.PAGE')));

			$this->display('home/'.$_ACI['ac_tpl_list']);
		}
	}

	public function show_archive()
	{
		$archiveId = intval(ARequest::get('archive_id'));
		$_AI = M('Archive')->get_archiveInfo($archiveId);
		if(empty($_AI))
		{
			halt();
		}
		/* 定义当前频道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']);
		if(empty($_ACI))
		{
			halt();
		}
		$_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);

		/* 状态检查 */
		$_AI['msg_err'] = '';
		if((1 != $_AI['a_status'] and ASession::get('member_id') != $_AI['member_id'])
			or -1 == $_AI['ac_view_ml_rank']
			or (0 < $_AI['ac_view_ml_rank'] and intval(ASession::get('ml_rank')) < $_AI['ac_view_ml_rank']))
		{
			/* 无权限,频道浏览权限 */
			$_AI['msg_err'] = L('PERMISSION_LIMIT');
		}
		/* 购买检查 */
		if(0 < $_AI['a_cost_points'] and ASession::get('member_id') != $_AI['member_id'])
		{
			$_where = array();
			$_where['member_id'] = array('EQ', ASession::get('member_id'));
			$_where['mo_product_type'] = array('EQ', 'archive');
			$_where['mo_product_name'] = array('EQ', 'ARCHIVE'.$archiveId);
			$_where['mo_status'] = array('EQ', 1);
			$_oi = M('MemberOrder')->where($_where)->find();
			if(empty($_oi))
			{
				$_AI['msg_err'] = L('ITEM_COST')
					.' <span class="fw_b fc_r">'.$_AI['a_cost_points'].'</span> '.L('POINTS')
					.' <a class="btn_l" href="'.Url::U('home@member/buy_archive_do?archive_id='.$archiveId).'">'.L('BUY').'</a>';
			}
		}

		if(empty($_AI['msg_err']))
		{
			/* 处理自动分页字段 */
			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#'))
			{
				$_content = explode('#uwa_paging#', $_AI[$pagingField]);

				$_GET[C('VAR.PAGE')] = intval(ARequest::get(C('VAR.PAGE'))) ? intval(ARequest::get(C('VAR.PAGE'))) : 1;
				$rowsNum = count($_content);
				$p = new APage($rowsNum, 1, Url::U('home@archive/show_archive?archive_id='.$archiveId.'&'.C('VAR.PAGE').'=_page_'));
				$this->assign('PAGE_LIST', $p->show());
				$_AI[$pagingField] = ltrim($_content[ARequest::get(C('VAR.PAGE')) - 1], '</p>');
				$_AI['a_title'] = $_AI['a_title'].'('.ARequest::get(C('VAR.PAGE')).')';
			}
			else
			{
				$this->assign('PAGE_LIST', '');
			}
		}

		$this->assign('_V', $_AI);
		
		if(!empty($_AI['a_tpl']))
		{
			$this->display('home/'.$_AI['a_tpl']);
		}
		else
		{
			$this->display('home/'.$_AI['ac_tpl_archive']);
		}
	}

	/* 更新统计 */
	public function get_count()
	{
		$archiveId = intval(ARequest::get('archive_id'));
		$type = ARequest::get('type');
		if('view' == $type)
		{
			M('Archive')->where('`archive_id` = \''.$archiveId.'\'')->field_inc('a_view_count');
			$count = M('Archive')->where('`archive_id` = \''.$archiveId.'\'')->get_field('a_view_count');
			echo "document.write('{$count}');";
			exit;
		}
		elseif('support' == $type)
		{
			$count = M('Archive')->where('`archive_id` = \''.$archiveId.'\'')->get_field('a_support_count');
			echo "document.write('{$count}');";
			exit;
		}
		elseif('do_support' == $type)
		{
			if(!I('feedback_short', 1))
			{
				$this->ajax_return(0, L('_TRY_LATER_'));
			}
			M('Archive')->where('`archive_id` = \''.$archiveId.'\'')->field_inc('a_support_count');
			I('feedback_short');
			$this->ajax_return(1, L('SUPPORT_SUCCESS'));
		}
		elseif('oppose' == $type)
		{
			$count = M('Archive')->where('`archive_id` = \''.$archiveId.'\'')->get_field('a_oppose_count');
			echo "document.write('{$count}');";
			exit;
		}
		elseif('do_oppose' == $type)
		{
			if(!I('feedback_short', 1))
			{
				$this->ajax_return(0, L('_TRY_LATER_'));
			}
			M('Archive')->where('`archive_id` = \''.$archiveId.'\'')->field_inc('a_oppose_count');
			I('feedback_short');
			$this->ajax_return(1, L('OPPOSE_SUCCESS'));
		}
	}
}

?>