<?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;
        }

        $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;
        }

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

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

        return $data;
    }

    private function getOne($info)
    {
        $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']);

        if ($info['video']) {
            $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] : '';
        }

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

        return $info;
    }
}