<?php

namespace Content\Lib;

use Dever;

class Manage
{
    private function search($table = 'content/news', $type = false)
    {
        $keyword = Dever::input('keyword');

        $yes = Dever::input('yes');

        $where = array();

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

        if ($yes) {
            $yes = Dever::db($table)->search(array('ids' => $yes));
        }
        if (!$keyword) {
            $where['limit'] = '0,50';
            $data = Dever::db($table)->search($where);
        } else {
            $where['name'] = $keyword;
            $data = Dever::db($table)->search($where);
        }

        $result = array();
        if ($yes) {
            foreach ($yes as $k => $v) {
                if (isset($data[$k])) {
                    unset($data[$k]);
                }
                $yes[$k]['selected'] = 'selected';
            }
            $data = $yes + $data;

            $data = array_merge($data, array());
        } else {
            $data = array_merge($data, array());
        }

        if (!$data) {
            Dever::alert('暂无数据');
        }

        return $data;
    }

    # 搜索文章
    public function search_news_api()
    {
        return $this->search('content/news', 1);
    }

    # 搜索专题
    public function search_feature_api()
    {
        return $this->search('content/news', 2);
    }

    # 搜索视频
    public function search_video_api()
    {
        return $this->search('content/news', 3);
    }

    # 搜索活动
    public function search_act_api()
    {
        return $this->search('content/news', 4);
    }

    # 搜索造物主
    public function search_creater_api()
    {
        return $this->search('content/creater');
    }

    # 搜索虚拟人
    public function search_xuniren_api()
    {
        return $this->search('content/xuniren');
    }
}