<?php
// +----------------------------------------------------------------------
// | WindSNS 国内领先企业级社交开源SNS框架系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://windsns.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 常州微诺信息科技有限公司 http://www.weinuo.net
// +----------------------------------------------------------------------
// | E-mail: open@weinuo.net
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\common\controller\Backend;
use fast\Tree;

/**
 * 分类管理
 *
 * @icon fa fa-list
 * @remark 用于统一管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
 */
class Links extends Backend
{


    public function links_list()
    {
        $linkslist = Db::name('links') -> alias('a')->join('ws_links_type w','a.links_type = w.links_typeid') -> order('linksid', 'desc')->paginate(30); //友情链接列表
        $page = $linkslist->render();
    	$this->assign('linkslist', $linkslist);
        $this->assign('page', $page);
        return $this->view->fetch('links_list');
    }



    public function links_typelist($linkstypesid)
    {
    	
    	$urllinkstypesid  =  input('get.linkstypesid'); 
        $linkslist = Db::name('links') -> alias('a') -> join('ws_links_type w','a.links_type = w.links_typeid') -> where(['links_type'=>$urllinkstypesid]) -> order('linksid', 'desc')->paginate(30); //列表
        $page = $linkslist->render();
    	$this->assign('linkslist', $linkslist);
        $this->assign('page', $page);
        return $this->view->fetch('links_typelist');

    }   



    public function links_add()
    {
    	
     $linkstype = Db::name('links_type')->order('links_typeid', 'desc')->select(); 
     $this->assign('linkstype', $linkstype);	
     return $this->view->fetch('links_add');
      
    }
   
   

    public function addlinksdo()
    {
        $time = time();
		$paytimess = date('Y-m-d H:i:s',$time);
	    $thistimes = strtotime($paytimess);
	    $kinksadd['links_weblittle'] =  input('post.links_weblittle'); 
	    $kinksadd['links_url'] =  input('post.links_url'); 
	    $thisweburl = input('post.links_url'); 
	    $kinksadd['links_addtimes'] =  $thistimes;    
	    $kinksadd['links_type'] =  input('post.links_type'); 
	    $kinksadd['links_state'] =  input('post.links_state'); 

        $sqllittle  = Db::name('links')->where(['links_url'=>$thisweburl])->find();
		if($sqllittle){
			return $this->view->fetch('links_addno');
			exit;
		}elseif(false === strpos($kinksadd['links_url'],'http://' ) && false === strpos($kinksadd['links_url'],'https://' )                ){
					
			
					 $kinksadd['links_url'] = 'http://'.$kinksadd['links_url'];
					 $wedid = Db::name('links')->insert($kinksadd);
					 return $this->view->fetch('links_addok');
				
			}else{

		 $wedid = Db::name('links')->insert($kinksadd);
		 return $this->view->fetch('links_addok');
		}

    }
   

   

    public function links_worldnulldo()
    {
	    $urlinksid  =  input('get.linksid'); 
	    $data['links_state'] =  2; 
	    $xiugaizhuangtai = Db::name('links')->where('linksid', $urlinksid)->update($data);// 
	    return  $this->fetch('links_listdook');
    }

   

    public function links_worldokdo()
    {
    	
    $urlinksid  =  input('get.linksid'); 
    $data['links_state'] =  1; 
    $xiugaizhuangtai = Db::name('links')->where('linksid', $urlinksid)->update($data);// 
    return  $this->fetch('links_listdook');

    }  
   
   
   

   
    public function links_edit($linksid)
    {
		$urlinksid  =  input('get.linksid'); 
		$linksinfos = Db::name('links') -> where('linksid',$urlinksid)->alias('a') -> join('ws_links_type w','a.links_type = w.links_typeid') -> select();//查询这个链接所有信息
	    $this->assign('linksinfos', $linksinfos);
	    
	    $linkstype = Db::name('links_type')->order('links_typeid', 'desc')->select(); 
	    $this->assign('linkstype', $linkstype);
	    return $this->view->fetch('links_edit');
    }
   
   
   

