<?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['form_title']		=$_POST['formtitle'];
$data['form_date']		=date('Y-m-d',strtotime($_POST['formdate']));
$data['form_fileupload']=$_FILES["formfileupload"]["name"];

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

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


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

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


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


include "pagingserch.php";

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

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


$pagecnt=$start;




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

	$count = count($forms);
	if($count > 0){
	$forms[$count-1]['totalrows'] = $total_pages;
	$forms[$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='viewform' and m.menu_id=s.menu_id "));
	
	$userid=$_SESSION['admin_id'];

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

}
?>