$page_field_types = ft_get_field_types(true, $page_field_type_ids);
// construct the tab list
$view_tabs = ft_get_view_tabs($view_id, true);
$tabs = array();
$same_page = ft_get_clean_php_self();
while (list($key, $value) = each($view_tabs)) {
    $tabs[$key] = array("tab_label" => $value["tab_label"], "tab_link" => "{$same_page}?tab={$key}&form_id={$form_id}&submission_id={$submission_id}");
}
// get a list of editable fields on this tab
$editable_tab_fields = array_intersect($page_field_ids, $editable_field_ids);
// if we're just coming here from the search results page, get a fresh list of every submission ID in this
// search result set. This is used to build the internal "<< previous   next >>" nav on this details page.
// They need to exactly correspond to the ordering of the search results or they don't make sense
$search = isset($_SESSION["ft"]["current_search"]) ? $_SESSION["ft"]["current_search"] : array();
if (isset($_SESSION["ft"]["new_search"]) && $_SESSION["ft"]["new_search"] == "yes") {
    $searchable_columns = ft_get_view_searchable_fields("", $view_info["fields"]);
    // extract the original search settings and get the list of IDs
    $submission_ids = ft_get_search_submission_ids($form_id, $view_id, $search["results_per_page"], $search["order"], $search["search_fields"], $searchable_columns);
    $_SESSION["ft"]["form_{$form_id}_view_{$view_id}_submissions"] = $submission_ids;
    $_SESSION["ft"]["new_search"] = "no";
}
list($prev_link_html, $search_results_link_html, $next_link_html) = _ft_code_get_link_html($form_id, $view_id, $submission_id, $search["results_per_page"]);
// construct the page label
$submission_placeholders = ft_get_submission_placeholders($form_id, $submission_id);
$edit_submission_page_label = ft_eval_smarty_string($form_info["edit_submission_page_label"], $submission_placeholders);
$validation_js = ft_generate_submission_js_validation($grouped_fields);
// get all the shared resources
$settings = ft_get_settings("", "core");
$shared_resources_list = $settings["edit_submission_onload_resources"];
$shared_resources_array = explode("|", $shared_resources_list);
$shared_resources = "";
/**
 * Deletes multiple form submissions at once.
 *
 * If required, deletes any files that were uploaded along with the original submissions. If one or
 * more files associated with this submission couldn't be deleted (either because they didn't exist
 * or because they didn't have permissions) the submission IS deleted, but it returns an error
 * indicating which files caused problems.
 *
 * @param integer $form_id the unique form ID
 * @param mixed $delete_ids a single submission ID / an array of submission IDs / "all". This column
 *               determines which submissions will be deleted
 * @param integer $view_id (optional) this is only needed if $delete_ids is set to "all". With the advent
 *               of Views, it needs to know which submissions to delete.
 * @return array returns array with indexes:<br/>
 *               [0]: true/false (success / failure)<br/>
 *               [1]: message string<br/>
 */
