<?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("config.php");
    //require("function.php");
    //require("naver.php");
	
	//mysql open
	reader_mysql();
	
	$do = GetQuery("do");

	$action = GetQuery("action");
	$jump = GetQuery("jump");
	
	$fid = GetNumber("fid",0);
	$gid = GetNumber("gid",0);
	
	//批处理列表
	$list = $_POST["list"];
	$list = implode(",", $list);	

	//上一页
	$jump = $_POST["jump"];
	
	if ($action){

		$name = GetQuery("name");
		$sort = GetNumber("sort",0);
		$status = GetNumber("status",0);
		$oid = GetNumber("id",0);
		$stat = GetNumber("stat",0);
		$description = GetQuery("description");
		
		$image = GetQuery("image");
		$quote = GetQuery("quote");

		switch($action){
			
			case "add":
			
				/*
				echo '<pre>';
				print_r($_FILES);				
				echo '</pre>';
				
				echo $_FILES["_image"]['name'][0];
				*/
				
				//exit();
		
				//批量插入
				foreach($_POST["_name"] as $key => $val){
					
					//不为空
					if( $val ){
						
						$name = $_POST["_name"][$key];
						
						$sort = intval($_POST["_sort"][$key],0);
						
						$quote = $_POST["_quote"][$key];
						
						$desc = $_POST["_description"][$key];
						
						//echo $key;
						
						$image = file_upload( "_image", $key , $Upload["image"] ) ;
						$image = is_string($image) ? $image : '';
						
						//echo $image;
						
						//print_r($key);
						
						//exit();
						
						//exit($image);
					
						//
						$sql="insert into ".VI_DBMODPRE."options(name,sort,fid,gid,dateline,description,stat,status,quote,image) values('".$name."',".$sort.",".$fid.",".$gid.",".time().",'".$description."',".$stat.",".$status.",'".$quote."','".$image."')";
						
						mysql_query($sql,$mysql);
						
						$Project["queries"]++;
						
						$id = mysql_insert_id($mysql);
						
						//错误处理
						if (!$id) {
							trigger_error($sql, E_USER_ERROR);
							exit();
						}
						
					}
				}
				
				//exit();
			
				/*
				$sql="insert into ".VI_DBMODPRE."options(name,sort,fid,gid,dateline,description,stat,status,quote,image) values('".$name."',".$sort.",".$fid.",".$gid.",".time().",'".$description."',".$stat.",".$status.",'".$quote."','".$image."')";
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				$id = mysql_insert_id($mysql);
				
				//错误处理
				if (!$id) {
					trigger_error($sql, E_USER_ERROR);
					exit();
				}
				*/
				
				reader_redirect("?do=list&fid=".$fid."&gid=".$gid,"信息提交成功!");
				
			break;
				
			case "edit":
			
				$sql="select * from ".VI_DBMODPRE."options where id=".$oid;
				$result=mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				if(mysql_num_rows($result)==1){
					$row=mysql_fetch_assoc($result);				
				}
			
			break;
				
			case "update":
				$sql="update ".VI_DBMODPRE."options set name='".$name."',sort=".$sort.",description='".$description."',status=".$status.",quote='".$quote."',image='".$image."' where id=".$oid;
				mysql_query($sql,$mysql);
				
				//echo $sql;
				//exit();
				
				$Project["queries"]++;
				
				//reader_redirect($jump,"信息更新成功！");
				reader_redirect("?do=list&fid=".$fid."&gid=".$gid,"信息修改成功!");
			break;
				
			case "delete":
				$sql="delete from ".VI_DBMODPRE."options where id in(".$list.")";
				mysql_query($sql,$mysql);
				
				$Project["queries"]++;
				
				//reader_redirect($jump,"信息删除成功！");
				reader_redirect("?do=list&fid=".$fid."&gid=".$gid,"信息删除成功!");
			break;
			
			case "status":

				$sql="update ".VI_DBMODPRE."options set status=".$status." where id in(".$list.")";
				mysql_query($sql);
				
				$Project["queries"]++;
				
				reader_redirect("?do=list&fid=".$fid."&gid=".$gid,"选项状态更改成功!");
				
			break;
							
							
		}		
                    
		//生成缓存
		cache_forms($fid,$appid,$mysql);
		
  	}	
	
	//加载缓存
	//$file = VI_ROOT."".$Project["cache"]."mod.forms.".$fid.".php";
	//loader_data($file);
	loader_cache($appid,"forms.".$fid);
	?>
