<?php 
//error_reporting(E_ALL);
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
$cupage = 'page';
if(isset($_GET['msg'])){
	if($_GET['msg'] == add){
		$info[] = 'Page Added Successfully';
	}
	if($_GET['msg'] == edit){
		$info[] = 'Page Updated Successfully';
	}
}

$page = $_GET['page'];
$obj = new DBClass();
$obj5 = new DBClass();

if(isset($_GET['action']) && $_GET['action']=="order")
{              
		$sq = "select * from ".TBL_PAGES." where page_id = '".$_GET['page_id']."'";
		$rt = $obj->executeQry($sq,$link);
		$rwt = $obj->fetchRow();
		$objs = new DbClass();
		$objr = new DbClass();
		$objt = new DbClass();
		$objn = new DbClass();
		$sqls = "Update ".TBL_PAGES." set page_order = '".$_GET['order']."' where page_id = '".$_GET['page_id']."'"; 
		$rst = $objs->executeQry($sqls,$link);
		if($rst) {
			$sqlr = "select * from ".TBL_PAGES." where page_id != '".$_GET['page_id']."'order by page_order";
			$rsr = $objt->executeQry($sqlr,$link);
			$rowc = $objt->getRow();
			$i=1;
			while($rowr=$objt->fetchRow()){
			if($i!=$_GET['order']) {
			$sql = "UPDATE ".TBL_PAGES." set page_order = '".$i."' where page_id = '".$rowr['page_id']."'";
			$rsp = $objn->executeQry($sql,$link); 
			$i++; }
			else {
				$i=$i+1;
				$sql = "UPDATE ".TBL_PAGES." set page_order = '".$i."' where page_id = '".$rowr['page_id']."'";
				$rsp = $objn->executeQry($sql,$link);
				$i++;
			}
			
			}
		}
	@header('Location: page.php');
}

if(isset($_GET['action']) && $_GET['action'] == 'statchng'){
	$sql = "SELECT * FROM ".TBL_PAGES." WHERE page_id = ".$_GET['pageid'];
	$rs = $obj->executeQry($sql,$link);
	$rows = $obj->fetchRow();
	$rowNum = $obj->getRow();
	if ($rowNum > 0){
		$updt = $rows['status']=='0'?'1':'0';
		$sql2 = "UPDATE ".TBL_PAGES." SET status = '".$updt."' WHERE page_id = ".$_GET['pageid'];
		$rs2 = $obj->executeQry($sql2,$link);
		if($rs2){
			$info[] = "Status Updated Successfully...!";
			//@header('Location: page.php');
		}
	}
}

if(isset($_GET['action']) && $_GET['action'] == 'delete'){
	$sql1 = "DELETE FROM ".TBL_PAGES." WHERE page_id = '".$_GET['id']."' ";
	$rs1 = $obj->executeQry($sql1,$link);
	if($rs1){
		$info[] = 'Record deleted successfully';
}               }

$sql = "SELECT * FROM ".TBL_PAGES;
$rs = $obj->executeQry($sql,$link);
$totalrows = $obj->getrow();

$limit = 10;
if(empty($page)) {
$page = 1;
}
$limitvalue = ($page - 1) * $limit;
$adjacents = 3;

$sql = "SELECT * FROM ".TBL_PAGES." ORDER BY page_order LIMIT $limitvalue, $limit";
$rs = $obj->executeQry($sql,$link);
$row = $obj->fetchrow();
$rownum = $obj->getrow();

if($rownum < 1){
	$error[] = 'No records forund........';
}

if($page > 1){     
	$prevpage = $page-1;
	//$pagination = '<a href=\''.ADMPAGES.'?page='.$prevpage.'\'>'.$prevpage.'</a>&nbsp;';
}

$numofpages = ceil($totalrows / $limit);
$lpm1 = $numofpages - 1;
if ($numofpages < 7 + ($adjacents * 2))
{    
	for ($counter = 1; $counter <= $numofpages; $counter++){
		if($page == $counter)
		$pagination .= $counter."&nbsp;";
		
		else 
		$pagination .= '<a href=\''.ADMPAGES.'?page='.$counter.'\'>'.$counter.'</a>&nbsp;';
		
	}
}