    public function edilinksdo($linkid)
    {
    	$urllinksid  =  input('get.linkid'); 
        $kinksadd['links_weblittle'] =  input('post.links_weblittle'); 
	    $kinksadd['links_url'] =  input('post.links_url'); 
	    $thisweburl = input('post.links_url');      
	    $kinksadd['links_type'] =  input('post.links_type'); 
	    $kinksadd['links_state'] =  input('post.links_state');

	    $sqllittle  = Db::name('links')->where(['links_url'=>$thisweburl])->find();
		if($sqllittle){
			return $this->view->fetch('links_addno');
			exit;
		}elseif(false === strpos($kinksadd['links_url'],'http://' ) && false === strpos($kinksadd['links_url'],'https://' )                ){
					
			
			$kinksadd['links_url'] = 'http://'.$kinksadd['links_url'];
			$xiugaizhuangtai = Db::name('links')->where('linksid', $urllinksid)->update($kinksadd);// 
            return  $this->fetch('links_listdook');
				
		}else{

        $xiugaizhuangtai = Db::name('links')->where('linksid', $urllinksid)->update($kinksadd);// 
        return  $this->fetch('links_listdook');
		}

    }  
   

    public function links_del($linksid) 
    {
         $urllinksid  =  input('get.linksid'); 
         $shanchu = Db::name('links')->where('linksid',$urllinksid)->delete();
         if($shanchu){
            return $this->view->fetch('links_delok');
         }else{
          	return $this->view->fetch('links_delno');
			exit;
         }
   }
   
   
  
    public function links_type()
    {

		$linksypeinfos = Db::name('links_type')->order('links_typeid', 'desc')->paginate(20); 
		$page = $linksypeinfos->render();
		$this->assign('linksypeinfos', $linksypeinfos);
		$this->assign('page', $page);
		return $this->view->fetch('links_type');
      
    }   
   
   
   
   
   
   

     public function addlinkstype()
    {
      $time = time();
	  $paytimess = date('Y-m-d H:i:s',$time);
	  $thistimes = strtotime($paytimess);
	  
	  $links_typenames = $this->request->request('links_typenames');
      $linkstypeadd['links_typenames'] =  input('post.links_typenames'); 
      $thislinksnames = input('post.links_typenames'); 
      $linkstypeadd['links_typestate'] =  input('post.links_typestate'); 
      $linkstypeadd['links_typetimes'] =  $thistimes; 

      $sqlnames  = Db::name('links_type')->where(['links_typenames'=>$thislinksnames])->find();

		if(!$links_typenames){
         	return $this->view->fetch('links_addtypenoa');
         	exit;
        }elseif($sqlnames){
			return $this->view->fetch('links_addtypeno');
			exit;
		}else{

		 $wedid = Db::name('links_type')->insert($linkstypeadd);
		 return $this->view->fetch('links_addtypeok');
		}  
      

    }  
   
   
   

   

    public function links_typeup($linksypeid)
    {
      $appstid =   Request::instance()->request('linksypeid');
      $linkstypenames['links_typenames'] =  input('post.links_typenames'); 
      $thislinksnames = input('post.links_typenames'); 
      $sqlnames  = Db::name('links_type')->where(['links_typenames'=>$thislinksnames])->find();
      
      if(!$linkstypenames['links_typenames']){
         	return $this->view->fetch('links_addtypenoa');
         	exit;
      }elseif($sqlnames){
			return $this->view->fetch('links_addtypeno');
			exit;
		}else{
      	    $xiugaizhuangtai = Db::name('links_type')->where('links_typeid', $appstid)->update($linkstypenames);// 
            return  $this->fetch('links_typedook');
      }

    }
   
   
   
      public function links_typedel($linksypeid) 
    {
        $urllinksypeid  =  input('get.linksypeid'); 
        $typewoldsum = Db::name('links')->where(['links_type' => $urllinksypeid])->count();
         
        if($typewoldsum > 1 or $typewoldsum == 1){
            return $this->view->fetch('links_deltypenoa');
			exit;
        }else{
         	
         	$shanchu = Db::name('links_type')->where('links_typeid',$urllinksypeid)->delete();
 	        if($shanchu){
	            return $this->view->fetch('links_addtypeok');
	        }else{
	          	return $this->view->fetch('links_delno');
				exit;
	        }

        }


   }
   
   
   
     
    public function links_typenulldo()
    {
    	
    $urllinksypeid  =  input('get.linksypeid'); 
    $data['links_typestate'] =  2;
    $xiugaizhuangtai = Db::name('links_type')->where('links_typeid', $urllinksypeid)->update($data);// 
    return  $this->fetch('links_typedook');

    }

   

    public function links_typeokdo()
    {
    	
    $urllinksypeid  =  input('get.linksypeid');
    $data['links_typestate'] =  1; 
    $xiugaizhuangtai = Db::name('links_type')->where('links_typeid', $urllinksypeid)->update($data);// 
    return  $this->fetch('links_typedook');

    }  
   
   
   
   
   
}
