/**
 *
 * exporting attachment files related to item.
 *
 * @param string $export_path storing directory name
 * @param resource $fhdl file handle that items are exported to.
 * @param int $item_id id of item with attachment files to export.
 * @return array( 'path' => $export_path,
 *                   'attachments' => array( file path of attachment1, file path of attachment2, ... ) )
 *            (relative path of $export_filepath)
 *         returns false if it failed
 */
function xnpExportFile($export_path, $fhdl, $item_id)
{
    $file = xnpGetFileInfo('t_file.file_id, t_file_type.name, t_file.original_file_name, t_file.file_size, t_file.mime_type, t_file.thumbnail_file, t_file.caption', "item_id = {$item_id} and is_deleted=0", $item_id);
    if (!$fhdl) {
        return false;
    }
    // create files directory under $export_path.
    $dir = $export_path . '/files';
    if (!file_exists($dir)) {
        if (!mkdir($dir)) {
            xoonips_error("can't make directory '{$dir}'");
            return false;
        }
    }
    // for absolete path of attachment file
    $files = array();
    foreach ($file as $f) {
        $file = array();
        list($file['file_id'], $file['file_type_name'], $file['original_file_name'], $file['file_size'], $file['mime_type'], $file['thumbnail_file'], $file['caption']) = $f;
        // copy atatchment file $file['file_id'] to $dir and renamed to original file name
        // output <file> to file handle $fhdl
        $hdl = fopen(xnpGetUploadFilePath($file['file_id']), 'rb');
        if (file_exists(xnpGetUploadFilePath($file['file_id']))) {
            if (!copy(xnpGetUploadFilePath($file['file_id']), $dir . '/' . $file['file_id'])) {
                xoonips_error('can\'t write a file \'' . $dir . '/' . $file['file_id'] . "' of the item(ID={$item_id})");
                return false;
            }
            if (!fwrite($fhdl, '<file' . " item_id=\"{$item_id}\"" . " file_type_name=\"{$file['file_type_name']}\"" . " original_file_name=\"{$file['original_file_name']}\"" . " file_name=\"files/{$file['file_id']}\"" . " file_size=\"{$file['file_size']}\"" . " mime_type=\"{$file['mime_type']}\"" . ">\n" . (isset($file['thumbnail_file']) ? '<thumbnail>' . base64_encode($file['thumbnail_file']) . "</thumbnail>\n" : '') . '<caption>' . $file['caption'] . "</caption>\n" . "</file>\n")) {
                fclose($hdl);
                xoonips_error("can't export <file> of the item(ID={$item_id})");
                return false;
            }
            $files[] = "files/{$file['file_id']}";
        }
    }
    return true;
}
function xnpUpdateAttachment($item_id, $name)
{
    global $xoopsDB;
    $formdata =& xoonips_getutility('formdata');
    // File under registration relates to this item.
    $fileID = $formdata->getValue('post', $name . 'FileID', 'i', false, 0);
    $table = $xoopsDB->prefix('xoonips_file');
    $xnpsid = $_SESSION['XNPSID'];
    $esc_sess_id = addslashes($xnpsid);
    // name -> file_type_id
    $sql = "select file_type_id from " . $xoopsDB->prefix('xoonips_file_type') . " where name='{$name}'";
    $result = $xoopsDB->query($sql);
    if ($result == false) {
        echo "xnpUpdateAttachment: bad file_type_name {$name} ";
        return false;
    }
    list($file_type_id) = $xoopsDB->fetchRow($result);
    // delete old file
    $sql = "select file_id, is_deleted from {$table} where item_id={$item_id} and file_type_id={$file_type_id} and is_deleted=0 and file_id <> {$fileID}";
    $result = $xoopsDB->queryF($sql);
    if ($result == false) {
        echo "Error: cannot update xoonips_file {$sql} " . mysql_error();
        return false;
    }
    while (list($file_id, $is_deleted) = $xoopsDB->fetchRow($result)) {
        $path = xnpGetUploadFilePath($file_id);
        if (is_file($path)) {
            unlink($path);
        }
        $result = $xoopsDB->queryF("update {$table} set is_deleted=1 where file_id={$file_id}");
    }
    if (!empty($fileID)) {
        $sql = "update {$table} set sess_id=NULL, item_id={$item_id} where sess_id='{$esc_sess_id}' and file_id={$fileID} and file_type_id={$file_type_id}";
        $result = $xoopsDB->queryF($sql);
        if ($result == false) {
            echo "Error: cannot update xoonips_file {$sql} " . mysql_error();
            return false;
        }
    }
    return true;
}