<?php
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
$obj = new DBClass();
$obj5 = new DBClass();
$cupage = 'page';
$headin = 'Add New Page';
$buton = 'Publish Page';
$cpage = basename($_SERVER['PHP_SELF']).'?'.$_SERVER['QUERY_STRING'];
$sql3= "SELECT * FROM ".TBL_PAGES;
$rs3 = $obj->executeQry($sql3,$link);
$row3 = $obj->fetchrow();	 

if(isset($_GET['action']) && $_GET['action'] == 'edit'){
	$headin = 'Edit Page';
	$buton = 'Edit Page';
	$obj2 = new DBClass();
	$sql = "SELECT * FROM ".TBL_PAGES." WHERE page_id = '".$_GET['id']."' ";
	$rs = $obj2->executeQry($sql,$link);
	$row = $obj2->fetchrow();
}

$sql = "SELECT max(page_order) as max_order FROM ".TBL_PAGES;
$rs1 =  $obj->executeQry($sql,$link);
$row1 = $obj->fetchRow();
$maxorder = $row1['max_order'] + 1;

if(isset($_POST['submit'])){

	if($_POST['submit'] == 'Publish Page'){
		if($row3['page_name'] == $_POST['page_name']){
			$error[] = 'A Page with same name already exist....';
		} else {
			
			$image = $_FILES['page_image']['name'];
			if(isset($_FILES['page_image']['name']) && $_FILES['page_image']['name']!='')
			{
				move_uploaded_file($_FILES['page_image']['tmp_name'],'../page_images/'.$_FILES['page_image']['name']);
			}
			$sqll = "INSERT INTO ".TBL_PAGES." (page_name,page_head,page_content,page_meta_title,page_meta_key,page_meta_desc,page_order,page_parent,page_image) VALUES ('".trim($_POST['page_name'])."','".trim($_POST['page_head'])."','".trim(htmlentities(addslashes($_POST['page_content'])))."','".trim($_POST['page_meta_title'])."','".trim($_POST['page_meta_key'])."','".trim($_POST['page_meta_desc'])."','".$maxorder."','".trim($_POST['page_parent'])."','$image')";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: page.php?msg=add');
			}
		}	
	}
	if($_POST['submit'] == 'Edit Page'){
// 		if($row3['page_name'] == $_POST['page_name']){
// 			$error[] = 'A Page with same name already exist....';
// 		} else {
			$img ='';
			
			if(isset($_FILES['page_image']['name']) && $_FILES['page_image']['name']!='')
			{
				if(move_uploaded_file($_FILES['page_image']['tmp_name'],'../page_images/'.$_FILES['page_image']['name'])!='')
				{
					$img = $_FILES['page_image']['name'];
				}
				else
				{
					$img = '';
				}
			}
			if($img == '')
			{
				$img = $row['page_image'];
			}
			$sqll = "UPDATE ".TBL_PAGES." SET page_name = '".trim($_POST['page_name'])."', page_head = '".trim($_POST['page_head'])."', page_content = '".trim(htmlentities(addslashes($_POST['page_content'])))."', page_meta_title = '".trim($_POST['page_meta_title'])."', page_meta_key = '".trim($_POST['page_meta_key'])."', page_meta_desc = '".trim($_POST['page_meta_desc'])."', page_parent = '".trim($_POST['page_parent'])."',page_image = '$img' WHERE page_id = '".$_GET['id']."'";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: page.php?msg=edit');
			}
		//}
	}

}

require_once(INCLUDES_DIR.ADMHEADER); 
include_once(ABS_ROOT_PATH."fckeditor/fckeditor.php") ;
?>
<link href="<?=EDITOR_DIR;?>sample.css" rel="stylesheet" type="text/css" />
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="29" align="center" class="submenu"><a href="page.php">Manage Pages</a></td>
    <td align="center" class="submenu">Add New Page</td>
  </tr>
