<?php
include("../common.php");
include("common.php");
$where = BLOG_PATH."edit";
include("include/sec.php");
include("../include/class/Image.php");
include("../include/class/ImageUpload.php");
include_once(S_ROOT.'./include/function_inter.php');
//$badwords_file = "../forum/badwords.txt";
//include("caching/options_map.inc");
//============
$titleHeader = $this_section;
$C_cat3 = "96;Nickandmilly Weekly Update ";
//============
$blog = new Blog();
$id = $_REQUEST["id"];
$blog->find($id);
$Cats = new Cats();
//access right check ================================================
$is_member = false;
if($blog->row["userID"]==$sys["memberID"] || $sys["isAdmin"] || $sys["isSuperMaster"] || $sys["isMaster"])
	$is_member = true;
else
	header("location:hotnet?id=$id&msg=Access is denied");
//================================================================

//***************************************** editable for photo start
$path = path($_POST["pDate"]);//pDate is from hidden field
$ID = $id;
$photoFolder = "images/$path";
if($_REQUEST["st"]=="photo")
	$msg = "You can click \"Browse\" to upload a photo from your computer for this info.";
$photo_num = 1;
$photo_w = 600;
$photo_h = 480;	
//***************************************** editable for photo  end


if($_POST["Submit"]=="Update" && $is_member){
		request($blog->fields,array("postDate"));	
		$subject = norm($subject);
		$content = norm($content);
		$tags = norm($tags);
		$cat3 = $_REQUEST["cat3"];
		//$cat3 = $C_cat3;//temp !!!		
		if($cat3) list($catID ,$catTitle)= split (";", $cat3);
						
		$remove_arr = array("parentID","series_parentID","rootID","userID","user","postDate","num_bbs","room","room_max","ip");
		
		
	//=====================photo
	$photoRemainArr = array();		
	$photoArr = array();
	for($i=0;$i<$photo_num;$i++){
		$photoArr[$i] = "photo".$i;
		$photoRemainArr[$i] = $_REQUEST["photoRemain".$i];
	}
	for($i=0;$i<$photo_num;$i++){
		$picture = $_FILES["picture".$i];
		if($picture["size"]>0){
			if(!file_exists($photoFolder))
				mkdir($photoFolder,0777);
			$$photoArr[$i] =  $picture["name"];
			$imageUpload = new ImageUpload($picture);
			$imgType = $imageUpload->getImgType($$photoArr[$i]);
			if($imgType){
				$$photoArr[$i] = $ID."_".$i.".".$imgType;						
				$newName = "$photoFolder/".$$photoArr[$i];
				if($imageUpload->upload($newName)){
					$msg = $imageUpload->getMsg();
					if($imageUpload->getWidth()>$photo_w){
						resize($newName,$photo_w,$photo_h,"$photoFolder/","");					
					}
					$photoRemainArr[$i] = $imgType;									
				}	
    		}
			else
				$msg = $imageUpload->getMsg();
		}
	}	
	//============================ photo end		
		
		$sql = sqlUpdate($blog->table,$blog->fields,$remove_arr,"where ID=$id");
		//echo $sql;		
		if(mysql_query($sql)){
			$hotnet = BLOG_PATH.hotnet_url($id,$blog->row["subject"]);					
			$msg = "You have updated successfully!<a target='_blank' class='white' href='$hotnet'>[preview]</a>";
			refresh($_site.BLOG_PATH."caching/robot_new.php","caching/new.html");
		}
		else
			$msg = "<font class=red>Failed to update</font>";			
}
else if($_POST["Submit"]=="Delete" && $is_member){
	if(mysql_query("delete from $blog->table where ID=$id")){
		$blog->score("delete",$blog->row["userID"]);
		$msg = "You have deleted this info successfully";
		header("location:".$blog->row["catID"]."/");
	}
	else
		$msg = "Faile to delete";
}
$subject1 = $blog->row["subject"];
$subjectStr = "<a href='$id'>".$blog->row["subject"]."</a>";
//navigation ---------------------------------------------------------------------------------
$sql = "SELECT child.*,parent.ID as catID,parent.title as catTitle, 
grandparent.ID as parentID,grandparent.title as parentTitle 
FROM ".$Cats->table." grandparent, ".$Cats->table." parent, ".$blog->table." child 
WHERE parent.ID = child.catID and grandparent.ID=parent.parentID and child.ID=$id";	
$t = mysql_query($sql);
$row = mysql_fetch_array($t);
$parentID = $row["parentID"];
$parentTitle = $row["parentTitle"];
$catTitle = $row["catTitle"];
$catID = $row["catID"];
$nav = "<a  href='/'>home &gt;&gt;</a> <a  href='.'>".$this_section." &gt;&gt; </a>
$parentTitle <a href='$catID/'>$catTitle</a>";	
//------------------------------------------------------------------------------------------
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Pragma" content="no-cache" />
<META name="description" content="<?=$_meta_des?>">
<META name="keywords" content="<?=$_meta_keyword?>">
<meta name="robots" content="follow,index">
<title><?=$titleHeader?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="<?=BLOG_PATH?>include/style.css" type="text/css">
<? include($tps."elements/meta.inc")?>
<script language=javascript>
function check(thisForm){
  var a =thisForm.subject;
  var b =thisForm.content; 
  var c = thisForm.cat3;
  var fields = new Array(a,b,c);
   //don't need to change the following lines 
  var error = false;
  var focusObj = null;
  var color1 = "#FF9900";
  var color2 = "#FFFFFF";
  
  for(i=0;i<fields.length;i++){
  	fields[i].style.background=color2;
	if(fields[i].value==""){
		fields[i].style.background=color1;
		if(!error)
			focusObj = fields[i];
		error=true;		
	}
  }  
  if(error){
  	focusObj.focus();
    alert("Please enter the required info in the highlight field.");
    return false;
  }
  else{
    //thisForm.Submit.value = "submitting ..."
  //	thisForm.Submit.disabled = true;
    return true;
  }  
}
function remind(){
		var agree=confirm("Are you sure to delete this info? ");
		if (agree)
			return true ;
		else
			return false ;		
}
function add_tag(thisField){
	var tagField = document.form1.tags;
	if(tagField.value=="")
		tagField.value = thisField.value;
	else{
		if(tagField.value.indexOf(thisField.value)==-1)	
			tagField.value = tagField.value +" "+thisField.value;
	}	
}
</script>
<script type=text/javascript>
<? include("caching/cats_js_s.html")?>
function display_cats(){
	var v = document.form1.cat2;
	for(i=1;i<cats.length;i++){
		var luke = cats[i][0];		
		v.options[i] = new Option(luke.substr(luke.indexOf(";")+1),luke);			    		
	}
}
function sub_cats(){
	var v1 = document.form1.cat2;
	var v2 = document.form1.cat3;
	var section = v1.selectedIndex;
	if(section>0){
		for(i=0;i<v2.length;i++){
			v2.options[i] = new Option("");
		}
		var test = cats[section][1];
		for(i=1;i<test.length;i++){
			var luke = test[i];		
	   		v2.options[i] = new Option(luke.substr(luke.indexOf(";")+1),luke);		
		}
	}
	else{
		for(i=0;i<v2.length;i++){
			v2.options[i] = new Option("");
		}
	}	
}
</script>
<script type="text/javascript" src="/include/js/bbs_encode.js"></script>
</HEAD>
<BODY onload="document.form1.subject.focus();display_cats()">
	<div id="wrapper"> 
    <? include($tps."elements/header.php")?>		     
	 <div id="main_body2">       
      		<div id="col_l">
         	<? include("panel_more.htm")?>
	  		</div>		  
            <div id="col_r">
      
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
           
            <td valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr> 
                  <td valign="top"> <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
                      <tr> 
                        <td height="38" valign="top"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
                            <tr> 
                              <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                  <tr> 
                                    <td height="18"> 
                                      <?=$nav?>
                                    </td>
                                  </tr>
                                </table></td>
                            </tr>
                            <tr> 
                              <td align="center" > 
                                <div class='msg'><?=$msg?></div>
                              </td>
                            </tr>
                            <tr> 
                              <td valign="top"> <table width="100%" border="0" cellpadding="2" cellspacing="2" >
                                <form action='edit' method="post" name="form1" id="form1" ENCTYPE="multipart/form-data" onsubmit="return check(this)">
                                  <tr valign="top" > 
                                    <td width="71" height="26" align="right" class="text_right">* 
                                      Subject:</td>
                                    <td height="26" colspan="2" align="left"><input type="text" style="width:550px" name="subject" value="<?=$row["subject"]?>" /> 
                                      <br /> </td>
                                  </tr>
                                  <tr> 
                                    <td height="10" align="right" valign="top" class="text_right">Category:<br /> 
                                      <br />
                                      Hint:<br />
                                      select top category first</td>
                                    <td colspan="2" valign="top" align="left"><table width="350"  border="0" cellpadding="0" cellspacing="0">
                                        <tr valign="top"> 
                                          <td width="120" height="16">* Top category</td>
                                          <td width="114" >* Sub-category</td>
                                        </tr>
                                        <tr valign="top"> 
                                          <td height="76" align="left"> 
                                            <?php
