<?php

if ($_GET["subpage2"]=="new") $webdoc["page_title"].=" - New"; //skript je pouzivan i pro vkladani novych zaznamu
else $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");
}

if (isset($_POST["button"])) //prevedeni dat z formulare pro dalsi zpracovani
{ 
  if ($_POST["parent_id"]>0) $record["parent_id"]=$_POST["parent_id"];
  $record["name"]=$_POST["name"];
  $record["description"]=$_POST["description"];
}

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

if ($_GET["subpage3"]=="edit") //editace
{
  if (isset($webdoc["category_id"])) //test existence category_id
  {
    //nacteni zaznamu do docasne prom.
    if ($tmp=database_get_category()) //text existence zaznamu v db
    {
      if (!isset($_POST["button"])) $record=$tmp; //pokud nebyl odeslan formular, nacteni zaznamu do formulare
    }
    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

if (!isset($_POST["button"])) return; //konec, nebyla-li poslana data v postu

//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 (!database_test_category($record["parent_id"])) $webdoc["message"]["form_error"][]=database_get_message("bad_parent");

if (isset($webdoc["message"])) return; //pokud je chybova hlaska, vypsani hlasky a konec

//ulozeni do db
if (database_save_category($record,&$category_id))
{
  if (isset($webdoc["category_id"]))
  {
    $tmp=database_get_category(); //zjisteni automaticky ukladanych hodnot
    $record["time"]=$tmp["time"];
    $record["person"]=$tmp["person"];
    $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

?>
