Exemplo n.º 1
0
/**
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.edit_custom_field
 * Type:     function
 * Name:     edit_custom_field
 * Purpose:  This is used on the Edit Submission pages. It does all the clever stuff needed to generate the
 *           actual markup for a single field, with whatever user-defined settings have been employed.
 *
 *           It's strongly coupled to the ft_get_grouped_view_fields function (when called with the form ID &
 *           submission ID params) to ensure that all data is efficiently returned for use by this function.
 * -------------------------------------------------------------
 */
function smarty_function_edit_custom_field($params, &$smarty)
{
    global $LANG, $g_root_url, $g_root_dir, $g_multi_val_delimiter, $g_table_prefix;
    if (empty($params["form_id"])) {
        $smarty->trigger_error("assign: missing 'form_id' parameter.");
        return;
    }
    if (empty($params["field_info"])) {
        $smarty->trigger_error("assign: missing 'field_info' parameter.");
        return;
    }
    if (empty($params["field_types"])) {
        $smarty->trigger_error("assign: missing 'field_types' parameter.");
        return;
    }
    if (empty($params["settings"])) {
        $smarty->trigger_error("assign: missing 'settings' parameter.");
        return;
    }
    $form_id = $params["form_id"];
    $field_info = $params["field_info"];
    $field_types = $params["field_types"];
    $settings = $params["settings"];
    $submission_id = isset($params["submission_id"]) ? $params["submission_id"] : "";
    // loop through the field types and store the one we're interested in in $field_type_info
    $field_type_info = array();
    foreach ($field_types as $curr_field_type) {
        if ($field_info["field_type_id"] == $curr_field_type["field_type_id"]) {
            $field_type_info = $curr_field_type;
            break;
        }
    }
    if ($field_info["is_editable"] == "no") {
        $markup_with_placeholders = trim($field_type_info["view_field_smarty_markup"]);
        if (empty($markup_with_placeholders)) {
            echo $field_info["submission_info"]["value"];
            return;
        }
    } else {
        $markup_with_placeholders = $field_type_info["edit_field_smarty_markup"];
    }
    // now construct all available placeholders
    $placeholders = array("FORM_ID" => $form_id, "VIEW_ID" => $field_info["view_id"], "SUBMISSION_ID" => $submission_id, "FIELD_ID" => $field_info["field_id"], "NAME" => $field_info["field_name"], "COLNAME" => $field_info["col_name"], "VALUE" => isset($field_info["submission_value"]) ? $field_info["submission_value"] : "", "SETTINGS" => $settings, "CONTEXTPAGE" => "edit_submission", "ACCOUNT_INFO" => isset($_SESSION["ft"]["account"]) ? $_SESSION["ft"]["account"] : array(), "g_root_url" => $g_root_url, "g_root_dir" => $g_root_dir, "g_multi_val_delimiter" => $g_multi_val_delimiter);
    // add in all field type settings and their replacements
    foreach ($field_type_info["settings"] as $setting_info) {
        $curr_setting_id = $setting_info["setting_id"];
        $curr_setting_field_type = $setting_info["field_type"];
        $default_value_type = $setting_info["default_value_type"];
        $value = $setting_info["default_value"];
        $identifier = $setting_info["field_setting_identifier"];
        foreach ($field_info["field_settings"] as $setting) {
            $found = false;
            while (list($setting_id, $setting_value) = each($setting)) {
                if ($setting_id == $curr_setting_id) {
                    $value = $setting_value;
                    break;
                }
            }
            reset($setting);
            if ($found) {
                break;
            }
        }
        // next, if the setting is dynamic, convert the stored value
        if ($default_value_type == "dynamic") {
            // dynamic setting values should ALWAYS be of the form "setting_name,module_folder/'core'". If they're not, just ignore it
            $parts = explode(",", $value);
            if (count($parts) == 2) {
                $value = ft_get_settings($parts[0], $parts[1]);
            }
        }
        // if this setting type is a dropdown list and $value is non-empty, get the list of options
        if ($curr_setting_field_type == "option_list_or_form_field" && !empty($value)) {
            if (preg_match("/^form_field/", $value)) {
                $value = ft_get_mapped_form_field_data($value);
            } else {
                $value = ft_get_option_list($value);
            }
        }
        $placeholders[$identifier] = $value;
    }
    echo ft_eval_smarty_string($markup_with_placeholders, $placeholders);
}
Exemplo n.º 2
0
/**
 * This should be the one and only place that actually generates the content for a field, for it
 * to be Viewed. This is used on the Submission Listing page, Edit Submission page (for viewable,
 * non-editable fields), in the Export Manager, in the Email Templates, and anywhere else that needs
 * to output the content of a field.
 *
 *    This function is the main source of slowness in 2.1.0. I'll be investigating ways to speed it up
 *    in the Beta.
 *
 * @param array $params a hash with the following:
 *              REQUIRED VALUES:
 *                form_id
 *                submission_id
 *                field_info - a hash containing details of the field:
 *                   REQUIRED:
 *                     field_id
 *                     field_type_id
 *                     col_name
 *                     field_name
 *                     settings - all extended settings defined for the field
 *                   OPTIONAL:
 *                     anything else you want
 *                field_types - all, or any that are relevant. But it should be an array, anyway
 *                value - the actual value stored in the field
 *                settings - (from ft_get_settings())
 * @return string
 */
