<?php

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

$person_role = 4;
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 (isset($_POST["button_edit_delete"]))
{
  $requirement_has_test_id=$_POST["requirement_has_test_id"];
  if (!database_query("DELETE FROM requirement_has_test WHERE requirement_has_test_id = '$requirement_has_test_id'"))
  {
    $webdoc["message"][]=database_get_message("bad_dependence");
    return;
  }
  $webdoc["test_id"]=$_POST["test_id"];
  $webdoc[main_table]="test";
  $record = database_get_record_test($webdoc["test_id"],$row);
  $record["edit"]=true;
  $template->template_assign("record",$record);
  $webdoc[main_table]="requirement_has_test";
  $record_requirement_id = database_get_record_test($webdoc["test_id"],$row);
  $template->template_assign("record_requirement_id",$record_requirement_id);
}
if (isset($_POST["button_edit_add"]))
{
  $webdoc["test_id"]=$_POST["test_id"];
  $requirement_id=$_POST["requirement"];
  
  $result=database_query("SELECT * FROM requirement_has_test WHERE requirement_id LIKE '$requirement_id' AND test_id LIKE '$webdoc[test_id]'");
  if (database_num_rows($result)>0)
  {
    $webdoc["message"]["form_error"][]=database_get_message("existing_requirement");
  }
  else
  {
    if (!database_query("INSERT INTO requirement_has_test (requirement_id, test_id) VALUES ('$requirement_id', '$webdoc[test_id]')"))
    {
      $webdoc["message"][]=database_get_message("error_saving");
      return;
    }
  }
  $record = database_get_record_test($webdoc["test_id"],$row);
  $record["edit"]=true;
  $template->template_assign("record",$record);
  $webdoc[main_table]="requirement_has_test";
  $record_requirement_id = database_get_record_test($webdoc["test_id"],$row);
  $template->template_assign("record_requirement_id",$record_requirement_id);
  $result=database_query("SELECT * FROM requirement ORDER BY requirement_id");
  $requirement=database_fetch_result($result,$row);
  $template->template_assign("requirement",$requirement);
}
elseif ($button)
{
  if (isset($_POST["button_edit"])) 
  {
  $webdoc["test_id"]=$_POST["test_id"];
  $record["test_id"]=$_POST["test_id"];
  $webdoc[main_table]="test";
  $record = database_get_record_test($webdoc["test_id"],$row);
  $record["edit"]=true;
  $template->template_assign("record",$record);
  $webdoc[main_table]="requirement_has_test";
  $record_requirement_id = database_get_record_test($webdoc["test_id"],$row);
  $template->template_assign("record_requirement_id",$record_requirement_id);
  }
  $record["name"]=$_POST["name"];
  $record["requirement_id"]=$_POST["requirement_id"];
  $record["acceptance_criteria"]=$_POST["acceptance_criteria"];
  $record["comments"]=$_POST["comments"];  
  $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["acceptance_criteria"]=="")
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_acceptance_criteria");
    $filled_ok=false;
  }
  if ($record["comments"]=="")
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_comments");
    $filled_ok=false;
  }
  
  if ($filled_ok) database_save_record_test($record);

}
else
{
  if ($_GET["subpage2"]==edit)
  {
    $webdoc["test_id"]=$_REQUEST["test_id"];
    $webdoc[main_table]="test";
    $record = database_get_record_test($webdoc["test_id"],$row);
    if ($record["terminator_id"]<>0)
    {
      $no_display=true;
      $webdoc["message"][]=database_get_message("edit_terminated");
    }
    else
    {
      $record["edit"]=true;
      $template->template_assign("record",$record);
      $webdoc[main_table]="requirement_has_test";
      $record_requirement_id = database_get_record_test($webdoc["test_id"],$row);
      $template->template_assign("record_requirement_id",$record_requirement_id);
      $result=database_query("SELECT * FROM requirement ORDER BY requirement_id");
      $requirement=database_fetch_result($result,$row);
      $template->template_assign("requirement",$requirement);
    }
  }
  elseif (isset($_REQUEST["requirement_id"]))
  {
    $record["requirement_id"]=$_REQUEST["requirement_id"];
    $template->template_assign("record",$record);
  }
  else
  {
    $url="./?subpage1=requirements";
    header("location: $url");
  }
}

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

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

?>