/** * This function is used for displaying and exporting the data. Basically it merges all information * about a particular field from the view_fields table with the form_fields and field_options table, * providing ALL information about a field in a single variable. * * It accepts the result of the ft_get_view_fields() function as the first parameter and an optional * boolean to let it know whether to return ALL results or not. * * TODO maybe deprecate? Only used mass_edit * * @param array $view_fields * @param boolean $return_all_fields */ function ft_get_submission_field_info($view_fields) { $display_fields = array(); foreach ($view_fields as $field) { $field_id = $field["field_id"]; $curr_field_info = array("field_id" => $field_id, "field_title" => $field["field_title"], "col_name" => $field["col_name"], "list_order" => $field["list_order"]); $field_info = ft_get_form_field($field_id); $curr_field_info["field_info"] = $field_info; $display_fields[] = $curr_field_info; } return $display_fields; }
/** * This handy function figures out the various components of an email and returns them in a hash: * from, reply_to, to, cc, bcc, subject, html_content and text_content * * This is used both when sending the emails but also for testing. This should be the only place that * email content is actually constructed. All other email functions should be using it, regardless of * what mechanism actually sends the email. * * @param integer $form_id * @param mixed $submission_id for non-test emails, this is included. For testing, it may be blank. * @param integer $email_id * @return array */ function ft_get_email_components($form_id, $submission_id = "", $email_id, $is_test = false, $test_settings = array()) { global $g_table_prefix, $g_root_dir, $LANG, $g_default_theme; $email_template = ft_get_email_template($email_id); // if the administrator limited the email content to fields in a particular View, pass those fields to the // template - NOT all of the form fields (which is the default) $fields_for_email_template = array(); if (!empty($email_template["limit_email_content_to_fields_in_view"])) { $view_fields = ft_get_view_fields($email_template["limit_email_content_to_fields_in_view"]); // here, $view_fields just contains the info from the view_fields table. We need the info from the form_fields // table instead - since it contains presentation information likely to be needed in the email templates $fields_for_email_template = array(); foreach ($view_fields as $view_field_info) { $fields_for_email_template[] = ft_get_form_field($view_field_info["field_id"], array("include_field_type_info" => true)); } } else { $fields_for_email_template = ft_get_form_fields($form_id, array("include_field_type_info" => true)); } // this returns a hash with three keys: html_content, text_content and submission_id $templates = _ft_get_email_template_content($form_id, $submission_id, $email_template, $is_test, $test_settings); $submission_id = $templates["submission_id"]; // unfortunately we need this, even though it was just called in _ft_get_email_template_content() $submission_info = ft_get_submission($form_id, $submission_id); // retrieve the placeholders and their substitutes $submission_placeholders = ft_get_submission_placeholders($form_id, $submission_id); $admin_info = ft_get_admin_info(); $file_info = array(); $updated_fields_for_email_template = array(); foreach ($fields_for_email_template as $field_info) { if ($field_info["is_file_field"] == "yes") { $field_id = $field_info["field_id"]; $field_settings = ft_get_field_settings($field_id); $field_info["folder_url"] = $field_settings["folder_url"]; $field_info["folder_path"] = $field_settings["folder_path"]; $filename = $field_info["field_name"]; $field_info["answer"] = $submission_placeholders["FILENAME_{$filename}"]; } $updated_fields_for_email_template[] = $field_info; } $fields_for_email_template = $updated_fields_for_email_template; $updated_fields_for_email_template = array(); foreach ($fields_for_email_template as $field_info) { while (list($placeholder, $value) = each($submission_placeholders)) { if ($placeholder != "ANSWER_{$field_info["field_name"]}") { continue; } $field_info["answer"] = $value; break; } reset($submission_placeholders); $updated_fields_for_email_template[] = $field_info; } $fields_for_email_template = $updated_fields_for_email_template; $return_info = array(); $return_info["email_id"] = $email_id; $return_info["attachments"] = array(); $smarty = new Smarty(); $smarty->template_dir = "{$g_root_dir}/global/smarty/"; $smarty->compile_dir = "{$g_root_dir}/themes/{$g_default_theme}/cache/"; $smarty->assign("LANG", $LANG); $smarty->assign("fields", $fields_for_email_template); if (!empty($templates["text"])) { list($templates["text"], $attachments) = _ft_extract_email_attachment_info($templates["text"], $form_id, $submission_placeholders); foreach ($attachments as $attachment_info) { if (!in_array($attachment_info, $return_info["attachments"])) { $return_info["attachments"][] = $attachment_info; } } $smarty->assign("eval_str", $templates["text"]); while (list($key, $value) = each($submission_placeholders)) { $smarty->assign($key, $value); } reset($submission_placeholders); $return_info["text_content"] = $smarty->fetch("eval.tpl"); } if (!empty($templates["html"])) { list($templates["html"], $attachments) = _ft_extract_email_attachment_info($templates["html"], $form_id, $submission_placeholders); foreach ($attachments as $attachment_info) { if (!in_array($attachment_info, $return_info["attachments"])) { $return_info["attachments"][] = $attachment_info; } } $smarty->assign("eval_str", $templates["html"]); while (list($key, $value) = each($submission_placeholders)) { // convert any newlines chars to page breaks for any answer fields. Hmm... if (strpos($key, "ANSWER_") === 0) { $value = nl2br($value); } $smarty->assign($key, $value); } $return_info["html_content"] = $smarty->fetch("eval.tpl"); } // compile the "to" / "from" / "reply-to" recipient list, based on this form submission. Virtually // everything is already stored in $email_template["recipients"], but needs to be extracted. // The notable exception is the FORM EMAIL FIELD information: that has to be constructed separately $return_info["to"] = array(); $return_info["cc"] = array(); $return_info["bcc"] = array(); foreach ($email_template["recipients"] as $recipient_info) { $recipient_type_key = $recipient_info["recipient_type"]; if ($recipient_info["recipient_type"] == "" || $recipient_info["recipient_type"] == "main") { $recipient_type_key = "to"; } if ($recipient_info["recipient_user_type"] == "form_email_field") { $header_info = _ft_get_form_email_field_headers($recipient_info["form_email_id"], $submission_info); $user_recipient = $header_info["recipient_line"]; $user_first_name = $header_info["first_name"]; $user_last_name = $header_info["last_name"]; $user_email = $header_info["email"]; $curr_recipient_info = array("recipient_line" => $user_recipient, "name" => "{$user_first_name} {$user_last_name}", "email" => $user_email); } else { $curr_recipient_info = array("recipient_line" => $recipient_info["final_recipient"], "name" => $recipient_info["final_name"], "email" => $recipient_info["final_email"]); } if (!empty($curr_recipient_info["email"])) { $return_info[$recipient_type_key][] = $curr_recipient_info; } } $return_info["from"] = array(); switch ($email_template["email_from"]) { case "admin": $return_info["from"] = array("recipient_line" => "{$admin_info["first_name"]} {$admin_info["last_name"]} <{$admin_info["email"]}>", "name" => "{$admin_info["first_name"]} {$admin_info["last_name"]}", "email" => $admin_info["email"]); break; case "client": $client_info = ft_get_account_info($email_template["email_from_account_id"]); $return_info["from"] = array("recipient_line" => "{$client_info["first_name"]} {$client_info["last_name"]} <{$client_info["email"]}>", "name" => "{$client_info["first_name"]} {$client_info["last_name"]}", "email" => $client_info["email"]); break; case "form_email_field": $header_info = _ft_get_form_email_field_headers($email_template["email_from_form_email_id"], $submission_info); $user_recipient = $header_info["recipient_line"]; $user_first_name = $header_info["first_name"]; $user_last_name = $header_info["last_name"]; $user_email = $header_info["email"]; $return_info["from"] = array("recipient_line" => $user_recipient, "name" => "{$user_first_name} {$user_last_name}", "email" => $user_email); break; case "custom": $return_info["from"] = array("recipient_line" => "{$email_template["custom_from_name"]} <{$email_template["custom_from_email"]}>", "name" => $email_template["custom_from_name"], "email" => $email_template["custom_from_email"]); break; } $return_info["reply_to"] = array(); switch ($email_template["email_reply_to"]) { case "admin": $return_info["reply_to"] = array("recipient_line" => "{$admin_info["first_name"]} {$admin_info["last_name"]} <{$admin_info["email"]}>", "name" => "{$admin_info["first_name"]} {$admin_info["last_name"]}", "email" => $admin_info["email"]); break; case "client": $client_info = ft_get_account_info($email_template["email_reply_to_account_id"]); $return_info["reply_to"] = array("recipient_line" => "{$client_info["first_name"]} {$client_info["last_name"]} <{$client_info["email"]}>", "name" => "{$client_info["first_name"]} {$client_info["last_name"]}", "email" => $client_info["email"]); break; case "form_email_field": $form_email_id = $email_template["email_reply_to_form_email_id"]; $header_info = _ft_get_form_email_field_headers($form_email_id, $submission_info); $user_recipient = $header_info["recipient_line"]; $user_first_name = $header_info["first_name"]; $user_last_name = $header_info["last_name"]; $user_email = $header_info["email"]; $return_info["reply_to"] = array("recipient_line" => $user_recipient, "name" => "{$user_first_name} {$user_last_name}", "email" => $user_email); break; case "custom": $return_info["reply_to"] = array("recipient_line" => "{$email_template["custom_reply_to_name"]} <{$email_template["custom_reply_to_email"]}>", "name" => $email_template["custom_reply_to_name"], "email" => $email_template["custom_reply_to_email"]); break; } $return_info["subject"] = ft_eval_smarty_string($email_template["subject"], $submission_placeholders); return array(true, $return_info); }
/** * Deletes a file that has been uploaded through a particular form submission file field. * * Now say that 10 times fast. * * @param integer $form_id the unique form ID * @param integer $submission_id a unique submission ID * @param integer $field_id a unique form field ID * @param boolean $force_delete this forces the file to be deleted from the database, even if the * file itself doesn't exist or doesn't have the right permissions. * @return array Returns array with indexes:<br/> * [0]: true/false (success / failure)<br/> * [1]: message string<br/> */ function ft_file_delete_file_submission($form_id, $submission_id, $field_id, $force_delete = false) { global $g_table_prefix, $LANG; // get the column name and upload folder for this field $field_info = ft_get_form_field($field_id); $col_name = $field_info["col_name"]; // if the column name wasn't found, the $field_id passed in was invalid. Return false. if (empty($col_name)) { return array(false, $LANG["notify_submission_no_field_id"]); } $field_settings = ft_get_field_settings($field_id); $file_folder = $field_settings["folder_path"]; $query = "\n SELECT {$col_name}\n FROM {$g_table_prefix}form_{$form_id}\n WHERE submission_id = {$submission_id}\n "; $result = mysql_query($query); $file_info = mysql_fetch_row($result); $file = $file_info[0]; $update_database_record = false; $success = true; $message = ""; if (!empty($file)) { if ($force_delete) { @unlink("{$file_folder}/{$file}"); $message = $LANG["notify_file_deleted"]; $update_database_record = true; } else { if (@unlink("{$file_folder}/{$file}")) { $success = true; $message = $LANG["notify_file_deleted"]; $update_database_record = true; } else { if (!is_file("{$file_folder}/{$file}")) { $success = false; $update_database_record = false; $replacements = array("js_link" => "return files_ns.delete_submission_file({$field_id}, true)"); $message = ft_eval_smarty_string($LANG["notify_file_not_deleted_no_exist"] . "({$file_folder}/{$file})", $replacements); } else { if (is_file("{$file_folder}/{$file}") && (!is_readable("{$file_folder}/{$file}") || !is_writable("{$file_folder}/{$file}"))) { $success = false; $update_database_record = false; $replacements = array("js_link" => "return files_ns.delete_submission_file({$field_id}, true)"); $message = ft_eval_smarty_string($LANG["notify_file_not_deleted_permissions"], $replacements); } else { $success = false; $update_database_record = false; $replacements = array("js_link" => "return files_ns.delete_submission_file({$field_id}, true)"); $message = ft_eval_smarty_string($LANG["notify_file_not_deleted_unknown_error"], $replacements); } } } } } // if need be, update the database record to remove the reference to the file in the database. Generally this // should always work, but in case something funky happened, like the permissions on the file were changed to // forbid deleting, I think it's best if the record doesn't get deleted to remind the admin/client it's still // there. if ($update_database_record) { $query = mysql_query("\n UPDATE {$g_table_prefix}form_{$form_id}\n SET {$col_name} = ''\n WHERE submission_id = {$submission_id}\n "); } extract(ft_process_hook_calls("end", compact("form_id", "submission_id", "field_id", "force_delete"), array("success", "message")), EXTR_OVERWRITE); return array($success, $message); }
/** * This is called when the user updates the field type on the Edit Field Options page. It deletes all old * now-irrelevant settings, but retains values that will not change based on field type. * * @param integer $form_id * @param integer $field_id * @param string $new_field_type */ function ft_change_field_type($form_id, $field_id, $new_field_type) { global $g_table_prefix; $field_info = ft_get_form_field($field_id); // if the field just changes from one multi-select field to another (radio, checkboxes, select or multi-select) // don't delete the field_option group: it's probable that they just wanted to switch the appearance. $old_field_type = $field_info["field_type"]; $multi_select_types = array("select", "multi-select", "radio-buttons", "checkboxes"); $clauses = array("field_type = '{$new_field_type}'"); if (!in_array($old_field_type, $multi_select_types) || !in_array($new_field_type, $multi_select_types)) { $clauses[] = "field_group_id = NULL"; } if ($new_field_type == "file") { $clauses[] = "field_size = 'medium'"; } $clauses_str = implode(",", $clauses); mysql_query("DELETE FROM {$g_table_prefix}field_settings WHERE field_id = {$field_id}"); mysql_query("\n UPDATE {$g_table_prefix}form_fields\n SET {$clauses_str}\n WHERE field_id = {$field_id}\n ") or die(mysql_error()); // if the user just changed to a file type, ALWAYS set the database field size to "medium" if ($old_field_type != $new_field_type && $new_field_type == "file") { _ft_alter_table_column("{$g_table_prefix}form_{$form_id}", $field_info["col_name"], $field_info["col_name"], "VARCHAR(255)"); } }
/** * Moves all files that are associated with a particular form field. This is used whenever the path for a particular * form field is changed. It it called automatically by the script to move all files to the correct location. This can * be used for standard file fields or for images uploaded through the Image Manager module. * * @param integer $field_id * @param string $source_folder * @param string $target_folder * @param string $image_type if the files being moved are images from the Image Manager, the script need to know * what image type it is (main_image, main_thumb, search_results_thumb) to know which ones to move. */ function ft_move_field_files($field_id, $source_folder, $target_folder, $image_type = "") { global $g_table_prefix, $g_multi_val_delimiter; if ($source_folder == $target_folder) { return; } $field_info = ft_get_form_field($field_id); $col_name = $field_info["col_name"]; $field_type = $field_info["field_type"]; $form_id = $field_info["form_id"]; if ($field_type != "file" && $field_type != "image") { return; } if ($field_type == "image" && empty($image_type)) { return; } $query = mysql_query("\n SELECT submission_id, {$col_name}\n FROM {$g_table_prefix}form_{$form_id}\n WHERE {$col_name} != ''''\n "); while ($row = mysql_fetch_assoc($query)) { $submission_id = $row["submission_id"]; $filename = $row[$col_name]; // if this is an image, the field actually contains up to THREE filenames (main image, main thumb, search // results thumb). Find the one we want to move and overwrite $filename if ($field_type == "image") { $image_info = ft_get_filenames_from_image_field_string($filename); switch ($image_type) { case "main_image": $filename = $image_info["main_image"]; break; case "main_thumb": $filename = $image_info["main_thumb"]; break; case "search_results_thumb": $filename = $image_info["search_results_thumb"]; break; default: continue; break; } } // move the file list($success, $new_filename) = ft_move_file($source_folder, $target_folder, $filename); // if the file was successfully moved but RENAMED, update the database record if ($success && $filename != $new_filename) { $db_field_str = ""; if ($image_type = "file") { $db_field_str = $new_filename; } else { switch ($image_type) { case "main_image": $image_field_string_sections = array("main_image:{$new_filename}", "main_thumb:{$image_info["main_thumb"]}", "search_results_thumb:{$image_info["search_results_thumb"]}"); break; case "main_thumb": $image_field_string_sections = array("main_image:{$image_info["main_image"]}", "main_thumb:{$new_filename}", "search_results_thumb:{$image_info["search_results_thumb"]}"); break; case "search_results_thumb": $image_field_string_sections = array("main_image:{$image_info["main_image"]}", "main_thumb:{$image_info["main_thumb"]}", "search_results_thumb:{$new_filename}"); break; } $db_field_str = implode($g_multi_val_delimiter, $image_field_string_sections); } mysql_query("\n UPDATE {$g_table_prefix}form_{$form_id}\n SET {$col_name} = '{$db_field_str}'\n WHERE submission_id = {$submission_id}\n ") or die(mysql_error()); } } }