<?php
$pgMod="image_upload";
$pgAct="view";

if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;


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

			echo "error==>Please Enter image_head type ";

		}
			
if($flgEr!=TRUE && $pgAct=="add")
	{
          
			
			$targetDir = "uploads/gallery/";
			 $allowTypes = array('jpg','png','jpeg','gif');
			
			if(!empty(array_filter($_FILES['files']['name']))){
        foreach($_FILES['files']['name'] as $key=>$val){
			
			$fileName = time(). "-".basename($_FILES['files']['name'][$key]); 
		// $fileName = basename($_FILES['files']['name'][$key]); 
			 $_POST["image_name"]=$fileName;
 
             //echo $LinksDetails["url_root"];
 
           $targetFilePath = $targetDir . $fileName;
		   // Check whether file type is valid
            $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
			 if(in_array($fileType, $allowTypes)){
			if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)) {
			

	   $flgIn=$db->insertAry("image_upload",$_POST);
	   			$_SESSION['msg']='Saved Successfully';
	  /* if(!is_null($flgIn))
	    {
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
			
		}*/
			}
			 }
	   
		}
	 }
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("image_head",$_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"=>"Notice", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("image_upload","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"=>"Notice", "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></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>image_head</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM image_head WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");

}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{

?>
<!-- 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">
					<h3><i class="icon-cog"></i> Upload Image</h3>
				</div>
				<!-- / Forms: Top Bar -->

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

						
                        <div class="control-group">
							<label class="control-label" for="Name">image_head </label>
							<div class="controls">
								<select  id="image_head"   name="head"  >
              <option value="0" >Select</option>
              <?php
              $sliderContent=$db->getRows("select head,id from image_head ");


              foreach($sliderContent as $sdetail){
                    ?>
              <option  value="<?php echo $sdetail['id'];?>" <?php  if($aryForm['id']==$sdetail['id'])echo 'selected="selected"';?> ><?php echo $sdetail['head'];?></option>
              <?php } ?>
            </select>
							</div>
						</div>
                       
                        <div class="control-group">
							<label class="control-label" for="linkname"> image_upload  </label>
							<div class="controls">
								<input type="file" name="files[]" multiple >

							</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=Notice&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button"><?php echo $pgMod; ?></button>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Notice", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT image_head.head,image_head.id, COUNT(image_upload.head) as count FROM `image_upload` inner join  image_head where image_upload.head=image_head.id GROUP by image_upload.head "); $i=0;

	if(is_array($aryList) && count($aryList)>0)
	{
		?>

        <table class="table" id="example">
            
            <thead>
                <tr>
                    <th>sr no.</th>

                    <th>image_head </th>
        
                    <th>image count</th>
        
                    <th>image</th>
                    
	            </tr>
            </thead>
            
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
          
        
        $imagedata=$db->getRows("select id,image_name from image_upload where head='".$clist['id']."'");
       
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    
                    <td><?php echo $i ?></td>
                    
                    <td><?php echo $clist["head"] ?></td>
                    			
                    <td><?php echo $clist["count"] ?></td>
                  <td>
                        <?php 
                        foreach($imagedata  as $img)
                        {
                         $aryPgAct["id"]=$img['id'];    
                         $aryPgAct['action']="delete";   
                     ?>
                   <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>')"  title="Delete"> <img src="uploads/gallery/<?php echo $img['image_name']; ?>" height="50" width="50"></a>
                     
                  <?php     }
                       ?>
                    
                    </td>
                   
                </tr>
            <?php } ?>
            </tbody>
            <tfoot>
                <tr>
                	<th></th>
                    <th></th>
                    <th colspan="2"></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>image_head</b> Record not found </i>
        </div>
        <?php
	} 
	?>
	</div></div></div>
	<?php
}
?>
<style>
.login {
  background: url(../img/user.png) no-repeat 6px center;
  width: 100px;
  height: 100px;
  display: block;
}
</style>
<style>
.top-bar-minimize{display:none;}
thead, tfoot {display: table-header-group;}
.top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 96.7% !important;overflow: inherit !important;} 
</style>
<!-- / Forms -->