<?php
$pgMod="content";
$pgAct="view";
error_reporting(0);
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
//echo"<pre>";print_r($_SESSION);exit;
if($_POST)
{

			 
//echo "<pre>";print_r($_REQUEST);print_r($_FILES);exit;	

	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	$date=date("Y-m-d");


		if(!isset($_POST['Name']) || trim($_POST['Name'])=='')
		{
			$flgEr=TRUE;

			echo "error==>Please enter  name";

		}elseif(!isset($_POST['ctype']) || trim($_POST['ctype'])=='')

		{

			$flgEr=TRUE;

			echo "error==>Please Select Content type ";

		}

		else{

			$socailData=json_encode($_POST["social"]);

			$status=0;

			if(isset($_POST["status"]))$status=1;

			$aryData=array(	'status'=>$status,

						'Name'=>POST('Name'),

						'type'=>POST('ctype'),

						'lorder'=>POST('lorder'),
						
						'date'=>$date,

						'icon'=>$_POST['icon'],

						'link'=>POST('link'),

						'videolink'=>POST('videolink'),

						'extra' =>$socailData,

						'content'=>POST('content')

						);

			if(isset($_FILES["lpimg"]["name"]) && !empty($_FILES["lpimg"]["name"]))

			{

				$lfilename = basename($_FILES['lpimg']['name']);

				$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));

				if(!in_array($lext,array('exe','html','htm','php')))

				{

					$lnewfile=md5(microtime()).".".$lext;

					//echo $_FILES['lpimg']['tmp_name'],"../uploads/gallery/".$lnewfile;

					if(move_uploaded_file($_FILES['lpimg']['tmp_name'],"./uploads/content/".$lnewfile))

					{

						$aryData['image']=$lnewfile;	

					}

				}

			 }

			 $flgIn=$db->insertAry("content",$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());
			
		}

			}
	
	
	
	if($flgEr!=TRUE && $pgAct=="add")
	{
		
           $_POST["added_by"]=$_SESSION["admin"]["uid"];
		   if(isset($_POST['status']))
			$_POST["status"]=1;
			else $_POST["status"]=0;
			
	   $flgIn=$db->insertAry("content",$_POST);
	   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'])!='')
	{
		$flgUp=$db->updateAry("content",$_POST,"where added_by='".$_SESSION["admin"]["uid"]."' and id='".$_GET['id']."'");
		if(!is_null($flgUp))
		{
			$_SESSION['msg']='update 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("content","where  id='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"cms", "page_id"=>$pgMod)));
} 
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 content WHERE  id='".$_GET['id']."'");
	
	
	
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
	//echo "<pre>";print_r($aryForm);echo "</pre>".$aryForm['root'];
