<? include_once("../includes/main.inc.php");
if(is_post_back()) {
	if(strstr($page_text,'/images/')){
	}else if(strstr($page_text,'images/')){
	}
	$page_title = addslashes($page_title);
	$page_url = $page_url?$page_url:$page_title;
	$page_url = strtolower(preg_replace('/[^A-Za-z0-9\-]/', '-',$page_url));
	$menu_position = $menu_position;
	$page_text1 = addslashes($page_text);

	if($page_id!='') {
		$sql = "update tbl_content set page_title='$page_title', parent='$parent', page_url='$page_url', menu_position='$menu_position', page_text='$page_text1',page_brief_desc='$page_brief_desc', title='$title', description='$description', keyword='$keyword' where page_id = $page_id ";
		db_query($sql);
		########### Uploading Image ################
		if($_FILES['sv_image']['name']!='')
		{
			if(strlen($sv_image) && file_exists(UP_FILES_FS_PATH."/background_image/thumb/".$sv_image)){
				@unlink(UP_FILES_FS_PATH."/background_image/thumb/".$sv_image);
			}
			$sv_image_1 = md5(uniqid(rand(), true)).'.'.file_ext($_FILES[sv_image]['name']);
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/background_image/thumb/".$sv_image_1) or die("Image is not uploaded");
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/background_image/original/".$sv_image_1) or die("Image is not uploaded");
			$n_width=300; // Fix the width of the thumb nail images
			$n_height=200; // Fix the height of the thumb nail imaage
			
			$tsrc=UP_FILES_FS_PATH."/background_image/thumb/".$sv_image_1;
			$add=UP_FILES_FS_PATH."/background_image/original/".$sv_image_1; // Path where thumb nail image will be stored
			
			//echo $tsrc;
			if (!($_FILES[sv_image][type] =="image/pjpeg" OR $_FILES[sv_image][type]=="image/gif" OR $_FILES[sv_image][type]=="image/jpeg" OR $_FILES[sv_image][type]=="image/png")){echo "Your uploaded file must be of JPG or GIF or PNG. Other file types are not allowed<BR>";
			exit;}
				///////// starting of JPG thumb nail creation//////////
			if($_FILES[sv_image][type]=="image/pjpeg" OR $_FILES[sv_image][type]=="image/jpeg")
			{
				$im=ImageCreateFromJPEG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImageJpeg($newimage,$tsrc);
				chmod("$tsrc",0777);
			}
			else{
				$im=ImageCreateFromPNG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImagePng($newimage,$tsrc);
				chmod("$tsrc",0777);
			}
			
			db_query("update tbl_content set sv_image='".$sv_image_1."' where page_id = $page_id ");
		}		
		
		set_session_msg("Page details has been updated successfully.");		
	} else{	
			########### Uploading Image ################
			if($_FILES['sv_image']['name']!='')
			{
				$sv_image_1 = md5(uniqid(rand(), true)).'.'.file_ext($_FILES[sv_image]['name']);
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/background_image/thumb/".$sv_image_1) or die("Image is not uploaded");
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/background_image/original/".$sv_image_1) or die("Image is not uploaded");
				///////// Start the thumbnail generation//////////////
				$n_width=300; // Fix the width of the thumb nail images
				$n_height=200; // Fix the height of the thumb nail imaage
				$tsrc=UP_FILES_FS_PATH."/background_image/thumb/".$sv_image_1;
				$add=UP_FILES_FS_PATH."/background_image/original/".$sv_image_1; // Path where thumb nail image will be stored
				
				//echo $tsrc;
				if (!($_FILES[sv_image][type] =="image/pjpeg" OR $_FILES[sv_image][type]=="image/gif" OR $_FILES[sv_image][type]=="image/jpeg" OR $_FILES[sv_image][type]=="image/png")){echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
				exit;}
					///////// starting of JPG thumb nail creation//////////
				if($_FILES[sv_image][type]=="image/pjpeg" OR $_FILES[sv_image][type]=="image/jpeg"){
				$im=ImageCreateFromJPEG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImageJpeg($newimage,$tsrc);
				chmod("$tsrc",0777);
				chmod("$tsrcBig",0777);
				}
				else{
				$im=ImageCreateFromPNG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImagePng($newimage,$tsrc);
				chmod("$tsrc",0777);
				}
							
			}
			########### End of Code Block ##############
					
		$sql = "insert into tbl_content set page_title='$page_title', parent='$parent', sv_image='".$sv_image_1."', page_url='$page_url', menu_position='$menu_position', page_text = '$page_text1', title='$title', description='$description', keyword='$keyword'";
		db_query($sql);
		set_session_msg("New Page has been added successfully.");
	}
	header("Location: page_list.php?start=$start");
	exit;
}

$page_id = $_REQUEST['page_id'];
if($page_id!='') {
	$sql = "select * from tbl_content where page_id = '$page_id'";
	$result = db_query($sql);
	if ($line_raw = mysql_fetch_array($result)) {
		//$line_raw = ms_form_value($line_raw);
		@extract($line_raw);
	}
}
?>
<link href="styles.css" rel="stylesheet" type="text/css">

