<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
*/

class control extends oabase {
    
    public function action_avatar() {
		$id = $_REQUEST['id'];
		if (!empty($id)) {
                   
                        $model = parent::model('user', 'oa');
                        $vo = $model -> get_top_menu_data($id);
                         $var_array = array(
                                  'id'=>$id,
                                   'pic'=>$vo['pic'],
                            );
                unset($model);
                TPL::assign($var_array);
	   
			//$this -> assign("id", $id);
			//$this -> assign("pic", M("User") -> where("id=$id") -> getField('pic'));
		}
                     TPL::display($this->cptpl.'popup/avatar.tpl');
		//$this -> display();
	}
        
        public function action_upload(){
            
		$files = $_FILES;
		//print_r($files);die;
		      $saveurl = "";
                    $tmp_name=str_replace('\\\\', '\\', $_FILES['file']['tmp_name']);

                    $str =$files['file']['name'];
                     $arr = explode('.', $str);
                    $num = count($arr) - 1;
                    $type = $arr[$num];

                    $filename = date("YmdHis") . rand(0000, 9999) . "." . $type;         
                    $destination_folder = 'data/attachment/kefu/' . date('Ym') . '/'; //上传文件路径
                    if (!file_exists($destination_folder)) {
                        mkdir($destination_folder);
                    }
                    $destination = $destination_folder.$filename;

                    $PHP_SELF=$_SERVER['PHP_SELF'];

                    $url='http://'.$_SERVER['HTTP_HOST'].substr($PHP_SELF,0,strrpos($PHP_SELF,'/')+1);
                    if(move_uploaded_file($tmp_name,$destination)){
                        $data['status']=1;
                        $data['info']='上传成功';
                        $data['name']=$url.$destination;
                        $data['name1']=$destination;
                        $data['name2']=$filename;
                        $data['size']=$this->sizecount($_FILES['file']['size']);
                         $uloadfile['name']=$_FILES['file']['name'];
                         $uloadfile['savename']=$filename;
                         $uloadfile['savepath']=$destination_folder;
                         $uloadfile['ext']=$type;
                         $uloadfile['mime']=$type;
                         $uloadfile['size']=$_FILES['file']['size'];
                         $uloadfile['md5']= md5($filename);  
                         $uloadfile['sha1']= sha1($filename);
                         $uloadfile['location']='';
                         $uloadfile['url']='';
                         $uloadfile['create_time']= time();
                         $model = parent::model('public', 'oa');
                         $model->addfile($uloadfile);
                         echo json_encode($data);
                    } else {

                    }
		
        }
              public function sizecount($filesize) {
                if($filesize >= 1073741824) {
                 $filesize = round($filesize / 1073741824 * 100) / 100 . ' gb';
                } elseif($filesize >= 1048576) {
                 $filesize = round($filesize / 1048576 * 100) / 100 . ' mb';
                } elseif($filesize >= 1024) {
                 $filesize = round($filesize / 1024 * 100) / 100 . ' kb';
                } else {
                 $filesize = $filesize . ' bytes';
                }
                return $filesize;
       }
        
