<?php
$pgMod="cms";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	
	
	if(trim($_POST['lorder'])!='' && is_numeric($_POST['lorder']) && $pgAct=="add")
	{
	$orderno=$db->getVal("SELECT lorder FROM cms WHERE lorder='".$_POST['lorder']."' ");
	}
	elseif(trim($_POST['lorder'])!='' && is_numeric($_POST['lorder']) && $pgAct=="edit")
	{
	$orderno=$db->getVal("SELECT lorder FROM cms WHERE lorder='".$_POST['lorder']."' AND id <>'".$_GET['id']."'");
	}
	
	elseif(!isset($_POST['linkname']) || trim($_POST['linkname'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Link name.");
	}
	elseif(!preg_match ("#^[-A-Za-z\&\#0-9\;' .]*$#", $_POST['linkname']))
	{
		$flgEr=TRUE;
		array_push($alert_err,"Link name with special characters are not allowed");
	}
	elseif(!isset($_POST['heading']) || trim($_POST['heading'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Heading.");
	}

	elseif(!isset($_POST['pbody']) || trim($_POST['pbody'])=='' || trim($_POST['pbody'])=='<br>')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Page body.");
	}
	elseif(trim($_POST['footer']=='1') && $_GET['action']=='add')
	{
		$footerlink=$db->getVal("SELECT count(id) FROM cms WHERE footer=1");
		if($footerlink>=2)
		{
		$flgEr=TRUE;
		array_push($alert_err,"You can add maximum 7 Footer Links.");
		}
	}/*
	elseif(trim($_POST['footer']=='1') && $_GET['action']=='edit')
	{
		
		$footerlink=$db->getVal("SELECT count(id) FROM cms WHERE footer=1 AND id<>'".$_GET['id']."'");
		
		if($footerlink>=2)
		{
		$flgEr=TRUE;
		array_push($alert_err,"You can add maximum 2 Footer Links.");
		}
	}*/
	elseif($_POST["pagetype"]==0)
	{
		$flgEr=TRUE;
		array_push($alert_err,"You must Select Page type ");
	}
	elseif($_POST["pagetype"]==2 && $_POST['underof']==0)
	{
		$flgEr=TRUE;
		array_push($alert_err,"you must select Under Of page ");
	}
	
	if($flgEr!=TRUE && $pgAct=="add" && count($alert_err)==0)
	{
			$aryData=array(	'linkname'	=>	trim($_POST['linkname']),
							'heading'	=>	trim($_POST['heading']),
						    'lorder'	=>	trim($_POST['lorder']),
						    'header'=>$_POST['header'],
							'topheader'=>$_POST['topheader'],
							
							
							'short_description'=>trim($_POST['short_description']),
							'footer'=>$_POST['footer'],
							'meta_title'=>	trim($_POST['meta_title']),
							'meta_description'	=>	trim($_POST['meta_description']),
							'meta_keywords'	=>	trim($_POST['meta_keywords']),
							'pbody'=>addslashes($_POST['pbody']),
							'status'			=>$_POST['status'],
							'sidebar'	=>	$_POST["sidebar"],
							'type'		=>	$_POST['pagetype'],
							'underof'	=>	$_POST['underof']);
			$aryData["school_id"]=$_SESSION["admin"]["school_id"];
       		$aryData["added_by"]=$_SESSION["admin"]["uid"];
			$flgIn=$db->insertAry("cms",$aryData);
			if(!is_null($flgIn))
			{
				$_SESSION['msg']='Saved Successfully';
				unset($_SESSION['form']);
				redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"cms", "page_id"=>$pgMod)));
			}
			else
			{
				array_push($alert_err,$db->getErMsg());
			
			}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='' && count($alert_err)==0)
	{
			$aryData=array(	'linkname'	=>	trim($_POST['linkname']),
							'heading'	=>	trim($_POST['heading']),
						    'lorder'	=>	trim($_POST['lorder']),
							'meta_title'=>	trim($_POST['meta_title']),
							'meta_description'	=>	trim($_POST['meta_description']),
							'short_description'=>trim($_POST['short_description']),
							'meta_keywords'	=>	trim($_POST['meta_keywords']),
							'pbody'=>addslashes($_POST['pbody']),
							'header'=>$_POST['header'],
							'topheader'=>$_POST['topheader'],
							'footer'=>$_POST['footer'],
							'status'			=>$_POST['status'],
							'sidebar'	=>	$_POST["sidebar"],
							'type'		=>	$_POST['pagetype'],
							'underof'	=>	$_POST['underof']);
			
			
			$flgUp=$db->updateAry("cms",$aryData,"where id='".$_GET['id']."'");
			if(!is_null($flgUp))
			{	
				$_SESSION['msg']='Saved Successfully';
				unset($_SESSION['form']);
				redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"cms", "page_id"=>$pgMod)));
			}
			else
			{
				array_push($alert_err,$db->getErMsg());
			
			}
	} 
}
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
		
		$res=$db->delete(CMS,"where 	id='".$_GET['id']."'");
		if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; 
		redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"cms", "page_id"=>$pgMod)));
		}
		else { array_push($alert_err,$db->getErMsg()); }
		
}

