<?php

/**
 * přidává nebo odebírá selecty podle toho, zda bylo kliknuto na tlačítko Cancel nebo Add
 * \param array &$filter hodnoty filtru. Může obsahovat pole categories_id, parents_id apod. Hodnoty těchto polí funkce přidává nebo odebírá
 * \param array $buttons_name pole názvů tlačítek (bez částí button_cancel_ a button_add_, které jsou vždy stejné)
 * \param array $array_id_name pole názvů polí s hodnotami id
 */
function filter_buttons_id_selections(&$filter,$buttons_name,$array_id_name)
{
  if (count($buttons_name)!=count($array_id_name)) return false;
  foreach ($buttons_name as $button_name_key => $button_name)
  {
    if (isset($_GET["button_cancel_$button_name"]))
    {
      foreach ($_GET["button_cancel_$button_name"] as $key => $value) unset($filter[$array_id_name[$button_name_key]][$key]);
    }
    if (isset($_GET["button_add_$button_name"])) $filter[$array_id_name[$button_name_key]][]="";
  }
}
$webdoc["page_title"].=" - Filter";

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

if (isset($_GET["test_id"])) $webdoc["test_id"]=(int)$_GET["test_id"];

kernel_get_url($filter,$columns,$pages,$sort); //precte url a podle toho nastavi filter. pokud v url nastaveni filtru neni, precte cookies a pokud v cookies neco je, presmeruje na odpovidajici url

$webdoc["url"]=kernel_create_pages_url($pages).kernel_create_sort_url($sort).kernel_create_filter_url($filter).kernel_create_columns_url($columns);

if ($webdoc["main_table"]=="requirement" || $webdoc["main_table"]=="product")
{
  //vytvoreni selectu rodicu
  $list=database_get_list($rows,$total); //zaznamy v tabulkach requirement nebo product
  $parents=array(-2=>"- - -",-1=>"-- [NO PARENT] --");
  foreach ($list as $key => $row)
  {
    $parents[$row["sequence_id"]]=$row["name"];
  }
  $template->template_assign("parents",$parents); //přiřazení pole do smarty

  //vytvoreni selectu kategorii (stromova struktura)
  $categories=database_get_categories_tree($count,$webdoc["main_table"]."_category",NULL,array(-2=>"- - -",-1=>"-- [ROOT] --")); //nacteni kategorii do stromu
  $template->template_assign("categories",$categories); //přiřazení stomu do smarty
}

//vytvoreni selectu uzivatelu (tvurcu zaznamu a schvalovatelu)
$list=database_get_person_list($rows); //zaznamy v tabulkach requirement nebo product
$approvers=array(-2=>"- - -",-1=>"-- [NO APPROVER] --");
foreach ($list as $key => $row)
{
  $approvers[$row["person_id"]]=$row["name"];
}
$template->template_assign("approvers",$approvers); //přiřazení pole do smarty
$creators=$approvers;
unset($creators[-1]); //u kazdeho zaznamu by vzdy mel byt creator
$template->template_assign("creators",$creators); //přiřazení pole do smarty

