<?php

/**
 *--------------------------------------
 * 会员收藏
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-11-16
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class MemberFavoriteCtrlr extends ManageCtrlr
{
	public function list_favorite()
	{
		$where = array();
		/* 筛选档案 */
		$archiveId = ARequest::get('archive_id') ? ARequest::get('archive_id') : 0;
		if($archiveId > 0)
		{
			$where['archive_id'] = array('EQ', $archiveId);
		}

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

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

		/* 列表排序 */
		$orderBy = ARequest::get('order_by') ? ARequest::get('order_by') : 'member_favorite_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('MemberFavorite')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('member_favorite/list_favorite?archive_id='.$archiveId
			.'&member_id='.$memberId
			.'&mf_title='.$mfTitle
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 收藏列表 */
		$_MFL = M('MemberFavorite')->get_favoriteList($where, $order, $limit);
		$this->assign('_MFL', $_MFL);

  		$this->display();
	}

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

		if(is_array($memberFavoriteId))
		{
			foreach($memberFavoriteId as $memberFavoriteId)
			{
				$result = M('MemberFavorite')->delete_favorite($memberFavoriteId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_FAVORITE').': MEMBER_FAVORITE_ID['.$memberFavoriteId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('member_favorite/list_favorite'));
				}
			}
		}
		else
		{
			$result = M('MemberFavorite')->delete_favorite($memberFavoriteId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_FAVORITE').': MEMBER_FAVORITE_ID['.$memberFavoriteId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('member_favorite/list_favorite'));
			}
		}

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

?>