function smarty_function_option_list_dropdown($params, &$smarty)
{
    global $LANG;
    if (empty($params["name_id"])) {
        $smarty->trigger_error("assign: missing 'name_id' parameter. This is used to give the select field a name and id value.");
        return;
    }
    $default_value = isset($params["default"]) ? $params["default"] : "";
    $onchange = isset($params["onchange"]) ? $params["onchange"] : "";
    $style = isset($params["style"]) ? $params["style"] : "";
    $attributes = array("id" => $params["name_id"], "name" => $params["name_id"], "onchange" => $onchange, "style" => $style);
    $attribute_str = "";
    while (list($key, $value) = each($attributes)) {
        if (!empty($value)) {
            $attribute_str .= " {$key}=\"{$value}\"";
        }
    }
    $groups = ft_get_option_lists("all");
    $rows = array();
    $rows[] = "<option value=\"\">{$LANG["phrase_please_select"]}</option>";
    foreach ($groups["results"] as $group_info) {
        $list_id = $group_info["list_id"];
        $option_list_name = $group_info["option_list_name"];
        $selected = $default_value == $list_id ? "selected" : "";
        $rows[] = "<option value=\"{$list_id}\" {$selected}>{$option_list_name}</option>\n";
    }
    $html = "<select {$attribute_str}>" . join("\n", $rows) . "</select>";
    return $html;
}
Example #2
0
$num_option_lists = $list_info["num_results"];
$option_lists = $list_info["results"];
$updated_field_option_groups = array();
$updated_option_lists = array();
foreach ($option_lists as $option_list) {
    $list_id = $option_list["list_id"];
    // add the number of fields that use this option group
    $option_list["num_fields"] = ft_get_num_fields_using_option_list($list_id);
    if ($option_list["num_fields"] > 0) {
        $option_list["fields"] = ft_get_fields_using_option_list($list_id, array("group_by_form" => true));
    }
    // add the total number of options in this group
    $option_list["num_option_list_options"] = ft_get_num_options_in_option_list($list_id);
    $updated_option_lists[] = $option_list;
}
$all_option_lists = ft_get_option_lists("all");
// ------------------------------------------------------------------------------------------------
// compile template info
$page_vars = array();
$page_vars["page"] = "option_lists";
$page_vars["text_option_list_page"] = ft_eval_smarty_string($LANG["text_option_list_page"], array("link" => "../add/step1.php"));
$page_vars["page_url"] = ft_get_page_url("option_lists");
$page_vars["head_title"] = $LANG["phrase_option_lists"];
$page_vars["option_lists"] = $updated_option_lists;
$page_vars["num_option_lists"] = $num_option_lists;
$page_vars["all_option_lists"] = $all_option_lists["results"];
$page_vars["order"] = $order;
$page_vars["js_messages"] = array("validation_delete_non_empty_option_list", "confirm_delete_option_list", "phrase_please_confirm", "word_yes", "word_no", "word_edit", "word_remove");
$page_vars["pagination"] = ft_get_page_nav($num_option_lists, $num_option_lists_per_page, $option_list_page);
$page_vars["head_string"] = <<<END
<script src="{$g_root_url}/global/scripts/manage_option_lists.js"></script>
/**
 * 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;
}