function ft_generate_viewable_field($params)
{
    global $LANG, $g_root_url, $g_root_dir, $g_multi_val_delimiter, $g_cache;
    // REQUIRED
    $form_id = $params["form_id"];
    $submission_id = $params["submission_id"];
    $field_info = $params["field_info"];
    $field_types = $params["field_types"];
    $value = $params["value"];
    $settings = $params["settings"];
    $context = $params["context"];
    // loop through the field types and store the one we're interested in in $field_type_info
    $field_type_info = array();
    foreach ($field_types as $curr_field_type) {
        if ($field_info["field_type_id"] == $curr_field_type["field_type_id"]) {
            $field_type_info = $curr_field_type;
            break;
        }
    }
    $markup_with_placeholders = trim($field_type_info["view_field_smarty_markup"]);
    $field_settings = $field_info["field_settings"];
    $output = "";
    if ($field_type_info["view_field_rendering_type"] == "none" || empty($markup_with_placeholders)) {
        $output = $value;
    } else {
        $account_info = isset($_SESSION["ft"]["account"]) ? $_SESSION["ft"]["account"] : array();
        // now construct all available placeholders
        $placeholders = array("FORM_ID" => $form_id, "SUBMISSION_ID" => $submission_id, "FIELD_ID" => $field_info["field_id"], "NAME" => $field_info["field_name"], "COLNAME" => $field_info["col_name"], "VALUE" => $value, "SETTINGS" => $settings, "CONTEXTPAGE" => $context, "ACCOUNT_INFO" => $account_info, "g_root_url" => $g_root_url, "g_root_dir" => $g_root_dir, "g_multi_val_delimiter" => $g_multi_val_delimiter);
        // add in all field type settings and their replacements
        foreach ($field_type_info["settings"] as $setting_info) {
            $curr_setting_id = $setting_info["setting_id"];
            $curr_setting_field_type = $setting_info["field_type"];
            $default_value_type = $setting_info["default_value_type"];
            $value = $setting_info["default_value"];
            $identifier = $setting_info["field_setting_identifier"];
            if (isset($field_settings) && !empty($field_settings)) {
                for ($i = 0; $i < count($field_settings); $i++) {
                    while (list($setting_id, $setting_value) = each($field_settings[$i])) {
                        if ($setting_id == $curr_setting_id) {
                            $value = $setting_value;
                            break;
                        }
                    }
                    reset($field_settings);
                }
            }
            // next, if the setting is dynamic, convert the stored value
            if ($default_value_type == "dynamic") {
                // dynamic setting values should ALWAYS be of the form "setting_name,module_folder/'core'". If they're not, just ignore it
                $parts = explode(",", $value);
                if (count($parts) == 2) {
                    $dynamic_setting_str = $value;
                    // "setting_name,module_folder/'core'"
                    if (!array_key_exists("dynamic_settings", $g_cache)) {
                        $g_cache["dynamic_settings"] = array();
                    }
                    if (array_key_exists($dynamic_setting_str, $g_cache["dynamic_settings"])) {
                        $value = $g_cache["dynamic_settings"][$dynamic_setting_str];
                    } else {
                        $value = ft_get_settings($parts[0], $parts[1]);
                        $g_cache["dynamic_settings"][$dynamic_setting_str] = $value;
                    }
                }
            }
            // if this setting type is a dropdown list and $value is non-empty, get the option list
            if ($curr_setting_field_type == "option_list_or_form_field" && !empty($value)) {
                if (preg_match("/form_field:/", $value)) {
                    $value = ft_get_mapped_form_field_data($value);
                } else {
                    $option_list_id = $value;
                    if (!array_key_exists("option_lists", $g_cache)) {
                        $g_cache["option_lists"] = array();
                    }
                    if (array_key_exists($option_list_id, $g_cache["option_lists"])) {
                        $value = $g_cache["option_lists"][$option_list_id];
                    } else {
                        $value = ft_get_option_list($option_list_id);
                        $g_cache["option_lists"][$option_list_id] = $value;
                    }
                }
            }
            $placeholders[$identifier] = $value;
        }
        if ($field_type_info["view_field_rendering_type"] == "php") {
            $php_function = $field_type_info["view_field_php_function"];
            // if this is a module, include the module's library.php file so we have access to the function
            if ($field_type_info["view_field_php_function_source"] != "core" && is_numeric($field_type_info["view_field_php_function_source"])) {
                $module_folder = ft_get_module_folder_from_module_id($field_type_info["view_field_php_function_source"]);
                @(include_once "{$g_root_dir}/modules/{$module_folder}/library.php");
            }
            if (function_exists($php_function)) {
                $output = $php_function($placeholders);
            }
        } else {
            $output = ft_eval_smarty_string($markup_with_placeholders, $placeholders);
        }
    }
    return $output;
}
Exemplo n.º 3
0
/**
 * Creates an identical copy of an existing Option List, or creates a new blank one. This can be handy if
 * the user was using a single group for multiple fields, but one of the form fields changed. They can just
 * create a new copy, tweak it and re-assign the field.
 *
 * If no Option List ID is passed in the first param, it creates a new blank Option List (sorry for the crappy
 * function name).
 *
 * @param integer $list_id
 * @param integer $field_id if this parameter is set, the new Option List will be assigned to whatever
 *   field IDs are specified. Note: this only works for Field Types that have a single
 * @return mixed the list ID if successful, false if not
 */
