<?php

namespace Content\Lib;

use Dever;

class Author
{
    # 更新数据
    public function data($id, $name, $data)
    {
        $source_table = Dever::input('source_table');

        $author = Dever::param('author_id', $data);

        if ($author && $id > 0 && $source_table) {
            $info = Dever::db('content/author')->one($author);
            if (!$info) {
                $insert['name'] = $author;
                $author = Dever::db('content/author')->insert($insert);
            }

            Dever::db($source_table)->update(array('where_id' => $id, 'author_id' => $author));
        }
    }

    # 搜索
    public function search_api()
    {
        $keyword = Dever::input('keyword');

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

        $where = array();

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

        $insert = array();
        if (!$data && $keyword) {
            $insert[0]['name'] = $keyword . '[新增]';
            $insert[0]['value'] = $keyword;
        }

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

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

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

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

        return $data;
    }
}