/**
 * download an attachment
 * this function is registered in xajax
 * @param string $list_title title of current list
 * @param int $attachment_id
 * @return xajaxResponse every xajax registered function needs to return this object
 */
function action_download_attachment($list_title, $attachment_id)
{
    global $logging;
    global $user;
    global $user_start_time_array;
    $logging->info("USER_ACTION " . __METHOD__ . " (user="******", attachment_id={$attachment_id})");
    # store start time
    $user_start_time_array[__METHOD__] = microtime(TRUE);
    # create necessary objects
    $response = new xajaxResponse();
    # get the file name and the attachment
    $list_table_attachment = new ListTableAttachment($list_title);
    $attachment_array = $list_table_attachment->select_record($attachment_id);
    $file_name = str_replace(" ", " ", $attachment_array[4]);
    $attachment = $attachment_array[5];
    # create a temp file with attachment
    $tmp_file_name = "download_" . $user->get_name() . strftime("_%d%m%Y_%H%M%S");
    $file_handler = fopen("uploads/" . $tmp_file_name, "w");
    fwrite($file_handler, $attachment);
    fclose($file_handler);
    # call the export handler
    $response->script("document.location.href = 'php/Html.Export.php?tmp_file={$tmp_file_name}&file_name={$file_name}'");
    # log total time for this function
    $logging->info(get_function_time_str(__METHOD__));
    return $response;
}