/**
 * 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);
}
Example #2
0
/**
 * Completely removes a form from the database. This includes deleting all form fields, emails, Views,
 * View fields, View tabs, View filters, client-form, client-view and public omit list (form & View),
 * and anything else !
 *
 * It also includes an optional parameter to remove all files that were uploaded through file fields in the
 * form; defaulted to FALSE.
 *
 * @param integer $form_id the unique form ID
 * @param boolean $remove_associated_files A boolean indicating whether or not all files that were
 *              uploaded via file fields in this form should be removed as well.
 */
function ft_delete_form($form_id, $remove_associated_files = false)
{
    global $g_table_prefix;
    extract(ft_process_hook_calls("start", compact("form_id"), array()), EXTR_OVERWRITE);
    $form_fields = ft_get_form_fields($form_id, array("include_field_type_info" => true));
    $success = true;
    $message = "";
    $file_delete_problems = array();
    if ($remove_associated_files) {
        $submission_id_query = mysql_query("SELECT submission_id FROM {$g_table_prefix}form_{$form_id}");
        $file_fields_to_delete = array();
        while ($row = mysql_fetch_assoc($submission_id_query)) {
            $submission_id = $row["submission_id"];
            foreach ($form_fields as $form_field_info) {
                if ($form_field_info["is_file_field"] == "no") {
                    continue;
                }
                // I really don't like this... what should be done is do a SINGLE query after this loop is complete
                // to return a map of field_id to values. That would then update $file_fields_to_delete
                // with a fraction of the cost
                $submission_info = ft_get_submission_info($form_id, $submission_id);
                $filename = $submission_info[$form_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" => $form_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_form");
        }
    }
    // remove the table
    $query = "DROP TABLE IF EXISTS {$g_table_prefix}form_{$form_id}";
    mysql_query($query) or ft_handle_error("Failed query in <b>" . __FUNCTION__ . "</b>, line " . __LINE__ . ": <i>{$query}</i>", mysql_error());
    // remove any reference to the form in form_fields
    mysql_query("DELETE FROM {$g_table_prefix}form_fields WHERE form_id = {$form_id}") or ft_handle_error("Failed query in <b>" . __FUNCTION__ . "</b>, line " . __LINE__ . ": <i>{$query}</i>", mysql_error());
    // remove any reference to the form in forms table
    mysql_query("DELETE FROM {$g_table_prefix}forms WHERE form_id = {$form_id}");
    mysql_query("DELETE FROM {$g_table_prefix}client_forms WHERE form_id = {$form_id}");
    mysql_query("DELETE FROM {$g_table_prefix}form_export_templates WHERE form_id = {$form_id}");
    mysql_query("DELETE FROM {$g_table_prefix}form_email_fields WHERE form_id = {$form_id}");
    mysql_query("DELETE FROM {$g_table_prefix}public_form_omit_list WHERE form_id = {$form_id}");
    mysql_query("DELETE FROM {$g_table_prefix}multi_page_form_urls WHERE form_id = {$form_id}");
    mysql_query("DELETE FROM {$g_table_prefix}list_groups WHERE group_type = 'form_{$form_id}_view_group'");
    // delete all email templates for the form
    $email_templates = ft_get_email_template_list($form_id);
    foreach ($email_templates as $email_template_info) {
        ft_delete_email_template($email_template_info["email_id"]);
    }
    // delete all form Views
    $views_result = mysql_query("SELECT view_id FROM {$g_table_prefix}views WHERE form_id = {$form_id}");
    while ($info = mysql_fetch_assoc($views_result)) {
        ft_delete_view($info["view_id"]);
    }
    // remove any field settings
    foreach ($form_fields as $field_info) {
        $field_id = $field_info["field_id"];
        mysql_query("DELETE FROM {$g_table_prefix}field_settings WHERE field_id = {$field_id}");
    }
    // as with many things in the script, potentially we need to return a vast range of information from this last function. But
    // we'l limit
    if (!$success) {
        $message = $file_delete_problems;
    }
    return array($success, $message);
}