<?php
include('../includes/con2pma3.php');
if (isset($_SESSION['UserName'])) {
 
$stdName =  $_POST['stdname'];            
$stdPrefix  =  $_POST['stdprefix'];       
$stdCode   =  $_POST['stdcode'];        
$stdNotes  = $_POST['stdnotes'];
echo $stdFile =  $_FILES['importfilename2']['name'];
$date = date('Y-m-d');
$id = $_SESSION['id'];
if (isset($_FILES["importfilename2"]["name"]) && $_FILES["importfilename2"]["name"]!='' && file_exists("../uploads/" . $_FILES["importfilename2"]["name"]))
					  {						  
						 header("location: Standards1.php?msg=FILE ALREADY EXISTS. RETRY WITH A DIFFERENT FILE NAME.");
						 exit;
					  }
					  else if(isset($_FILES["importfilename2"]["name"]) && $_FILES["importfilename2"]["name"]!='' && !file_exists("../uploads/" . $_FILES["importfilename2"]["name"])){
						  $stdFile=$_POST['stdFileHidden'];
						  $imgpath =  $_FILES["importfilename2"]["name"];
						  move_uploaded_file($_FILES["importfilename2"]["tmp_name"],"../uploads/" . $imgpath);
						  if (($handle = fopen("../uploads/".$imgpath, "r")) !== false) {
							  $stdFile =  $_FILES['importfilename2']['name'];
						  }
						  else{
							  header("location: Standards1.php?msg=FILE IS NOT UPLOADED PLEASE TRY AGAIN.");
						  }
						  
					  }

    if (isset($_REQUEST['del'])) {
        $SID = $_REQUEST['del'];
        if (mysql_query("UPDATE scope_std SET `stdStatus`=2 WHERE `stdID` = '$SID'")) {
            header("location: Standards1.php?msg=DELETED");
        } else {
            header("location: Standards1.php?msg=NOT DELETED");
        }
    } else if (isset($_REQUEST['Edit'])) {
        $SID = $_REQUEST['Edit'];
          if($stdFile==""){ 
              $resultPre = mysql_query("SELECT stdFile FROM scope_std where stdId='".$SID."'"); 
			$stdFile = mysql_result($resultPre,0,0);
                    
        } 
        if (mysql_query("UPDATE scope_std SET `stdName`='$stdName',`stdPrefix`='$stdPrefix',`stdCode`='$stdCode',`stdFile`='$stdFile',`stdNotes`='$stdNotes' WHERE `stdId` = '$SID'")) {
//        if(mysql_query("UPDATE manual_assignments SET `companyProjects` = '$projectName', `companyClients` = '$clientName' WHERE `ProjectEstimate` = '$BudgetID'"))
            header("location: Standards1.php?msg=UPDATED");
        } else {
            header("location: Standards1.php?msg=NOT UPDATED");
        }
    } else {


        if (mysql_query("INSERT INTO scope_std SET `stdName`='$stdName',`stdPrefix`='$stdPrefix',`stdCode`='$stdCode',`stdDateCreated`='$date',`stdFile`='$stdFile',`stdNotes`='$stdNotes',`stdCompany`='$id'")) {
            
     
            header("location: Standards1.php?msg=SAVED");
        } else {
            header("location: Standards1.php?msg=NOT SAVED");
        }
    }
} else {
    header("location: ../index.php?msg=YOU ARE NOT LOGINED");
}
?> 