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

if($_POST)
{
   $ch = curl_init();
        $cfile = new CURLFile($_FILES['image']['tmp_name'], $_FILES['image']['type'], $_FILES['image']['name']);
 
        $data = array("myimage"=>$cfile);
 
        $url = "https://bps.savarnacloudtech.com/api/gallery.php";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        $responce = curl_exec($ch);

        redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"website", "page_id"=>$pgMod)));
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
    $ch = curl_init();
            $data = array("image"=>$_GET['img'],"action"=>$_GET['action'],"id"=>$_GET['id'],"page_id"=>$_GET['page_id']);
            $url = "https://bps.savarnacloudtech.com/api/delete.php";
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
            $responce = curl_exec($ch);
            redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"website", "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 slider WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
//	echo "<pre>";print_r($aryForm);echo "</pre>".$aryForm['root'];exit;
?>
<!-- Forms --> 
	<div class="row-fluid">
        <form method="post" action="" enctype="multipart/form-data">
			<!-- Forms: Box -->
			<div class="span12">

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

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

						<input type="hidden" name="temp" value="">
                        
                        <div class="control-group">
							<label class="control-label" for="heading">Image</label>
							<div class="controls">
								<input type="file" id="image" name="image" class="input-block-level" >
							</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{
	?>
	<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=website&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"=>"website", "page_id"=>$pgMod,"action"=>"add");

            $url = "https://bps.savarnacloudtech.com/api/get_gallery.php";

            $context  = stream_context_create($options);
            $result = file_get_contents($url, false, $context);

            $temp = json_decode($result,true);
            // $aryList = $temp['slider'];

            $i=0;
            ?>
            <table class="table" id="example"> 
                <thead>
                    <tr>
                        <th>S.No.</th>
                        <th>Image</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach($temp as $clist){ $i++; 
                $aryPgAct["id"]=$clist['id'];
                ?>
                    <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                        <td><?php echo $i; ?></td>
                        <td><img src="<?php echo $clist['slider']; ?>" style="width:50px;height:50px;" /></td>
                        <td>
                            <?php $aryPgAct['action']="delete";
                             $aryPgAct['img']=$clist['gallery'];
                            ?>
                            <a  href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>  
                        </td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
            <?php
        
        ?>
        </div>
  </div>
</div>
	<?php
}
?>
<!-- / Forms -->