<?php
namespace member\controller;
ini_set('max_execution_time', '0');
class Folder extends Member {
/**
 * 106云盘自动发卡平台网盘系统
 * ============================================================================
 * 版权所有 2016-2028 扎兰屯市姝妍计算机服务有限公司，并保留所有权利。
 * 网站地址: http://www.106ym.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 */
    public function index(){
    	$folder =  M("folder")->where(array('reid'=>0,'uid'=>session("user_auth.id")))->select();
    	$this->folder = $folder;
    	$this->display();
    }
    
    public function lists($folder=''){
    	$uid=session("user_auth.id");
    	$spk = array (
		    'pk1' => $uid,
		    'pk2' => '',
	        'pk3' => '',
	        'pk4' => array('type' => 'INF_MAX'),
		);
		$epk = array (
		    'pk1' => $uid,
		    'pk2' => '',
	        'pk3' => '',
	        'pk4' => array('type' => 'INF_MIN'),
		);
		
		$list=O('folder')->select(array(
			'spk'=>$spk,
			'epk'=>$epk,
			'filter'=>array(
				'status'=>array('eq',0),
				'folder'=>array('eq',$folder)
			),
		));
		
		$userconfig=userconfig_get($uid);
		if($userconfig['orderby']==1){
		    $list=array_sort($list,'pk4',SORT_DESC);
		}else if($userconfig['orderby']==2){
		    $list=array_sort($list,'name',SORT_ASC);
		}else if($userconfig['orderby']==3){
		    $list=array_sort($list,'about',SORT_ASC);
		}
		
		$this->ajaxReturn($list);
		
    }
	
	public function lists_recycle($re=''){
    	$uid=session("user_auth.id");
    	$spk = array (
		    'pk1' => $uid,
		    'pk2' => $re,
	        'pk3' => array('type' => 'INF_MAX'),
	        'pk4' => '',
		);
		$epk = array (
		    'pk1' => $uid,
		    'pk2' => $re,
	        'pk3' => array('type' => 'INF_MIN'),
	        'pk4' => '',
		);
		
		$list=O('folder')->select(array(
			'spk'=>$spk,
			'epk'=>$epk,
			'filter'=>array(
				'status'=>array('eq',-1),
			),
		));
		
		$this->ajaxReturn($list);
		
    }
    
    public function add(){
    	$uid=session("user_auth.id");
		$attr=I('attr',array());
		$attr['status']=0;
		$pk=array ( 
	        'pk1' => $uid,
	        'pk2' => '',
	        'pk3' => '',
	        'pk4' => getTime(),
	    );
		$re=O('folder')->add(array(
			'pk'=>$pk,
			'attr'=>$attr,
		));
    	$this->ajaxReturn($re);
    }
    
    public function mod(){
    	$uid=session("user_auth.id");
		$attr=I('attr',array());
		$pk4=I('pk4','');
		$pk=array(
			'pk1'=>$uid,
			'pk2'=>I('pk2',''),
			'pk3'=>I('pk3',''),
			'pk4'=>I('pk4',''),
		);
		$re=O('folder')->save(array(
			'pk'=>$pk,
			'attr'=>$attr,
		));
		if($attr['status']==-1){
		    //删除子文件夹 
	    	$spk = array (
			    'pk1' => $uid,
			    'pk2' => '',
		        'pk3' => '',
		        'pk4' => array('type' => 'INF_MAX'),
			);
			$epk = array (
			    'pk1' => $uid,
			    'pk2' => '',
		        'pk3' => '',
		        'pk4' => array('type' => 'INF_MIN'),
			);
			
			$list_folder=O('folder')->select(array(
				'spk'=>$spk,
				'epk'=>$epk,
				'filter'=>array(
					'status'=>array('eq',0),
					'folder'=>array('eq',$folder),
				),
			));
			foreach ($list_folder as $k => $v) {
				$pids=explode(',', $v['pids']);
				if(in_array($pk4, $pids)){
				    $pk=array(
						'pk1'=>$v['pk1'],
						'pk2'=>$v['pk2'],
						'pk3'=>$v['pk3'],
						'pk4'=>$v['pk4'],
					);
					$attr=array(
						'status'=>-1
					);
					O('folder')->save(array(
						'pk'=>$pk,
						'attr'=>$attr,
					));
				}
			}
			
			//删除子文件
			$spk = array (
			    'pk1' => $uid,
			    'pk2' => '',
		        'pk3' => '',
		        'pk4' => array('type' => 'INF_MAX'),
			);
			$epk = array (
			    'pk1' => $uid,
			    'pk2' => '',
		        'pk3' => '',
		        'pk4' => array('type' => 'INF_MIN'),
			);
			
			$list_file=O('file')->select(array(
				'spk'=>$spk,
				'epk'=>$epk,
				'filter'=>array(
					'status'=>array('eq',0),
				),
			));
			foreach ($list_file as $k => $v) {
				$pids=explode(',', $v['folder_pids']);
				if(in_array($pk4, $pids)){
				    $pk=array(
						'pk1'=>$v['pk1'],
						'pk2'=>$v['pk2'],
						'pk3'=>$v['pk3'],
						'pk4'=>$v['pk4'],
					);
					$attr=array(
						'status'=>-1
					);
					O('file')->save(array(
						'pk'=>$pk,
						'attr'=>$attr,
					));
				}
			}
		}
		
    	$this->ajaxReturn($re);
    	
    }

