/**
 * This installs a single field type and all related data: settings, setting options and validation. If
 * a field type with that identifier already exists, it returns false, with an appropriate error message.
 * The field type data is all stored in the field_types.php file in this folder.
 *
 * @param integer $field_type_identifier "textbox", "textarea", etc.
 */
function cft_install_field_type($field_type_identifier, $group_id)
{
    global $g_table_prefix, $LANG;
    $field_type_info = ft_get_field_type_by_identifier($field_type_identifier);
    if (!empty($field_type_info)) {
        return array(false, "The database has already been populated with the {$field_type_identifier} field type.");
    }
    $cft_field_types = cft_get_field_types();
    $data = $cft_field_types[$field_type_identifier];
    // Step 1: install the main field_types record
    $count_query = mysql_query("SELECT count(*) as c FROM {$g_table_prefix}field_types WHERE group_id = {$group_id}");
    $count_result = mysql_fetch_assoc($count_query);
    $next_list_order = $count_result["c"] + 1;
    $query = "\n    INSERT INTO {$g_table_prefix}field_types (is_editable, non_editable_info, managed_by_module_id, field_type_name,\n      field_type_identifier, group_id, is_file_field, is_date_field, raw_field_type_map, raw_field_type_map_multi_select_id,\n      list_order, compatible_field_sizes, view_field_rendering_type, view_field_php_function_source, view_field_php_function,\n      view_field_smarty_markup, edit_field_smarty_markup, php_processing, resources_css, resources_js)\n    VALUES (\n      '{$data["field_type"]["is_editable"]}',\n      {$data["field_type"]["non_editable_info"]},\n      {$data["field_type"]["managed_by_module_id"]},\n      '{$data["field_type"]["field_type_name"]}',\n      '{$data["field_type"]["field_type_identifier"]}',\n      {$group_id},\n      '{$data["field_type"]["is_file_field"]}',\n      '{$data["field_type"]["is_date_field"]}',\n      '{$data["field_type"]["raw_field_type_map"]}',\n      NULL,\n      {$next_list_order},\n      '{$data["field_type"]["compatible_field_sizes"]}',\n      '{$data["field_type"]["view_field_rendering_type"]}',\n      '{$data["field_type"]["view_field_php_function_source"]}',\n      '{$data["field_type"]["view_field_php_function"]}',\n      '{$data["field_type"]["view_field_smarty_markup"]}',\n      '{$data["field_type"]["edit_field_smarty_markup"]}',\n      '{$data["field_type"]["php_processing"]}',\n      '{$data["field_type"]["resources_css"]}',\n      '{$data["field_type"]["resources_js"]}'\n    )\n  ";
    $result = mysql_query($query);
    if (!$result) {
        $error = mysql_error();
        cft_rollback_new_installation();
        return array(false, "Failed to insert field type {$field_type_identifier}: {$error}");
    }
    $field_type_id = mysql_insert_id();
    // Step 2: field type settings
    $setting_id_used_for_raw_field_map = "";
    for ($i = 1; $i <= count($data["settings"]); $i++) {
        $setting_info = $data["settings"][$i - 1];
        $field_label = $setting_info["field_label"];
        $field_setting_identifier = $setting_info["field_setting_identifier"];
        $field_type = $setting_info["field_type"];
        $field_orientation = $setting_info["field_orientation"];
        $default_value_type = $setting_info["default_value_type"];
        $default_value = $setting_info["default_value"];
        $use_for_option_list_map = isset($setting_info["use_for_option_list_map"]) ? $setting_info["use_for_option_list_map"] : false;
        $query = "\n      INSERT INTO {$g_table_prefix}field_type_settings (field_type_id, field_label, field_setting_identifier,\n        field_type, field_orientation, default_value_type, default_value, list_order)\n      VALUES ({$field_type_id}, '{$field_label}', '{$field_setting_identifier}', '{$field_type}',\n        '{$field_orientation}', '{$default_value_type}', '{$default_value}', {$i})\n    ";
        $result = mysql_query($query);
        if (!$result) {
            $error = mysql_error();
            cft_rollback_new_installation();
            return array(false, "Failed to insert setting {$field_setting_identifier}: {$error}");
        }
        $setting_id = mysql_insert_id();
        // if this setting is being used for the raw field type option list, update the field type record
        if ($use_for_option_list_map) {
            mysql_query("\n        UPDATE {$g_table_prefix}field_types\n        SET    raw_field_type_map_multi_select_id = {$setting_id}\n        WHERE  field_type_id = {$field_type_id}\n      ");
        }
        for ($j = 1; $j <= count($setting_info["options"]); $j++) {
            $option_info = $setting_info["options"][$j - 1];
            $option_text = $option_info["option_text"];
            $option_value = $option_info["option_value"];
            $is_new_sort_group = $option_info["is_new_sort_group"];
            $query = "\n        INSERT INTO {$g_table_prefix}field_type_setting_options (setting_id, option_text, option_value, option_order, is_new_sort_group)\n        VALUES ({$setting_id}, '{$option_text}', '{$option_value}', {$j}, '{$is_new_sort_group}')\n      ";
            $result = mysql_query($query);
            if (!$result) {
                $error = mysql_error();
                cft_rollback_new_installation();
                return array(false, "Failed to insert setting option {$field_setting_identifier}, {$option_text}: {$error}");
            }
        }
    }
    // Step 4: Validation
    for ($i = 1; $i <= count($data["validation"]); $i++) {
        $rule_info = $data["validation"][$i - 1];
        $rsv_rule = $rule_info["rsv_rule"];
        $rule_label = $rule_info["rule_label"];
        $rsv_field_name = $rule_info["rsv_field_name"];
        $custom_function = $rule_info["custom_function"];
        $custom_function_required = $rule_info["custom_function_required"];
        $default_error_message = $rule_info["default_error_message"];
        $query = "\n      INSERT INTO {$g_table_prefix}field_type_validation_rules (field_type_id, rsv_rule, rule_label,\n        rsv_field_name, custom_function, custom_function_required, default_error_message, list_order)\n      VALUES ({$field_type_id}, '{$rsv_rule}', '{$rule_label}', '{$rsv_field_name}', '{$custom_function}',\n        '{$custom_function_required}', '{$default_error_message}', {$i})\n    ";
        $result = mysql_query($query);
        if (!$result) {
            $error = mysql_error();
            cft_rollback_new_installation();
            return array(false, "Failed to insert validation rule {$rule_label}: {$error}");
        }
    }
    return array(true, "");
}
Exemple #2
0
/**
 * This installation function + module is unique. For Core 2.1.5 and later, it's bundled with all Core scripts
 * and installs the field types. It can't be removed. This function is called explicitly in the main Form
 * Tools installation function.
 *
 * @param integer $module_id
 */