function ft_delete_submissions($form_id, $view_id, $submissions_to_delete, $omit_list, $search_fields, $is_admin)
{
    global $g_table_prefix, $LANG;
    $submission_ids = array();
    if ($submissions_to_delete == "all") {
        // get the list of searchable columns for this View. This is needed to ensure that ft_get_search_submission_ids receives
        // the correct info to determine what submission IDs are appearing in this current search.
        $searchable_columns = ft_get_view_searchable_fields($view_id);
        $submission_ids = ft_get_search_submission_ids($form_id, $view_id, "all", "submission_id-ASC", $search_fields, $searchable_columns);
        $submission_ids = array_diff($submission_ids, $omit_list);
    } else {
        $submission_ids = $submissions_to_delete;
    }
    $submissions_to_delete = $submission_ids;
    extract(ft_process_hook_calls("start", compact("form_id", "view_id", "submissions_to_delete", "omit_list", "search_fields", "is_admin"), array("submission_ids")), EXTR_OVERWRITE);
    $form_info = ft_get_form($form_id);
    $form_fields = ft_get_form_fields($form_id);
    $auto_delete_submission_files = $form_info["auto_delete_submission_files"];
    $submission_ids_qry = array();
    foreach ($submission_ids as $submission_id) {
        $submission_ids_qry[] = "submission_id = {$submission_id}";
    }
    $where_clause = "WHERE " . join(" OR ", $submission_ids_qry);
    // loop the form templates to find out if there are any file fields. If there are - and the user
    // configured it - delete any associated files
    $file_delete_problems = array();
    $form_has_file_field = false;
    if ($auto_delete_submission_files == "yes") {
        $file_field_type_ids = ft_get_file_field_type_ids();
        $file_fields_to_delete = array();
        foreach ($submissions_to_delete as $submission_id) {
            foreach ($form_fields as $field_info) {
                $field_type_id = $field_info["field_type_id"];
                if (!in_array($field_type_id, $file_field_type_ids)) {
                    continue;
                }
                $form_has_file_field = true;
                $submission_info = ft_get_submission_info($form_id, $submission_id);
                $filename = $submission_info[$field_info['col_name']];
                // if no filename was stored, it was empty - just continue
                if (empty($filename)) {
                    continue;
                }
                $file_fields_to_delete[] = array("submission_id" => $submission_id, "field_id" => $field_info["field_id"], "field_type_id" => $field_type_id, "filename" => $filename);
            }
        }
        if (!empty($file_fields_to_delete)) {
            list($success, $file_delete_problems) = ft_delete_submission_files($form_id, $file_fields_to_delete, "ft_delete_submissions");
        }
    }
    // now delete the submission
    mysql_query("DELETE FROM {$g_table_prefix}form_{$form_id} {$where_clause}");
    if ($auto_delete_submission_files == "yes") {
        if (empty($file_delete_problems)) {
            $success = true;
            if (count($submission_ids) > 1) {
                $message = $form_has_file_field ? $LANG["notify_submissions_and_files_deleted"] : $LANG["notify_submissions_deleted"];
            } else {
                $message = $form_has_file_field ? $LANG["notify_submission_and_files_deleted"] : $LANG["notify_submission_deleted"];
            }
        } else {
            $success = false;
            if (count($submission_ids) > 1) {
                $message = $LANG["notify_submissions_deleted_with_problems"] . "<br /><br />";
            } else {
                $message = $LANG["notify_submission_deleted_with_problems"] . "<br /><br />";
            }
            foreach ($file_delete_problems as $problem) {
                $message .= "&bull; <b>{$problem["filename"]}</b>: {$problem["error"]}<br />\n";
            }
        }
    } else {
        $success = true;
        if (count($submission_ids) > 1) {
            $message = $LANG["notify_submissions_deleted"];
        } else {
            $message = $LANG["notify_submission_deleted"];
        }
    }
    // TODO update sessions to ensure the first submission date and num submissions for this form View are correct
    _ft_cache_form_stats($form_id);
    _ft_cache_view_stats($form_id, $view_id);
    $_SESSION["ft"]["form_{$form_id}_select_all_submissions"] = "";
    $_SESSION["ft"]["form_{$form_id}_selected_submissions"] = array();
    $_SESSION["ft"]["form_{$form_id}_all_submissions_selected_omit_list"] = array();
    // loop through all submissions deleted and send any emails
    reset($submission_ids);
    foreach ($submission_ids as $submission_id) {
        ft_send_emails("on_delete", $form_id, $submission_id);
    }
    $submissions_to_delete = $submission_ids;
    extract(ft_process_hook_calls("end", compact("form_id", "view_id", "submissions_to_delete", "omit_list", "search_fields", "is_admin"), array("success", "message")), EXTR_OVERWRITE);
    return array($success, $message);
}