<?php

function export_record_to_pdf($record,$header,$titles,$items,$items2)
{
  global $webdoc;
  require('export/fpdf/fpdf.php');

  class PDF extends FPDF
  {
    function item($record,$title,$item,$item2)
    {
      $this->SetFillColor(200,220,255); //Background color
      $this->Cell(0,6,"$title:",0,1,'L',1); //Title
      
      if (is_array($record[$item])) foreach ($record[$item] as $key => $row)
      {
        $this->Ln(1); //Line break
        $txt=$row;
        if ($item=="requirements") $txt.=" (".$record["states"][$key].", ".$record["priorities"][$key].")";
        $this->MultiCell(0,5,strip_tags($txt)); //Output justified text
      }
      else
      {
        $this->Ln(1); //Line break
        $this->MultiCell(0,5,strip_tags($record[$item])); //Output justified text
      }
      if ($item2 && $record[$item2])
      {
        $this->Ln(1); //Line break
        $this->MultiCell(0,5,strip_tags($record[$item2])); //Output justified text
      }
      $this->Ln(); //Line break
    }
  }

  $pdf=new PDF();
  
  $pdf->SetTitle($header.": ".$record["name"]); //titulek
  $pdf->SetAuthor($record["creator"]); //creator
  $pdf->AddPage(); //nova strana
  $pdf->SetFont('Arial','',16); //pismo arial 16
  $pdf->SetFillColor(200,200,200); //barva pozadi - seda
  $pdf->Cell(0,10,"$header",0,1,'C',1); //hlavni nadpis
  $pdf->Ln(5); // zalomeni
  $pdf->SetFont('Arial','',12); //pismo arial 12
  
  foreach ($items as $key => $item)
  {
    $pdf->item($record,$titles[$key],$item,$items2[$key]);
  }
  
  $pdf->Output("Export ".$header." ".$record["name"].".pdf","D");
}

function export_record_to_doc($record,$header,$titles,$items,$items2)
{
  global $webdoc;

  function item(&$output,$record,$title,$item,$item2)
  {
    $output.="<h2 style=\"background: #C8DCFF; font-weight: normal; font-family: sans-serif; font-size: 14pt; border: 3px solid #C8DCFF;\">$title:</h2>";
    $output.="<p style=\"border-left: 3px solid white; border-right: 3px solid white;\">";
    if (is_array($record[$item]))
    { 
      foreach ($record[$item] as $key => $row)
      {
        if (isset($txt)) $txt.="<br />";
        $txt.=$row;
        if ($item=="requirements") $txt.=" (".$record["states"][$key].", ".$record["priorities"][$key].")";
      }
      $output.=$txt;
    }
    else $output.=$record[$item];
    if ($item2 && $record[$item2]) $output.="<br />".$record[$item2];
    $output.="</p>";
  }

  $output='<h1 style="background: #C8C8C8; border-style: solid; border-color: #C8C8C8; border-width: 15px 3px 15px 3px; text-align: center; font-weight: normal; font-family: sans-serif; font-size:20pt">'.$header.'</h1>'; 
  $output.='<body style="font-family: sans-serif; font-size: 14pt">';
  
  foreach ($items as $key => $item)
  {
    item($output,$record,$titles[$key],$item,$items2[$key]);
  }
  
  $output.='</body>';

  header("Content-Type: application/octet-stream"); 
  header("Content-Disposition: attachment; filename=\"Export ".$header." ".$record["name"].".doc\";");
  echo $output;
}

function export_list_to_xls($list,$columns)
{
  global $webdoc;
  
  require_once "export/php_writeexcel/class.writeexcel_workbook.inc.php";
  require_once "export/php_writeexcel/class.writeexcel_worksheet.inc.php";
  
  $fname = tempnam("/tmp", "export.xls"); //nacteni docasneho adresare
  $workbook = &new writeexcel_workbook($fname); //vytvoreni noveho sesitu
  $worksheet1 =& $workbook->addworksheet('List'); //vytvoreni listu
  $worksheet1->freeze_panes(1, 0); // ukotveni pricky pod prvni radek
  $header =& $workbook->addformat(array(bold=>1)); //format prvniho radku
  $content =& $workbook->addformat(); //format obsahu
  $worksheet1->set_selection('A2'); //vybrana bunka
  $i=0; //cislo sloupce
  foreach ($columns as $column => $value) //vypsani sloupcu
  {
    //nastaveni sirky sloupcu
    switch ($column)
    {
      case "description": case "comments": case "acceptance_criteria": case "sw_rev": case "hw_rev": case"setup": case "result": case "duration": $width=25; break;
      case "created": case "approved": case "tested": case "terminate": $width=17; break;
      default: $width=12; 
    }
    $worksheet1->set_column(chr(65+$i).':'.chr(65+$i), $width);
    $worksheet1->write(0, $i, $value, $header); //vypsani nazvu sloupcu
    $i++;
  }

  $i=1;
  foreach ($list as $key => $record) //pruchod zaznamy 
  {
    $j=0;
    foreach ($columns as $column => $value) //pruchod pres sloupce
    {
      $worksheet1->write($i, $j, strip_tags($record[$column]), $content); //vypsani obsahu zaznamu do bunek
      $j++;
    }
    $i++;
  }  
  
  $workbook->close();

  switch ($webdoc["main_table"])
  {
    case "requirement": $title="Requirements"; break;
    case "product": $title="Products"; break;
    case "test": $title="Tests"; break;
    case "test_run": $title="Running tests"; break;
  }
 
  header("Content-Type: application/octet-stream");
  header("Content-Disposition: attachment; filename=\"Export $title.xls\";");
  $fh=fopen($fname, "rb");
  fpassthru($fh); //vypsani souboru
  unlink($fname);//smazani z docasneho adresare
}

?>