if( $parentID && $parentTitle) $cat2 = $parentID.";".$parentTitle;
?>
                                            <select size="12" name="cat2" onclick="sub_cats()" onblur="return" style="width:150px">
                                              <option selected="selected" value="<?=$cat2?>"> 
                                              <?=$op_arr[$cat2]?>
                                              </option>
                                            </select></td>
                                          <td > 
                                            <?php
if( $catID && $catTitle) $cat3 = $catID.";".$catTitle;
?>
                                            <select size="12" name="cat3" style="width:150px">
                                              <option selected="selected" value="<?=$cat3?>"> 
                                              <?=$op_arr[$cat3]?>
                                              </option>
                                            </select></td>
                                        </tr>
                                      </table></td>
                                  </tr>
                                
                                  <tr> 
                                    <td height="10" align="right" valign="top" class="text_right"> 
                                      Content</td>
                                    <td colspan="2" valign="top" align="left"><table cellpadding="0" cellspacing="0" style="border:1 solid #CCCCCC">
                                        <tr> 
                                          <td align="left"> <table cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
                                              <tr> 
                                                <td align="left"><textarea name="content" id="des" style="width:550px; height:150px;border:#B7CBCF 1px solid"><?=$row["content"]?></textarea></td>
                                              </tr>
                                            </table></td>
                                        </tr>
                                      </table>
                                      <br /> </td>
                                  </tr>
                                  <tr> 
                                    <td height="23" align="right" valign="top" class="text_right">Photo: 
                                    </td>
                                    <td height="23" colspan="2" align="left" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
                                        <tr valign="top"> 
                                          <td width="533" height="79"><a href="javascript:addimage1('des')"><strong>[</strong>Enter 
                                            a photo Full URL<img src="/library/htmlarea/images/ed_image.gif" width="18" height="18" align="absmiddle" border="0"  style="cursor: hand;" /></a><strong>]</strong> 
                                            (must be allowed external link)<br /> 
                                            <br />
                                            Or from your computer<br /> <iframe scrolling="No" frameborder="0" style="width:100%;height:120px" src="photo/upload.php"></iframe> 
                                          </td>
                                        </tr>
                                      </table></td>
                                  </tr>
                                  <tr> 
                                    <td height="23" align="right" valign="top" class="text_right">Tags:</td>
                                    <td height="23" colspan="2" align="left" valign="top"><input name="tags" type="text" value="<?=$row["tags"]?>" style="width:550px" /> 
                                      <br /> <div class="small"><span>Hint:</span> 
                                        multiple tags are separated by commas</div></td>
                                  </tr>
                                  <tr> 
                                    <td height="23" class="text_right"><input name='Submit' type='submit' id="Submit" onclick="return remind()" value='Delete' /></td>
                                    <td width="237" height="23" align="left"><input name="st" type="hidden" id="st" value="Update" /> 
                                      <input name="id" type="hidden" value="<?=$id?>" /> 
                                      <input name="pDate" type="hidden"  value="<?=$row["postDate"]?>" /> 
                                      <input name='Submit' type='submit' id="Submit" value='Update' /> 
                                    </td>
                                    <td width="289">&nbsp;</td>
                                  </tr>
                                </form>
                              </table></td>
                            </tr>
                          </table></td>
                      </tr>
                    </table></td>
                </tr>
              </table></td>
          </tr>
        </table>
      </div>		
      <!--col_r end-->
	  <div style="clear: both;">&nbsp;</div>	
    </div>	
	</div>
	<? include($tps."elements/footer.php")?>
</body>
</html>