/**
 * Returns HTML for Digital Download UI
 *
 * @param int $product_id
 * @return HTML
 */
function wpsc_select_product_file($product_id = null)
{
    global $wpdb;
    $product_id = absint($product_id);
    $file_list = wpsc_uploaded_files();
    $args = array('post_type' => 'wpsc-product-file', 'post_parent' => $product_id, 'numberposts' => -1, 'post_status' => 'all');
    $attached_files = (array) get_posts($args);
    $output = '<table id="wpsc_digital_download_table" class="wp-list-table widefat posts select_product_file">';
    $output .= '<thead>';
    $output .= '<tr>';
    $output .= '<th>' . _x('Title', 'Digital download UI', 'wp-e-commerce') . '</th>';
    $output .= '<th>' . _x('Size', 'Digital download UI', 'wp-e-commerce') . '</th>';
    $output .= '<th>' . _x('File Type', 'Digital download UI', 'wp-e-commerce') . '</th>';
    $output .= '<th id="wpsc_digital_download_action_th">' . _x('Actions', 'Digital download UI', 'wp-e-commerce') . '</th>';
    $output .= '</tr>';
    $output .= '</thead>';
    $output .= '<tfoot>';
    $output .= '<tr>';
    $output .= '<th>' . _x('Title', 'Digital download UI', 'wp-e-commerce') . '</th>';
    $output .= '<th>' . _x('Size', 'Digital download UI', 'wp-e-commerce') . '</th>';
    $output .= '<th>' . _x('File Type', 'Digital download UI', 'wp-e-commerce') . '</th>';
    $output .= '<th id="wpsc_digital_download_action_th">' . _x('Actions', 'Digital download UI', 'wp-e-commerce') . '</th>';
    $output .= '</tr>';
    $output .= '</tfoot>';
    $num = 0;
    $output .= '<tbody>';
    $delete_nonce = _wpsc_create_ajax_nonce('delete_file');
    foreach ((array) $attached_files as $file) {
        $file_dir = WPSC_FILE_DIR . $file->post_title;
        $file_size = 'http://s3file' == $file->guid ? __('Remote file sizes cannot be calculated', 'wp-e-commerce') : wpsc_convert_byte(filesize($file_dir));
        $file_url = add_query_arg(array('wpsc_download_id' => $file->ID, '_wpnonce' => wp_create_nonce('wpsc-admin-download-file-' . $file->ID)), admin_url());
        $deletion_url = wp_nonce_url("admin.php?wpsc_admin_action=delete_file&amp;file_name={$file->post_title}&amp;product_id={$product_id}&amp;row_number={$num}", 'delete_file_' . $file->post_title);
        $class = !wpsc_is_odd($num) ? 'alternate' : '';
        $file_type = get_post_mime_type($file->ID);
        $icon_url = wp_mime_type_icon($file_type);
        $output .= '<tr class="wpsc_product_download_row ' . $class . '">';
        $output .= '<td style="padding-right: 30px;"><img src="' . $icon_url . '"><span>' . $file->post_title . '</span></td>';
        $output .= '<td>' . $file_size . '</td>';
        $output .= '<td>' . $file_type . '</td>';
        $output .= '<td><a href="' . esc_url($file_url) . '">' . _x('Download', 'Digital download row UI', 'wp-e-commerce') . '</a><a data-file-name="' . esc_attr($file->post_title) . '" data-product-id="' . esc_attr($product_id) . '" data-nonce="' . esc_attr($delete_nonce) . '" class="file_delete_button" href="' . $deletion_url . '" >' . _x("Delete", "Digital download row UI", 'wp-e-commerce') . '</a></td>';
        $output .= '</tr>';
        $num++;
    }
    $output .= '</tbody>';
    $output .= '</table>';
    if (empty($attached_files)) {
        $output .= "<p class='no-item'>" . __('There are no files attached to this product. Upload a new file or select from other product files.', 'wp-e-commerce') . "</p>";
    }
    $output .= "<div class='" . (is_numeric($product_id) ? 'edit_' : '') . "select_product_handle'></div>";
    $output .= "<script type='text/javascript'>\r\n";
    $output .= "var select_min_height = " . 25 * 3 . ";\r\n";
    $output .= "var select_max_height = " . 25 * ($num + 1) . ";\r\n";
    $output .= "</script>";
    return $output;
}
/**
 * Returns HTML for Digital Download UI
 * 
 * @param int $product_id 
 * @return HTML
 */
function wpsc_select_product_file($product_id = null)
{
    global $wpdb;
    $product_id = absint($product_id);
    $file_list = wpsc_uploaded_files();
    $args = array('post_type' => 'wpsc-product-file', 'post_parent' => $product_id, 'numberposts' => -1, 'post_status' => 'all');
    $attached_files = (array) get_posts($args);
    $output = '<table class="wp-list-table widefat fixed posts select_product_file">';
    $output .= '<thead>';
    $output .= '<tr>';
    $output .= '<th>' . _x('Title', 'Digital download UI', 'wpsc') . '</th>';
    $output .= '<th>' . _x('Size', 'Digital download UI', 'wpsc') . '</th>';
    $output .= '<th>' . _x('File Type', 'Digital download UI', 'wpsc') . '</th>';
    $output .= '<th>' . _x('Trash', 'Digital download UI', 'wpsc') . '</th>';
    $output .= '<th>' . _x('Preview', 'Digital download UI', 'wpsc') . '</th>';
    $output .= '</tr>';
    $output .= '</thead>';
    $num = 0;
    $output .= '<tbody>';
    foreach ((array) $attached_files as $file) {
        $file_dir = WPSC_FILE_DIR . $file->post_title;
        $file_size = 'http://s3file' == $file->guid ? __('Remote file sizes cannot be calculated', 'wpsc') : wpsc_convert_byte(filesize($file_dir));
        $file_url = WPSC_FILE_URL . $file->post_title;
        $deletion_url = wp_nonce_url("admin.php?wpsc_admin_action=delete_file&amp;file_name={$file->post_title}&amp;product_id={$product_id}&amp;row_number={$num}", 'delete_file_' . $file->post_title);
        $class = !wpsc_is_odd($num) ? 'alternate' : '';
        $output .= '<tr class="wpsc_product_download_row ' . $class . '">';
        $output .= '<td style="padding-right: 30px;">' . $file->post_title . '</td>';
        $output .= '<td>' . $file_size . '</td>';
        $output .= '<td>.' . wpsc_get_extension($file->post_title) . '</td>';
        $output .= "<td><a class='file_delete_button' href='{$deletion_url}' >" . _x('Delete', 'Digital download row UI', 'wpsc') . "</a></td>";
        $output .= '<td><a href=' . $file_url . '>' . _x('Download', 'Digital download row UI', 'wpsc') . '</a></td>';
        $output .= '</tr>';
        $num++;
    }
    $output .= '</tbody>';
    $output .= '</table>';
    if (empty($attached_files)) {
        $output .= "<p class='no-item'>" . __('There are no files attached to this product. Upload a new file or select from other product files.', 'wpsc') . "</p>";
    }
    $output .= "<div class='" . (is_numeric($product_id) ? 'edit_' : '') . "select_product_handle'></div>";
    $output .= "<script type='text/javascript'>\r\n";
    $output .= "var select_min_height = " . 25 * 3 . ";\r\n";
    $output .= "var select_max_height = " . 25 * ($num + 1) . ";\r\n";
    $output .= "</script>";
    return $output;
}