<?php
session_start();
include_once("../class/class.MySQL.php");
require '../class/resizeImage.php';
$db	=new MySQL();


if($_GET['action']=='insert')
{

// add data to  employee
$data=array();


$data['policie_title']		=$_POST['policytitle'];
$data['policie_date']		=date('Y-m-d',strtotime($_POST['policydate']));
$data['policie_fileupload']=$_FILES["policyfileupload"]["name"];

$allowedExts = array("doc", "docx", "pdf");
$temp = explode(".", $_FILES["policyfileupload"]["name"]);
$extension = end($temp);
if (in_array($extension, $allowedExts))
  {
  if ($_FILES["policyfileupload"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["policyfileupload"]["error"] . "<br>";
    }
  else
    {
   "Upload: " . $_FILES["policyfileupload"]["name"] . "<br>";
    "Type: " . $_FILES["policyfileupload"]["type"] . "<br>";
    "Size: " . ($_FILES["policyfileupload"]["size"] / 1024) . " kB<br>";
    "Temp file: " . $_FILES["policyfileupload"]["tmp_name"] . "<br>";

    if(file_exists("../upload/circulars/". $_FILES['policyfileupload']['name']))
      {
      echo "<script>alert('filename already exists.')</script> ";
      }
    else
      {
      move_uploaded_file($_FILES["policyfileupload"]["tmp_name"],
      "../upload/circulars/". $_FILES["policyfileupload"]["name"]);
       echo "<script>alert('Your Resume has Been Submitted!!')</script> ";
      }
    }
  }


//mysql_query("insert into employee (e_mname)values($a)");
$db->Insert($data,'policies');
		

}
		
if($_GET['action']=='viewpolicy')
{

$from_date1 = '';$to_date1 = '';


if(!empty($_REQUEST['to_date'])){

$to_date=$_REQUEST['to_date'];
$to_date1 = date('Y-m-d', strtotime($to_date));
}

if(!empty($_REQUEST['from_date'])){

$from_date=$_REQUEST['from_date'];
$from_date1 = date('Y-m-d', strtotime($from_date));
}


if(!empty($from_date1) && !empty($to_date1))
{

$cond.='and policie_date>="'.$from_date1.'" and policie_date<="'.$to_date1.'"';
}
elseif(!empty($from_date1) && empty($to_date1))
{
$cond.='and policie_date>="'.$from_date1.'"';
}
elseif(empty($from_date1) && !empty($to_date1))
{
$cond.='and policie_date<="'.$to_date1.'"';
}
	
	$query = "SELECT * FROM policies where 1 $cond";
	


include "pagingserch.php";

$cond.=" LIMIT $start, $limit";

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=15;
}


$pagecnt=$start;




	$policy = $db->fetchQuery("SELECT * FROM policies where 1 $cond");
	


	
	$count = count($policy);
	if($count >0){
	$policy[$count-1]['totalrows'] = $total_pages;
	$policy[$count-1]['lastPage'] = $lastpage;
	}else{
	$policy[0]['totalrows'] = $total_pages;
	$policy[0]['lastPage'] = $lastpage;
	}
	
	$sub_rights=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  s.s_menu_name='viewpolices' and m.menu_id=s.menu_id "));
	
	$userid=$_SESSION['admin_id'];

	$where['user_id']	= $userid;
	$where['s_submenu_id']	= $sub_rights['s_id'];
	if($userid==1)
	{
	$policy[count($policy)-1]['permissions'][0] = array("user_id"=>$userid);
	}
	else
	{
	$policy[count($policy)-1]['permissions'] =  $db->fetchAll("tbl_user_rights",$where);
	}
	
	
	
	echo '{"policy":'.json_encode($policy).'}';
	}
	
			
if($_GET['action']=='removeded')
{
$where['id']	= $_GET['did'];
	
$deductions = $db->Delete('policies',$where);

}
?>