<?php

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

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

if (isset($_SESSION["$webdoc[main_table]_record_saved"])) //oznameni o uspesnem ulozeni po presmerovani na editaci
{
  unset($_SESSION["$webdoc[main_table]_record_saved"]);
  $webdoc["message"][]=database_get_message("record_saved");
}

//$button indikuje, zda se kliklo na nektere z tlacitek true/false 
$button=(isset($_POST["button_save"]) || isset($_POST["button_add_dependence"]) || isset($_POST["button_add_requirement"]) || count($_POST["button_cancel_dependence"])>0 || count($_POST["button_cancel_requirement"])>0);

if ($button) //prevedeni dat z formulare pro dalsi zpracovani
{ 
  $record["name"]=$_POST["name"];
  $record["description"]=$_POST["description"];
  $record["comments"]=$_POST["comments"];
  $record["dependences_id"]=$_POST["dependences_id"];
  $record["requirements_id"]=$_POST["requirements_id"];
  $record["states_id"]=$_POST["states_id"];
  $record["priorities_id"]=$_POST["priorities_id"];
  if ($_POST["category_id"]>0) $record["category_id"]=$_POST["category_id"];
  if ($_POST["is_product_specific"]==1) $record["is_product_specific"]=1;
  
  //odstraneni nektere zavislosti - kliknuto na tlacitko cancel
  if (is_array($_POST["button_cancel_dependence"]))
  {
    foreach ($_POST["button_cancel_dependence"] as $key => $value) unset($record["dependences_id"][$key]);
  }
  //odstraneni nektereho pozadavku - kliknuto na tlacitko cancel
  if (is_array($_POST["button_cancel_requirement"]))
  {
    foreach ($_POST["button_cancel_requirement"] as $key => $value)
    {
      unset($record["requirements_id"][$key]);
      unset($record["states_id"][$key]);
      unset($record["priorities_id"][$key]);
    }
  }
  
  //pridani zavislosti - kliknuto na tlacitko add
  if (isset($_POST["button_add_dependence"])) $record["dependences_id"][count($record["dependences_id"])+1]="";
  //pridani pozadavku - kliknuto na tlacitko add
  if (isset($_POST["button_add_requirement"]))
  {
    $key=count($record["requirements_id"])+1;
    $record["requirements_id"][$key]="";
    $record["states_id"][$key]="";
    $record["priorities_id"][$key]="";
  }  
  
}

if ($_GET["subpage2"]=="edit" || $_GET["subpage2"]=="copy") //editace
{
  if (isset($webdoc["sequence_id"])) //test existence sequence_id
  {
    //nacteni zaznamu do docasne prom.
    if ($tmp=database_get_record()) //text existence zaznamu v db
    {
      if (!$button) //pokud nebyl odeslan formular, nacteni zaznamu do formulare
      {
        $record=$tmp;
      }
      elseif (!isset($_POST["button_save"])) //byl odeslan formular, ale nebylo to ulozeni (napr. se kliklo na Add, Cancel..)
      {
        $record["created"]=$tmp["created"];
        $record["creator"]=$tmp["creator"];
        $record["approved"]=$tmp["approved"];
        $record["approver"]=$tmp["approver"];
      }
    }
    else //zaznam neexistuje
    {
      $webdoc["message"][]=database_get_message("record_not_found");
      $template->template_assign("no_display_form",true); //formular se nezobrazi
    }
  }
  else return; //neni-li sequence_id, konec
}

if (isset($record)) $template->template_assign("record",$record); //zobrazeni hodnot ve formulari

$tree=database_get_categories_tree($count,$webdoc["main_table"]."_category"); //nacteni kategorii do stromu
$template->template_assign("tree",$tree); //přiřazení stomu do smarty

$template->template_assign("yesno",Array(NULL=>database_get_message("no"),1=>database_get_message("yes"))); //select ano - ne

//vytvoreni selectu zavislosti
if ($webdoc["main_table"]=="requirement")
{
  $list=database_get_list($rows,$total);
  $dependences=Array(NULL=>"- - -");
  foreach ($list as $key => $row)
  {
    if ($row["sequence_id"]!=$webdoc["sequence_id"])
    {
      $dependences[$row["sequence_id"]]=$row["name"];
    }
  }
  $template->template_assign("dependences",$dependences); //přiřazení zaznamu do smarty
}

//vytvoreni selectu pozadavku pro provazani product-requirement
if ($webdoc["main_table"]=="product")
{
  $list=database_get_select("requirement",$tmp["requirements_id"]);
  $requirements=Array(NULL=>"- - -");
  foreach ($list as $key => $row)
  {
    $requirements[$row["requirement_id"]]=$row["name"];
    if ($row["old"]) $requirements[$row["requirement_id"]].=" --[OLDER VERSION]--";
  }
  $list=database_get_select("states"); //nacteni stavu
  foreach ($list as $key => $row) $states[$row["state_id"]]=$row["name"];

  $list=database_get_select("priorities"); //nacteni priorit
  foreach ($list as $key => $row) $priorities[$row["priority_id"]]=$row["name"];

  $template->template_assign("requirements",$requirements); //přiřazení requirementu do smarty
  $template->template_assign("states",$states); //přiřazení stavu do smarty
  $template->template_assign("priorities",$priorities); //přiřazení priorit do smarty
}

//pokud bylo kliknuto na Save - ulozenini
if (isset($_POST["button_save"]))
{
  //kontrola formularovych poli
  if ($record["name"]=="") $webdoc["message"]["form_error"][]=database_get_message("filled_name");
  
  if ($record["description"]=="") $webdoc["message"]["form_error"][]=database_get_message("filled_description");
  
  if (isset($record["category_id"]) && !database_test_existing_category($record["category_id"])) $webdoc["message"]["form_error"][]=database_get_message("bad_category");

  if (isset($record["dependences_id"]) && !database_test_existing_records($record["dependences_id"],"parent",true)) $webdoc["message"]["form_error"][]=database_get_message("bad_dependence");

  if (isset($record["requirements_id"]) && !database_test_existing_records($record["requirements_id"],"requirement")) $webdoc["message"]["form_error"][]=database_get_message("bad_requirements");
  
  if (isset($record["states_id"]) && !database_test_existing_records($record["states_id"],"states")) $webdoc["message"]["form_error"][]=database_get_message("bad_states");
  
  if (isset($record["priorities_id"]) && !database_test_existing_records($record["priorities_id"],"priorities")) $webdoc["message"]["form_error"][]=database_get_message("bad_priorities");
  
  if (isset($webdoc["message"])) return; //pokud je chybova hlaska, vypsani hlasky a konec

  //ulozeni do db
  if (database_save_record($record,$record_id,$webdoc["sequence_id"],$webdoc["version"]))
  {
    if (isset($webdoc["sequence_id"]))
    {
      $tmp=database_get_record(); //zjisteni automaticky ukladanych hodnot
      $record["created"]=$tmp["created"];
      $record["creator"]=$tmp["creator"];
      $record["approved"]=$tmp["approved"];
      $record["approver"]=$tmp["approver"];
      $template->template_assign("record",$record);
    }
    $webdoc["message"][]=database_get_message("record_saved"); //uspesna editace
  }
  else $webdoc["message"][]=database_get_message("error_saving"); //nezdarilo se ulozeni
}
?>