<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: ,v1 10:39 2008-7-27 Leilei $
*/

require("../../include/header.php");
html_start("擂台编辑 - VeryIDE");
?>

<!--body start-->
	<?php

	//module
    require("naver.php");
    
	//mysql open
	reader_mysql();
	
	$jump = GetQuery("jump");
	$action = GetQuery("action");
	$fid = GetNumber("fid",0);
	$gid = GetNumber("gid",0);
	
	if ($action){

		$name = GetQuery("name");
		$status = GetNumber("status",0);
		$tags = GetQuery("tags");
		$skin = GetQuery("skin");
		$thumb = GetQuery("thumb");
		$description = GetQuery("description");
		$quote = GetQuery("quote");
		$category = GetQuery("category");
		
		//配置
		$config = $_POST["config"];
		$config = format_json( fix_json( $config ) );
		
		//删除空白字符
		$config = preg_replace("/([\r\n\t\f])/"," ",$config);		
		
		//开始时间
		$start = GetQuery("start");
		$start_time = GetQuery("start_time");
		
		if( !$start || !isDateTime($start." ".$start_time) ){
			$start = time();				
		}else{				
			$start = strtotime($start." ".$start_time);
		}
		
		//结束时间
		$expire = GetQuery("expire");
		$expire_time = GetQuery("expire_time");
		
		if( !$expire || !isDateTime($expire." ".$expire_time) ){
			$expire = time();
		}else{
			$expire = strtotime($expire." ".$expire_time);
		}
		
		switch($action){
			case "add":
			
				//检查权限
				checker_func("mod-pk-add");		
			
				$sql="insert into ".VI_DBMODPRE."forms(name,aid,account,status,description,tags,dateline,start,expire,skin,quote,thumb,appid,config,ip,stat,category) values('".$name."',".$_SESSION["Manager"]["id"].",'".$_SESSION["Manager"]["account"]."',".$status.",'".$description."','".$tags."',".time().",".$start.",".$expire.",'".$skin."','".$quote."','".$thumb."','".$appid."','".$config."','".GetIP()."',0,'".$category."')";
				
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				$fid = mysql_insert_id($mysql);
				
				//错误处理
				if (!$fid) {
					trigger_error($sql, E_USER_ERROR);
					exit();
				}
				
				//生成缓存
				cache_rows($appid,$mysql,VI_DBMODPRE."forms","select * from ".VI_DBMODPRE."forms where id=".$fid);
				
				reader_redirect($appid.".list.php?mark=".$id,"信息提交成功!");
				exit();
				
			break;
				
			case "update":
			
				//检查权限
				checker_func("mod-pk-mod");		
			
				$sql="update ".VI_DBMODPRE."forms set name='".$name."',status=".$status.",description='".$description."',tags='".$tags."',skin='".$skin."',quote='".$quote."',thumb='".$thumb."',config='".$config."',start=".$start.",expire=".$expire.",category='".$category."',modify=".time().",mender='".$_SESSION["Manager"]["account"]."' where id=".$fid;
				mysql_query($sql);
				
				$Project["queries"]++;
				
				//生成缓存
				cache_rows($appid,$mysql,VI_DBMODPRE."forms","select * from ".VI_DBMODPRE."forms where id=".$fid);
				
				reader_redirect($jump,"信息修改成功!");
				exit();
			
			break;
			
			case "edit";
			
				//检查权限
				//checker_func("mod-pk-mod");		
			
				$sql="select * from ".VI_DBMODPRE."forms where id=".$fid;
				$result=mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				if(mysql_num_rows($result)==1){
					$row=mysql_fetch_assoc($result);
	
					$config = fix_json($row["config"]);	
					
				}
				
				//var_dump($config);
			
			break;
		}
		
	}
	  
	//mysql close
	reader_mysql(true);
    
    ?>
    
 	<?php    
    echo loader_script(array(VI_BASE."js/mo.calendar.js"),"utf-8",$Project["version"]);    
    ?>
    
