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


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



if($_POST)
{
//echo"<pre> ";print_r($_REQUEST);echo"</pre>";exit;
	
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	
		$date=date("d,m,y");

		$studentList=array();
		
		
		
if($_REQUEST['classname']=="all")
{

$staff=$db->getRows("select name,idno from staff");


$contentid=$db->getVAL("select max(content_id) from student_notice");
foreach($staff as $st)

{
   $aryData=array(	'notice_type'	=>	'N/A',

					'faculty'	=>	$st['idno'],
					'type'	=>	1,

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

					'Added_by'   =>$_SESSION['admin']['uid'],
					
					'date'	=>	$_POST['date'],

					'school_id'   =>$_SESSION['admin']['school_id'],
					
					'session'   =>$_SESSION['admin']['session'],
                	'content_id'	=> $contentid + 1,
				
				

					'status'	=> 1

						);
					

$targetDir = "uploads/notice/";
		 $allowTypes = array('jpg','png','jpeg','gif','pdf');
	if(!empty(array_filter($_FILES["upload_notice"]["name"]))){
    foreach($_FILES["upload_notice"]["name"] as $key=>$val){
		
		$fileName = basename($_FILES["upload_notice"]["name"][$key]); 
        $a=str_replace(' ', '_', $fileName);
        $targetFilePath = $targetDir . $a;
	   // Check whether file type is valid
        $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
		 if(in_array($fileType, $allowTypes)){
		if(move_uploaded_file($_FILES["upload_notice"]["tmp_name"][$key], $targetFilePath)) {
		}
		$aryData['upload_notice'].=$fileName.',';
		 }
   
	}
		$rffileName=str_replace(' ', '_', $aryData['upload_notice']);
		$aryData['upload_notice']=rtrim($rffileName,',');
 }
	
else
{
$aryData['upload_notice']='N/A';    
}
          $_POST["added_by"]=$_SESSION["admin"]["uid"];
		  $_POST['school_id']=$_SESSION['admin']['school_id'];
					
 	      $flgIn=$db->insertAry("student_notice",$aryData);






	}


    
}
else{
		

if(isset($_REQUEST['notice_type']) && ($_REQUEST['notice_type']==0 || $_REQUEST['notice_type']=="") )
{
$_REQUEST['notice_type']="N/A";

}
//$contentid=$db->getRow("select max(content_id) from student_notice");
			$aryData=array(	'notice_type'	=>	'N/A',

						'faculty'	=>	$_REQUEST['classname'],
						'type'	=>	1,

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

						'Added_by'   =>$_SESSION['admin']['uid'],
						

						'school_id'   =>$_SESSION['admin']['school_id'],
							'content_id'	=> $db->getVAL("select max(content_id) from student_notice")+1,
						
						'session'   =>$_SESSION['admin']['session'],

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

						'status'	=> 1

							);
							print_r($aryData);
							//die();


	$targetDir = "uploads/notice/";
			 $allowTypes = array('jpg','png','jpeg','gif','pdf');
		if(!empty(array_filter($_FILES["upload_notice"]["name"]))){
        foreach($_FILES["upload_notice"]["name"] as $key=>$val){
			
			$fileName = basename($_FILES["upload_notice"]["name"][$key]); 
	
            $a=str_replace(' ', '_', $fileName);
           $targetFilePath = $targetDir . $a;
		   // Check whether file type is valid
            $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
			 if(in_array($fileType, $allowTypes)){
			if(move_uploaded_file($_FILES["upload_notice"]["tmp_name"][$key], $targetFilePath)) {
			
			}
			$aryData['upload_notice'].=$fileName.',';
	
			 }
	   
		}
			$rffileName=str_replace(' ', '_', $aryData['upload_notice']);
			$aryData['upload_notice']=rtrim($rffileName,',');
	 }
		
else
{
   $aryData['upload_notice']='N/A';    
}
		
		     $_POST["added_by"]=$_SESSION["admin"]["uid"];
			  $_POST['school_id']=$_SESSION['admin']['school_id'];
						
	 	    $flgIn=$db->insertAry("student_notice",$aryData);	

		}		

	
if($flgEr!=TRUE && $pgAct=="add")
	{
              

			if(!is_null($flgIn))

			{

				$_SESSION['msg']='Saved Successfully';
				unset($_SESSION['form']);
				redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Notice", "page_id"=>$pgMod)));
			}

			else

			{

			array_push($alert_err,$db->getErMsg());

			}

	}

	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{

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

		$flgUp=$db->updateAry("student_notice",$aryData,"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("student_notice","where  content_id='".$_GET['cid']."'");
	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>complain</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>Student Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM student_notice WHERE  id='".$_GET['id']."'");
//echo "<pre>";echo $db->getLastQuery()."<br>";print_r($aryForm);exit;

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

 ?>
<!-- Forms --> 

<script language="javascript">

	function getsubname(subname)
	{
	var c=$("#classname").val();
	
	$.post("newajax.php",{type:"getsubname",clsname:c,subname:subname},function(data){
			$("#subdiv").html(data);
		});
	}
	
	$(document).ready(function(){
		getsubname($('#hidden_sname').val());
	});

</script>


<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>

	<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> Create a New Notice</h3>
				</div>
				<!-- / Forms: Top Bar -->

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

 //             $sliderContent=$db->getRows("select notice_type from notice where school_id=".$_SESSION['admin']['school_id']);

//echo "<pre>";print_r($sliderContent);echo"<pre>";
?>           

						
                    <!--    <div class="control-group">
							<label class="control-label" for="Name">Notice type</label>
					    <div class="controls">
            <select  id="notice_type"   name="notice_type"  >
              <option value="0" >Select</option>
              <?php
              $sliderContent=$db->getRows("select notice_type,id from notice where school_id=".$_SESSION['admin']['school_id']);


              foreach($sliderContent as $sdetail){
                    ?>
              <option  value="<?php echo $sdetail['id'];?>" <?php  if($aryForm['id']==$sdetail['id'])echo 'selected="selected"';?> ><?php echo $sdetail['notice_type'];?></option>
              <?php } ?>
            </select>
            
            <?php if($pgAct=="edit" && isset($_REQUEST['id']) && trim($_REQUEST['id']) !=''){
            ?>
            <input type="hidden"  id="hidden_sname" value="<?php echo $aryForm['subject'];?>">
            <?php } ?>
          </div>
						</div> 
						!-->
						
			  <div class="control-group">
            <div class="control-group" style="margin-left:-2%;">
							<label class="control-label" for="idate"> Date</label>
							<div class="controls" style="width:20%;">
								<input type="text" id="date" name="date" class="input-block-level datepicker" value="<?php echo  date('Y-m-d '); ?>">
							</div>
						</div>
          <label class="control-label" for="type">Teacher</label>
          
          <div class="controls">
            <select  id="classname"   name="classname" onchange="getsubname('classname')" >
              <option value="" >Select</option>
               <option value="all" >All Teacher</option>
              <?php
              $sliderContent=$db->getRows("select name,idno from staff where school_id=".$_SESSION['admin']['school_id']);
              foreach($sliderContent as $sdetail){
                    ?>
              <option  value="<?php echo $sdetail['idno'];?>" <?php  if($aryForm['name']==$sdetail['name'])echo 'selected="selected"';?> ><?php echo $sdetail['name'];?></option>
              <?php } ?>
            </select>
            
            <?php if($pgAct=="edit" && isset($_REQUEST['id']) && trim($_REQUEST['id']) !=''){
            ?>
            <input type="hidden"  id="hidden_sname" value="<?php echo $aryForm['name'];?>">
            <?php } ?>
          </div> 
          
        </div>			
						
                        
                        <div class="control-group">
							<label class="control-label" for="Name">Upload attatchments</label>
							<div class="controls">
								<input type="file" id="upload_notice" name="upload_notice[]" class="input-block-level" multiple  >
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="detail">Notice Detail</label>
							<div class="controls">
								<textarea name="notice_detail" id="notice_detail" class="input-block-level" style="resize:none" ><?php echo $aryForm["notice_detail"]; ?></textarea>
							</div>
						</div>
                        
					

                       
                        <!--<div class="control-group" style="display:none">
                          <label class="control-label" for="type">Subject Name</label>
                          <div class="controls" id="subdiv">
                            <select  id="subjectname"  class="form-control" name="subjectname">
                              <option value="0" >Please select Class name first</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>
				<!-- / Forms: Content -->

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

        </form>
	</div>
<?php   
}else{
?>
<style>
.hoverclass{
hover:hoverclass;
}
</style>    

	<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">Add <?php echo $pgMod; ?></button>
 
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Notice", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM student_notice where session='".$_SESSION["admin"]["session"]."' and  type='1' group by content_id order by id  DESC "); $i=0;
	

//echo "<pre>";print_r($aryList);echo "<pre>";


	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            
            <thead>

                <tr>

                    <th>Sr No.</th>
                    
                  

                    <th>Notice Detail</th>

                <!--<th>Message</th>-->
                    <th>Date</th>

                    <th>Action</th>
                    
	            </tr>

            </thead>
            
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 


			
                     $aryPgAct["id"]=$clist['id'];
                     $aryPgAct["cid"]=$clist['content_id'];
             ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    
                    <td><?php echo $i ?></td>
                    
			      

					<td><?php  echo   $clist['notice_detail'];	?>
</td>
              		<!--<td><?php echo  $clist['class']; ?> </td>-->
                    	
                	<td><?php echo date("d/m/Y", strtotime($clist['date']));  ?> </td>
                    						
                    <td>
                   
                    <?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>
                    <button style="margin-top:5px" onClick="window.location='./?src=Notice&cid=<?php echo $clist['content_id']?>&page_id=<?php echo $pgMod; ?>&action=viewclass'" class="btn btn-info" type="button">View All Teacher</button>
  <?php  
  
  $r=$clist['upload_notice'];
 if($r==""){
 
                			   
                			    ?>

  <a      class="btn btn-primary"   rel="noopener noreferrer" >No File</a>
                        <?php } 
                        else{
                         $res=explode(",", $clist['upload_notice']);
                			 // print_r($res)  ;
                			    foreach($res as $result){
                        ?>
                              <a href="uploads/notice/<?php echo $result; ?>"
                        class="btn btn-primary" target="_blank"  rel="noopener noreferrer" >View File</a>
                      <?php  }}
                        ?>
                    </td>
                
                </tr>
            <?php } ?>
            </tbody>
            
            <tfoot>
                <tr>
                	<th></th>
                	<th></th>
                    <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>complain</b> Record not found </i>
        </div>
        <?php
	} 
	?>
	</div></div></div>
	<?php
}

