<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	2011/6/5 Lay
*/

require '../../include/loader.php';
html_start("辩论编辑 - VeryIDE");
?>

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

	//module
    require 'naver.php';
    
	//连接数据库
	System :: connect();
	
	$jump = getgpc('jump');
	$action = getgpc('action');
	$fid = getnum('fid',0);
	$gid = getnum('gid',0);
	
	if ($action){

		$name = getgpc('name');
		$state = getnum('state',0);
		$tags = getgpc("tags");
		$skin = getgpc("skin");
		$thumb = getgpc("thumb");
		$description = getgpc("description");
		$quote = getgpc("quote");
		$category = getgpc("category");
		
		//配置
		$config = clear_array( $_POST['config'] );
		
		//编辑前加载数据
		if( $action == "update" ){
			
			//加载缓存
			if( !Cached :: loader($appid,"form.".$fid) ){
				exit('Cache Error!');
			}else{
				$fconfig = $_CACHE[$appid]['form']["config"];	
			}
			
			//正方
			$config["POSITIVE_RESULT"] = $fconfig["POSITIVE_RESULT"];
			
			//反方
			$config["NEGATIVE_RESULT"] = $fconfig["NEGATIVE_RESULT"];
		}
		
		$config = format_json( fix_json( $config ) );
		
		//删除空白字符
		$config = preg_replace("/([\r\n\t\f])/"," ",$config);		
		
		//开始时间
		$start = getgpc('start');
		
		if( !$start || !isDateTime($start["date"]." ".$start["time"]) ){
			$start = time();				
		}else{				
			$start = strtotime($start["date"]." ".$start["time"]);
		}
		
		//结束时间
		$expire = getgpc("expire");
		
		if( !$expire || !isDateTime($expire["date"]." ".$expire["time"]) ){
			$expire = time();				
		}else{				
			$expire = strtotime($expire["date"]." ".$expire["time"]);
		}
		
		switch($action){
			case "add":
			
				//检查权限
				System :: check_func( 'mod-pk-add', false );		
			
				$sql="INSERT INTO `mod:form`(name,aid,account,state,description,tags,dateline,start,expire,skin,quote,thumb,appid,config,ip,stat,category) values('".$name."',".$_SESSION['Manager']['id'].",'".$_SESSION['Manager']['account']."',".$state.",'".$description."','".$tags."',".time().",".$start.",".$expire.",'".$skin."','".$quote."','".$thumb."','".$appid."','".$config."','".GetIP()."',0,'".$category."')";
				
				System :: $db -> execute( $sql );
				
				$fid = System :: $db -> getInsertId();
				
				//生成缓存
				Cached :: rows( $appid, "SELECT * FROM `mod:form` WHERE id=".$fid, array( 'jsonde' => array('config') ) );
				
				System :: redirect($appid.".list.php?mark=".$id,"信息提交成功!");
				
			break;
				
			case "update":
			
				//检查权限
				System :: check_func( 'mod-pk-mod', false );		
			
				$sql="UPDATE `mod:form` SET name='".$name."',state=".$state.",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;
				System :: $db -> execute($sql);
				
				//生成缓存
				Cached :: rows( $appid, "SELECT * FROM `mod:form` WHERE id=".$fid, array( 'jsonde' => array('config') ) );
				
				System :: redirect($jump,"信息修改成功!");
			
			break;
			
			case "edit";
			
				$sql="SELECT * FROM `mod:form` WHERE id=".$fid;
				$row = System :: $db -> getOne( $sql );

				if( $row ){	
					$config = fix_json($row['config']);						
				}
			
			break;
		}
		
	}
	  
        //关闭数据库
        System :: connect();
    
    ?>
    
 	<?php    
    echo loader_script(array(VI_BASE."js/mo.calendar.js"),"utf-8",$_G['product']['version']);    
    ?>
    
