<?php

// +----------------------------------------------------------------------

// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]

// +----------------------------------------------------------------------

// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.

// +----------------------------------------------------------------------

// | Author: Dean <zxxjjforever@163.com>

// +----------------------------------------------------------------------

/**

 * 文章内页

 */

namespace Phonegame\Controller;

use Common\Controller\PhonegameController;

class ArticleController extends PhonegameController {

    //文章内页

    public function index() {
    	$id=intval($_GET['id']);
    	$article=sp_sql_post($id,'');
    	if(empty($article)){
    	    header('HTTP/1.1 404 Not Found');
    	    header('Status:404 Not Found');
    	    if(sp_template_file_exists(MODULE_NAME."/404")){
    	        $this->display(":404");
    	    }
    	    return ;
    	}
		/*排行榜*/
		$games=M("m_game")->where('isdisplay=1')->order('downloadnum desc')->limit(10)->select();
        /**
         *修改时间：2016-02-26 10：20 修改人：yu  
         */
		$news = sp_sql_posts("cid:38;field:post_title,tid,smeta;order:listorder asc;limit:2;"); 
		$act = sp_sql_posts("cid:36;field:post_title,tid,smeta;order:listorder asc;limit:2;");
    	$termid=$article['term_id'];
    	$term_obj= M("Terms");
    	$term=$term_obj->where("term_id='$termid'")->find();
    	$article_id=$article['object_id'];
    	$posts_model=M("Posts");
    	$posts_model->save(array("id"=>$article_id,"post_hits"=>array("exp","post_hits+1")));
    	$article_date=$article['post_modified'];
		$join = "".C('DB_PREFIX').'posts as b on a.object_id =b.id';
    	$join2= "".C('DB_PREFIX').'users as c on b.post_author = c.id';
    	$rs= M("TermRelationships");
       	$next=$rs->alias("a")->join($join)->join($join2)->where(array("post_modified"=>array("egt",$article_date), "tid"=>array('neq',$id), "status"=>1,'term_id'=>$termid))->order("post_modified asc")->find();
    	$prev=$rs->alias("a")->join($join)->join($join2)->where(array("post_modified"=>array("elt",$article_date), "tid"=>array('neq',$id), "status"=>1,'term_id'=>$termid))->order("post_modified desc")->find();
		$this->assign('act',$act);
		$this->assign('news',$news);
		$this->assign('games',$games);
    	$this->assign("next",$next);
    	$this->assign("prev",$prev);   	
    	$smeta=json_decode($article['smeta'],true);
    	$content_data=sp_content_page($article['post_content']);
    	$article['post_content']=$content_data['content'];
    	$this->assign("page",$content_data['page']);
    	$this->assign($article);
    	$this->assign("smeta",$smeta);
    	$this->assign("term",$term);
    	$this->assign("article_id",$article_id);
    	$tplname=$term["one_tpl"];
    	$tplname=sp_get_apphome_tpl($tplname, "article");
    	$this->display($this->template.'/template1'.'/Index'.'/article');
    }
    public function do_like(){
    	$this->check_login();
    	$id=intval($_GET['id']);//posts表中id
    	$posts_model=M("Posts");    	
    	$can_like=sp_check_user_action("posts$id",1);    	
    	if($can_like){
    		$posts_model->save(array("id"=>$id,"post_like"=>array("exp","post_like+1")));
    		$this->success("赞好啦！");
    	}else{
    		$this->error("您已赞过啦！");
    	}
    }
}

