<?php

namespace Cas\Controller;

use Cas\Dao\LotteryEvents;
use KIF\Core\Request;
use KIF\Verify;
use Cas\Module\LotteryLog;
use Cas\Dao\UserInfo;
use Cas\Module\Count;
use Cas\Dao\ArticleLikeCounter;
use Cas\Dao\ArticleLike;
use Cas\Module\Lottery;

/**
 * "文章"活动
 */
class EventsArticle extends EventsController {
	public function doDefault() {
		$thisTime = time ();
		//$events_id = Request::g ( 'events_id' );
		$events_id = $this->getEventsCreateTimeToId();
		if (! Verify::unsignedInt ( $events_id )) {
			self::fail_exit ( '无效id' );
		}
		
		
		// 获取“文章”信息
		$objDLotteryEvents = new LotteryEvents ();
		$articleInfo = $objDLotteryEvents->get ( $events_id );
		
		# 检查是否移动端
		$title = $articleInfo['weixinShare']['title'] ? $articleInfo['weixinShare']['title']: $articleInfo['events_name'];
		$this->checkIsFromMobile($title);
		
		# 获取登录地址
		$url = Request::g('url');
		$plat_form_id = $this->getPlatFormId();
		
		
		$objMLottery  = new Lottery();
		$thisUrl  = $objMLottery -> getEventsUrl($articleInfo['type'],$articleInfo['create_time'],$url);
		
		$loginUrl = $this -> getLoginUrl($thisUrl);
		$this->setOutput('loginUrl', $loginUrl);
		
		# 判断是否需要登录
		$ckLogin = $this->ckLogin();
		$this->setOutput('ckLogin', $ckLogin);
		
		# 获取用户信息
		$objUserData = new UserInfo();
		$uid = $this->getRunTimeUid();
		$userData = $objUserData -> get($uid);
		$this->setOutput('userData', $userData);
		
		# 检查当前活动是否需要登录
		$authorize = $articleInfo['authorize'];
		if ($authorize && $loginUrl) {
				if($ckLogin==true){
					self::redirect($loginUrl);
				}
		}
		
		$objMLotteryLog = new LotteryLog ( $uid, $events_id );
		$other = array (
				'访问页面:' => '成功' 
		);
		$objMLotteryLog->addUserParticipateLog ( $other );
		
		# 记录PV UV日志 访问
		$objCount = new Count();
		$objCount->setPassLog( $events_id,$plat_form_id);
		
		# 获取点赞数
		$objArticleLikeCounter = new ArticleLikeCounter();
		$likes = $objArticleLikeCounter->getLikes($events_id);
		$likes = $likes + $articleInfo['article_praise'];

		# 是否已经点赞
		$objArticleLike = new ArticleLike();
		$isHasLikes = $objArticleLike->isHasLike($events_id, $uid);
		
		# 页面点击数
		$clickNum = $articleInfo['click_num'] + $articleInfo['article_pass_base_num']?$articleInfo['click_num'] + $articleInfo['article_pass_base_num']:0;
		# 当前访问后增加一个点击
		$objDLotteryEvents -> modify(array('click_num' => $articleInfo['click_num']+1 ), array('id' => $events_id));
		
		preg_match_all('/<embed\s{1}src=\"([^\"]+)\"[^>]+>/i', $articleInfo['article_text'], $matches);
		if ($matches) foreach ($matches[0] as $key => $tmpMatch) {
			$tmp_new_video_html = '<iframe class="video_iframe" height=498 width=510 src="'.$matches[1][$key].'" frameborder=0 allowfullscreen></iframe>';
			$articleInfo['article_text'] = str_replace($tmpMatch, $tmp_new_video_html, $articleInfo['article_text']);
		}
		
		$this->tpl = 'events_article';
		$this->setOutput('title', $articleInfo['weixinShare']['title'] ? $articleInfo['weixinShare']['title'] : $articleInfo['events_name']);
		$this->setOutput('display', $articleInfo['list_display']);
		$this->setOutput('article', $articleInfo);
		$this->setOutput('weixinShare', $articleInfo['weixinShare']);
		$this->setOutput('likes', $likes);
		$this->setOutput('isHasLikes', $isHasLikes);
		$this->setOutput('clickNum', $clickNum);
	}
	
	public function doLikes() {
		$type = Request::p('type'); //点赞类型。1点赞、2取消
		if (!in_array($type, array(1,2))) {
			$this->ajax_fail_exit('操作失败');
		}
		
		$events_id = Request::p('events_id');
		if (!Verify::unsignedInt($events_id)) {
			$this->ajax_fail_exit('操作失败');
		}
		
		$uid = $this->getRunTimeUid();
		if (!Verify::unsignedInt($uid)) {
			$this->ajax_fail_exit('操作失败');
		}
		
		$objArticleLike = new ArticleLike();
		$objArticleLikeCounter = new ArticleLikeCounter();
		
		if ($type == 1) {
			$objArticleLike->like($events_id, $uid);
			$objArticleLikeCounter->incrLikes($events_id);
		}
		
		if ($type == 2) {
			$objArticleLike->cancalLike($events_id, $uid);
			$objArticleLikeCounter->decrLikes($events_id);
		}
		
		$this->ajax_success_exit();
	}
	
	/**
	 * 
	 */
	public function display() {
		return $this->render ();
	}
}