if($pgAct=="viewclass" )
{   ?>
    
	<div class="row-fluid">
       
			<!-- Forms: Box -->
			<div class="span12" style="
    margin-top: 3%;
">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> All Classes</h3>
				</div>
			
				<div class="well no-padding">
				     <table class="table" id="example">
            
            <thead>

                <tr>

                    <th>Sr No.</th>
                    
                  

                    <th>Teacher Name</th>

                    <!--<th>Class</th>-->
                    <th>Date</th>

                   
                    
	            </tr>

            </thead>
            
            <tbody>
            <?php 
            	$aryListv=$db->getRows("SELECT *,staff.name as stname FROM student_notice left join staff on student_notice.faculty=staff.idno  where student_notice.session='".$_SESSION["admin"]["session"]."' and content_id='".$_GET['cid']."' "); $iv=0;
            foreach($aryListv as $clistv){ $iv++; 


			
                    
             ?>
                <tr class="grade<?php if($iv%2==0)echo "C";else "U";?>">
                    
                    <td><?php echo $iv ?></td>
                    
			      

					<!--<td><?php  echo  $clistv['notice_detail'];	?>-->
              		<td><?php echo  $clistv['stname']; ?> </td>
                    	
                	<td><?php echo date("d/m/Y", strtotime($clistv['date']));  ?> </td>
                    						
                    
                </tr>
            <?php } ?>
            </tbody>
            
            
            
        </table>
             
	</div>


<?php 
   
}

?>
<!-- / Forms -->

<script>
     $(function() {
	$(".datepicker").each(function(index, element) {
        var d = new Date(); // for now
		datetext=d.getHours();
		$( this ).datepicker({
		  changeMonth: true,
		  changeYear: true,
		  showOn: "button",
		  buttonImage: "js/images/calendar.gif",
		  buttonImageOnly: true,
		  buttonText: "Select date",
		  dateFormat: 'yy-mm-dd ',
		  onSelect: function(datetext){
			var d = new Date(); // for now
			datetext=datetext;
			$(this).val(datetext);
		  }
		});
    });
});
</script>