if ($numofpages >= 7 + ($adjacents * 2))
{
	if($page < 1 + ($adjacents * 3)){
		for ($counter = 1; $counter <= 4 + ($adjacents * 2); $counter++){
			if($page == $counter)
			$pagination .= $counter."&nbsp;";
			
			else 
			$pagination .= '<a href=\''.ADMPAGES.'?page='.$counter.'\'>'.$counter.'</a>&nbsp;';
			
		}
		$pagination .= "...";
		$pagination .= "<a href=\"" . ADMPAGES."?page=" . $lpm1 . "\">$lpm1</a>&nbsp;";
		$pagination .= "<a href=\"" . ADMPAGES."?page=" . $numofpages . "\">$numofpages</a>&nbsp;";
	}
	
	elseif($numofpages - ($adjacents * 2) > $page && $page > ($adjacents * 2))
	{
		$pagination .= "<a href=\"" . ADMPAGES."?page=" . "1\">1</a>&nbsp;";
		$pagination .= "<a href=\"" . ADMPAGES."?page=" . "2\">2</a>&nbsp;";
		$pagination .= "...";
		for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
		{
			if ($counter == $page)
				$pagination .= $counter;
			else
				$pagination .= "<a href=\"" . ADMPAGES."?page=" . $counter . "\">$counter</a>&nbsp;";					
		}
		$pagination .= "...";
		$pagination .= "<a href=\"" . ADMPAGES."?page=" . $lpm1 . "\">$lpm1</a>&nbsp;";
		$pagination .= "<a href=\"" . ADMPAGES."?page=" . $numofpages . "\">$numofpages</a>&nbsp;";		
	}
	else
	{
		$pagination .= "<a href=\"" . ADMPAGES."?page=" . "1\">1</a>&nbsp;";
		$pagination .= "<a href=\"" . ADMPAGES."?page=" . "2\">2</a>&nbsp;";
		$pagination .= "...";
		for ($counter = $numofpages - (1 + ($adjacents * 3)); $counter <= $numofpages; $counter++)
		{
			if ($counter == $page)
				$pagination .= $counter;
			else
				$pagination .= "<a href=\"" . ADMPAGES."?page=" . $counter . "\">$counter</a>&nbsp;";					
		}
	}

}
?>
<?php require_once(INCLUDES_DIR.ADMHEADER); ?>
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="29" align="center" class="submenu">Manage Pages</td>
    <td align="center" class="submenu"><a href="add_page.php">Add New Page</a></td>
	<td align="center" class="submenu"><a href="page_hindi.php">Manage Hindi Pages</a></td>
	<td align="center" class="submenu"><a href="members_page.php">Manage Members Pages</a></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">Manage Pages</td>
            <td width="391"></td>
            <td width="63"><!--Sort By:--></td>
            <td width="131"><!--<select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)">
              <option>item1</option>
            </select>--></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top" bgcolor="#FFFFFF"><table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="25"><?php ShowError($error); ShowInfo($info);?></td>
          </tr>
          <!--<tr>
            <td class="bg_message"><table width="800" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="39"><img src="images/icon_mess.jpg" width="39" height="26" /></td>
                <td>&nbsp;</td>
              </tr>
            </table></td>
          </tr>-->
          <tr>
            <td height="9"></td>
          </tr>
          <tr>
            <td></td>
          </tr>
<?php if($rownum > 0) { ?>
          <tr>
            <td class="bdr1"><table width="801" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td height="8" class="bg1" ><img src="../images/bg1.PNG" width="7" height="8" /></td>
              </tr>
              <tr>
                <td><table width="801" border="0" cellpadding="0" cellspacing="0" class="main">
                  <tr>
                    <td width="60" height="30" align="center" valign="middle" class="bdr2"> S. No</td>
                    <td width="254" align="center" valign="middle" class="bdr2"> Title</td>
                    <td width="200" align="center" valign="middle" class="bdr2">Parent</td>
                    <td width="70" align="center" valign="middle" class="bdr2">Image</td>
                    <td width="71" align="center" valign="middle" class="bdr2">Status</td>
                    <td width="140" align="center" valign="middle" class="bdr3">Actions</td>
                  </tr>
<?php           
		$i = 1;
		do {
			$stat = ($row['status'] == '0')?'<img src="'.IMAGE_DIR.'icon_status1.png" name="Image62" width="28" height="24" border="0" id="Image62" title="Active" />':'<img src="'.IMAGE_DIR.'deactive_icon.png" border="0" title="Inactive" />';

?>

                  <tr class="<?php echo (($i%2) == 0) ? "bg_td1" : "bg_td1";?>">
                    <td align="center"><?php echo $i; ?></td>
                    <td><?php echo $row['page_name'];?></td>
                    <td>
                    <?php 
					if($row['page_parent']>0) {
					$sql1 = "SELECT * FROM ".TBL_PAGES." where page_id = '".$row['page_parent']."'";
$rs1 = $obj5->executeQry($sql1,$link);
$row1 = $obj5->fetchrow(); 
echo $row1['page_name'];
					}
					else {
						echo '';	
					}
?>
                    </td>
                    <td align="center"><a href="#"><img src="../page_images/<?php echo $row['page_image'] ?>" name="Image63" width="50" height="50" border="0" id="Image63" /></a></td>
                    <td align="center"><a href="page.php?action=statchng&pageid=<?=$row['page_id'];?>&status=<?=$row['page_status'];?>&page=<?php echo $page;?>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image62','','../images/icon_status2.png',1)"><?=$stat;?></a></td>
                    
                    <td align="center">&nbsp; &nbsp;&nbsp;<a href="view_page.php?id=<?php echo $row['page_id'];?>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image64','','../images/icon_view1.png',1)"><img src="../images/icon_view.png" name="Image64" width="28" height="24" border="0" id="Image64" /></a>&nbsp;&nbsp;
			<a href="add_page.php?action=edit&id=<?php echo $row['page_id'];?>" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image65','','../images/icon_edit2.png',1)"><img src="../images/icon_edit1.png" name="Image65" width="28" height="24" border="0" id="Image65" /></a>
			<a href="page.php?action=delete&id=<?=$row['page_id'];?>&page=<?php echo $page;?>" onClick="if(confirm('Are you sure, You want delete this record?')){location.replace('page.php?action=delete&id=<?=$row['page_id']?>');}return false;" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image66','','../images/icon_del2.png',1)"><img src="../images/icon_del1.png" name="Image66" width="28" height="24" border="0" id="Image66" /></a>
			</td>
                  </tr>
		<?php $i++; } while($row = $obj->fetchRow());
		
		?>
                  </table></td>
              </tr>
              <tr>
                <td height="2" class="bg2"><img src="../images/bg2.PNG" width="12" height="2" /></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          </table></td>
      </tr>
      <tr>
        <td valign="top" class="bg_innerfoot"><table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td align="right" class="footer_num"><?php if ($rownum > 0)echo $pagination;?></td>
            </tr>
          </table></td>
      </tr><?php } ?>
      </table></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>

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