function ft_duplicate_option_list($list_id = "", $field_ids = array())
{
    global $g_table_prefix, $LANG;
    // to ensure that all new field option groups have unique names, query the database and find the next free
    // group name of the form "New Option List (X)" (where "New Option List" is in the language of the current user)
    $lists = ft_get_option_lists("all");
    $list_names = array();
    foreach ($lists["results"] as $list_info) {
        $list_names[] = $list_info["option_list_name"];
    }
    $base_new_option_list = $LANG["phrase_new_option_list"];
    $new_option_list_name = $base_new_option_list;
    if (in_array($new_option_list_name, $list_names)) {
        $count = 2;
        $new_option_list_name = "{$base_new_option_list} ({$count})";
        while (in_array($new_option_list_name, $list_names)) {
            $count++;
            $new_option_list_name = "{$base_new_option_list} ({$count})";
        }
    }
    if (empty($list_id)) {
        $query = mysql_query("\n      INSERT INTO {$g_table_prefix}option_lists (option_list_name, is_grouped)\n      VALUES ('{$new_option_list_name}', 'no')\n    ");
        if (!$query) {
            return false;
        }
        $new_list_id = mysql_insert_id();
    } else {
        $option_list_info = ft_get_option_list($list_id);
        $is_grouped = $option_list_info["is_grouped"];
        $query = mysql_query("\n      INSERT INTO {$g_table_prefix}option_lists (option_list_name, is_grouped)\n      VALUES ('{$new_option_list_name}', '{$is_grouped}')\n    ");
        if (!$query) {
            return false;
        }
        $new_list_id = mysql_insert_id();
        // add add the option groups and their field options
        foreach ($option_list_info["options"] as $grouped_option_info) {
            $group_info = $grouped_option_info["group_info"];
            $options = $grouped_option_info["options"];
            $group_type = "option_list_{$new_list_id}";
            $group_name = $group_info["group_name"];
            $list_order = $group_info["list_order"];
            $new_list_group_info = ft_add_list_group($group_type, $group_name, $list_order);
            $new_list_group_id = $new_list_group_info["group_id"];
            foreach ($options as $option_info) {
                $option_info = ft_sanitize($option_info);
                $order = $option_info["option_order"];
                $value = $option_info["option_value"];
                $name = $option_info["option_name"];
                $is_new_sort_group = $option_info["is_new_sort_group"];
                mysql_query("\n          INSERT INTO {$g_table_prefix}field_options (list_id, list_group_id, option_order,\n            option_value, option_name, is_new_sort_group)\n          VALUES ({$new_list_id}, {$new_list_group_id}, '{$order}', '{$value}', '{$name}', '{$is_new_sort_group}')\n            ") or die(mysql_error());
            }
        }
    }
    // if we need to map this new option list to a field - or fields, loop through them and add them
    // one by one. Note: field types may use multiple Option Lists, which makes this extremely difficult. But
    // to make it as generic as possible, this code picks the first Option List field for the field type (as determined
    // by the setting list order)
    if (!empty($field_ids)) {
        foreach ($field_ids as $field_id) {
            $field_type_id = ft_get_field_type_id_by_field_id($field_id);
            $field_settings = ft_get_field_type_settings($field_type_id);
            $option_list_setting_id = "";
            foreach ($field_settings as $field_setting_info) {
                if ($field_setting_info["field_type"] == "option_list_or_form_field") {
                    $option_list_setting_id = $field_setting_info["setting_id"];
                    break;
                }
            }
            // this should ALWAYS have found a setting, but just in case...
            if (!empty($option_list_setting_id)) {
                mysql_query("DELETE FROM {$g_table_prefix}field_settings WHERE field_id = {$field_id} AND setting_id = {$option_list_setting_id}");
                @mysql_query("\n          INSERT INTO {$g_table_prefix}field_settings (field_id, setting_id, setting_value)\n          VALUES ({$field_id}, {$option_list_setting_id}, {$new_list_id})\n           ");
            }
        }
    }
    return $new_list_id;
}
function smarty_function_display_option_list($params, &$smarty)
{
    $option_list_id = $params["option_list_id"];
    $format = $params["format"];
    $name = isset($params["name"]) ? $params["name"] : "";
    $default_value = isset($params["default_value"]) ? $params["default_value"] : "";
    $option_list_info = ft_get_option_list($option_list_id);
    switch ($format) {
        case "radios":
            $is_grouped = $option_list_info["is_grouped"];
            $count = 1;
            foreach ($option_list_info["options"] as $group) {
                if (!empty($group["group_info"]["group_name"])) {
                    echo "<div><b>{$group["group_info"]["group_name"]}</b></div>";
                }
                foreach ($group["options"] as $option_info) {
                    $value = htmlspecialchars($option_info["option_value"]);
                    $display_text = $option_info["option_name"];
                    $selected = "";
                    $checked = $option_info["option_value"] == $default_value ? "checked" : "";
                    echo "<input type=\"radio\" name=\"{$name}\" id=\"{$name}_{$count}\" value=\"{$value}\" {$checked} /> " . "<label for=\"{$name}_{$count}\">{$display_text}</label><br />";
                    $count++;
                }
            }
            break;
        case "checkboxes":
            $is_grouped = $option_list_info["is_grouped"];
            $count = 1;
            foreach ($option_list_info["options"] as $group) {
                if (!empty($group["group_info"]["group_name"])) {
                    echo "<div><b>{$group["group_info"]["group_name"]}</b></div>";
                }
                foreach ($group["options"] as $option_info) {
                    $value = htmlspecialchars($option_info["option_value"]);
                    $display_text = $option_info["option_name"];
                    $checked = "";
                    if (is_array($default_value) && in_array($option_info["option_value"], $default_value)) {
                        $checked = "checked";
                    } else {
                        if ($option_info["option_value"] == $default_value) {
                            $checked = "checked";
                        }
                    }
                    echo "<input type=\"checkbox\" name=\"{$name}[]\" id=\"{$name}_{$count}\" value=\"{$value}\" {$checked} /> " . "<label for=\"{$name}_{$count}\">{$display_text}</label><br />";
                    $count++;
                }
            }
            break;
        case "select":
            $is_grouped = $option_list_info["is_grouped"];
            $count = 1;
            echo "<select name=\"{$name}\">";
            foreach ($option_list_info["options"] as $group) {
                if (!empty($group["group_info"]["group_name"])) {
                    echo "<optgroup label=\"{$group["group_info"]["group_name"]}\">";
                }
                foreach ($group["options"] as $option_info) {
                    $value = htmlspecialchars($option_info["option_value"]);
                    $display_text = $option_info["option_name"];
                    $selected = $option_info["option_value"] == $default_value ? "selected" : "";
                    echo "<option value=\"{$value}\" {$selected}>{$display_text}</option>\n";
                    $count++;
                }
                if (!empty($group["group_info"]["group_name"])) {
                    echo "</optgroup>";
                }
            }
            echo "</select>";
            break;
        case "multi-select":
            $is_grouped = $option_list_info["is_grouped"];
            $count = 1;
            echo "<select name=\"{$name}[]\" multiple size=\"5\">";
            foreach ($option_list_info["options"] as $group) {
                if (!empty($group["group_info"]["group_name"])) {
                    echo "<optgroup label=\"{$group["group_info"]["group_name"]}\">";
                }
                foreach ($group["options"] as $option_info) {
                    $value = htmlspecialchars($option_info["option_value"]);
                    $display_text = $option_info["option_name"];
                    $selected = "";
                    if (is_array($default_value) && in_array($option_info["option_value"], $default_value)) {
                        $selected = "selected";
                    } else {
                        if ($option_info["option_value"] == $default_value) {
                            $selected = "selected";
                        }
                    }
                    echo "<option value=\"{$value}\" {$selected}>{$display_text}</option>\n";
                    $count++;
                }
                if (!empty($group["group_info"]["group_name"])) {
                    echo "</optgroup>";
                }
            }
            echo "</select>";
            break;
    }
}
Exemplo n.º 5
0
<?php

