/**
 * 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);
}
Beispiel #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;
}