<?php
/*
	[Office 515158] (C) 2009-2015 天生创想 Inc.
	$Id: mod_document mobile $
*/
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
empty($do) && $do = 'list';
$type = getGP('type','G','int');
$father = getGP('father','G','int');
if($father=='') $father=0;
if($type==''){
	$type=1;
}
if($type==1){
	$type_name='个人文件柜';	
}elseif($type==2){
	$type_name='公共文件柜';	
}elseif($type==3){
	$type_name='网络硬盘';	
}elseif($type==4){
	$type_name='下载管理';	
}elseif($type==5){
	$type_name='规章制度';	
}elseif($type==6){
	$type_name='报表管理';	
}
if ($do == 'list') {
	if ($type=='1'){
		$documentnum = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."document_type where father='".$father."' and uid=".$_USER->id." and type='".$type."' order by id asc");
		$documentsql = "SELECT * FROM ".DB_TABLEPRE."document_type where father='".$father."' and uid=".$_USER->id." and type='".$type."' order by id asc";
	}else{
		$documentnum = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."document_type where father='".$father."' and type='".$type."' order by id asc");
		$documentsql = "SELECT * FROM ".DB_TABLEPRE."document_type where father='".$father."' and type='".$type."' order by id asc";
	}
	$documenttype = $db->fetch_all($documentsql);
	$wheresql .= " AND documentid ='".$father."'";
	if(!is_superadmin()){
		$wheresql .= "  and (readuser LIKE'%".get_realname($_USER->id)."%' or uid='".$_USER->id."' or readuser='全体人员')";
	}
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."document WHERE 1 $wheresql and type = '".$type."' ORDER BY id desc");
    $sql = "SELECT * FROM ".DB_TABLEPRE."document WHERE 1 $wheresql and type = '".$type."' ORDER BY id desc LIMIT 100";
	$result = $db->fetch_all($sql);
	include_once('template/document.php');

}elseif ($do == 'views') {
	$id = getGP('id','G','int');
	if($id!=''){
		$row = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."document  WHERE id = '".$id."'");
		$filetype=preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$row['title']);
	}
	include_once('template/document_view.php');
}elseif ($do == 'add') {
	$view = getGP('view','G');
	$id = getGP('id','G','int');
	if($view=='save'){
		$document=array();
		if($id!='0' && $id!=''){
			$document['title'] = unescape(getGP('title','G')).'.'.getGP('filetype','G');
			$row = $db->fetch_one_array("SELECT id FROM ".DB_TABLEPRE."document  WHERE title = '".getGP('title','G')."' and id!='".$id."' and type='".$type."'");
			if($row['id']!=''){
				echo 'false';
				exit;
			}
			update_db('document',$document, array('id' => $id));
		}else{
			$row = $db->fetch_one_array("SELECT id FROM ".DB_TABLEPRE."document  WHERE title = '".getGP('title','G')."' and type='".$type."'");
			if($row['id']!=''){
				echo 'false';
				exit;
			}
			$document['title'] = unescape(getGP('title','G'));
			$document['date'] = get_date('Y-m-d H:i:s',PHP_TIME);
			$document['content'] = trim(getGP('content','G'));
			$document['annex'] = trim(getGP('annex','G'));
			$document['documentid'] = trim(getGP('documentid','G'));
			if($type==1){
				$document['key'] = 1;
				$document['readuser'] = '自己';
			}else{
				$document['key'] = 0;
				$document['readuser'] = '全体人员';
			}
			$document['uid'] = $_USER->id;
			$document['type'] = $type;
			insert_db('document',$document);
		}
		echo 'true';
		exit;
	}
	include_once('template/document_add.php');

}
?>