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

if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(isset($_FILES["photo"]["name"]) && !empty($_FILES["photo"]["name"])){
		$lfilename = basename($_FILES['photo']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['photo']['tmp_name'],"media/photo/".$lnewfile))
		{
			$_POST["photo"]=$lnewfile;
		}
	}
	//echo "<pre>";print_r($_POST);exit;
	if($flgEr!=TRUE && $pgAct=="add")
	{
        $_POST["status"]=1;
       
		$_POST["school_id"]=$_SESSION["admin"]["school_id"];
		$_POST["added_by"]=$_SESSION["admin"]["uid"];
		$flgIn=$db->insertAry("staff_role",$_POST);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			//sms($_POST["mobile"],"Dear ".$_POST["name"].", You are successfully joined  ".$LinksDetails["site_name"]." , heartly congrates from ".$LinksDetails["site_name"]." campus.","faculty_enrollment");
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Faculty", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
			
		}
	}
	elseif($pgAct=="permission"){
		//echo "<pre>";
		foreach($_POST["name"] as $name){
			$all=0;$none=0;
			if($_POST["permission-".$name][0]=="all")$all=1;
			if($_POST["permission-".$name][0]=="none")$none=1;
			$aryData=array(	'page_name'	=>	trim($name),
								'a'	=>	$all,
								'n'	=>	$none,
								'role_id'=>	$_GET["id"]		
				);

			$is_check=$db->getVal("select id from staff_privilages where school_id='".$_SESSION["admin"]["school_id"]."' and page_name='".$name."' and role_id='".$_GET["id"]."'");

			if($is_check!=""){

				$flgIn=$db->updateAry("staff_privilages",$aryData," where school_id='".$_SESSION["admin"]["school_id"]."' and page_name='".$name."' and role_id='".$_GET["id"]."'");
			}else{
            $aryData["school_id"]=$_SESSION["admin"]["school_id"];
            $aryData["added_by"]=$_SESSION["admin"]["uid"];

			$flgIn=$db->insertAry("staff_privilages",$aryData);
			}
		}
		if($db->getErMsg()=="")
		{
			$_SESSION['msg']='Saved Successfully';

		}else{
			$alert_err[0]=$db->getErMsg();
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("staff_role",$_POST,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
	
		if(!is_null($flgUp))
		{
			//echo $db->getlastQuery();exit;
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			//sms($_POST["mobile"],"Dear ".$_POST["name"].", Your detail is updated successfully in ".$LinksDetails["site_name"],"faculty_enrollment");
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Faculty", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	} 
}
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$flgUp=$db->delete("staff_role","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"=>"Faculty", "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 staff_role WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{ 
	if(isset($_SESSION['form']))
	{
		$aryForm=$_SESSION['form'];
		unset($_SESSION['form']);
	}
?>
<!-- Forms --> 
<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>
<form method="post" action="" id="printableDiv" enctype="multipart/form-data">
     <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Create New Role</h3>
                <div style="float:right; margin-top:5px">
                	<button type="button" onclick="printDiv('printableDiv')" class="btn btn-primary">Print</button>
                    <button type="submit" class="btn btn-primary"> Save</button>
                    <button type="reset" class="btn"> Cancel</button>
                </div>
            </div>
            <div class="well">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				
				<!-- / Forms: Top Bar -->

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

                        <div class="control-group">
							<label class="control-label" for="name">Role Name</label>
                            <div class="controls">
                                    <input type="text" id="name" name="role_name"  class="input-block-level" value="<?php echo $aryForm["role_name"]; ?>" required>
                                </div>
                        </div>

				</div>

			</div>

            </div>
        </div>
        
	</div>
</form>
<!-- / Forms -->
<?php
}

elseif($pgAct=="permission" && isset($_GET['id']) && trim($_GET['id']) !=''){
	error_reporting(22);
	
// 	$files1 = array("Circular","MarkAttendance","AssignHomework","SubmitResult","Performance","ApplyLeave","AddResultGrade","ReportCardEntry","MyAttendance",
// 	"LiveClass","LiveClassMeet","SchoolDues","AddExam","AddTimetable","Syllabus","YoutubeLive","ExamAssessment","AddAssignment","ExamResult","GPS","LiveClassAll",
//         "LiveClassReport","StudentDetail","MyTimetable","TeacherMarkAttendance","Timetable","OnlineContent","OnlineContentInternal", "AddPractice",
//         "SyllabusCovered","FaceAttendance","StudentAttendanceReport","FaceAttendanceLog", "UpdateHouseColor", "OmrScan", "SalarySlip", "InventoryRecord", 
//         "CollectionGraph", "OutstandingGraph", "StudentAttendanceGraph","TeacherAttendanceGraph","NewStudent","Calendar","DailyActivity","BusDetail",
//         "HostelDetail","LiveGPSTracking","CummulativeTermWiseReport","TaskReport","SectionUpdate","BusAttendance","ExamQuestion");	
        
        
        $files1 = array(
            "Leave", "Calendar", "Attendance Log", "Payslip", "Notice", "Helpline", "Profile", "Attendance", "Chat", "Setting"
            );
        
        ?>
    
    <form method="post" action="">
	<table class="responsive display table table-bordered" width="100%" id="postList">
		<thead>
        	<tr>
            	<th class="first"><div>Sr.No</div></th>
				<th>Page Name</th>
                <th>All</th>
                <th class="last" width="25px">NoAccess</th>
            </tr>
       </thead>
       <tbody>
	<?php $i=0;
	foreach($files1 as $images)
	{
		{$i++;
		$per=checkStaffPermission($images,$_GET["id"]);
			?>
            <tr>
            	<td><?php echo $i;?></td>
            	<td><input type="hidden" name="name[]" value="<?php echo $images; ?>" /><?php echo $images; ?></td>
                <td><input type="radio" name="permission-<?php echo $images; ?>[]" value="all" <?php if($per=="all")echo "checked"; ?> /></td>
                <td><input type="radio" name="permission-<?php echo $images; ?>[]" value="none" <?php if($per=="none")echo "checked"; ?> /></td>
            </tr>
			<?php
		}
	}
	?></tbody>
    </table>
    <div class="form-row row-fluid">
                <div class="span12">
                    <div class="row-fluid">
                        <div class="form-actions">
                        <div class="span3"></div>
                        <div class="span9 controls">
                            <button type="submit" class="btn btn-info">Save changes</button>
                            <button type="button" onclick="window.location=''" class="btn">Cancel</button>
                        </div>
                        </div>
                    </div>
                </div> 
            </div>
    </form>
	<?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=Faculty&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"=>"Faculty", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select * from staff_role where school_id='".$_SESSION["admin"]["school_id"]."'  order by role_name");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>Serial No</th>
                    <th>Role Name</th>

                    <th>Action</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                </tr>
            </tfoot>
            <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 $clist["role_name"] ?></td>

                    <td>
                     <?php $aryPgAct['action']="edit"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Edit</button></a>
                    <?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> !-->
                <a href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>&id=<?php echo $clist['id'] ?>" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>  
                <?php $aryPgAct['action']="permission"; ?>
                         <a href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>" class="confirmation"><button type="button" class="btn btn-default">permission</button></a>  
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?>
	</div></div></div>
	<?php
}
?>