/**
 * Used for retrieving the data for a mapped form field; i.e. a dropdown, radio group or checkbox group
 * field whose source contents is the contents of a different form field.
 *
 * @param integer $form_id
 * @param array $results a complex data structure
 */
function ft_get_mapped_form_field_data($setting_value)
{
    global $g_table_prefix;
    $trimmed = preg_replace("/form_field:/", "", $setting_value);
    // this prevents anything wonky being shown if the following query fails (for whatever reason)
    $formatted_results = "";
    list($form_id, $field_id, $order) = explode("|", $trimmed);
    if (!empty($form_id) && !empty($field_id) && !empty($order)) {
        $map = ft_get_field_col_by_field_id($form_id, $field_id);
        $col_name = $map[$field_id];
        $query = @mysql_query("\n      SELECT submission_id, {$col_name}\n      FROM   {$g_table_prefix}form_{$form_id}\n      ORDER BY {$col_name} {$order}\n    ");
        if ($query) {
            $results = array();
            while ($row = mysql_fetch_assoc($query)) {
                $results[] = array("option_value" => $row["submission_id"], "option_name" => $row[$col_name]);
            }
            // yuck! But we need to force the form field info into the same format as the option lists,
            // so the Field Types don't need to do additional work to display both cases
            $formatted_results = array("type" => "form_field", "form_id" => $form_id, "field_id" => $field_id, "options" => array(array("group_info" => array(), "options" => $results)));
        }
    }
    return $formatted_results;
}
/**
 * This is the hook for the ft_get_uploaded_files core function. It returns an array of hashes;
 *
 * @param array $params
 */
function ft_file_get_uploaded_files_hook($params)
{
    global $g_table_prefix;
    $form_id = $params["form_id"];
    $field_ids = isset($params["field_ids"]) && is_array($params["field_ids"]) ? $params["field_ids"] : array();
    $module_field_type_id = ft_get_field_type_id_by_identifier("file");
    $data = array();
    foreach ($field_ids as $field_id) {
        $field_type_id = ft_get_field_type_id_by_field_id($field_id);
        if ($field_type_id != $module_field_type_id) {
            continue;
        }
        $result = ft_get_field_col_by_field_id($form_id, field_id);
        $col_name = $result[$field_id];
        if (empty($col_name)) {
            continue;
        }
        $query = mysql_query("SELECT submission_id, {$col_name} FROM {$g_table_prefix}form_{$form_id}");
        if (!$query) {
            continue;
        }
        $field_settings = ft_get_field_settings($field_id);
        while ($row = mysql_fetch_assoc($query)) {
            // here, nothing's been uploaded in the field
            if (empty($row[$col_name])) {
                continue;
            }
            $data[] = array("submission_id" => $row["submission_id"], "field_id" => $field_id, "field_type_id" => $module_field_type_id, "folder_path" => $field_settings["folder_path"], "folder_url" => $field_settings["folder_url"], "filename" => $row[$col_name]);
        }
    }
    return array("uploaded_files" => $data);
}