/**
 * 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);
}
Exemple #2
0
$_SESSION["ft"]["new_search"] = "yes";
$_SESSION["ft"]["current_search"] = array("form_id" => $form_id, "results_per_page" => $results_per_page, "order" => $order, "search_fields" => $search_fields);
// check that the current page is stored in sessions is, in fact, a valid page. e.g. if the person
// was having 10 submissions listed per page, had 11 submissions, and was on page 2 before deleting
// the 11th, when they returned to this page, they'd have page 2 stored in sessions, although there
// is no longer a second page. So for this fringe case, we update the session and refresh the page to
// load the appropriate page
$total_pages = ceil($search_num_results / $results_per_page);
if (isset($_SESSION["ft"]["view_{$view_id}_page"]) && $_SESSION["ft"]["view_{$view_id}_page"] > $total_pages) {
    $_SESSION["ft"]["view_{$view_id}_page"] = $total_pages;
    header("location: index.php");
}
// this sets the total number of submissions that the admin can see in this form and View in the form_X_num_submissions
// and view_X_num_submissions keys. It's used to generate the list of searchable dates
_ft_cache_form_stats($form_id);
_ft_cache_view_stats($form_id, $view_id);
if (!isset($_SESSION["ft"]["form_{$form_id}_select_all_submissions"])) {
    $_SESSION["ft"]["form_{$form_id}_select_all_submissions"] = "";
}
// get a list of all submission IDs in this page
$submission_ids = array();
for ($i = 0; $i < count($search_rows); $i++) {
    $submission_ids[] = $search_rows[$i]["submission_id"];
}
$submission_id_str = implode(",", $submission_ids);
// set as STRING for used in JS below
$select_all_submissions_returned = $_SESSION["ft"]["form_{$form_id}_select_all_submissions"] == "1" ? "true" : "false";
// figure out which submissions should be selected on page load
$preselected_subids = array();
$all_submissions_selected_omit_list_str = "";
if ($select_all_submissions_returned == "true") {