<!--main-->
	<?php
	
	echo '
    <div id="nav">
        <strong>'.$VICACHE["mod.forms"]["name"].' &gt;&gt; '.$VICACHE["mod.forms"]["groups"][$gid]["name"].'</strong> 	
    </div>
    
    <ul id="naver">
        <li'.($do=='new'?' class="active"':'').'><a href="?do=new&fid='.$fid.'&gid='.$gid.'">'.($action=="edit"?'编辑':'新增').'子选项</a></li>
        <li'.($do=='list'?' class="active"':'').'><a href="?do=list&fid='.$fid.'&gid='.$gid.'">管理子选项</a></li>
    </ul>
	';
	?>
    
	<?php    
    if ( in_array($VICACHE["mod.forms"]["groups"][$gid]["type"],array("text","textarea","file")) ){
    	echo '
			<div id="board">
			<p>
				<strong>标签说明：</strong>
				此类型项目不需要子类标签！
			</p>
			</div>
		   ';
		   
		   $do = '';
	}
    ?>
    <div id="box">
    
    <?php 
	if($do=="new"){
		
		$config = fix_json($VICACHE["mod.forms"]["groups"][$gid]["config"]);
		
		//展示模板
		$template = $config["GROUP_STYLE"] ? $Modules[$appid]["style"][$config["GROUP_STYLE"]] : $config["GROUP_TEMPLATE"];
		
		//exit($template);
		
	?>
		<?php    
        echo loader_script(array(VI_BASE."js/ubb.js"),"utf-8",$Project["version"]);    
        ?>

    	<form action="?fid=<?php echo $fid;?>&gid=<?php echo $gid;?>" method="post" verify="true" enctype="multipart/form-data">
        
        <table cellpadding="0" cellspacing="0" class="form">
        
        	<tr>
            	<td width="60"><strong>目标类型：</strong></td>
                <td>
                <?php
				echo $Modules[$appid]["groups"][$VICACHE["mod.forms"]["groups"][$gid]["type"]];
				?>
                </td>
            </tr>
            
            <?php
			if( $config["GROUP_STAT"] && !$action ){
			?>
        
            <tr>
                <td><strong>起始票数：</strong></td>
                <td>
                <?php echo $config["GROUP_STAT"];?>
                <input name="stat" type="hidden" id="stat" value="<?php echo $config["GROUP_STAT"];?>" />
                </td>
            </tr>
            
            <?php
			}
			?>
            
            <?php
			//批量添加
			if( $action == "" ){			
			?>
        
        	<tr>
            	<td><strong>批量创建：</strong></td>
                <td>
				
                	<div id="tag-box">
                    
                    <table id="tag-<?php echo $i;?>" cellpadding="0" cellspacing="1" border="0" class="gird">
                        <tr>
                            <td>
                            	<span class="action">
									<a onclick="javascript:Planer.move(this,'up');void(0);">上移</a>
									<a onclick="javascript:Planer.move(this,'down');void(0);">下移</a>
									<a onclick="javascript:Planer.remove(this);void(0);">删除</a>
								</span>
								
                           		名称：<br />
                                <input name="_name[]" type="text" class="text" value="<?php echo $key;?>" size="35" fix_name="标签名称" fix_null="yes" />

                                <input name="_sort[]" type="text" size="10" fix_name="标签排序" fix_number="no" title="显示排序" class="text digi" onclick="Mo.Soler(this,this.value,this.title,(function(){ var a = []; for(var i=-9;i<=90;i++){ a.push(i);} return a; })(),10);" readonly="true" value="<?php echo $row["sort"] ? $row["sort"] : "0";?>" />
								
								<?php
								if( stripos($template,"{LINK}") !== false ){
								?>								
                                <br />
                                链接：<br />
                                <input name="_quote[]" type="text" class="text" value="<?php echo $val;?>" size="51" />
								<?php
								}
								?>
																
								<?php
								if( stripos($template,"{IMAGE}") !== false ){
								?>								
                                <br />
                                图片：<br />
                                <input name="_image[]" type="file" class="text" value="<?php echo $val;?>" size="40" />
								<?php
								}
								?>
								
								<?php
								if( stripos($template,"{DESC}") !== false ){
								?>								
                                <br />
                                描述：<br />
                                <textarea name="_description" cols="50" rows="5" id="_description"><?php echo $row["description"];?></textarea>								
								<?php
								}
								?>
                            </td>
                        </tr>
                    </table>
                    
                    </div>
                
                    <script type="text/javascript">
                    var Planer = new Mo.Planer($("tag-box"));
                    </script>
                    
                    <a onclick="javascript:Planer.copy();void(0);">添加</a>
                
                </td>
            </tr>
            
            <?php
			}else{			
			?>
        
        	<tr>
            	<td><strong>标签名称：</strong></td>
                <td><input name="name" type="text" class="text" id="name" value="<?php echo $row["name"];?>" size="35" fix_name="标签名称" fix_null="yes" />
                </td>
            </tr>
        
        	<tr>
            	<td><strong>标签排序：</strong></td>
                <td><input name="sort" type="text" size="35" fix_name="标签排序" fix_number="no" title="显示排序" class="text digi" onclick="Mo.Soler(this,this.value,this.title,(function(){ var a = []; for(var i=-9;i<=90;i++){ a.push(i);} return a; })(),10);" readonly="true" value="<?php echo $row["sort"] ? $row["sort"] : "0";?>" />
                </td>
            </tr>
            
            <?php
			if( stripos($template,"{DESC}") !== false ){
			?>
        
        	<tr>
            	<td><strong>标签描述：</strong></td>
                <td>
				<script type="text/javascript">
                	Mo.UBB.show({
						id:"description",
						toolbar:["font","size","color","bold","italic","under","link","zoom","zoomin","zoomout","html","stat","about"]							
					});
                </script>
                <textarea name="description" cols="50" rows="7" id="description"><?php echo $row["description"];?></textarea>
                </td>
            </tr>
            
            <?php
			}
			?>
            
            <?php
			if( stripos($template,"{IMAGE}") !== false ){
			?>
        
            <tr text='填写此表单的关键字词,将用于显示与此相关的表单'>
                <td><strong>上传图片：</strong></td>
                <td>
                <script type="text/javascript">
                new Serv.Upload("image","<?php echo $row["image"];?>",{'format':['<?php echo implode("','",$Upload["image"]);?>'],'again':true,'recovery':true,'input':true});
                </script>
                </td>
            </tr>
            
            <?php
			}
			?>
            
            <?php
			if( stripos($template,"{LINK}") !== false ){
			?>
        
            <tr text='填写此表单的关键字词,将用于显示与此相关的表单'>
                <td><strong>链接地址：</strong></td>
                <td>
                <input name="quote" type="text" class="text" id="quote" value="<?php echo $row["quote"];?>" size="60" />
                </td>
            </tr>
            
            <?php
			}
			?>
        
            
            <?php
			}			
			?>
        
        	<tr>
            	<td><strong>项目状态：</strong></td>
                <td>
                    <label>
                    <input name="status" type="radio" class="radio" value="1" checked> 
                    完全开放
                    </label>
                    <label>
                    <input name="status" type="radio" class="radio" value="0">
                    禁用项目
                    </label>
                </td>
                
            </tr>
         
        	<tr class="nobr">
            	<td><strong></strong></td>
                <td>
					<?php
                    if ($action=="edit"){
						 echo '
						<input name="action" type="hidden" id="action" value="update" />
						<input name="id" type="hidden" id="id" value="'.$row["id"].'" />
						';
                       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>
            
          </table>
        
        <script type="text/javascript">
        	Mo("input[name=status]").value("<?php echo $row["status"];?>");
        </script>
        
	 </form>
	
    <?php
	}elseif($do=="list"){
	?>
    
    <form name="formPost" id="formPost" method="post" action="?fid=<?php echo $fid;?>&gid=<?php echo $gid;?>">
		<input name="action" id="action" type="hidden" value="mass" />
		<input name="status" id="status" type="hidden" value="" />
		
		<input name="jump" type="hidden" value="" />
		<script>Mo('#formPost input[name=jump]').value( location.href );</script>

		<table width="100%" border="0" cellpadding="1" cellspacing="1" class="table" id="table">
	
			<?php
            if( mysql_num_rows($result)> $Global["pagesize"]/2 ){
            ?>
            <tr class="thead">
                <td colspan="10" class="first">
                    <?php echo $Modules[$appid]["tool"];?>
                </td>
            </tr>
            <?php
            }
            ?>
	
			<tr class="title">
            <td></td>
            <td>ID</td>
            <td>排序</td>
			<td>标签名称</td>
			<td>统计</td>
			<td>链接地址</td>
			<td>添加时间</td>
            <td>状态</td>
			<td width="60">操作</td>
			</tr>
			<?php
				$sql="SELECT * FROM ".VI_DBMODPRE."options where gid=".$gid." order BY sort asc";
				
				$result=mysql_query($sql,$mysql);
				
				$Project["queries"]++;

				while($row=mysql_fetch_assoc($result)){
					
					?>
					
					<tr class="line" mark="<?php echo $row["id"];?>" edit="<?php echo $appid;?>.option.php?do=new&action=edit&fid=<?php echo $fid;?>&gid=<?php echo $gid;?>&id=<?php echo $row["id"];?>&jump={jump}">
                        <td title="<?php echo $row["id"];?>">
                            <input name="list[]" type="checkbox" class="checkbox" value="<?php echo $row["id"];?>">
                        </td>
						<td><?php echo $row["id"];?></td>
						<td><?php echo $row["sort"];?></td>
						<td><strong class="text-yes"><?php echo $row["name"];?></strong></td>
						<td><?php echo $row["stat"];?></td>
						<td><script>Mo.Tools("RsFile","<?php echo $row["image"];?>","");</script><a target="_blank" href="'.$row["quote"].'"><?php echo $row["quote"];?></a></td>
						<td><?php echo date($Global["dateformat"],$row["dateline"]);?></td>
                        <td>
                            <?php echo $Project["status"][$row["status"]];?>           
                        </td>
						<td>
							<script type="text/javascript">
                            Mo.Tools("RsEdit","?do=new&action=edit&fid=<?php echo $row["fid"];?>&gid=<?php echo $row["gid"];?>&id=<?php echo $row["id"];?>","");
                            Mo.Tools("RsDelete","?do=list&action=delete&fid=<?php echo $row["fid"];?>&gid=<?php echo $row["gid"];?>&id=<?php echo $row["id"];?>","");
							</script>
						</td>
					</tr>
                    
                    <?php
                    
				}
			?>
		
			<tr class="tfoot">
				<td colspan="10" class="first">
					<?php echo $Modules[$appid]["tool"];?>
				</td>
			</tr>
        </table>
    
    </form>
	
    <?php 
	
	}
	
	//mysql close
	reader_mysql(true);
	?>
	
    </div>
    	
<!--body end-->

<?php html_close();?>