<?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_MEMBERPAGE;
$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_MEMBERPAGE." WHERE id = '".$_GET['id']."' ";
	$rs = $obj2->executeQry($sql,$link);
	$row = $obj2->fetchrow();
}


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

	if($_POST['submit'] == 'Publish Page'){
		if(($row3['page_name'] == $_POST['page_name']) && ($row3['MemberID'] == $_POST['MemberID'])){
			$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'],'../download/page_images/'.$_FILES['page_image']['name']);
			}
			$sqll = "INSERT INTO ".TBL_MEMBERPAGE." (page_name,page_head,page_content,MemberID,page_image) VALUES ('".trim($_POST['page_name'])."','".trim($_POST['page_head'])."','".trim(htmlentities(addslashes($_POST['page_content'])))."','".trim($_POST['MemberID'])."','$image')";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: members_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'],'../download/page_images/'.$_FILES['page_image']['name'])!='')
				{
					$img = $_FILES['page_image']['name'];
				}
				else
				{
					$img = '';
				}
			}
			if($img == '')
			{
				$img = $row['page_image'];
			}
			$sqll = "UPDATE ".TBL_MEMBERPAGE." SET page_name = '".trim($_POST['page_name'])."', page_head = '".trim($_POST['page_head'])."', page_content = '".trim(htmlentities(addslashes($_POST['page_content'])))."', MemberID = '".trim($_POST['MemberID'])."',page_image = '$img' WHERE id = '".$_GET['id']."'";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: members_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="members_page.php">Manage Members Page</a></td>
    <td align="center" class="submenu">Add New Members 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"><div class="err_msg" style="margin-left:10px;"><?php ShowError($error); ShowInfo($info);?></div></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%">Select Page: </td>
                    <td align="left" width="75%"><select id="page_name" name="page_name">
						<option value="">---Select Page--</option>
						<option value="Home Page" <?php echo ($row['page_name']=='Home Page')?'selected="selected"':''; ?>>Home Page</option>
						<option value="Our Services" <?php echo ($row['page_name']=='Our Services')?'selected="selected"':''; ?>>Our Services</option>
						<option value="Our Products" <?php echo ($row['page_name']=='Our Products')?'selected="selected"':''; ?>>Our Products</option>
					</select></td>
                    </tr>
					<tr>
                    <td>Select Member:</td>
                    <td><select name="MemberID" id="MemberID">
                    <option>--Select Member--</option>
                    <?php
						$sql3= "SELECT * FROM ".TBL_DIRECTORY." where status = '0'";
						$rs3 = $obj5->executeQry($sql3,$link);
						//$row3 = $obj->fetchrow();
						while($row3 = $obj5->fetchrow()) { ?>
							<option value="<?php echo $row3['memberID'] ?>" <?php echo ($row['MemberID']==$row3['memberID'])?'selected="selected"':''; ?> ><?php echo $row3['organisation_name'] ?></option>
						<? }
					 ?>
                    </select></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>
                    
                    <?php if(isset($row['page_image']) && $row['page_image']!='') { ?>
                    <tr>
                    <td>
                    current image
                    </td>
                    <td>
                    <img src="../download/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 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); ?>