?>
<!-- Forms --> 
	<div class="row-fluid">
        <form method="post" enctype="multipart/form-data" action="">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
                <?php 
				if($pgAct=="edit"){
					?>
					<h3><i class="icon-cog"></i> Update Your Content </h3>
					<?php
                    }else{
				?>
					<h3><i class="icon-cog"></i> Add New Content </h3>
				
                <?php } ?>
                </div>
                
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">

						
                        <div class="control-group">
							<label class="control-label" for="Name">Title/Heading</label>
							<div class="controls">
								<input type="text" id="Name" name="Name" class="input-block-level" value="<?php if($_POST)echo $_POST["Name"]; else  echo $aryForm["Name"]; ?>"  >
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="icon">Icon</label>
							<div class="controls">
								<input type="text" id="icon" name="icon" class="input-block-level" value="<?php if($_POST)echo $_POST["icon"]; else  echo $aryForm["icon"]; ?>"  >
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="Name">Type</label>
							<div class="controls">
								<!--<input type="text" id="type" name="ctype" class="input-block-level" value="<?php if($_POST)echo $_POST["Name"]; else  echo $aryForm["Name"]; ?>"  > -->

                                <select id="type" name="ctype" class="form-control" onchange="checkTest(this)">

          <?php

                    $pages=$db->getRows("select * from content where status=1");

                    foreach($pages as $c){

                    ?>

          <option value="<?php echo $c["heading"]; ?>" <?php echo selected($c['heading'],$aryForm['type']); ?>><?php echo $c["linkname"]; ?></option>

          <?php } ?>

        </select> 
        
							</div>
						</div>
                        
                   <div <?php if($aryForm["type"]!="TESTIMONIALS"){echo 'style="display:none"';} ?> id="socialDiv">

          	<?php $socialData=json_decode($aryForm["extra"]); ?>
                        
                        <div class="control-group">
							<label class="control-label" for="twitter_url">Twitter url</label>
							<div class="controls">
								<input type="text" name="social[twitter_url]" id="twitter_url" class="input-block-level" value="<?php echo $socialData->twitter_url ?>"  >
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="facebook_url">Facebook url</label>
							<div class="controls">
								<input type="text" name="social[facebook_url]" id="facebook_url" class="form-control" value="<?php echo $socialData->facebook_url ?>"  >
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="youtube_url">Youtube url</label>
							<div class="controls">
								<input type="text" name="social[youtube_url]" id="youtube_url" class="form-control" value="<?php echo $socialData->youtube_url ?>"  >
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="google_url">Google url</label>
							<div class="controls">
								<input type="text" name="social[google_url]" id="google_url" class="form-control" value="<?php echo $socialData->google_url ?>"  >
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="linked_url">Linked url</label>
							<div class="controls">
								<input type="text" name="social[linked_url]" id="linked_url" class="form-control" value="<?php echo $socialData->linked_url ?>"  >
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="pinterest_url">Pinterest url</label>
							<div class="controls">
								<input type="text" name="social[pinterest_url]" id="pinterest_url" class="form-control" value="<?php echo $socialData->pinterest_url ?>"  >
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="rss_url">Rss url</label>
							<div class="controls">
								<input type="text" name="social[rss_url]" id="rss_url" class="form-control" value="<?php echo $socialData->rss_url ?>"  >
							</div>
						</div>
                        
                    </div>  
                        
                        <div class="control-group">
							<label class="control-label" for="lpimg">Image</label>
							<div class="controls">
								<input type="file"  name="lpimg" class="input-block-level"  >
                                 <?php

					  if($aryForm['image']!='')

					  {

						

						 ?>

        <br/>

        <a href="<?php echo URL_ROOT ?>uploads/slider/<?php echo $aryForm['image'];?>">Download Attachment</a>

        <?php

					  }

					  			?>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="lorder">Order</label>
							<div class="controls">
								<input type="text" id="lorder" name="lorder" class="input-block-level" value="<?php echo $aryForm['lorder']; ?>">
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="link">Link</label>
							<div class="controls">
								<input type="text" id="link" name="link" class="input-block-level" value="<?php echo $aryForm['link']; ?>">
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="lorder">Video link</label>
							<div class="controls">
								<input type="text" id="videolink" name="videolink" class="input-block-level" value="<?php echo $aryForm['videolink']; ?>">
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="content">Content</label>
							<div class="controls">
								<textarea name="content" class="form-control textarea" rows="10" ><?php echo unPOST($aryForm['content']); ?></textarea>
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="linkname"> Activate  </label>
							<div class="controls">
								<input name="status" type="checkbox" value="1" <?php if($aryForm['status']==1)echo "checked"; ?> > Check this to activate above Content.

							</div>
						</div>
                        
						<div class="form-actions">
                        
							<button type="submit" class="btn btn-primary"> Save</button>
							<button type="button" class="btn"> Cancel</button>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

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

        </form>
	</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);
		$range=$LinksDetails["record_per_page"];

		$page="";$filterAry=array();
		if(isset($_GET["range"]))$range=$_GET["range"];
		if(isset($_GET['nextpage'])) $page=$_GET['nextpage'];
		if (isset($_GET['search']))
		{
			$search = $_GET['search'];
			$filterAry[] =" (type like '%" . $search . "%' or Name like '%" . $search . "%'  ) ";
		}
		if (isset($_GET['online']))
		{
			$filterAry[] =" is_online=1 ";
		}
		if (isset($_GET['pending']))
		{
			$filterAry[] =" status=1 ";
		}
		if (isset($_GET['active']))
		{
			$filterAry[] =" status=2 ";
		}
		$sqlfilter="";
		if(is_array($filterAry) && count($filterAry)>0)$sqlfilter=implode(" AND ",$filterAry);
		if($sqlfilter=="")$sqlfilter =" 1 ";
		$pagination['table']		=	"content" ;
		$pagination['targetpage']	=	"index.php" ;
		$pagination['sqlfilter']	=	$sqlfilter ;
		$pagination['size']			=	$range;
		$pagination['nextpage']		= 	$page ;
		$pagination['option']		= 	"page_id=".$pgMod ;
		$resultData 				= 	$objPagination->getData($pagination);
		$i=0;
	//print_r($resultData);
	
	//echo"error==><pre>yesss";echo $db->getLastQuery();exit;
		$aryList=$resultData["data"];//$db->getRows($sql);
		//echo"error==><pre>";echo $db->getLastQuery();exit;
		//$sqlLimit="SELECT * FROM ".EVENT." ORDER BY event_id ASC ";
		//$aryList=$db->getRows($sqlLimit);
		if(is_array($aryList))
		{
		  if(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="page_id" value="<?php echo $pgMod; ?>" />
              <input style="width:200px" class="search-bar form-control" placeholder="Enter username or Type" 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>

        <div class="table-responsive">
          <table  class="table table-bordered">
            <thead>
              <tr>
                <th class="first"><div></div></th>
                <th><a href="#" title="linkname">LinkName </a></th>
                <th><a href="#" title="linkname">Name</a></th>
                <th><a href="#" title="Underof">Section</a></th>
                <th><a href="#" title="status">Status</a></th>
                <th class="last" >Actions</th>
              </tr>
            </thead>
            <tbody>
              <?php
			 //$question_ary=array();
				foreach($aryList as $iList)
				{
					 //echo"==>error<pre>";print_r($iList);exit;
					$aryPgAct["id"]=$iList['userId'];
					
				    $aryPgAct['page_id']=$pgMod;
					//echo"==>error<pre>";print_r($iList);exit;
					//echo"==>error<pre>";print_r($iList['class']);exit;
					
					$question_ary=json_decode($iList["question"],true);
					//$question_ary=json_decode($clist["question"],true);
					//echo"==>error<pre>";print_r($iList);print_r($clist["question"]);exit;
					
					
				 ?>
              <tr id="tr_<?php echo $iList['userId'] ?>">
                <td></td>
                
                
                <td class="text-slash"><?php echo $iList['link']; ?></td>
                <td class="text-slash"><?php echo ucwords($iList['Name']); ?></td>
               
			    <td class="text-slash"><?php echo $db->getVal("select linkname from content where heading='".$iList["type"]."' and status=1"); ?></td>
               
			    <td class="text-slash">
                  <?php if( $iList["status"]==1){ ?>
                  <span class="btn btn-info" >Active</span>
                  <?php }else{ ?>
                  <span  class="btn btn-danger">Inactive</span>
                  <?php } ?>
                </td>

                <td class="text-slash">
                    <?php $aryPgAct['action']="edit"; ?>
                          <a href="<?php echo URL_ADMIN_HOME."?page_id=".$pgMod."&id=".$iList['id']."&action=edit";?>"><button type="button" class="btn btn-info">Edit</button></a>

                    <?php $aryPgAct['action']="delete"; ?>
                         <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME."?page_id=".$pgMod."&id=".$iList['id']."&action=delete";?>')"><button type="button" class="btn btn-danger">Delete</button></a>

                </td> 

              </tr>
              <?php			
				  }
				?>
            </tbody>
          </table>
        </div>
        <?php echo $resultData["pagestring"]; ?>
        
        <?php
		   }
			else
			{
			  echo '<div class="alert alert-info alert-dismissable">
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
						<strong>Notice !</strong> Sorry No record Found. </div>';
			}
		}
		else
		{
			echo '<div class="alert alert-danger alert-dismissable">
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
						<strong>error !</strong> '.$db->getErMsg().'. </div>';
		}
			  ?>
    
    
	</div></div></div>
	<?php
}
?>
<!-- / Forms -->