<?php
/**
 * @desc: 自定义标签库
 * @Author: Dr.Xing <ezfwork@foxmail.com>
 * @Since: 2018/2/1 19:33
 */

namespace seven\taglib;


use think\template\TagLib;
use think\Db;


class Xs extends Taglib
{

    protected $tags = [
        'nav' => ['attr' => 'name,showlevel', 'close'=> 0],
        'posts' => ['attr' => 'name,ids,cid,field,limit,order,where,ispage,pagesize,type,key', 'close' => 0],
        'ad' => ['attr' => 'id', 'close' => 0],
    ];

    public function tagAd($tag)
    {
        if (isset($tag['id']) && !empty($tag['id'])) {
            $id = intval($tag['id']);
            $parseStr = '<?php ';
            $parseStr .= 'echo get_ad(' . $id . ');';
            $parseStr .= "?>";
            return $parseStr;
        }
        return;
    }

    public function tagNav($tag)
    {
        $name = isset($tag['name']) ? $tag['name'] : '' ; //导航ID
        $class = isset($tag['class']) ? $tag['class'] : ''; //导航css
        $extend = isset($tag['extend']) ? $tag['extend'] : ''; //导航附加属性
        $name = preg_replace('/(\w+)/i','${1}',$name);
        $class = str_replace('"', "'", $class);
        $extend = str_replace('"', "'", $extend);
        $navwrapattr= 'id:'.$name.';class:'.$class.';extend:'.$extend.';';

        $itemclass = isset($tag['itemclass']) ? $tag['itemclass'] : ''; //菜单项css
        $itemextend = isset($tag['itemextend']) ? $tag['itemextend'] : ''; //菜单项附加属性
        $itemclass = str_replace('"', "'", $itemclass);
        $itemextend = str_replace('"', "'", $itemextend);

        $childwrap = isset($tag['childwrap']) ? $tag['childwrap'] : ''; //子菜单项的外围标签
        $childclass = isset($tag['childclass']) ? $tag['childclass'] : ''; //子菜单项的外围标签css
        $childextend = isset($tag['childextend']) ? $tag['childextend'] : ''; //子菜单项的外围附加属性
        $childwrap = preg_replace('/(\w+)/i','${1}',$childwrap);
        $childextend = str_replace('"', "'", $childextend);
        $childclass = str_replace('"', "'", $childclass);
        $childwrapattr = 'wrap:'.$childwrap.';class:'.$childclass.';extend:'.$childextend.';';

        $itemtpl = '';
        if($itemclass!='' && $itemextend!='')
            $itemtpl = "<li><a class='".$itemclass."' ".$itemextend." href='#href'>#name</a> #childlist</li>";

        $parseStr = '<?php ';
        $parseStr .= 'echo get_nav("' . $navwrapattr . '","' . $itemtpl . '","' . $childwrapattr . '");';
        $parseStr .= "?>";
        if (!empty($parseStr)) {
            return $parseStr;
        }
        return;
    }


    /**
     * 返回文章(列表/单篇)
     * @param $tag
     * @return string
     */
    public function tagPosts($tag)
    {
        $name   = $tag['name']; //供后期volist/foreach调用的名称
        $ids    = isset($tag['ids']) ? $tag['ids'] : ''; //获取单篇文章id
        $cid    = isset($tag['cid']) ? $tag['cid'] : ''; //栏目id
        $field  = isset($tag['field']) ? $tag['field'] : ''; //获取字段
        $limit  = isset($tag['limit']) ? $tag['limit'] : ''; //获取数量
        $order  = isset($tag['order']) ? $tag['order'] : ''; //排序
        $where  = isset($tag['where']) ? $tag['where'] : ''; //条件
        $ispage = isset($tag['ispage']) ? $tag['ispage'] : 'false'; //是否分页，默认不分页
        $pagesize = !empty($tag['ispage']) && !empty($tag['pagesize']) && is_numeric($tag['pagesize'])
            ? intval($tag['pagesize'])
            : 10; //config('posts.pagesize');

        //$where=var_export($where, true);

        $options= "ids:".$ids.';cid:'.$cid.';field:'.$field.';limit:'.$limit.';order:'.$order.';where:'.$where.';';

        $parseStr = '<?php ';
        $parseStr .='$'.$name .'=get_posts("'.$options.'",null,'.$ispage.',"'.$pagesize.'");';
        $parseStr .="?>";

        if (!empty($parseStr)) {
            return $parseStr;
        }
        return;
    }

    /**
     * 返回singlepage的menu
     * @param $tag
     * @return string
     */
    public function tagMenu($tag)
    {
        $name   = $tag['name'];
        $menuid    = isset($tag['menuid']) ? $tag['menuid'] : '';
        $parseStr = '<?php ';
        $parseStr .='$'.$name .'=get_menu('.'"'.$menuid.'");';
        $parseStr .="?>";
        if (!empty($parseStr)) {
            return $parseStr;
        }
        return;
    }
}