<?php
$pgMod="student_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")
		{

$class=$db->getRows("select class from class");

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

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

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

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

				{

					$lnewfile=md5(microtime()).".".$lext;
					if(move_uploaded_file($_FILES['upload_notice']['tmp_name'],"uploads/notice/".$lnewfile))

					{

						$aryData['upload_notice']=$lnewfile;	

					}


				}

			}

foreach($class as $c)
   
   {
   	$aryData=array(	'notice_type'	=>	'N/A',

						'class'	=>	$c['class'],
						'type'	=>	2,

						'notice_detail'	=>	POST('notice_detail'),

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

						'school_id'   =>$_SESSION['admin']['school_id'],
						
						'session'   =>$_SESSION['admin']['session'],

						'date'	=> date('Y-m-d'),

						'status'	=> 1

							);

if(isset($_FILES["upload_notice"]["name"]) && !empty($_FILES["upload_notice"]["name"]))
{
  $aryData['upload_notice']=$lnewfile;   
}
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['classname']) &&  $_REQUEST['classname']=="" )
{
$_REQUEST['classname']="";

}
*/


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

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

						'class'	=>	$_REQUEST['classname'],
						'type'	=>	2,

						'notice_detail'	=>	POST('notice_detail'),

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

						'school_id'   =>$_SESSION['admin']['school_id'],
						
						'session'   =>$_SESSION['admin']['session'],

						'date'	=> date('Y-m-d'),

						'status'	=> 1

							);

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

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

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

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

				{

					$lnewfile=md5(microtime()).".".$lext;
					if(move_uploaded_file($_FILES['upload_notice']['tmp_name'],"uploads/notice/".$lnewfile))

					{

						$aryData['upload_notice']=$lnewfile;	

					}

				}

			}
			
		
			
if(isset($_FILES["upload_notice"]["name"]) && !empty($_FILES["upload_notice"]["name"]))
{
  $aryData['upload_notice']=$lnewfile;   
}
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("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("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  id='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("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">
          
          <label class="control-label" for="type">Class</label>
          
          <div class="controls">
            <select  id="classname"   name="classname" onchange="getsubname('classname')" >
              <option value="" >Select</option>
               <option value="all" >All Class</option>
              <?php
              $sliderContent=$db->getRows("select class from class where school_id=".$_SESSION['admin']['school_id']);
              foreach($sliderContent as $sdetail){
                    ?>
              <option  value="<?php echo $sdetail['class'];?>" <?php  if($aryForm['class']==$sdetail['class'])echo 'selected="selected"';?> ><?php echo $sdetail['class'];?></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">
							<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"   >
							</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='./?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("page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM student_notice "); $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>Class</th>
                    <th>Date</th>

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

            </thead>
            
            <tbody>
            <?php foreach($aryList 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><?php  echo   substr($clist['notice_detail'],0,100);	?>
</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>
                  

                    </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
}
?>
<!-- / Forms -->