<?php
	
	if(!defined('VI_BASE')) {
		ob_clean();
		exit('Access Denied');
	}
	
	/////////////////////////////////
	
	$submit = getgpc('submit');
	$smarty->assign("submit", $submit );
	
	if( $submit ){
	
		// || !$_SESSION['Member']['modify']
		if( !$error && $config['USER_MODE'] == "REG" && !$_SESSION['Member']['id'] ){		
			$error = "login";		
		}
		
		if( !$error && $_CACHE[$appid]['form']["start"]>time() || !$_CACHE[$appid]['form']['state'] ){		
			$error = "state";		
		}
		
		if( !$error && $_CACHE[$appid]['form']["expire"]<time() ){		
			$error = "expire";		
		}
		
		//没有错误
		if( $error == '' ){
		
			$name = getgpc('name');
			$quote = getgpc('quote');
			$description = getgpc('description');
			
			//var_dump( $_FILES['image'] );
			//exit;
		
			//遍历选项组
			foreach( $_CACHE['vote']['group'] as $gid => $group ){
			
				//记录用户提交数据
				//ACCOUNT	姓名	PHONE	电话号码	IDCARD	身份证	EMAIL	电子邮箱
				$data = format_json( fix_json( $_POST["config"] ) );
				
				//上传文件
				$file = $_FILES['image'];
				if( $file["name"][$gid] && !$file['error'][$gid] ){
							
					//检查上传文件
					$result = uploader_check( $file["name"][$gid] );
					
					//子文件夹
					$folder = VI_ROOT.$_G['project']['upload'].$result['type'];
					
					//文件大小
					$size = $file["size"][$gid];
					
					//临时名称
					$tmp = $file["tmp_name"][$gid];
				
					//如果是图片
					if( $result['type']=="image" ){					
						list($width, $height, $image_type, $attr) = getimagesize($tmp);						
					}else{
						$width = $height = 0;	
					}
		
					//移动上传文件							
					$absval = uploader_save( $folder, $tmp, $file["name"][$gid], $result["extra"] );
					
					//转换成相对路径
					$attach = url_merge( str_replace( VI_ROOT, VI_BASE, $absval ) );
					
				}
				
				$sql="INSERT INTO `mod:option`(fid,gid,name,image,quote,config,state,stat,dateline) values('".$fid."',".$gid.",'".$name[$gid]."','".$attach."','".$quote[$gid]."','".$data."', -1, 0 ,".time().")";
				
				System :: $db -> execute( $sql );
				
			}		
			
			//发邮件
			/////////////////////////////////
			
			if( $config["FORM_NOTICE"] == "Y" ){
				
				//创建者邮件地址
				$address = $_CACHE['system']['admin'][ $_CACHE[$appid]['form']["aid"] ]["email"];	
				
				if( $address ){
				
					$content = str_replace( array('{TIME}','{USER}','{EVENT}','{URL}','{VERYIDE}'),
								array( date('Y-m-d H:i:s') ,$_CACHE[$appid]['form']["account"], $_CACHE[$appid]['form']["name"], Module::get_index($appid)."?id=".$fid."&ref=email", VI_HOST ) ,$_G['setting']['passport']["event"] );
					
					if( $address ){
					
						System :: sendmail( $_CACHE[$appid]['form']["name"] ,$address,$config["FORM_SEND"], $content );
					
					}
				
				}
			
			}
			
			/////////////////////////////////
			
			$error = "";
			
		}
		
		$smarty->assign("error", $error );		
		
	}else{
		
		
	}
	
	
	
?>