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


if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['name']) || trim($_POST['name'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter roll Name.");
	}
	if($pgAct=="permission"){
		//echo "<pre>";
		foreach($_POST["name"] as $name){
			$read=0;$write=0;$all=0;$none=0;
			if($_POST["permission-".$name][0]=="read")$read=1;
			elseif($_POST["permission-".$name][0]=="write")$write=1;
			elseif($_POST["permission-".$name][0]=="all")$all=1;
			elseif($_POST["permission-".$name][0]=="none")$none=1;
			$aryData=array(	'page_name'	=>	trim($name),
								'a'	=>	$all,
								'n'	=>	$none,
								'roll_id'=>	$_GET["id"]		
				);
			//echo "<pre>";print_r($aryData);
			$is_check=$db->getVal("select id from tbl_privilages where school_id='".$_SESSION["admin"]["school_id"]."' and page_name='".$name."' and roll_id='".$_GET["id"]."'");
			if($is_check!=""){
				$flgIn=$db->updateAry("tbl_privilages",$aryData," where school_id='".$_SESSION["admin"]["school_id"]."' and page_name='".$name."' and roll_id='".$_GET["id"]."'");
			}else{
				
                $aryData["school_id"]=$_SESSION["admin"]["school_id"];
                $aryData["added_by"]=$_SESSION["admin"]["uid"];

				$flgIn=$db->insertAry("tbl_privilages",$aryData);
			}
		} 
		if($db->getErMsg()=="")
		{
			$_SESSION['msg']='Saved Successfully';
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod,"action"=>$pgAct,"id"=>$_GET["id"])));
		}else{
			$alert_err[0]=$db->getErMsg();
		}
	}
	elseif($flgEr!=TRUE && $pgAct=="add")
	{
			$aryData=array(	'name'	=>	$_POST['name'],
			                 "school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
							'status'=>	trim($_POST['status'])			
			);
			$flgIn=$db->insertAry("tbl_roll",$aryData);
			if(!is_null($flgIn))
			{
				
				$_SESSION['msg']='Saved Successfully';
				unset($_SESSION['form']);
				redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod)));
			}
			else
			{
				array_push($alert_err,$db->getErMsg());
				
			}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
			$aryData=array(	'name'	=>	$_POST['name'],
							'status'=>	trim($_POST['status'])	
						);

			$flgUp=$db->updateAry("tbl_roll",$aryData,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
			if(!is_null($flgUp))
			{
				$_SESSION['msg']='update Successfully';
				unset($_SESSION['form']);
				redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod)));
			}
			else
			{
				array_push($alert_err,$db->getErMsg());
			
			}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
		
	$res=$db->delete("tbl_roll","where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; 
		redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod)));
	}
	else { array_push($alert_err,$db->getErMsg()); }
			
		
		
}
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
}
?>
<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=Promote&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    </div>
    <div class="well no-padding">
<?php 