	public function tree($sid=0,$top=false){
		$uid=session("user_auth.id");
		$userconfig=userconfig_get($uid);
		//文件夹列表
		$list=O('folder')->select(array(
			'spk' => array (
			    'pk1' => $uid,
			    'pk2' => '',
		        'pk3' => '',
		        'pk4' => array('type' => 'INF_MAX'),
			),
			'epk' => array (
			    'pk1' => $uid,
			    'pk2' => '',
		        'pk3' => '',
		        'pk4' => array('type' => 'INF_MIN'),
			),
			'filter'=>array(
				'status'=>array('eq','0'),
			),
		));
		
		
		if($userconfig['orderby']==1){
		    $list=array_sort($list,'pk4',SORT_DESC);
		}else if($userconfig['orderby']==2){
		    $list=array_sort($list,'name',SORT_ASC);
		}else if($userconfig['orderby']==3){
		    $list=array_sort($list,'about',SORT_ASC);
		}
		
		foreach ($list as $k => &$v) {
			$v['id']=$v['pk4'];
			$v['pId']=$v['folder'];
			$v['pid']=$v['folder'];
			$v['url']='';
			$v['target']='_blank';
			$v['open']=true;
		}
		
		$tree=new \Wd\Tree();  
		$tree->init($list); 
		
		$str="<option value=\$id \$selected>\$spacer\$name</option>"; 
		ob_clean(); 
		echo '<select name="attr[folder]">';
		if($top){
		    echo '<option value="">顶级目录</option>';
		}
		echo $tree->get_tree(0, $str,$sid); 
		echo '</select>';
	}
    
	
	public function tongbu(){
		$uid=session("user_auth.id");
		$pk1=trim(I('pk1',''));
		$pk4=trim(I('pk4',''));
		$tongbu_password=I('tongbu_password','');
		$folder=$_REQUEST['attr']['folder'];
		
		if($uid==$pk1){
		    $this->ajaxReturn(array('status'=>0,'info'=>'不能同步自己的文件夹'));
		}
		
		//判断信息是否正确
		$info_folder_tongbu=O('folder')->find(array(
			'pk'=>array(
				'pk1'=>$pk1,
				'pk2'=>'',
				'pk3'=>'',
				'pk4'=>$pk4,
			),
			'filter'=>array(
				'tongbu_password'=>array('eq',$tongbu_password),
				'tongbu'=>array('eq','支持')
			),
		));
		
		if(empty($info_folder_tongbu)){
		    $this->ajaxReturn(array('status'=>0,'info'=>'同步信息不正确'));
		}
		
		
		
		//根据传递的目录获取父级目录
		if(!empty($folder)){
			$info_folder=O('folder')->find(array(
				'pk'=>array(
					'pk1'=>$uid,
					'pk2'=>'',
					'pk3'=>'',
					'pk4'=>$folder,
				)
			));
			if(empty($info_folder['folder_pids'])){
				$folder_pids=$folder;
			}else{
				$folder_pids=$info_folder['folder_pids'].','.$info_folder['pk4'];
			}
		}else{
			$folder_pids='';
		}
		
		//需要复制的目录
		$spk = array (
		    'pk1' => $pk1,
		    'pk2' => '',
		    'pk3' => '',
		    'pk4' => array('type' => 'INF_MAX'),
		);
		$epk = array (
		    'pk1' => $pk1,
		    'pk2' => '',
		    'pk3' => '',
		    'pk4' => array('type' => 'INF_MIN'),
		);
		$list_folder_tongbu=O('folder')->select(array(
			'spk'=>$spk,
			'epk'=>$epk,
			'filter'=>array('status'=>array('eq',0)),
		));
		foreach ($list_folder_tongbu as $k => &$v) {
			if(!in_array($info_folder_tongbu['pk4'], preg_split("/[,]+/s",$v['folder_pids'] ,-1,PREG_SPLIT_NO_EMPTY))){
			    unset($list_folder_tongbu[$k]);
			}
		}
		unset($v);
		
		$info_folder_tongbu['folder']=$folder;
		array_push($list_folder_tongbu,$info_folder_tongbu);
		$add_rows=array();
		$folder_tongbu=array();
		foreach ($list_folder_tongbu as $k => &$v) {
			$v['uid']=$uid;
			$v['folder_pids']=$folder_pids.','.$v['folder_pids'];
			$v['folder_pids']=preg_split("/[,]+/s", $v['folder_pids'],-1,PREG_SPLIT_NO_EMPTY);
			$v['folder_pids']=implode(',', $v['folder_pids']);
			$v['pk1']=$uid;
			$add_rows[$k]=$v;
			array_push($folder_tongbu,$v['pk4']);
//			$pk=array(
//				'pk1'=>$uid,
//				'pk2'=>$v['pk2'],
//				'pk3'=>$v['pk3'],
//				'pk4'=>$v['pk4'],
//			);
//			$attr=$v;
//			unset($attr['pk1']);
//			unset($attr['pk2']);
//			unset($attr['pk3']);
//			unset($attr['pk4']);
//			array_push($add_rows,array('pk'=>$pk,'attr'=>$attr));
		}
		unset($v);
		O('folder')->write(array(
			'add'=>$add_rows,
			'add_condition'=>'IGNORE',
		));
		
		//复制文件
		$spk = array (
		    'pk1' => $pk1,
		    'pk2' => '',
		    'pk3' => '',
		    'pk4' => array('type' => 'INF_MAX'),
		);
		$epk = array (
		    'pk1' => $pk1,
		    'pk2' => '',
		    'pk3' => '',
		    'pk4' => array('type' => 'INF_MIN'),
		);
		
		$list_file_tongbu=O('file')->select(array(
			'spk'=>$spk,
			'epk'=>$epk,
			'filter'=>array('status'=>array('eq',0)),
		));
		$add_rows=array();
		foreach ($list_file_tongbu as $k => &$v) {
			if(!in_array($v['folder'], $folder_tongbu)){
			    unset($list_file_tongbu[$k]);
				continue;
			}else{
				$v['uid']=$uid;
				$v['folder_pids']=$folder_pids.','.$v['folder_pids'];
				$v['folder_pids']=preg_split("/[,]+/s", $v['folder_pids'],-1,PREG_SPLIT_NO_EMPTY);
				$v['folder_pids']=implode(',', $v['folder_pids']);
			}
			$v['pk1']=$uid;
			$add_rows[$k]=$v; 
//			$pk=array(
//				'pk1'=>$uid,
//				'pk2'=>$v['pk2'],
//				'pk3'=>$v['pk3'],
//				'pk4'=>$v['pk4'],
//			);
//			$attr=$v;
//			unset($attr['pk1']);
//			unset($attr['pk2']);
//			unset($attr['pk3']);
//			unset($attr['pk4']);
//			array_push($add_rows,array('pk'=>$pk,'attr'=>$attr));
		}
		unset($v);
		$re=O('file')->write(array(
			'add'=>$add_rows,
			'add_condition'=>'IGNORE',
		));
		
		$this->ajaxReturn(array('status'=>1));
	}
}