<?php

namespace Content\Lib;

use Dever;

class Manage
{
    /**
     * 获取标签信息：根据名称获取多个标签，一般用于autocomplete
     *
     * @return mixed
     */
    public function getByCreaterName_api()
    {
        $data = Dever::db('content/creater')->getAllByName(array
            (
                'name' => Dever::input('term'))
        );
        if ($data) {
            return $data;
        }
        return array
            (
            0 => array('id' => -1, 'value' => '没有找到您搜索的数据', 'label' => '没有找到您搜索的数据'),
        );
    }

    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');
    }

    public function updateCreater($id, $name, $data)
    {   
        $name = Dever::param('name', $data);
        if ($name) {
            $first = mb_substr($name, 0, 1);
            if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $first)) {
                $pinyin = new zh2py();
                $first = $pinyin::conv($first);
            }
            $shou = strtolower($first);

            $update['shou'] = $shou;
            $update['where_id'] = $id;
            Dever::db('content/creater')->update($update);
        }
    }

    public function updateXnr($id, $name, $data)
    {   
        $ename = Dever::param('ename', $data);
        if ($ename) {
            $first = mb_substr($ename, 0, 1);
            if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $first)) {
                $pinyin = new zh2py();
                $first = $pinyin::conv($first);
            }
            $shou = strtolower($first);

            $update['shou'] = $shou;
            $update['where_id'] = $id;
            Dever::db('content/xuniren')->update($update);
        }
    }
}

class zh2py
{
    //根据汉字区位表,(http://www.mytju.com/classcode/tools/QuWeiMa_FullList.asp)
    //我们可以看到从16-55区之间是按拼音字母排序的,所以我们只需要判断某个汉字的区位码就可以得知它的拼音首字母.
    //区位表第一部份,按拼音字母排序的.
    //16区-55区
    /*
        'A'=>0xB0A1, 'B'=>0xB0C5, 'C'=>0xB2C1, 'D'=>0xB4EE, 'E'=>0xB6EA, 'F'=>0xB7A2, 'G'=>0xB8C1,'H'=>0xB9FE,
    'J'=>0xBBF7, 'K'=>0xBFA6, 'L'=>0xC0AC, 'M'=>0xC2E8, 'N'=>0xC4C3, 'O'=>0xC5B6, 'P'=>0xC5BE,'Q'=>0xC6DA,
    'R'=>0xC8BB, 'S'=>0xC8F6, 'T'=>0xCBFA, 'W'=>0xCDDA, 'X'=>0xCEF4, 'Y'=>0xD1B9, 'Z'=>0xD4D1
    */
    private static $FirstTable = array(
    0xB0C5, 0xB2C1, 0xB4EE, 0xB6EA, 0xB7A2, 0xB8C1, 0xB9FE, 0xBBF7, 0xBFA6, 0xC0AC, 0xC2E8,
    0xC4C3, 0xC5B6, 0xC5BE, 0xC6DA, 0xC8BB, 0xC8F6, 0xCBFA, 0xCDDA, 0xCEF4, 0xD1B9, 0xD4D1,0xD7FA
    );
    private static $FirstLetter = "ABCDEFGHJKLMNOPQRSTWXYZ";
    //区位表第二部份,不规则的,下面的字母是每个区里面对应字的拼音首字母.从网上查询整理出来的,可能会有部份错误.
    //56区-87区
    private static $SecondTable = array(
        "CJWGNSPGCGNEGYPBTYYZDXYKY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YXYQDLQCAFYWYXQHZ",
        );
    function utf8_to_gbk($string)//编码转换,必须转换成GB2312字符,这里只是简单的判断并不是很准确,可以自己写一个.
    {
        return mb_convert_encoding($string,'gb2312','utf-8'); 
    }

    function conv($str)
    {
    $str = self::utf8_to_gbk($str);
    $len = strlen($str);
    $newStr = '';
    for($i=0; $i<$len ; ++$i)
    {
        $H = ord($str[$i]);
        $L = ord($str[$i+1]);
        //字符集非法
        if ($H < 0xB0 ||  $L < 0xA1 || $H > 0xF7 || $L == 0xFF)
        {
        $newStr .= $str[$i];
        continue;
        }
        if ($H < 0xD8)//($H >= 0xB0 && $H <=0xD7)//查询文字在一级汉字区(16-55)
        {
        $W = ($H << 8) | $L;
        foreach(self::$FirstTable as $key=>$value)
        {
            if ($W < $value)
            {
            $newStr .= self::$FirstLetter[$key];
            break;
            }
        }
        }
        else// if (H >= 0xD8 && H <= 0xF7)//查询中文在二级汉字区(56-87)
        $newStr .=self::$SecondTable[$H - 0xD8][$L-0xA1];
        ++$i;
    }
    return $newStr;
    }
}