//odstraneni a pridavani kategorii, rodicu, zavislosti - kliknuto na tlacitko cancel nebo add
switch ($webdoc["main_table"])
{
  case "requirement":
    //vytvoreni selectu zavislosti
    $dependences=$parents;
    $dependences[-1]="-- [INDEPENDENT] --";
    $template->template_assign("dependences",$dependences); //přiřazení zaznamu do smarty
  
    //vytvoreni selectu pro nastaveni active
    $active_options=array(NULL=>"- - -",2=>"Active",1=>"Not active");
    $template->template_assign("active_options",$active_options);
  
    //vytvoreni selectu pro nastaveni is_product_specific
    $is_product_specific_options=array(NULL=>"- - -",2=>"Specific",-1=>"Not specific");
    $template->template_assign("is_product_specific_options",$is_product_specific_options);
    
    $buttons_name=array("category","parent","creator","approver","dependence");
    $array_id_name=array("categories_id","parents_id","creators_id","approvers_id","dependences_id"); //pole vsech nazvu promennych filtru, ktere jsou typu array        
  break;
  case "product":
    $buttons_name=array("category","parent","creator","approver");
    $array_id_name=array("categories_id","parents_id","creators_id","approvers_id"); //pole vsech nazvu promennych filtru, ktere jsou typu array        
  break;
  case "test":
    $terminators=$approvers;
    $terminators[-1]="-- [NO TERMINATOR] --";
    $template->template_assign("terminators",$terminators); //přiřazení pole do smarty
    
    $buttons_name=array("creator","approver","terminator");
    $array_id_name=array("creators_id","approvers_id","terminators_id"); //pole vsech nazvu promennych filtru, ktere jsou typu array        
  break;
  case "test_run":
    $testers=$approvers;
    $testers[-1]="-- [NO TESTER] --";
    $template->template_assign("testers",$testers); //přiřazení pole do smarty
    
    $buttons_name=array("creator","approver","tester");
    $array_id_name=array("creators_id","approvers_id","testers_id"); //pole vsech nazvu promennych filtru, ktere jsou typu array        
  break;
}
filter_buttons_id_selections(&$filter,$buttons_name,$array_id_name);

$template->template_assign("url",$webdoc["url"]);
$template->template_assign("pages",$pages); //nastaveni hodnot stranky ve formulari
$template->template_assign("sort",$sort); //nastaveni hodnot razeni ve formulari
$template->template_assign("filter",$filter); //nastaveni hodnot filtru ve formulari
$template->template_assign("columns",$columns); //nastaveni hodnot sloupcu ve formulari

//pokud bylo kliknuto na filtrate - kontrola a presmerovani na zobrazeni odpovidajicich zaznamu
if (isset($_GET["button_filtrate"]))
{
  if (!kernel_date_control($filter["created_from"])) $webdoc["message"]["form_error"][]=database_get_message("field")." ".database_get_message("created")." ".database_get_message("from")." ".database_get_message("not_correct");
  if (!kernel_date_control($filter["created_to"])) $webdoc["message"]["form_error"][]=database_get_message("field")." ".database_get_message("created")." ".database_get_message("to")." ".database_get_message("not_correct");
  if (!kernel_date_control($filter["approved_from"])) $webdoc["message"]["form_error"][]=database_get_message("field")." ".database_get_message("approved")." ".database_get_message("from")." ".database_get_message("not_correct");
  if (!kernel_date_control($filter["approved_to"])) $webdoc["message"]["form_error"][]=database_get_message("field")." ".database_get_message("approved")." ".database_get_message("to")." ".database_get_message("not_correct");
  if ($webdoc["main_table"]=="test")
  {
    if (!kernel_date_control($filter["terminate_from"])) $webdoc["message"]["form_error"][]=database_get_message("field")." ".database_get_message("terminate")." ".database_get_message("from")." ".database_get_message("not_correct");
    if (!kernel_date_control($filter["terminate_to"])) $webdoc["message"]["form_error"][]=database_get_message("field")." ".database_get_message("terminate")." ".database_get_message("to")." ".database_get_message("not_correct");
  }
  elseif ($webdoc["main_table"]=="test_run")
  {
    if (!kernel_date_control($filter["tested_from"])) $webdoc["message"]["form_error"][]=database_get_message("field")." ".database_get_message("tested")." ".database_get_message("from")." ".database_get_message("not_correct");
    if (!kernel_date_control($filter["tested_to"])) $webdoc["message"]["form_error"][]=database_get_message("field")." ".database_get_message("tested")." ".database_get_message("to")." ".database_get_message("not_correct");
  }

  if (isset($webdoc["message"])) return; //pokud je chybova hlaska, vypsani hlasky (sablonou) a konec
  
  $webdoc["url"]="./?subpage1=$_GET[subpage1]".$webdoc["url"]; //vytvoreni nove url z hodnot filtru
  header("location: $webdoc[url]"); //presmerovani
  echo database_get_message("continue")." <a href=\"$webdoc[url]\">".database_get_message("here")."</a>."; //zobrazeni hlasky "pokracovat zde", pokud selze presmerovani
  exit;
}

?>