redirect('/admin/navigation/lists'); } public function lists() { $data = $this->request->param(); $arg_where = ['country_code' => $this->country_code]; $search = []; $this->init_search($search); if (isset($data['name']) && $data['name'] != '') { $arg_where['name'] = ['like', "%$data[name]%"]; $search['name'] = $data['name']; } if (isset($data['nav_type']) && $data['nav_type'] != '') { $arg_where['nav_type'] =$data['nav_type']; $search['nav_type'] = $data['nav_type']; } $arg_order = ['id' => 'desc']; $arg_field = ['*']; // 获取数据 $where1 = $arg_where; $where1['pid'] = 0; $dataObject = model('navigation')->getNavigationLists($where1, $arg_order, $arg_field); //echo model('navigation')->getLastSql(); die; $dataList = self::NavigationHandle(self::navDataHandling($dataObject->items())); $result = []; if(!empty($dataList)) { // 子级数据组合 $where2 = $arg_where; $where2['pid'] = ['in', array_column($dataList, 'id')]; //echo "
==="; print_r($where2);   
            $data2Object = model('navigation')->getNavigationLists($where2, $arg_order, $arg_field);

            $items_data = self::NavigationHandle(self::navDataHandling($data2Object->items()));
            //echo model('navigation')->getLastSql(); die;  
            
            $items_group = [];
            if(!empty($items_data))
            {
                foreach($items_data as $tv)
                {
                    $items_group[$tv['pid']][] = $tv;
                }
            }

            // 数据集合
            if(!empty($items_group))
            {
                foreach($dataList as $dv)
                {
                    if(array_key_exists($dv['id'], $items_group))
                    {
                        $dv['is_sub_data'] = 1;
                        $result[] = $dv;
                        $result = array_merge($result, $items_group[$dv['id']]);
                    } else {
                        $result[] = $dv;
                    }
                }
            } else {
               
                $result = $dataList;
            }
        }  
        
        $dataType = config('common_data_type_list');
        
        //echo model('navigation')->getLastSql();
       //echo "
