<?php

switch ($_GET["subpage2"])
{
  case "edit": $webdoc["page_title"].=" - Edit Test Run"; break;
  // case "copy": $webdoc["page_title"].=" - Copy"; break;
  default: $webdoc["page_title"].=" - New Test Run";
}

$person_role = 5;
if (!auth($person_role)) 
{
  $webdoc["message"][]=database_get_message("no_permission");
  $template->template_assign("no_display",true);
  return;
}

/** ------------------------------------------------------
 *  ZACATEK vlastniho PHP kodu
    ------------------------------------------------------ */ 

if (isset($_POST["button_save"]) OR isset($_POST["button_edit"]))
{
$button=true;
}

$no_display=false;

if ($button)
{
  if (isset($_POST["button_edit"])) 
  {
  $webdoc["test_run_id"]=$_POST["test_run_id"];
  $record["test_run_id"]=$webdoc["test_run_id"];
  $record["edit"]=true;
  }
  $record["name"]=$_POST["name"];
  $record["test_id"]=$_POST["test_id"];
  $record["product_id"]=$_POST["product_id"];
  $record["sw_rev"]=$_POST["sw_rev"];
  $record["hw_rev"]=$_POST["hw_rev"];
  $record["setup"]=$_POST["setup"];
  $record["result"]=$_POST["result"];
  $record["duration"]=$_POST["duration"];
  $record["description"]=$_POST["description"];  
  $record["creator_id"]=$webdoc["auth"];
  $record["created"]=Date("Y-m-d H:i:s");
  
  if (isset($record)) $template->template_assign("record",$record); //zobrazeni hodnot ve formulari
  
  $filled_ok=true;
  if ($record["name"]=="")
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_name");
    $filled_ok=false;
  }
  if ($record["setup"]=="")
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_setup");
    $filled_ok=false;
  }
  
  if ($filled_ok) database_save_record_test($record);

}
else
{
  if ($_GET["subpage2"]==edit)
  {
    $webdoc["test_run_id"]=$_REQUEST["test_run_id"];
    $record = database_get_record_test($webdoc["test_run_id"],$row);
    $record["edit"]=true;
    if ($record["tested"]<>0)  { $webdoc["message"][]=database_get_message("test_run_tested"); $no_display=true; }
    elseif ($record["approved"]<>0)  { $webdoc["message"][]=database_get_message("test_run_approved"); $record["is_approved"]=1; }
    $template->template_assign("record",$record);
  }
  elseif (isset($_REQUEST["test_id"]))
  {
    $record["test_id"]=$_REQUEST["test_id"];
    $template->template_assign("record",$record);
  }
  elseif (isset($_REQUEST["product_id"]))
  {
    $record["product_id"]=$_REQUEST["product_id"];
    $template->template_assign("record",$record);
  }
  else
  {
    $url="./?subpage1=tests";
    header("location: $url");
  }
}

$template->template_assign("no_display",$no_display);

/** ------------------------------------------------------
 *  KONEC vlastniho PHP kodu
    ------------------------------------------------------ */ 

?>