$sortable_id = "option_list";
if (isset($request["update_page"])) {
    $request["sortable_id"] = $sortable_id;
    list($g_success, $g_message) = ft_update_option_list($list_id, $request);
}
$list_info = ft_get_option_list($list_id);
$total_options = 0;
foreach ($list_info["options"] as $option_info) {
    $total_options += count($option_info["options"]);
}
$placeholders = array("link1" => "edit.php?page=form_fields", "link2" => "index.php?add_option_list=1&create_option_list_from_list_id={$list_info["list_id"]}");
// get a list of all existing Option Lists; this is used to ensure the uniqueness of the option list names
// (necessary only from a user point of view)
$lists = ft_get_option_lists("all");
$list_names = array();
foreach ($lists["results"] as $curr_list_info) {
    if ($list_id == $curr_list_info["list_id"]) {
        continue;
    }
    $list_names[] = "\"" . htmlspecialchars($curr_list_info["option_list_name"]) . "\"";
}
$list_names = implode(",", $list_names);
$existing_option_list_names_js = "page_ns.option_list_names = [{$list_names}];";
// ------------------------------------------------------------------------------------------------
// compile template info
$page_vars["list_info"] = $list_info;
$page_vars["text_option_list_used_by_fields"] = ft_eval_smarty_string($LANG["text_option_list_used_by_fields"], $placeholders);
$page_vars["tabs"] = $tabs;
$page_vars["page_url"] = ft_get_page_url("edit_option_list");