<?php

namespace Content\Lib;

use Dever;

class News
{
    # 获取几条数据
    public function getHome($type = 1, $limit = 3, $page = 'article')
    {
        $where = array();

        if ($type) {
            $where['type'] = $type;
        }

        $where['limit'] = '0,' . $limit;
        $data = Dever::db('content/news')->getHome($where);

        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->getOne($v);
            }
        }

        return $data;
    }

    # 获取几条数据
    public function getAll($type = 1, $name = '')
    {
        $where = array();

        if ($type && $type > 0) {
            $where['type'] = $type;
        }
        if($name) {
            $where['name'] = $name;
            $where['tag'] = $name;
        }

        if ($type == 3) {
            $data = Dever::db('content/news')->getAllVideo($where);
        } else {
            $data = Dever::db('content/news')->getAll($where);
        }

        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->getOne($v);
            }
        }

        return $data;
    }

    public function getInfo($type, $id)
    {
        $where = array();

        if ($type) {
            $where['type'] = $type;
        }
        $where['id'] = $id;

        $data = Dever::db('content/news')->getOne($where);

        if ($data) {
            $data = $this->getOne($data);
        }

        return $data;
    }

    private function getOne($info)
    {
        if ($info['small_pic']) {
            $info['small_pic'] = str_replace('http://8.131.66.239/', 'http://www.virtualbeings.cn/', $info['small_pic']);
        }
        if ($info['content_video_pic']) {
            $info['content_video_pic'] = str_replace('http://8.131.66.239/', 'http://www.virtualbeings.cn/', $info['content_video_pic']);
        }
        
        $info['pic'] = str_replace('http://8.131.66.239/', 'http://www.virtualbeings.cn/', $info['pic']);
        $info['link'] = Dever::url('main/article?type='.$info['type'].'&id=' . $info['id']);
        $info['pdate_string'] = date('M d, Y', $info['pdate']);
        if ($info['author']) {
            $info['pdate_string'] .= ' by ' . $info['author'];
        }
        $info['pdate_s'] = date('Y/m/d', $info['pdate']);

        $info['video_link'] = Dever::url('main/content.video?id=' . $info['id']);
        /*
        if ($info['video'] && strstr($info['video'], '.qq.com')) {
            $info['video_info'] = Dever::qqvideo($info['video'], 2);
            $info['video_id'] = isset($info['video_info'][1]) ? $info['video_info'][1] : 0;
            $info['video_link'] = isset($info['video_info'][0]) ? $info['video_info'][0] : '';
        } else {
            $info['video_info'] = '';
            $info['video_id'] = '';
            $info['video_link'] = $info['video'];
        }
        */

        $info['sname'] = $info['name'] . '<br />' . $info['desc'];

        return $info;
    }
}