<?php

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

if (isset($_GET["export"]))
{
  //kontrola spravneho obsahu $_GET["export"]
  if ($_GET["export"]!="record_to_pdf" && $_GET["export"]!="record_to_doc" && $_GET["export"]!="list_to_xls" && $_GET["export"]!="history_to_xls") $webdoc["message"]=database_get_message("bad_option");

  //kontrola existence hlavni promenne (sequence_id, test_id, test_run_id) pri exportovani jednoho zaznamu nebo historie
  if ($_GET["export"]=="record_to_pdf" || $_GET["export"]=="record_to_doc"  || $_GET["export"]=="history_to_xls")
  {
    if (!isset($webdoc["sequence_id"]) && ($webdoc["main_table"]=="requirement" || $webdoc["main_table"]=="product" || ($webdoc["main_table"]=="test" && $_GET["export"]=="history_to_xls"))) $webdoc["message"]=database_get_message("record_not_found");
    elseif (!isset($webdoc["test_id"]) && $webdoc["main_table"]=="test" && ($_GET["export"]=="record_to_pdf" || $_GET["export"]=="record_to_doc" || $_GET["export"]=="history_to_xls")) $webdoc["message"]=database_get_message("record_not_found");
    elseif (!isset($webdoc["test_run_id"]) && $webdoc["main_table"]=="test_run") $webdoc["message"]=database_get_message("record_not_found");
  }

  $time=time()+31536000; //cas vyprseni cookies, jeden rok
  
  if (!isset($_GET["use_filter"])) setcookie("export_no_filter",1,$time); else setcookie("export_no_filter","",0);
  if (!isset($_GET["use_columns"])) setcookie("export_no_columns",1,$time); else setcookie("export_no_columns","",0);
  if (!isset($_GET["use_pages"])) setcookie("export_no_pages",1,$time); else setcookie("export_no_pages","",0);
  
  if (!isset($webdoc["message"]))
  {
    require('../libraries/export.php');
    
    setcookie("export",$_GET["export"],$time);
    
    //export jednoho zaznamu do pdf nebo doc 
    if ($_GET["export"]=="record_to_pdf" || $_GET["export"]=="record_to_doc")
    {
      //nastaveni polozek, ktere se maji vypsat. V kazde casti jine
      switch ($webdoc["main_table"])
      {
        case "requirement":
          $header="Requirement"; //nadpis v dokumentu
          $items=array("name","category","parent","created","approved","active","description","comments","dependences","is_product_specific"); //identifikatory polozek
          $items2=array(NULL,NULL,NULL,"creator","approver"); //u nekterych kolonek jsou dve polozky (napr. created, creator)
          $titles=array("Name","Category","Parent","Created","Approved","Active","Description","Comments","Dependences","Is product specific"); //nazvy polozek tak, jak se zobrazi v dokumentu
        break;
        case "product":
          $header="Product"; 
          $items=array("name","category","parent","created","approved","description","comments","requirements");
          $items2=array(NULL,NULL,NULL,"creator","approver");
          $titles=array("Name","Category","Parent","Created","Approved","Description","Comments","Requirements (state, priority)");
        break;
        case "test":
          $header="Test"; 
          $items=array("name","created","approved","acceptance_criteria","comments","terminate");
          $items2=array(NULL,"creator","approver",NULL,NULL,"terminator");
          $titles=array("Name","Created","Approved","Acceptance criteria","Comments","Terminated"); 
        break;
        case "test_run":
          $header="Test Run"; 
          $items=array("name","created","approved","tested","sw_rev","hw_rev","setup","result","duration","description");
          $items2=array(NULL,"creator","approver","tester"); 
          $titles=array("Name","Created","Approved","Tested","SW Rev","HW Rev","Setup","Result","Duration","Description");
        break;
      }
      
      //ziskani zaznamu
      if ($webdoc["main_table"]=="test") $record=database_get_record_test($webdoc["test_id"]); //vypis zaznamu z casti tests a running tests
      elseif ($webdoc["main_table"]=="test_run") $record=database_get_record_test($webdoc["test_run_id"]);
      else $record=database_get_record(); //vypis zaznamu z casti requirements a products

      if ($record)
      {
        //mensi upravy nekterych zaznamu
        if (!isset($record["category"])) $record["category"]="-";
        if (!isset($record["parent"])) $record["parent"]="-";
        if (!isset($record["approved"])) $record["approved"]="No";
        if ($record["active"]==1) $record["active"]="Yes"; else $record["active"]="No";
        if ($record["is_product_specific"]) $record["is_product_specific"]="Yes"; else $record["is_product_specific"]="No"; 
      
        if ($_GET["export"]=="record_to_pdf") export_record_to_pdf($record,$header,$titles,$items,$items2); //export do pdf
        if ($_GET["export"]=="record_to_doc") export_record_to_doc($record,$header,$titles,$items,$items2); //export do doc
        
      }
      else $webdoc["message"]=database_get_message("record_not_found");
    }
    //export seznamu aktualnich zaznamu nebo historie jednoho zaznamu do xls
    elseif ($_GET["export"]=="list_to_xls" || $_GET["export"]=="history_to_xls")
    {
      kernel_get_url($filter,$columns,$pages,$sort); //ziskani promennych filtru, sloupcu, strankovani a razeni z url
    
      unset($columns["actions"]); //odstraneni sloupce actions, aby se zbytecne nezobrazoval
      
      //pokud se nema pouzit nastaveni strankovani, filtru nebo sloupcu, zrusi se.
      if (!isset($_GET["use_filter"]) || $_GET["export"]=="history_to_xls") unset($filter);
      if (!isset($_GET["use_columns"])) switch ($webdoc["main_table"]) //u sloupcu se musi pouzit vychozi nastaveni
      {
        case "requirement": $columns=array("name"=>"1","category"=>"1","parent"=>"1","created"=>"1","creator"=>"1","approved"=>"1","approver"=>"1","active"=>"1","description"=>"1","comments"=>"1","is_product_specific"=>"1"); break;
        case "product": $columns=array("name"=>"1","category"=>"1","parent"=>"1","created"=>"1","creator"=>"1","approved"=>"1","approver"=>"1","description"=>"1","comments"=>"1"); break;
        case "test": $columns=array("name"=>"1","created"=>"1","creator"=>"1","approved"=>"1","approver"=>"1","acceptance_criteria"=>"1","comments"=>"1","terminate"=>"1","terminator"=>"1"); break;
        case "test_run": $columns=array("name"=>"1","created"=>"1","creator"=>"1","approved"=>"1","approver"=>"1","tested"=>"1","tester"=>"1","sw_rev"=>"1","hw_rev"=>"1","setup"=>"1","result"=>"1","duration"=>"1","description"); break;
      }
      if (!isset($_GET["use_pages"])) unset($pages);
      
      foreach ($columns as $column => $val) switch ($column) //spravne pojmenovani sloupcu tak, jak se zobrazi v dokumentu
      {
        case "name": $columns["name"]="Name"; break;
        case "category": $columns["category"]="Category"; break;
        case "parent": $columns["parent"]="Parent"; break;
        case "created": $columns["created"]="Created"; break;
        case "creator": $columns["creator"]="Creator"; break;
        case "approved": $columns["approved"]="Approved"; break;
        case "approver": $columns["approver"]="Approver"; break;
        case "active": $columns["active"]="Active"; break;
        case "description": $columns["description"]="Description"; break;
        case "is_product_specific": $columns["is_product_specific"]="Is product specific"; break;
        case "acceptance_criteria": $columns["acceptance_criteria"]="Acceptance criteria"; break;
        case "comments": $columns["comments"]="Comments"; break;
        case "terminate": $columns["terminate"]="Terminated"; break;
        case "terminator": $columns["terminator"]="Terminator"; break;
        case "tested": $columns["tested"]="Tested"; break;
        case "tester": $columns["tester"]="Tester"; break;
        case "sw_rev": $columns["sw_rev"]="SW Rev"; break;
        case "hw_rev": $columns["name"]="HW Rev"; break;
        case "setup": $columns["name"]="Setup"; break;
        case "result": $columns["name"]="Result"; break;
        case "duration": $columns["name"]="Duration"; break;
      }
      
      $list=database_get_list($count,$total,$pages,$sort,$columns,$filter); //ziskani seznamu
      if ($count>0 ) export_list_to_xls($list,$columns);
      else $webdoc["message"]=database_get_message("list_empty");
    }
  }
  if (!$webdoc["message"]) exit;
  else
  {
    session_start();
    unset($filter); unset($columns); //pokud se skript neukonci, je nutne smazat nastaveni nastroju,
    unset($pages); unset($sort);     //aby je funkce kernel_get_url spravne znovu nastavila. Jinak by se mohly zobrazit vsechny sloupce
  }
  
}

$webdoc["page_title"].=" - Export";

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

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_columns_url($columns).kernel_create_filter_url($filter);

if (isset($_GET["export"])) $export=$_GET["export"];
elseif (isset($_COOKIE["export"])) $export=$_COOKIE["export"];
if (!isset($export) || ($webdoc["main_table"]=="test_run" && $export=="history_to_xls") || (($webdoc["main_table"]=="requirement" || $webdoc["main_table"]=="product" || ($webdoc["main_table"]=="test" && $export=="history_to_xls")) && !isset($webdoc["sequence_id"])) || ($webdoc["main_table"]=="test_run" && !isset($webdoc["test_run_id"])) || ($webdoc["main_table"]=="test" && ($export=="record_to_pdf" || $export=="record_to_doc" || $export=="history_to_xls") && !isset($webdoc["test_id"])))
{ 
  $export="list_to_xls";
}

$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
$template->template_assign("export",$export);

?>