/**
 * 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;
}
Example #2
0
     // here, create_view_from_view_id either contains the ID of the View that the user wants to copy,
     // or "blank_view_no_fields", meaning a totally blank View or "blank_view_all_fields" meaning
     // they want all View fields added by default
     if (isset($request["create_view_from_view_id"]) && !empty($request["create_view_from_view_id"])) {
         $duplicate_view_id = $request["create_view_from_view_id"];
     }
     $view_id = ft_create_new_view($form_id, $group_id, $view_name, $duplicate_view_id);
     // always set the default Edit View tab to the first one
     $_SESSION["ft"]["edit_view_tab"] = 1;
     echo "{ \"success\": \"1\", \"view_id\": \"{$view_id}\" }";
     break;
 case "create_new_view_group":
     $form_id = $_SESSION["ft"]["form_id"];
     $group_type = "form_{$form_id}_view_group";
     $group_name = $request["group_name"];
     $info = ft_add_list_group($group_type, $group_name);
     echo ft_convert_to_json($info);
     break;
 case "delete_view":
     $view_id = $request["view_id"];
     ft_delete_view($view_id);
     echo "{ \"success\": \"1\", \"view_id\": \"{$view_id}\" }";
     break;
     // this is called when the user clicks on the "Save Changes" button on the Edit Field dialog on the
     // Fields tab
 // this is called when the user clicks on the "Save Changes" button on the Edit Field dialog on the
 // Fields tab
 case "update_form_fields":
     $form_id = $request["form_id"];
     $changed_field_ids = $request["data"]["changed_field_ids"];
     // update whatever information has been included in the request