        public function action_actor() {
                $dept = parent::model('dept', 'oa');
		$list = array();
		$list = $dept ->get_menu_list();
                $publicmodel=parent::model('public', 'oa');
		$tree = $publicmodel->list_to_tree($list);
                
                $Position=parent::model('position', 'oa');
                $list1 = $Position ->get_menu_list();
                $tree1 = $publicmodel->list_to_tree($list1);
                $var_array = array(
                    'list_dept'=>$publicmodel->popup_tree_menu($tree),
                    'list_position'=>$publicmodel->popup_tree_menu($tree1),
                    'type'=>'dept'
                  );     
               TPL::assign($var_array);
//		$plugin['jquery-ui'] = true;
//		$this -> assign("plugin", $plugin);
//
//		$this -> _dept_list();
//		$this -> _position_list();
//		$this -> _group_list();
//
//		$this -> assign('type', );
                 TPL::display($this->cptpl.'popup/actor.tpl');
	}
                public function action_message() {
                $dept = parent::model('dept', 'oa');
		$list = array();
		$list = $dept ->get_menu_list();
                $publicmodel=parent::model('public', 'oa');
		$tree = $publicmodel->list_to_tree($list);
                
                $Position=parent::model('position', 'oa');
                $list1 = $Position ->get_menu_list();
                $tree1 = $publicmodel->list_to_tree($list1);
                $var_array = array(
                    'list_dept'=>$publicmodel->popup_tree_menu($tree),
                    'list_position'=>$publicmodel->popup_tree_menu($tree1),
                    'type'=>'dept'
                  );     

               TPL::assign($var_array);
//		$plugin['jquery-ui'] = true;
//		$this -> assign("plugin", $plugin);
//
//		$this -> _dept_list();
//		$this -> _position_list();
//		$this -> _group_list();
//
//		$this -> assign('type', );
                 TPL::display($this->cptpl.'popup/message.tpl');
	}
        public 	function action_read() {
                $publicmodel=parent::model('public', 'oa');
		$type = $_REQUEST['type'];
                $id =$_REQUEST['id'];
		switch ($type) {
			case "dept" :
				$model = parent::model('dept', 'oa');
				$dept = $publicmodel->tree_to_list($publicmodel->list_to_tree($model ->get_menu_list(), $id));
				$dept = $publicmodel->rotate($dept);
                                if(empty($dept)){
                                    $dept = "$id";  
                                } else {
                                    $dept = implode(",", $dept['id']) . ",$id";  
                                }
				$data = $model -> popup($dept);
				break;
			case "group" :
				$user_list = D("Group") -> get_user_list($id);

				$model = D("UserView");
				if (!empty($user_list)) {
					$where['id'] = array('in', $user_list);
					$where['is_del'] = array('eq', 0);
				} else {
					$where['_string'] = '1=2';
				}

				$data = $model -> where($where) -> select();
				break;

			case "position" :
                                $model = parent::model('position', 'oa');
                                $data = $model -> popup($id);

				break;

			case "personal" :
				$model = D("UserTag");
				if ($id == "#") {
					$data = $model -> get_data_list("Contact");
					$data = rotate($data);
					$data = $data['row_id'];
					if (!empty($data)) {
						$where['id'] = array('not in', implode(",", $data));
					} else {
						$where['_string'] = '1=2';
					}
				} else {
					$test = $model;
					$data = $model -> get_data_list("Contact", $id);
					$data = rotate($data);

					$data = $data['row_id'];

					if (!empty($data)) {
						$where['id'] = array('in', implode(",", $data));
					} else {
						$where['_string'] = '1=2';
					}
				}
				$model = M("Contact");
				$where['is_del'] = array('eq', 0);
				$data = $model -> where($where) -> field('id,name,position as position_name,email') -> select();
				break;
			default :
		}
		$new = array();
		$return['status'] = 1;
		$return['data'] = $data;
		echo json_encode($return);
	}
        
        public function action_json(){
            	header("Content-Type:text/html; charset=utf-8");
		$type = $_REQUEST['type'];
		$key = $_REQUEST['key'];
       
                $searchsql = " name LIKE '%".$key."%' OR letter LIKE '%".$key."%' OR email LIKE '%".$key."%'";
		$model = parent::model('user', 'oa');
		$dept = $model -> get_List($searchsql);

		if ($type == "all") {
			$where = array();
			$map = array();
			$model = M("Contact");
			$where['name'] = array('like', "%" . $key . "%");
			$where['letter'] = array('like', "%" . $key . "%");
			$where['email'] = array('like', "%" . $key . "%");
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
			$map['email'] = array('neq', '');
			$map['user_id'] = array('eq', get_user_id());
			$personal = $model -> where($map) -> field('id,name,email') -> select();
		}
		if (empty($dept)) {
			$dept = array();
		}
		if (empty($personal)) {
			$personal = array();
		}
		$contact = array_merge_recursive($dept, $personal);
		exit(json_encode($contact));
        }
    
}
?>
