<?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['circulartitle']		=$_POST['circulartitle'];
$data['circulardate']		=date('Y-m-d',strtotime($_POST['circulardate']));
$data['fileupload']=$_FILES["fileupload"]["name"];

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

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


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

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

$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 circulardate>="'.$from_date1.'" and circulardate<="'.$to_date1.'"';
}
elseif(!empty($from_date1) && empty($to_date1))
{
$cond.='and circulardate>="'.$from_date1.'"';
}
elseif(empty($from_date1) && !empty($to_date1))
{
$cond.='and circulardate<="'.$to_date1.'"';
}
	
	 $query = "SELECT * FROM circular where 1 $cond";
	


include "pagingserch.php";

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

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


$pagecnt=$start;

	//echo "SELECT * FROM circular where 1 $cond";


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


	
	$count = count($circular);
	if($count >0){
	$circular[$count-1]['totalrows'] = $total_pages;
	$circular[$count-1]['lastPage'] = $lastpage;
	}
	
	$sub_rights=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  s.s_menu_name='viewcirculars' 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)
	{
	$circular[count($circular)-1]['permissions'][0] = array("user_id"=>$userid);
	}
	else
	{
	$circular[count($circular)-1]['permissions'] =  $db->fetchAll("tbl_user_rights",$where);
	}
	
	
	
	echo '{"circulars":'.json_encode($circular).'}';
	
	}
	
			
if($_GET['action']=='removeded')
{

$where['id']	= $_GET['did'];
	
$deductions = $db->Delete('circular',$where);

}
?>