if(($pgAct=="edit" && !isset($_SESSION['form'])) || $pgAct=="view")
{
	$aryForm=$db->getRow("SELECT * FROM tbl_roll 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']);
	}
	$aryFrmAct=array("src"=>"Promote", "page_id"=>$pgMod, "action"=>$pgAct);
	if($pgAct=="edit") $aryFrmAct['id']=$_GET['id'];
 
	?>
    <form action="#" method="post" enctype="multipart/form-data">        <div class="control-group">
            <label class="control-label" for="name">Roll Name</label>
            <div class="controls">
                <input type="text" id="name" name="name" class="input-block-level" value="<?php echo $aryForm['name']; ?>">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="Status">Status</label>
            <div class="controls">
                <select name="status" class="text">
                    <option value="1"<?php echo selected("1",$aryForm['status']); ?>>Active</option>
                    <option value="0"<?php echo selected("0",$aryForm['status']); ?>>Inactive</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>
    </form>
<?php
}
elseif($pgAct=="permission" && isset($_GET['id']) && trim($_GET['id']) !=''){
	error_reporting(22);
    if($_SERVER["HTTP_HOST"] == "bps.savarnacloud.com"){
        $files1 = array("Student","Faculty","Library","Account","Attandence","TimeTable","Hostel","Transport","Promote","sms","TC","Exam","ExamCBSE","Enquiry","Notice","OnlineExam","cms","Budget","DeleteReceipt","Payroll","ERP","AddEditStudent","DeleteStudent","PayFee","MakeDiscount","ResetDiscount","InventoryEditDelete","BackDatePayment","ViewStudentPersonalDetail",        "CollectionGraph", "OutstandingGraph", "StudentAttendanceGraph","TeacherAttendanceGraph","ManualFaceAttendance", "StudentStrengthGraph", "VehicleGraph", "RouteGraph","website","HR","Reception");
    } else {
	   // $files1 = array("Student","Faculty","Library","Account","Attandence","TimeTable","Hostel","Transport","Promote","sms","TC","Exam","ExamCBSE","Enquiry","Notice","OnlineExam","cms","Budget","DeleteReceipt","Payroll","AddEditStudent","DeleteStudent","PayFee","MakeDiscount","ResetDiscount","InventoryEditDelete","BackDatePayment","ViewStudentPersonalDetail",        "CollectionGraph", "OutstandingGraph", "StudentAttendanceGraph","TeacherAttendanceGraph","ManualFaceAttendance", "StudentStrengthGraph", "VehicleGraph", "RouteGraph","website","HR","Reception");
	   $files1 = array("Faculty","Attendance","Payroll","SMS","Promote","Exam","Notice","HR","Reception");
    }
	?>
    <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=checkPermission($images,$_GET["id"]);
			?>
            <tr>
            	<td><?php echo $i;?></td>
            	<td><input type="hidden" name="name[]" value="<?php echo $images; ?>" /><?php if($images=="Exam"){echo "Portal";}else 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{
    $aryPgAct=array("src"=>"Promote", "page_id"=>$pgMod,"action"=>"add");
	$sqlLimit="SELECT * FROM tbl_roll where school_id='".$_SESSION["admin"]["school_id"]."'  ORDER BY name,id ASC";
	$aryList=$db->getRows($sqlLimit);
	if(is_array($aryList))
	{
	  if(count($aryList)>0)
	   { 
	   $i=0;
	   ?> 
		 <table class="data-table">
         <tfoot>
                	<tr>
                    	<td colspan="5">
                        </td>
                    </tr>
                </tfoot>
       	   <thead>
            <tr>
             <th class="first"><div></div></th>

            <th>Roll Name</th>
            <th>Status</th>
            <th class="last" width="25px">Actions</th>
            <th>&nbsp;</th>
            </tr>
            </thead>
            <tbody>
            <?php
			foreach($aryList as $iList)
			{
				$i++;
				$aryPgAct["id"]=$iList['id'];
			 ?>
             <tr>
             <td nowrap="nowrap" 	><?php echo $i; ?></td>
             <td nowrap="nowrap" ><?php 
				echo ucwords($iList['name']);
				?>
                </td>
                <td nowrap="nowrap"  > <?php if($iList["status"]==0){ ?>
                    <span class="label btn-red2 margin-right-sm">Inactive</span>
                    <?php }else{ ?>
                    <span class="label btn-green">Active</span>
                    <?php } ?></td>
               <td nowrap="nowrap" >
               <?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>  
                    <?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>
                 <td>&nbsp;</td>
             </tr>
              <?php			
			  }
			?>
            </tbody>
         </table>
	  <?php
	   }
	    else
		{
		  echo '<div class="alert alert-info">
					<button type="button" class="close" data-dismiss="alert">×</button>
					<strong>Warning!</strong> No Record Found .
				</div>';
		}
	}
	else
	{
		echo '<div class="alert alert-error">
					<button type="button" class="close" data-dismiss="alert">×</button>
					<strong>Oh snap!</strong>'.$db->getErMsg().'.
				</div>'; 
	}
}
?>
</div>
</div>
</div><?php
if(isset($_GET['id']) && $_GET['id']!="" && $action='edit')
	$aryPgAct=array("src"=>"Promote", "page_id"=>$pgMod,"action"=>"edit","id"=>$_GET['id']);
else
	$aryPgAct=array("src"=>"Promote", "page_id"=>$pgMod,"action"=>"add");
?>