<!--main-->

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

	<div id="box">
	    <form action="" method="post" name="from1" mode="edit" verify="true">
                        
        <table cellpadding="0" cellspacing="0" class="form">
    
            <tr><td colspan="2" class="section"><strong>基本信息</strong></td></tr>
    
            <tr>
                <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($_G['setting']['global']['category'])));?>'] }, function( value ){ this.value = value; }, 1 , 23 );" readonly="true" value="<?php echo $row["category"];?>" onfocus="this.blur();" />
                    
                    <input name="name" type="text" class="text" id="name" value="<?php echo $row['name'];?>" size="65" fix_name="表单主题" fix_null="yes" />
                    <select name="skin" id="skin" style=" width:120px;">
                    <?php
                    //遍历皮肤目录
                    $root = "content/theme/";
                    
                    $dirs = loop_dir( $root );
        
                    foreach( $dirs as $file ){
                        
                        //皮肤名称
                        $name = fileparm( $root.$file."/style.css", 'name' );
                        
                        //忽略没有名称的，为删除皮肤提供方法
                        if( $name ){	
                            echo '<option value="'.$file.'">'.($name?$name:$file).'</option>';
                        }
                        
                    }
                    ?>
                    </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("Tip","<strong>添加新风格</strong> <br />将风格文件夹存放至 <span class=text-key>module/<?php echo $appid;?>/content/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="81" rows="9" id="description" style="float:left;"><?php echo $row["description"];?></textarea>
                <div id="preview" onclick="var s = Mo('#skin').value();if(s && '<?php echo $row['id'];?>'){ window.open('content/?id=<?php echo $row['id'];?>&skin='+s,'skin'); }"></div>
                
                <div style="clear:both;"></div>
                <script type="text/javascript">
                    new Serv.Upload("<?php echo $appid;?>","<?php echo $row["file"];?>",{'format':['<?php echo implode("','",$_G['upload']['image']);?>','<?php echo implode("','",$_G['upload']['flash']);?>'],'again':true,'recovery':true,'input':false,'ubb':Mo.$('description')});
                </script>
                </td>
                
            </tr>
        
            <tr text='表单个性化功能'>
                <td><strong>特殊标记</strong></td>
                <td>
                    <a href="javascript:;"  onclick="Mo('#description').value('$CLOCK',true);" >倒计时</a>
                    <a href="javascript:;"  onclick="Mo('#description').value('[header=宽度,高度,样式(CSS)]Flash地址[/header]',true);" >插入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[date]" 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 name="start[time]" type="text" 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[date]" 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 name="expire[time]" type="text" 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="state" value="1" checked> 
                    开放表单
                    </label>
                    <label>
                    <input type="radio" class="radio" name="state" 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("','",$_G['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>
                
                    <table cellpadding="0" cellspacing="1" border="0" class="gird">
                        <thead>
                            <td>
                            正方
                            </td>
                            <td>
                            反方
                            </td>
                        </thead>
                        <tr>
                            <td>
                            名称
                            <input name="config[POSITIVE]" type="text" size="13" class="text" value="<?php echo $config["POSITIVE"];?>" />
                            <input name="config[POSITIVE_RESULT]" type="hidden" value="<?php echo intval($config["POSITIVE_RESULT"])+0;?>" />
                            票数：<?php echo $config["POSITIVE_RESULT"] ? $config["POSITIVE_RESULT"] : 0;?>
                            </td>
                            <td>
                           	名称
                            <input name="config[NEGATIVE]" type="text" size="13" class="text" value="<?php echo $config["NEGATIVE"];?>" />
                            <input name="config[NEGATIVE_RESULT]" type="hidden" value="<?php echo intval($config["NEGATIVE_RESULT"])+0;?>" />
                            票数：<?php echo $config["NEGATIVE_RESULT"] ? $config["NEGATIVE_RESULT"] : 0;?>
                            </td>
                        </tr>
                        <tr>
                            <td>
                            观点<br />
                            <script type="text/javascript">
								Mo.UBB.show({id:"POSITIVE_POINT",toolbar:["font","size","color","bold","italic","under","link","image","flash","zoom","zoomin","zoomout"]});
							</script>
							<textarea name="config[POSITIVE_POINT]" cols="50" rows="5" style="100px;" id="POSITIVE_POINT" fix_name="正方观点" fix_null="yes"><?php echo $config["POSITIVE_POINT"];?></textarea>
                            </td>
                            <td>
                            观点<br />
                            <script type="text/javascript">
								Mo.UBB.show({id:"NEGATIVE_POINT",toolbar:["font","size","color","bold","italic","under","link","image","flash","zoom","zoomin","zoomout"]});
							</script>
							<textarea name="config[NEGATIVE_POINT]" cols="50" rows="5" id="NEGATIVE_POINT" fix_name="乙方观点" fix_null="yes"><?php echo $config["NEGATIVE_POINT"];?></textarea>
                            </td>
                        </tr>
                    </table>
                    
                </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
            
            //载入表单高级选项
            file_exists(VI_ROOT."module/passport/") && 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=state]").value("<?php echo isset($row['state']) ? $row['state'] : 1;?>"); 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();?>