<?php

/**
 * funkce pro vkladani a zobrazovani souboru k zaznamum
 * \param array $params: pole proměnných prevzatých z sablony. Musí obsahovat vyse uvedene promenné
 * \param object &$smarty: objekt smarty 
 * \return false pokud dojde k chybě 
 */
 
  
function smarty_function_files($params,&$smarty)
{
  global $webdoc;
  extract($params); //vytvoreni samostatnych promennych z pole $params
  $params["smarty_include_vars"]=Array(); //definovani novych promennych v $params, bez nich to nebude fungovat
 
  if (!isset($table_id))
  {
    $smarty->trigger_error("files: nebyl zadan parametr  table_id"); //Smarty error
    return false;
  }
  
  if (!isset($record_id))
  {
    $smarty->trigger_error("files: nebyl zadan parametr  record_id"); //Smarty error
    return false;
  }

  if (isset($table_id) AND isset($record_id))
  {
    $output = '
    <table border="0">
    ';
    
    $result=database_query("SELECT * FROM file_list WHERE table_id LIKE '$table_id' AND record_id LIKE '$record_id'");
    while ($zaznam = database_fetch_array($result))
    {
      $output .= '<tr><td><a href="../files/' . $zaznam["file_name"] . '" target="_blank">'; if ($zaznam["removed"]) $output .= '<strike>'; $output .= $zaznam["file_name"]; if ($zaznam["removed"]) $output .= '</strike>'; $output .= '</a></td>';
                  if ($zaznam["removed"]) $output .= '<td>removed</td>';
                  else
                  {
                  if ($zaznam["creator_id"]==$webdoc["auth"])
                  {
                  $output .= '<td><form action="" method="post">
                  <input type="hidden" name="file_list_id" value="' . $zaznam["file_list_id"] . '">
                  <input type="submit" name="delete_file" value="Delete file">
                  </form></td></tr>
                  ';
                  }
                  else
                  {
                    $output .= '<td></td>';
                  }
                  }    
    }
    $output .= '</table><br>Add new file:<br>
    ';
    $output .=  '<form action="" method="post" enctype="multipart/form-data">
                 <input type="file" name="soubor" />
                 <input type="hidden" name="table_id" value="' . $table_id . '">
                 <input type="hidden" name="record_id" value="' . $record_id . '">
                 <input type="hidden" name="MAX_FILE_SIZE" value="10000000">
                 <input type="submit" name="save_file" value="Save file">
                 </form>';
    return $output;
  }

}

?>