if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM ".CMS." WHERE id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
	if(isset($_SESSION['form']))
	{
		$aryForm=$_SESSION['form'];
		unset($_SESSION['form']);
	}
?>
<script>
function hide_underof(valu)
{
	if(valu==1){
		$('#section_underof').hide('slow');
		$("#under").val('0');
	}
	else if(valu==2){
		$('#section_underof').show('slow');}
	else{
		alert("please select Page type");
		$('#section_underof').hide('slow');
		$("#under").val('0');
	}	
}
function insertlink(ids)
{
	if ($(ids).attr("checked") == "checked"){
	  $("#elink").show();
	} else {
	  $("#elink").hide();
	}
}
<?php if(isset($_GET["id"]) && $_GET["id"]!=""){ ?>
$(document).ready(function(){
	insertlink($("#external"));
});
<?php } ?>
</script>
<!-- Forms --> 
	<div class="row-fluid">
        
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Create a New Page</h3>
				</div>
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
                <form method="post" action="">
				<div class="well">

						<div class="control-group">
							<label class="control-label" for="category"> Link Name </label>
							<div class="controls">
								<input type="text" name="linkname" id="name" value="<?php echo $aryForm['linkname']; ?>" class="input-block-level"></input>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="category"> Page Heading </label>
							<div class="controls">
								<input type="text" name="heading" id="name" value="<?php echo $aryForm['heading']; ?>" class="input-block-level"></input>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="category"> Page type </label>
							<div class="controls">
								<select  id="type" onchange="hide_underof(this.value)" class="span12 m-wrap" name="pagetype">
                    <option value="0">Select</option>
                    <option value="1" selected="selected">Primary</option>
                    <option value="2"<?php echo selected("2",$aryForm['type']); ?>>Secondary</option>
                    </select>
							</div>
						</div>
                        
                        <div class="control-group" <?php if($pgAct=="add") echo 'style="display:none"'; else if($aryForm['type']=='0') echo 'style="display:none"';  ?>  id="section_underof">
							<label class="control-label" for="category"> Under of </label>
							<div class="controls">
								<select id="under" name="underof" class="span12 m-wrap">
                    <option value="0">Select Page</option>
                    <?php
                    $pages=$db->getRows("select * from cms where status=1");
                    foreach($pages as $c){
                    ?>
                    <option value="<?php echo $c["id"]; ?>"<?php echo selected($c['id'],$aryForm['underof']); ?>><?php echo $c["linkname"]; ?></option>
                    <?php } ?>
                    </select>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="category"> Order </label>
							<div class="controls">
								<input type="text" name="lorder" id="lorder" value="<?php echo $aryForm['lorder']; ?>" class="input-block-level"></input>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="category"> Meta Title </label>
							<div class="controls">
								<input type="text" name="meta_title" id="meta_title" value="<?php echo $aryForm['meta_title']; ?>" class="input-block-level"></input>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="category"> Meta Keywords </label>
							<div class="controls">
								<input type="text" name="meta_keywords" id="meta_keywords" value="<?php echo $aryForm['meta_keywords']; ?>" class="input-block-level"></input>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="category"> Meta Description </label>
							<div class="controls">
								<textarea id="meta_description"  style="width:99%" rows="10" name="meta_description"><?php echo $aryForm['meta_description']; ?></textarea>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="category"> Short Description </label>
							<div class="controls">
								<textarea id="short_description"  style="width:99%" rows="10" name="short_description"><?php echo $aryForm['short_description']; ?></textarea>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="category"> Page Body </label>
							<div class="controls">
								<textarea id="editor" class="ckeditor" style="width:99%" rows="10" name="pbody"><?php echo stripcslashes($aryForm['pbody']); ?></textarea>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="category"> Position </label>
							<div class="controls">
								 <input type="checkbox" value="1" name="header" <?php if($aryForm['header']==1){ echo "checked";} ?> />&nbsp;Header&nbsp;&nbsp; 
                                 <input type="checkbox" value="1" name="topheader" <?php if($aryForm['topheader']==1){ echo "checked";} ?> />&nbsp;Top Header&nbsp;&nbsp; 
                                 
                                 
                       <input type="checkbox" value="2" name="footer"  <?php if($aryForm['footer']==1){ echo "checked";} ?> />&nbsp;Footer
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="category"> Status </label>
							<div class="controls">
								 <select name="status" class="span12 m-wrap">
                            <option value="1"<?php echo selected("1",$aryForm['status']); ?>>Active</option>
                            <option value="0"<?php echo selected("0",$aryForm['status']); ?>>Inactive</option>
                        </select>
							</div>
						</div>
                        
						<div class="form-actions">
							<button type="submit" class="btn btn-primary"> Save</button>
							<button type="button" class="btn"> Cancel</button>
						</div>
                        
				</div>
                </form>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box -->

        
	</div>