<!--main-->

	<?php
	
	//显示权限状态
	switch($action){
		case "":
			echo checker_func("mod-pk-add",true);
		break;
		
		case "edit":
			echo checker_func("mod-pk-mod",true);
		break;
	}
	
	?>

	<div id="box">
	    <form action="" method="post" name="from1" mode="edit" verify="true">
	    
			<textarea name="config" cols="50" rows="6" id="config" style="display:none;"><?php echo $row["config"];?></textarea>
                        
            <table cellpadding="0" cellspacing="0" class="form">
        
				<tr><td colspan="2" class="section"><strong>基本信息</strong></td></tr>

                <tr text='按内容性质进行分类归档'>
                    <td width="60"><strong>内容分类：</strong></td>
                    <td>
                        <input type="text" name="category" id="category" class="text digi" fix_name="内容分类" fix_null="yes" onclick="Mo.Soler( this, event, this.value, 'mo-soler', { '内容分类' : ['<?php echo implode("','",explode(" ",trim($Global["category"])));?>'] }, function( value ){ this.value = value; }, 1 , 23 );" readonly="true" value="<?php echo $row["category"];?>" onfocus="this.blur();" />
                    </td>
                </tr>
        
                <tr>
                    <td width="60"><strong>表单主题：</strong></td>
                    <td>
                    	<input name="name" type="text" class="text" id="name" value="<?php echo $row["name"];?>" size="60" fix_name="表单主题" fix_null="yes" />
                        <select name="skin" id="skin" style=" width:120px;">
                        <?php
                        //遍历皮肤目录
                        $root = "content/theme/";
                        
                        if ($handle = opendir($root)) {
                            while (false !== ($file = readdir($handle))) {
                                if  (($file  !=   ".") && ($file   !=   ".."))   {
                                    $name = getcssname($root.$file."/style.css");
                                    echo '<option value="'.$file.'">'.($name?$name:$file).'</option>';							
                                    $i++;
                                }
                            }
                            closedir($handle);
                        }
                        ?>
                        </select>
                    
                        <script type="text/javascript">
						
						Mo("#skin").bind( 'change', function( ele, index, event ){
							
							Mo("#preview").show().html( '<img src="content/theme/'+ Mo(this).value() +'/preview.jpg" onerror="this.src=\'<?php echo VI_BASE;?>image/none.gif\'" />' );							
						
						});						
						
                        Mo.Tools("Help","","<strong>添加新风格</strong> <br />将风格文件夹存放至 <span class=text-key><?php echo $Modules[$appid]["path"];?>theme/</span> 下，系统会自动识别。<br />风格文件夹中的 <span class=text-key>preview.jpg</span> 将作为此风格的预览图使用。");
                        </script>
                    
                    </td>
                    
                </tr>
            
                <tr>
                    <td><strong>简要介绍：</strong></td>
                    <td>
                    <script type="text/javascript">
					Mo.UBB.show({
						id:"description",
						toolbar:["font","size","color","bold","italic","under","link","image","flash","zoom","zoomin","zoomout","html","stat","about"]							
					});
                    </script>
                    <textarea name="description" cols="50" rows="9" id="description" style="float:left;"><?php echo $row["description"];?></textarea>
                    <div id="preview" onclick="var s=new getSelect('skin').value;if(s){window.open('content/?id=<?php echo $row["id"];?>&skin='+s,'skin');}" title="预览此风格"></div>
                    
                    <div style="clear:both;"></div>
                    <script type="text/javascript">
                        new Serv.Upload("<?php echo $appid;?>","<?php echo $row["file"];?>",{'format':['<?php echo implode("','",$Upload["image"]);?>','<?php echo implode("','",$Upload["flash"]);?>'],'again':true,'recovery':true,'input':false,'ubb':"description"});
                    </script>
                    </td>
                    
                </tr>
            
                <tr text='表单个性化功能'>
                    <td><strong>特殊标记</strong></td>
                    <td>
                        
                        <a href="javascript:;"  onclick="setValue('description','$COUNT','insert');" >统计人数</a>
                        <a href="javascript:;"  onclick="setValue('description','$VIEW','insert');" >点击数</a>
                        |
                        <a href="javascript:;"  onclick="setValue('description','$CLOCK','insert');" >倒计时</a>
                        <a href="javascript:;"  onclick="setValue('description','$SEARCH','insert');" >用户列表</a>
                        <a href="javascript:;"  onclick="setValue('description','[header=宽度,高度,样式(CSS)]Flash地址[/header]','insert');" >插入Flash</a>
                        
                        <script type="text/javascript">
                        Mo.Tools("Tip","<strong>统计人数</strong> 将会在页面上实时显示参于本活动的人数<br /><strong>点 击 数</strong> 将会在页面上动态显示本活动被点击次数<br /><strong>倒 计 时</strong> 将会在页面上动态显示本活动倒数记时器<br /><strong>用户列表</strong> 在页面上显示参与本活动用户的页面链接<br /><strong>隐藏统计</strong> 根据需要可以将统计按钮隐藏起来<br /><strong>隐藏标题</strong> 根据需要可以将表单标题进行隐藏<br /><strong>隐藏编号</strong> 根据需要可以将表单选项前的自动编号进行隐藏");
                        </script>
                        
                    </td>                    
                </tr>
            
                <tr text='表单自动关闭时间'>
                    <td><strong>起止时间：</strong></td>
                    <td>
                        <input name="start" type="text" class="text date" value="<?php echo date("Y-m-d",($row["start"]?$row["start"]:time()));?>" size="12" readonly="true" onclick="return showCalendar(this,'y-mm-dd');" title="年-月-日">
                        
                        <input type="text" name="start_time" title="小时:分钟" class="text time" onclick="Mo.Timer(this,this.value);" readonly="true" value="<?php echo $row["start"] ? date("H:i",$row["start"]) : "00:00";?>" />
                        
                        -
                        <input name="expire" type="text" class="text date" value="<?php echo date("Y-m-d",($row["expire"]?$row["expire"]:time()+604800));?>" size="12" readonly="true" onclick="return showCalendar(this,'y-mm-dd');" title="年-月-日">
                        
                        <input type="text" name="expire_time" title="小时:分钟" class="text time" onclick="Mo.Timer(this,this.value);" readonly="true" value="<?php echo $row["expire"] ? date("H:i",$row["expire"]) : "23:59";?>" />
                    </td>
                </tr>

                <tr>
                    <td><strong>表单状态：</strong></td>
                    <td>
                        <label>
                        <input type="radio" class="radio" name="status" value="1" checked> 
                        开放表单
                        </label>
                        <label>
                        <input type="radio" class="radio" name="status" value="0">
                        关闭表单
                        </label>
                    </td>
                    
                </tr>
 				
                <tr>
                    <td><strong>预览图片：</strong></td>
                    <td>
                    <script type="text/javascript">
                    new Serv.Upload("thumb","<?php echo $row["thumb"];?>",{'format':['<?php echo implode("','",$Upload["image"]);?>'],'again':true,'recovery':true,'input':true});
                    </script>
                    </td>
                </tr>
                
               
				<tr><td colspan="2" class="section"><strong>正方与反方</strong></td></tr>
             
                <tr>
                    <td><strong>正方名称：</strong></td>
                    <td>
                        <input name="config[POSITIVE]" type="text" size="13" class="text" value="<?php echo $config["POSITIVE"];?>" />
                    </td>
                </tr>
            				
                <tr>
                    <td><strong>正方观点：</strong></td>
                    <td>
						<script type="text/javascript">
                            Mo.UBB.show({id:"POSITIVE_POINT",toolbar:["font","size","color","bold","italic","under","link","image","flash","zoom","zoomin","zoomout","help","html","stat"]});
                        </script>
                        <textarea name="config[POSITIVE_POINT]" cols="50" rows="4" id="POSITIVE_POINT" fix_name="正方观点" fix_null="yes"><?php echo $config["POSITIVE_POINT"];?></textarea>
                    </td>
                    
                </tr>
            				
                <tr>
                    <td><strong>反方名称：</strong></td>
                    <td>
                        <input name="config[NEGATIVE]" type="text" size="13" class="text" value="<?php echo $config["NEGATIVE"];?>" />
                    </td>
                </tr>
            
                <tr>
                    <td><strong>反方观点：</strong></td>
                    <td>
						<script type="text/javascript">
                            Mo.UBB.show({id:"NEGATIVE_POINT",toolbar:["font","size","color","bold","italic","under","link","image","flash","zoom","zoomin","zoomout","help","html","stat"]});
                        </script>
                        <textarea name="config[NEGATIVE_POINT]" cols="50" rows="4" id="NEGATIVE_POINT" fix_name="乙方观点" fix_null="yes"><?php echo $config["NEGATIVE_POINT"];?></textarea>
                    </td>
                    
                </tr>
                        
                <tr>
                    <td><strong>用户评论：</strong></td>
                    <td>
                        <label><input type="radio" class="radio" name="config[HIDE_MESSAGE]" value="N" /> 开启</label>
                        <label><input type="radio" class="radio" name="config[HIDE_MESSAGE]" value="Y" /> 关闭</label>
                    </td>
                    
                </tr>
                
                <?php
				
                //载入表单高级选项
                $Licence['type'] != 'free' && require(VI_ROOT."/module/passport/include.form.php");				
				
				?>              
              
                <tbody>
                    <tr class="nobr">
                        <td><strong></strong></td>
                        <td>
                            <?php
                                if ($action=="edit"){
                                    echo '
                                    <input name="action" type="hidden" id="action" value="update" />
                                    <input name="fid" type="hidden" id="fid" value="'.$fid.'" />
                                    ';
                                    echo '<input type="submit" name="Submit" value="修改此表单" class="submit" />';
                                }else{
                                    echo '<input name="action" type="hidden" id="action" value="add" />';
                                    echo '<input type="submit" name="Submit" value="新增此表单" class="submit" />';
                                }
                            ?>
                        </td>				
                    </tr>
                </tbody>
            
            </table>
         
			<script type='text/javascript'>
				Mo("#category").value( "<?php echo $row["category"];?>" );
				Mo("input[name=status]").value( "<?php echo $row["status"];?>" );
				Mo("#skin").value( "<?php echo $row["skin"];?>" );
				
				Mo("#skin").item(0).onchange();
			
				Mo("input[name='config[HIDE_MESSAGE]']").value("<?php echo $action ? $config['HIDE_MESSAGE'] : "N";?>");
            
            </script>
	 </form>
	</div>
	
<!--body end-->

<?php html_close();?>