News.class.php 5.67 KB
<?php
/**
 * 默认控制器
 */
class Controller_News extends Controller_Abstract
{
    
    public function indexAction()
    {
        $requestTag =  trim($this->_request->tag);//标签
        $limit = 40;
        $tag = $requestTag;
        if($this->_platform == 'android' || $this->_platform == 'iphone')
        {
        	$exceptTags = array('视频');
        	if($requestTag == '视频')
        	{
        	    $tag = '手机视频';//html5视频为手机视频
        	}
        }
        else
        {
        	$exceptTags = array('手机视频');
        }
        $total = Facade_News::getTotal($tag, $exceptTags);
        $page = new Lib_Helper_Pagination($total, $limit);
        if ($tag)
        {
            $page->setParames(array('tag' => $requestTag)) ;
        }
        $tagKeys = Facade_News::$types;
        $list = Facade_News::getList($tag, $page->getOffset(), $limit, $exceptTags);
        $this->_view['list'] = $list;
        $newTags = Facade_News::getTags();
        $this->_view['total'] = 0;
        $tags = $temp = array();
        foreach($newTags as $tag)
        {
            $temp[$tag['tag']] = $tag;
        }
        foreach($tagKeys as $tagKey)
        {
        	$tags[$tagKey] = array('tag' => $tagKey, 'num'=> 0);
        	if(isset($temp[$tagKey]))
        	{
        	    $tags[$tagKey] = $temp[$tagKey];
        	}
        }
        if($this->_platform == 'android' || $this->_platform == 'iphone')
        {
            $tags['视频'] = $temp['手机视频'];
        }
        $this->_view['tags'] = $tags;
        foreach($this->_view['tags'] as $val)
        {
        	$this->_view['total']  += $val['num'];
        }
        $page->setOptions(array('afterAppend' => 'setLayout'));
        $this->_view['pagination'] = $page->getPagination();
        $this->_view['current_tag'] = $requestTag;
    }
    
    /**
     * 发布资讯
     */
    public function publishAction()
    {
        $secret = $this->_request->secret;
    	if($secret == SITE_PUBLISH_NEWS_SECRET)
    	{
    	    $tag_id = $this->_request->tag_id;
    	    if(!isset(Facade_News::$types[$tag_id]))
    	    {
    	        return $this->returnJson(false, 403, '','频道不存在');
    	    }
    	    $tag = Facade_News::$types[$tag_id];
    	    $brand_ids =  $this->_request->brand_ids;
    	    $title = $this->_request->title;
    	    $content = $this->_request->content;
    	    $pics = $this->_request->pics;
    	    if(empty($brand_ids)|| empty($title) || empty($content) || empty($pics))
    	    {
    	        return $this->returnJson(false, 403, '','参数不能为空');
    	    }
    	    $brand_ids = array_filter(explode(',', $brand_ids));
    	    $pics = array_filter(explode(',', $pics));
    	    $thumb = $pics[0];
    	    list($width, $height, $type, $attr) = getimagesize($thumb);
    	    $thumb_size = json_encode(array('width'=> $width,'height'=> $height));
    	    $data = array('title' => $title, 'content' => $content, 'pics' => implode('|', $pics),'tag'=> $tag,
    	            'thumb' => $thumb, 'thumb_size'=> $thumb_size);
    	    //新建
    	    $id = Facade_News::setInfo($data);
    	    foreach($brand_ids as $brandID)
    	    {
    	        Facade_Brand::setBrandNewsRela($id, $brandID);
    	    }
    	    return $this->returnJson(true, 200, '','发布成功');
    	}
    	else
    	{
    	    return $this->returnJson(false, 403, '','密钥不正确');
    	}
    }
    
    /**
     * 详情页
     */
    public function detailAction()
    {
        $id = intval($this->_request->id);
        $news = array();
        if (empty($id))
        {
            return $this->_redirect('news/index');
        }
        $info = Facade_News::getOneById($id);
        if (!$info)
        {
            return $this->_redirect('news/index');
        }
        if($this->_platform == 'android' || $this->_platform == 'iphone')
        {
            $exceptTags = array('视频');
            $news[] = Facade_News::getSameNewsById($id, $exceptTags);
            $news = array_merge($news, Facade_News::getList(array(), 0, 3, $exceptTags));
            $news = array_slice(array_filter($news), 0, 3);
        }
        $info['title'] = strip_tags($info['title']);
        $info['pics'] = array_filter(explode('|', $info['pics']));
        $info['thumb'] = Lib_Images::getImageUrl($info['thumb'], 'source','fragmentimg');
        //增加浏览量
        Facade_News::updateHits($id);
        $this->_view['info'] = $info;
        $this->_view['banners'] = Facade_Index::getIndex(4);
        $this->_view['news'] = $news;
    }
    
    /**
     * 详情页相关新闻
     * 
     * @return json
     */
    public function getdetailnewsAction()
    {
        $id = intval($this->_request->id);
        $list = array('recom'=> array(), 'hotVideo'=> array(), 'lastVideo' => array());
        $tag = '视频';
        $exceptTags = array('手机视频');
        $recom = array();
        $recom[] = Facade_News::getSameNewsById($id, $exceptTags);
        $recom = array_merge($recom, Facade_News::getList(array(), 0, 3, $exceptTags));
        $recom = array_slice(array_filter($recom), 0, 3);
        $hotVideo = array_values(Facade_News::getListByHits($tag, 0, 5, $exceptTags));
        $lastVideo = array_values(Facade_News::getList($tag, 0, 5, $exceptTags));
        $list['recom'] = $recom;
        $list['hotVideo'] = $hotVideo;
        $list['lastVideo'] = $lastVideo;
        foreach($list as $key => $listnews)
        {
            if(!empty($listnews))
            {
            	foreach($listnews as $key2 => $news)
            	{
            	    //不需要内容
            	    $list[$key][$key2]['content'] = '';
            	}
            }	
        }
        return $this->returnJson(true, 200, $list);
    }
}