<?php   
}else{
	?>
    
<link href="<?php echo $LinksDetails["url_root"];?>assets/css/custom.css" rel="stylesheet" type="text/css" />

	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    <button style="float:right; margin-top:5px" onclick="window.location='./?src=cms&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"cms", "page_id"=>$pgMod,"action"=>"add");
	$range="15";
	$page="";
	$filterAry=array();
	
	if(isset($_GET["range"]))$range=$_GET["range"];
	if(isset($_GET['nextpage'])) $page=$_GET['nextpage'];
	$sqlfilter	=	"  status='1' and school_id= ".$_SESSION["admin"]["school_id"];
	$sqlfilter .= " order by id DESC ";
	
	if (isset($_GET['search']))
		{
			$search = $_GET['search'];
			$filterAry[] =" (lorder like '%" . $search . "%' or heading like '%" . $search . "%'  ) ";
		
		}
	if(is_array($filterAry) && count($filterAry)>0)$sqlfilter=implode(" AND ",$filterAry);
	$pagination['table']		=	CMS ;
	$pagination['targetpage']	=	"index.php" ;
	$pagination['sqlfilter']	=	$sqlfilter ;
	$pagination['size']			=	$range;
	$pagination['nextpage']		= 	$page ;
	$pagination['option']		= 	"page_id=".$pgAct ;
	$resultData 				= 	$objPagination->getData($pagination);
	$i=0;
	
	
	
	
	
	
	$aryList=$resultData["data"];//$db->getRows($sql); $i=0;
	$paggings=$resultData["pagestring"];
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <div class="pull-right">
          <form method="get" action="index.php" style="float:right; margin:3px;">
            <div class="input-group margin-bottom">
                <input type="hidden" name="src" value="cms" />
              <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
              <input style="width:200px" class="search-bar form-control" placeholder="Enter username or emailid" value="<?php echo $_GET["search"] ?>" id="appendedInputButton" size="26" name="search" type="text">
              <button class="input-group-addon" type="submit">Go!</button>
            </div>
          </form>
        </div>
        <table class="table">
       	   <thead>
            <tr>
                <th class="first"><div></div></th>
                <th><a href="#" title="linkname">Link Name</a></th>
                <th><a href="#" title="Underof">Underof</a></th>
                <th><a href="#" title="Location">Location</a></th>
                <th><a href="#" title="url">Url</a></th>
                <th><a href="#" title="status">Status</a></th>
                <th class="last">Actions</th>
            </tr>
            </thead>
            <tbody>
            <?php
			foreach($aryList as $iList)
			{
				$aryPgAct["id"]=$iList['id'];
				$position="";
				if($iList['header']==1)$position.=" <small class='label pull-right bg-green'>Header</small>"; 
				if($iList['footer']==1)$position.=" <small class='label pull-right bg-green'>Footer</small>"; 
				if($iList['m_left']==1)$position.=" <small class='label pull-right bg-green'>SideBar</small>";
			 ?>
             <tr>
              <td><?php echo ucwords($iList['lorder']); ?></td>
             <td><?php echo ucwords($iList['heading']); ?></td>
             <td><?php echo $position; ?></td>
             <td><?php if($iList['underof']==0)
			 				echo "Parent";
						else 
							echo $db->getVal("select linkname from cms where id=".$iList['underof']); 
					?></td>
                <td><?php echo href("page.php","page_id=".$iList["id"]."&type=pages") ?></td>
                <td><?php if($iList["status"]==1)echo '<span class="label label-success">active</span>';else echo '<span class="label label-important">banned</span>'; ?></td>
               <td>
                     <?php $aryPgAct['action']="edit"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Edit</button></a>
                    <?php $aryPgAct['action']="delete"; ?>
                         <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>')" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>  
                    </td>
             </tr>
              <?php			
			  }
			?>
            </tbody>
            <tfoot>
                <tr>
                    <th colspan="5">
						<?php echo $paggings; ?>
                    </th>
                </tr>
            </tfoot>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?>
	</div></div></div>
	<?php
}
?>
<!-- / Forms -->