<?php

namespace Push\Lib;

use Dever;

class Data
{
    public function get($key, $name, $total = 10, $col = '1,2,3,4,5,6,7', $function = '10',$page = false, $limit = false)
    {
        $info = Dever::db('push/info')->one(array('key' => $key));

        if (!$info) {
            $insert['key'] = $key;
            $insert['name'] = $name;
            $insert['num'] = $total;
            $insert['col'] = $col;
            $insert['function'] = $function;
            $info['id'] = Dever::db('push/info')->insert($insert);
            $info['num'] = $insert['num'];
        }

        $data = array();
        if ($info) {
            if (!$info['num']) {
                $info['num'] = 20;
            }
            if ($limit > 0) {
                $info['num'] = $limit;
            }

            $where = array();
            $where['info_id'] = $info['id'];

            if ($page) {
                $where['page'] = array($info['num'], 'list');
                $data = Dever::db('push/data')->getAllPage($where);
            } else {
                $where['limit'] = '0,' . $info['num'];
                $data = Dever::db('push/data')->getAll($where);
            }

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

        return $data;
    }

    private function getOne($info)
    {
        $info['id'] = 0;
        if ($info['type'] < 10) {
            $table = Dever::config('base')->table_name[$info['type']];
            $method = 'one';
            $id = str_replace('content/', '', $table) . '_id';
            $id = $info[$id];
            unset($info[$id]);
            if ($id > 0) {
                $info['id'] = $id;
                $data = Dever::db($table)->$method($id);
                if (isset($data) && $data) {
                    if (isset($data['name']) && $data['name'] && !$info['name']) {
                        $info['name'] = $data['name'];
                    }

                    if (isset($data['desc']) && $data['desc'] && !$info['desc']) {
                        $info['desc'] = $data['desc'];
                    }

                    if (isset($data['info']) && $data['info'] && !$info['info']) {
                        $info['info'] = $data['info'];
                    }

                    if (isset($data['author']) && $data['author'] && !$info['author']) {
                        $info['author'] = $data['author'];
                    }

                    if (isset($data['pdate']) && $data['pdate'] && !$info['pdate']) {
                        $info['pdate'] = $data['pdate'];
                    }

                    if (isset($data['pic']) && $data['pic'] && !$info['pic']) {
                        $info['pic'] = $data['pic'];
                    }

                    if (isset($data['link']) && $data['link']) {
                        $info['link'] = $data['link'];
                    }

                    if (isset($data['video']) && $data['video']) {
                        $info['video'] = $data['video'];
                        $info['video_info'] = Dever::qqvideo($data['video']);
                        $info['video_id'] = $info['video_info'][1];
                        $info['video_link'] = $info['video_info'][0];
                    }
                }
            }
        }

        if ($info['link']) {
            if (!strstr($info['link'], 'http')) {
                $info['link'] = 'http://' . $info['link'];
            }
        }

        if ($info['pdate']) {
            $info['pdate'] = date('M d, Y', $info['pdate']);

            if ($info['author']) {
                $info['pdate'] .= ' by ' . $info['author'];
            }
        }

        $info['cdate'] = date('Y-m-d', $info['cdate']);

        if (isset($info['pic']) && $info['pic']) {
            $info['pic'] = Dever::load('main/lib/core')->replacePic($info['pic']);
        }

        if (isset($info['name']) && $info['name']) {
            $info['name'] = Dever::cut($info['name'], 20, '...');
        }

        return $info;
    }
}