</table>
<table width="1000" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="17"></td>
  </tr>
  <tr>
    <td><table width="910" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td class="pagehead"><table width="900" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="315" height="35"><?php echo $headin;?></td>
            </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top" bgcolor="#FFFFFF" class="innermain">
        	<form name="pageform" id="pageform" method="post" action="" enctype="multipart/form-data">

        <table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="25">&nbsp;</td>
          </tr>
          <tr>
            <td valign="top" class="bdr4"><table width="800" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td bgcolor="#990066" class="form_head">Page Name &amp; Heading</td>
                </tr>
              <tr>
                <td valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td align="left" width="25%">Page Name: </td>
                    <td align="left" width="75%"><input type="text" name="page_name" id="page_name" value="<?php echo ($_POST['page_name'] == '') ? $row['page_name'] : $_POST['page_name']; ?>" /></td>
                    </tr>
                  <tr>
                    <td>Page Heading:</td>
                    <td><input type="text" name="page_head" id="page_head" value="<?php echo ($_POST['page_head'] == '') ? $row['page_head'] : $_POST['page_head']; ?>" /></td>
                    </tr>
                    <tr>
                    <td>Parent Page:</td>
                    <td><select name="page_parent" id="page_parent">
                    <option value="0"></option>
                    <?php
						$sql3= "SELECT * FROM ".TBL_PAGES." where page_parent = '0' AND status = '0'";
						$rs3 = $obj5->executeQry($sql3,$link);
						//$row3 = $obj->fetchrow();
						while($row3 = $obj5->fetchrow()) { ?>
							<option value="<?php echo $row3['page_id'] ?>" <?php echo ($row['page_parent']==$row3['page_id'])?'selected="selected"':''; ?> ><?php echo $row3['page_name'] ?></option>
						<? }
					 ?>
                    </select></td>
                    </tr>
                    <?php if(isset($row['page_image']) && $row['page_image']!='') { ?>
                    <tr>
                    <td>
                    current image
                    </td>
                    <td>
                    <img src="../page_images/<?php echo $row['page_image']?>" height="50" width="200" border="1" />
                    </td>
                    </tr>
                    <?php } ?>
                    <tr>                             
                    <td>
                    Page image:
                    </td>
                    <td>
                    <input type="file" name="page_image" id="page_image" value="" >
                    </td>
                    </tr>
                  </table></td>
              </tr>
              <tr>
                <td bgcolor="#990066" class="form_head">Page Content</td>
              </tr>
              <tr>
                <td valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
			<td align="center">
				<?php
				$oFCKeditor = new FCKeditor('page_content') ;
				$oFCKeditor->BasePath =  SITE_URL.'fckeditor/' ;
				$oFCKeditor->Width = 600;
				$oFCKeditor->Height = 400;
				$oFCKeditor->Value = ($_POST['page_content'] == '') ? html_entity_decode(stripslashes($row['page_content'])) : $_POST['page_content'];
				$oFCKeditor->Create() ;
				
				?>				
			</td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td bgcolor="#990066" class="form_head">SEO Content</td>
              </tr>
              <tr>
                <td align="left" valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td>Meta Title:</td>
                    <td><input type="text" name="page_meta_title" id="page_meta_title" value="<?php echo ($_POST['page_meta_title'] == '') ? $row['page_meta_title'] : $_POST['page_meta_title']; ?>" /></td>
                  </tr>
                  <tr>
                    <td>Meta Keyword:</td>
                    <td><input type="text" name="page_meta_key" id="page_meta_key" value="<?php echo ($_POST['page_meta_key'] == '') ? $row['page_meta_key'] : $_POST['page_meta_key']; ?>" /></td>
                  </tr>
                  <tr>
                    <td>Meta Description:</td>
                    <td><input type="text" name="page_meta_desc" id="page_meta_desc" value="<?php echo ($_POST['page_meta_desc'] == '') ? $row['page_meta_desc'] : $_POST['page_meta_desc']; ?>" /></td>
                  </tr>
                  </table></td>
              </tr>
              <tr>
                <td align="right" valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td><input name="submit" type="submit" class="bg_bt" id="button" value="<?php echo $buton;?>" /></td>
                  </tr>
                  </table></td>
              </tr>
              </table></td>
          </tr>
          </table>
          </form>
          </td>
      </tr>
      </table>
      
      </td>
  </tr>
  <tr>
    <td height="40">&nbsp;</td>
  </tr>
</table>

<?php require_once(INCLUDES_DIR.ADMFOOTER); ?>