<? include("top.inc.php");?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td id="pageHead"><div id="txtPageHead">
       Add/Edit Page Content</div></td>
  </tr>
</table>
<script type="text/javascript" src="fckeditor/fckeditor.js"></script>
<table width="100%" align="center">
    <tr><td>
        <form method="post" action="" enctype="multipart/form-data" name="form1" id="form1" onSubmit="return validate(this);">
        <table width="90%" align="center" border=0 cellpadding="0"  cellspacing="0" class="text">
        <tr>
        <td  colspan="2" height="23"  align="right"><a href="page_list.php?start=<?=$start?>" class="redcolor">Back To Content List</a></td>
        </tr>
        <tr><td colspan="2" height="23"></td></tr>
        <tr><td width="170" class="lightGrayBg"><b>Parent Menu</b></td>
            <td class="lightGrayBg">
            <select id="parent" name="parent">
                <option value="0">--Select Parent Menu--</option>
                <?php
                $sql=mysql_query("select page_title,page_id from tbl_content where page_status='Active' order by page_title ASC");
                if(!$sql){ echo mysql_error().'Error at line 138'; }else{
                    while($res=mysql_fetch_array($sql)){
                        if($res['page_id']==$parent)
                        {
                            echo "<option value=".$res['page_id']." selected='selected'>".$res['page_title']."</option>";
                        }
                        else
                        {
                            echo "<option value=".$res['page_id'].">".$res['page_title']."</option>";
                        }
                    }
                }
                ?>
            </select>

        </td></tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr>
             <td class="lightGrayBg"><b>Page Title/ Menu Name</b></td>
             <td class="lightGrayBg"><input type="text" name="page_title" id="page_title" size="50" value="<?=$page_title?>"  /></td>
        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr>
          <td class="lightGrayBg"><b>Page URL</b></td>
          <td class="lightGrayBg"><?php if($page_url=='home'){echo "*";}else{ ?><input type="text" name="page_url" id="page_url" size="50" value="<?=$page_url?>"  /><?php } ?></td>        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr>
           <td class="lightGrayBg"><b>Menu Position</b></td>
           <td class="lightGrayBg"><input type="text" name="menu_position" id="menu_position" size="50" value="<?=$menu_position?>"  /></td>
        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>
        <tr>
           <td valign="top" class="lightGrayBg"><b>Page Text</b></td>
           <td class="lightGrayBg">		
            <textarea name="page_text" id="page_text" rows="10" cols="50"><?=stripslashes($page_text)?></textarea>		
           </td>
        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>
		        <tr><td colspan="2">
		<h2>&curren; <u>Meta Data</u> &curren;</h2>
		&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Title </b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="title" id="title"  ><?=stripslashes($title)?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Description </b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="description" id="description"  ><?=stripslashes($description)?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Keyword</b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="keyword" id="keyword"  ><?=stripslashes($keyword)?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>

        <!--<tr>
            <td class="tdLabel" width="20%">Background Image</td>
            <td class="" valign="middle">
                <input name="sv_image" type="file"/>&nbsp;&nbsp;&nbsp;&nbsp; 
            </td>
        </tr>
        <tr><td colspan="2">&nbsp;</td>
        <td>&nbsp;</td></tr>-->
            <? /*if($page_id!='')
            {
                
                if(strlen($sv_image) && file_exists(UP_FILES_FS_PATH."/background_image/thumb/".$sv_image)){
                $imgsrc=UP_FILES_WS_PATH."/background_image/thumb/".$sv_image;
                $size=getimagesize(UP_FILES_FS_PATH."/background_image/thumb/".$sv_image);
                }
                if(strlen($sv_image) && file_exists(UP_FILES_FS_PATH."/background_image/original/".$sv_image)){
                $imgsrcBig=UP_FILES_WS_PATH."/background_image/original/".$sv_image;
                $size=getimagesize(UP_FILES_FS_PATH."/background_image/original/".$sv_image);
                $img=ImageCreateFromJPEG($imgsrcBig);
                $width=ImageSx($img); // Original picture width is stored
                $height=ImageSy($img); // Original picture height is stored
                }
            ?>
        <tr>
            <td>( Current Image )</td>
            <td>
            <?php if($imgsrc){ ?>
            <a href="<?=$imgsrc?>" target="_blank" title="<?php echo $page_title; ?>"><IMG SRC="<?=$imgsrc?>" BORDER="1"  HSPACE="10" width="120" height="100" VSPACE="5" style="overflow:scroll;" ALT="<?=$page_title?>" TITLE="<?=$page_title?>"  ></a>
            <?php }else{ echo 'No Images'; } ?>
            <br />
</td>
        </tr>
        <tr>
            <td colspan="2">&nbsp; </td>
        </tr>
        <?php }*/ ?>
        
        <tr>
          <td class="lightGrayBg">&nbsp;</td>
          <td align="left" class="lightGrayBg"><input class="button" type=submit name="submit" value="Save"></td>
       </tr>
   </table>
</form>
    <script type="text/javascript">
		 var oFCKeditor=new FCKeditor('page_text')	;
		 oFCKeditor.BasePath="fckeditor/";	
		 oFCKeditor.ReplaceTextarea();
	 </script>
    </td></tr></table>    
  <? include("bottom.inc.php");?>