==="; print_r($result);die;
        $value = [
            'list' => $result,
            'page' => $dataObject->render(),
            'search' => $search,
            'dataType' => $dataType,
        ];
        $this->assign($value);
        
        //获取文章数据

        
        
        return $this->fetch();
    }

    public function add() {

        $value['typeOption'] = config('common_nav_type_list');
        $value['dataType'] = config('common_data_type_list');
        
    
      
        $this->assign($value);
        return $this->fetch();
    }

    public function create() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data) || !is_array($data)) {
                return $this->error(Lang::get('incorrect operation'));
            }
            $validaterule = ['name' => 'require', 'nav_type' => 'require',];
            $validatemsg = ['name.require' => '名称不能为空', 'nav_type.require' => '导航类型不能为空',];
            $valid_result = $this->validate($data, $validaterule, $validatemsg);
            if (true !== $valid_result) {
                // 验证失败 输出错误信息
                return $this->error($valid_result);
            }
            $data['pid'] = empty($data['pid']) ?  0 : $data['pid'];
            $data['url'] = empty($data['url']) ?  '' : $data['url'];
            $data['value'] = empty($data['value']) ?  '' : $data['value'];
            $data['sort'] = intval($data['sort']);
            $data['stat'] = empty($data['stat']) ? 0 : $data['stat'];
            $data['is_new_window_open'] = $data['is_new_window_open'];

            $data['country_code'] = $this->country_code;

            
            
            
            $model = model('navigation')->insertRow($data);
            if ($model && $model->getData('id')) {
                return $this->redirect(url('/admin/navigation/lists'));
            } else {
                return $this->error(Lang::get('Operation Failed'));
            }
        }
        return $this->error(Lang::get('Incorrect Operation'));
    }

    public function edit($id = 0) {
        $id = intval($id);
        
        
        if ($id > 0) {
            $navigation = model('navigation')->where('id', $id)->find();  
            //echo model('navigation')->getLastSql();
            //echo "
=="; print_r($navigation); die;
            if (empty($navigation)) {
                return $this->error(Lang::get('incorrect operation'));
            }
            $value['navigation'] = $navigation;
            
            $value['nav_header_pid_list'] = self::LevelOneNav(['nav_type'=>$navigation['nav_type']]);
        } else {
            return $this->error(Lang::get('incorrect operation'));
        }


        $value['typeOption'] = config('common_nav_type_list');
        $value['dataType'] = config('common_data_type_list');
        
        //获取分类层级目录
        $argc_where = array('isshow' => 1, 'country_code' => $this->country_code);
        $argc_order = array('sort' => 'asc', 'id' => 'asc');
        $argc_field = ['id', 'pid', 'haschild', 'name', 'shortname', 'sort', 'description', 'isshow', 'recommend', 'picture', 'icon', 'image', 'm_icon', 'unique_id'];
        
        $categoryList = Loader::model('product_category')->getList($argc_where,$argc_order, $argc_field);
        
        $value['categoryList']  = $this->list_to_tree($categoryList);        
        
        //获取文章
        $article_list  = self::articleCategoryListContent();        
        $value['article_list']  = $article_list;        
        //echo "
=="; print_r($value['categoryList']); die;
        $value['blog_list']  =  self::blogListContent();        
      
        $this->assign($value);
        return $this->fetch();
    }
    protected function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0) {
        //header('content-type:text/html;charset=utf-8;');
        // 创建Tree
        $tree = [];
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = [];
            foreach ($list as $key => $data) {
                $list[$key] = $data->toArray();
                $refer[$data[$pk]] = & $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] = & $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent = & $refer[$parentId];
                        $parent[$child][] = & $list[$key];
                    }
                }
            }
        }
        return $tree;
    }
    public function update() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data) || !is_array($data)) {
                return $this->error(Lang::get('incorrect operation'));
            }
            $validaterule = ['id' => 'require', 'name' => 'require', 'nav_type' => 'require',];
            $validatemsg = ['id.require' => 'ID不能为空','name.require' => '名称不能为空', 'nav_type.require' => '导航类型不能为空',];
            $valid_result = $this->validate($data, $validaterule, $validatemsg);
            if (true !== $valid_result) {
                // 验证失败 输出错误信息
                return $this->error($valid_result);
            }
            $data['pid'] = empty($data['pid']) ?  '0' : $data['pid'];
            $data['url'] = empty($data['url']) ?  '' : $data['url'];
            $data['sort'] = intval($data['sort']);
            $data['stat'] = empty($data['stat']) ? 0 : $data['stat'];
            $data['is_new_window_open'] = $data['is_new_window_open'];

            $data['country_code'] = $this->country_code;
            $data['id'] = $data['id'];
            
            $model = model('navigation')->updateRow($data);
            if ($model && $model->getData('id')) {
                return $this->redirect(url('/admin/navigation/lists'));
            } else {
                return $this->error(Lang::get('operation failed'));
            }
        }
        return $this->error(Lang::get('incorrect operation'));
    }


    public function updatesort() {
        $id = $this->request->param('id', 0);
        $sort = $this->request->param('sort', 0);
        $sort = intval($sort);
        $id = intval($id);
        if ($this->request->isAjax() && $id) {
            $model = model('navigation')->updateRow(['id' => $id, 'sort' => $sort]);
            if ($model && $model->getData('id')) {
                return $this->success(Lang::get('operation successed'), url('/admin/navigation/lists'));
            } else {
                return $this->error(Lang::get('operation failed'));
            }
        }
        return $this->error(Lang::get('incorrect operation'));
    }

    public function delete($id = 0) {
        $id = intval($id);
        
        
        if ($id > 0) {
            $model = model('navigation')->deleteRow($id);
  
            if ($model) {
                return $this->success(Lang::get('operation successed'), url('/admin/navigation/lists'));
            } else {
                return $this->error(Lang::get('operation failed'));
            }
        }
      
        return $this->error(Lang::get('incorrect operation'));
    }

    public function deletes() {
        if ($this->request->isPost()) {
            $ids = $this->request->post('ids');
            $in_ids = explode(',', trim($ids, ','));

            $result = model('navigation')->deleteRows($in_ids);
            if ($result) {
                return $this->success(Lang::get('operation successed'), url('/admin/navigation/lists'));
            } else {
                return $this->error(Lang::get('operation failed'));
            }
        }
        return $this->error(Lang::get('incorrect operation'));
    }


    public function toggleisshow() {
        $id = $this->request->param('id', 0);
        $flag = $this->request->param('flag', 0);
        $id = intval($id);
        if ($id > 0) {
            $model = Loader::model('Navigation')->updateRow(['id' => $id, 'stat' => !$flag]);
            if ($model && $model->getData('id')) {
                
                return $this->success(Lang::get('operation successed'), url('/admin/Navigation/lists'));
            } else {
                return $this->error(Lang::get('operation failed'));
            }
        }
        return $this->error(Lang::get('incorrect operation'));
    }
    //获取导航数据
    public function navDataAll($nav_type=''){
        $argc_where = array('pid' => 0, 'is_show' => 1,'nav_type'=>$nav_type);
        $argc_order = array('sort' => 'asc', 'id' => 'asc');
        $argc_field = array('id', 'pid', 'url', 'name', 'stat', 'sort','value','data_type','is_new_window_open');
        $NavOptions = model('Navigation')->getCategoryOptions($argc_where, $argc_order, $argc_field, 100);


        // 获取导航数据
        $data = self::navDataHandling($NavOptions);
        if(!empty($data))
        {
            // 获取子数据
            $items = [];
            $ids = array_column($data, 'id');
            $new_where = array('pid' => $ids, 'is_show' => 1,'nav_type'=>$nav_type);
            
            $itemsOptions = model('Navigation')->getCategoryOptions($new_where, $argc_order, $argc_field, 100);
            $items_data = self::navDataHandling($itemsOptions);
            if(!empty($items_data))
            {
                foreach($items_data as $it)
                {
                    $items[$it['pid']][] = $it;
                }
            }

            // 数据组合
            foreach($data as &$v)
            {
                $v['items'] = isset($items[$v['id']]) ? $items[$v['id']] : [];
            }
        }


        // 没数据则赋空数组值
        if(empty($data))
        {
            $data = [];
        }

        // 缓存
        $this->cacheSet('cache_home_nav_'.$nav_type.'_key', $data, 3600);    
       
        return $data;
        
    }


    //导航数据处理
    public function navDataHandling($data = []){
            
       if(!empty($data) && is_array($data))
        {
            foreach($data as $k=>$v)
            {
                // url处理
                switch($v['data_type'])
                {
                    // 文章
                    case 'article':
                        $v['url'] = url_rewrite('index/article/index', ['id'=>$v['value']]);
                        break;

                    // 博客
                    case 'article':
                        $v['url'] = url_rewrite('index/blog/index', ['id'=>$v['value']]);
                        break;

                    // 自定义页面=>'customview':


                    // 商品分类
                    case 'goods_category':
                        $v['url'] = url_rewrite('index/search/index', ['cid'=>$v['value']]);
                        break;

                }
                $data[$k] = $v;
            }
        }
        return $data;
        
    }

    //获取分类和所有文章
    public function articleCategoryListContent(){
        
        $arg_where = array('pid' => 0, 'isshow' => 1, 'siteid' => $this->siteid, 'country_code' => $this->country_code);
        $arg_order = array('sort' => 'asc', 'id' => 'asc');
        $arg_field = array('id', 'pid', 'haschild', 'name', 'sort', 'isshow', 'recommend', 'picture');
        $data = model('article_category')->getCategoryLists($arg_where, $arg_order, $arg_field, 24);        
        
        if(!empty($data))
        {
            foreach($data as &$v)
            {
                $new_where = array('a.country_code' => $this->country_code);
                $new_order = ['a.createtime' => 'desc', 'a.sort' => 'asc', 'a.id' => 'desc'];
                $new_field = ['a.id,a.cid,a.name,a.sort,a.ishot,a.recommend,a.picture'];
                if ($v['id'] > 0) {
                    $arg_where['a.cid'] = $v['id'];
                }
                $items = model('article')->getArticleLists($new_where, $new_order, $new_field, 12);
                //echo model('article')->getLastSql(); die;
                if(!empty($items))
                {
                    foreach($items as &$vs)
                    {
                        // url
                        $vs['url'] =  url_rewrite('article', array('id' => $vs['id']));
                    }
                }
                $v['items'] = $items;
            }
        }
        return $data;        
    }
    
    //获取博客和所有内容
    public function blogListContent(){
        
        $arg_where = array('stat' => 1, 'siteid' => $this->siteid, 'country_code' => $this->country_code);
        $arg_order = array('public_time' => 'desc', 'id' => 'asc');
        $arg_field = array('id', 'title', 'icon', 'h5_icon', 'is_top', 'public_time', 'add_time');
        $data = model('blog')->getBlotLists($arg_where, $arg_order, $arg_field, 24);        
        
        if(!empty($data))
        {
            foreach($data as &$v)
            {

                $v['url'] =  url_rewrite('blog', array('id' => $v['id']));

            }
        }
        return $data;        
    }
    
    //导航数据处理
    public function NavigationHandle($data=[]){
        
        if(!empty($data) && is_array($data))
        {
            $nav_type_list = config('common_nav_type_list');
            foreach($data as &$v)
            {
                // 数据类型
                $v['data_type_text'] = isset($nav_type_list[$v['data_type']]) ? $nav_type_list[$v['data_type']] : '';

                // 时间
                $v['add_time'] = date('Y-m-d H:i:s', $v['createtime']);
                $v['upd_time'] = empty($v['updtime']) ? '' : date('Y-m-d H:i:s', $v['updtime']);
            }
        }
        return $data;        
        
    }
    
    
    //获取一级导航列表
    public function LevelOneNav($params = [])
    {
        if(empty($params['nav_type']))
        {
            return [];
        }
        $arg_where = array('pid' => 0, 'stat' => 0, 'nav_type' => $params['nav_type'], 'country_code' => $this->country_code);
        $arg_order = array('sort' => 'asc', 'id' => 'asc');
        $arg_field = array('*');
        
        $data = model('navigation')->getNavigationLists($arg_where, $arg_order, $arg_field, 24); 
        return $data;   
        //return $this->json(200, 'ok', $data);
    }  
    
    public function fetchLevelOneNav()
    {
        if ($this->request->isPost()) {
            $nav_type = $this->request->post('nav_type');

            $arg_where = array('pid' => 0, 'stat' => 0, 'nav_type' => $nav_type, 'country_code' => $this->country_code);
            $arg_order = array('sort' => 'asc', 'id' => 'asc');
            $arg_field = array('*');
            
            $data = model('navigation')->getNavigationLists($arg_where, $arg_order, $arg_field, 24); 
            //echo "
=="; print_r($data);die; 
            return $this->json(200, 'ok', $data);
        }
    }  
    
    public function checkType($params = []){
        if ($this->request->isPost()) {
            $nav_type = $this->request->post('nav_type');
            $nav_value = $this->request->post('value');
            
            $value['nav_type']  = $nav_type;        
            $value['value']  = $nav_value;        

            if($nav_type == 'article'){
       
                //获取文章
                $article_list  = self::articleCategoryListContent();        
                $value['list']  = $article_list;        
                
            }elseif($nav_type == 'blog'){
                $value['list']  =  self::blogListContent();                    
            }
            elseif($nav_type == 'goods_category'){
                //获取分类层级目录
                $argc_where = array('isshow' => 1, 'country_code' => $this->country_code);
                $argc_order = array('sort' => 'asc', 'id' => 'asc');
                $argc_field = ['id', 'pid', 'haschild', 'name', 'shortname', 'sort', 'description', 'isshow', 'recommend', 'picture', 'icon', 'image', 'm_icon', 'unique_id'];
                
                $categoryList = Loader::model('product_category')->getList($argc_where,$argc_order, $argc_field);
                
                $value['list']  = $this->list_to_tree($categoryList);                 
            }

            
            $this->assign($value);
            
            Config::set('default_ajax_return', 'html');
            $this->view->engine(['type' => 'php', 'view_suffix' => 'html', 'tpl_replace_string' => [],]);

            return $this->fetch();
        }        
    }
}