<?php

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

class FlinkCtrlr extends ManageCtrlr
{
	public function list_flink()
	{
		/* 分类列表 */
		$_FCL = M('FlinkCategory')->get_categoryList();
		$this->assign('_FCL', $_FCL);

		$where = array();
		/* 筛选分类 */
		$flinkCategoryId = ARequest::get('flink_category_id') ? ARequest::get('flink_category_id') : 0;
		if($flinkCategoryId > 0)
		{
			$where['__FLINK__.flink_category_id'] = array('EQ', $flinkCategoryId);
		}

		/* 筛选类型 */
		$fShowType = ARequest::get('f_show_type') ? ARequest::get('f_show_type') : '';
		if('t' == $fShowType)
		{
			$where['__FLINK__.f_show_type'] = array('EQ', 0);
		}
		elseif('l' == $fShowType)
		{
			$where['__FLINK__.f_show_type'] = array('EQ', 1);
		}

		/* 筛选状态 */
		$fStatus = ARequest::get('f_status') ? ARequest::get('f_status') : '';
		if('n' == $fStatus)
		{
			$where['__FLINK__.f_status'] = array('EQ', 0);
		}
		elseif('p' == $fStatus)
		{
			$where['__FLINK__.f_status'] = array('EQ', 1);
		}

		/* 列表排序 */
		$orderBy = ARequest::get('order_by') ? ARequest::get('order_by') : 'flink_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('Flink')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('flink/list_flink?flink_category_id='.$flinkCategoryId
			.'&f_show_type='.$fShowType
			.'&f_status='.$fStatus
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 链接列表 */
		$_FL = M('Flink')->get_flinkList($where, $order, $limit);
		$this->assign('_FL', $_FL);

		$this->display();
	}

	public function add_flink()
	{
		/* 分类列表 */
		$_FCL = M('FlinkCategory')->get_categoryList();
		$this->assign('_FCL', $_FCL);

		$this->display();
	}
	public function add_flink_do()
	{
		$data = ARequest::get();
		$result = M('Flink')->add_flink($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('FLINK').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('flink/list_flink'));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('FLINK').': FLINK_ID['.$result['data'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('flink/list_flink'));
	}

	public function edit_flink()
	{
		$flinkId = ARequest::get('flink_id');
		$_FI = M('Flink')->get_flinkInfo($flinkId);
		if(empty($_FI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('flink/list_flink'));
		}
		$this->assign('_FI', $_FI);

		/* 分类列表 */
		$_FCL = M('FlinkCategory')->get_categoryList();
		$this->assign('_FCL', $_FCL);

		$this->display();
	}
	public function edit_flink_do()
	{
		$data = ARequest::get();
		$result = M('Flink')->edit_flink($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('FLINK').': FLINK_ID['.$data['flink_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('flink/list_flink'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('FLINK').': FLINK_ID['.$data['flink_id'].']');
		$this->success(L('EDIT_SUCCESS'), Url::U('flink/list_flink'));
	}

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

		if(is_array($flinkId))
		{
			foreach($flinkId as $flinkId)
			{
				$result = M('Flink')->delete_flink($flinkId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('FLINK').': FLINK_ID['.$flinkId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('flink/list_flink'));
				}
			}
		}
		else
		{
			$result = M('Flink')->delete_flink($flinkId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('FLINK').': FLINK_ID['.$flinkId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('flink/list_flink'));
			}
		}

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

	public function toggle_flink_status_do()
	{
		$data['flink_id'] = ARequest::get('flink_id');
		$data['f_status'] = ARequest::get('f_status');
		$result = M('Flink')->edit_flink($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('FLINK').': FLINK_ID['.$data['flink_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('flink/list_flink'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('FLINK').': FLINK_ID['.$data['flink_id'].']');
		$this->success(L('TOGGLE_SUCCESS'), Url::U('flink/list_flink'));
	}

	public function update_flink_do()
	{
		$flinkId = ARequest::get('flink_id');
		$fDisplayOrder = ARequest::get('f_display_order');
		$_L_ID = is_array($flinkId) ? implode(',', $flinkId) : $flinkId; /* 日志记录ID */

		if(!is_array($flinkId) or empty($flinkId))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('FLINK').': FLINK_ID['.$flinkId.']'.L('ITEM_NOT_EXIST'), 0);
			$this->error(L('ITEM_NOT_EXIST'), Url::U('flink/list_flink'));
		}

		$data = array();
		$error = false;
		foreach($flinkId as $k => $id)
		{
			$data['flink_id'] = $id;
			$data['f_display_order'] = $fDisplayOrder[$k];
			$result = M('Flink')->edit_flink($data);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('FLINK').': FLINK_ID['.$flinkId.']', 0);
				$this->error(L('EDIT_FAILED'), Url::U('flink/list_flink'));
			}
		}

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

?>