function cft_install_module()
{
    global $g_table_prefix, $LANG, $cft_field_types;
    $field_types = ft_get_field_types();
    if (!empty($field_types)) {
        return array(true, "");
    }
    // first, insert the groups for the forthcoming field types
    $query = mysql_query("\n\t  INSERT INTO {$g_table_prefix}list_groups (group_type, group_name, custom_data, list_order)\n\t  VALUES ('field_types', '{\$LANG.phrase_standard_fields}', '', 1)\n\t");
    if (!$query) {
        return array(false, "Problem inserting list group item #1: " . mysql_error());
    }
    $group1_id = mysql_insert_id();
    $query = mysql_query("\n    INSERT INTO {$g_table_prefix}list_groups (group_type, group_name, custom_data, list_order)\n    VALUES ('field_types', '{\$LANG.phrase_special_fields}', '', 2)\n  ");
    if (!$query) {
        $error = mysql_error();
        cft_rollback_new_installation();
        return array(false, "Problem inserting list group item #2: " . $error);
    }
    $group2_id = mysql_insert_id();
    // install each field type one-by-one. If ANYTHING fails, return immediately and inform the user. This should
    // NEVER occur, because the only time this code is ever executed is when first installing the module
    list($success, $error) = cft_install_field_type("textbox", $group1_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    list($success, $error) = cft_install_field_type("textarea", $group1_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    list($success, $error) = cft_install_field_type("password", $group1_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    list($success, $error) = cft_install_field_type("dropdown", $group1_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    list($success, $error) = cft_install_field_type("multi_select_dropdown", $group1_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    list($success, $error) = cft_install_field_type("radio_buttons", $group1_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    list($success, $error) = cft_install_field_type("checkboxes", $group1_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    list($success, $error) = cft_install_field_type("date", $group2_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    list($success, $error) = cft_install_field_type("time", $group2_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    list($success, $error) = cft_install_field_type("phone", $group2_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    list($success, $error) = cft_install_field_type("code_markup", $group2_id);
    if (!$success) {
        cft_rollback_new_installation();
        return array($success, $error);
    }
    return array(true, "");
}