Exemple #1
0
/**
 * Used in the Add External Form process. This generates a JS object that maps "raw" field types to those
 * field types specified in the Custom Field module. This allows the script to provide a list of appropriate
 * field types for each form field, from which the user can choose.
 *
 * Any fields that aren't mapped to a "raw" field won't get listed here. They can be used when editing forms,
 * but not when initially adding them. Also, for Option List fields (checkboxes, radios, dropdowns, multi-selects),
 * this function ONLY returns those custom fields that specify an Option List. Without it, the user wouldn't be
 * able to map the options in their form to an Option List associated with a field type.
 *
 * @return string a JS object
 */
function ft_get_raw_field_types_js($namespace = "page_ns")
{
    global $g_table_prefix, $g_raw_field_types;
    $field_types = ft_get_field_types();
    $mapped = array();
    while (list($raw_field_type, $field_type_label) = each($g_raw_field_types)) {
        $curr_mapped_field_types = array();
        foreach ($field_types as $field_type_info) {
            if ($field_type_info["raw_field_type_map"] != $raw_field_type) {
                continue;
            }
            if (in_array($raw_field_type, array("radio-buttons", "checkboxes", "select", "multi-select"))) {
                if (empty($field_type_info["raw_field_type_map_multi_select_id"])) {
                    continue;
                }
            }
            $curr_mapped_field_types[] = array("field_type_id" => $field_type_info["field_type_id"], "field_type_name" => ft_eval_smarty_string($field_type_info["field_type_name"]), "compatible_field_sizes" => $field_type_info["compatible_field_sizes"], "raw_field_type_map_multi_select_id" => $field_type_info["raw_field_type_map_multi_select_id"]);
        }
        $mapped[$raw_field_type] = $curr_mapped_field_types;
    }
    reset($g_raw_field_types);
    $js = $namespace . ".raw_field_types = " . ft_convert_to_json($mapped);
    return $js;
}
     foreach ($changed_field_ids as $field_id) {
         if (!isset($request["data"]["field_{$field_id}"])) {
             continue;
         }
         // if this is a NEW field, we just ignore it here. New fields are only added by updating the main page, not
         // via the Edit Field dialog
         if (preg_match("/^NEW/", $field_id)) {
             continue;
         }
         list($success, $message) = ft_update_field($form_id, $field_id, $request["data"]["field_{$field_id}"]);
         if (!$success) {
             $problems[] = array("field_id" => $field_id, "error" => $message);
         }
     }
     if (!empty($problems)) {
         $problems_json = ft_convert_to_json($problems);
         echo "{ \"success\": \"0\", \"problems\": {$problems_json}{$return_str} }";
     } else {
         echo "{ \"success\": \"1\"{$return_str} }";
     }
     break;
     // used to return a page outlining all the form field placeholders available
 // used to return a page outlining all the form field placeholders available
 case "get_form_field_placeholders":
     $form_id = $request["form_id"];
     $text_reference_tab_info = ft_eval_smarty_string($LANG["text_reference_tab_info"], array("g_root_url" => $g_root_url));
     $page_vars = array();
     $page_vars["form_id"] = $form_id;
     $page_vars["form_fields"] = ft_get_form_fields($form_id, array("include_field_type_info" => true));
     $page_vars["text_reference_tab_info"] = $text_reference_tab_info;
     ft_display_page("admin/forms/form_placeholders.tpl", $page_vars);
        if ($component == "core") {
            $missing_files = sc_check_core_files();
            $return_info["component_type"] = "core";
            $return_info["component_name"] = "Form Tools Core";
        }
        if (preg_match("/^module_/", $component)) {
            $module_id = preg_replace("/^module_/", "", $component);
            $module_info = ft_get_module($module_id);
            $missing_files = sc_check_module_files($module_info["module_folder"]);
            $return_info["component_type"] = "module";
            $return_info["component_name"] = $module_info["module_name"];
        }
        if (preg_match("/^theme_/", $component)) {
            $theme_id = preg_replace("/^theme_/", "", $component);
            $theme_info = ft_get_theme($theme_id);
            $missing_files = sc_check_theme_files($theme_info["theme_folder"]);
            $return_info["component_type"] = "theme";
            $return_info["component_name"] = $theme_info["theme_name"];
        }
        if (!empty($missing_files)) {
            $return_info["result"] = "fail";
            $return_info["missing_files"] = $missing_files;
        }
        echo ft_convert_to_json($return_info);
        break;
    case "find_table_orphans":
        $remove_orphans = isset($request["remove_orphans"]) ? true : false;
        $results = sc_find_table_orphans($request["table_name"], $remove_orphans);
        echo ft_convert_to_json($results);
        break;
}
/**
 * Helper function that's called as a wrapper to the PHP json_encode function. If it doesn't exist,
 * it manually encodes the value as a JSON object.
 *
 * I didn't just make this function override json_encore for those servers that support it (PHP 5.2+)
 * because I couldn't locate a 100% guaranteed identical plain vanilla PHP equivalent. Hence this custom
 * function.
 *
 * @param mixed $arr
 * @return string the JSON object (as a PHP string)
 */
function ft_convert_to_json($arr)
{
    $parts = array();
    $is_list = false;
    // find out if the given array is a numerical array
    $keys = array_keys($arr);
    $max_length = count($arr) - 1;
    // see if the first key is 0 and last key is length - 1
    if (isset($keys[0]) && $keys[0] == 0 && $keys[$max_length] == $max_length) {
        $is_list = true;
        // see if each key corresponds to its position
        for ($i = 0; $i < count($keys); $i++) {
            // a key fails at position check: it's a hash
            if ($i != $keys[$i]) {
                $is_list = false;
                break;
            }
        }
    }
    foreach ($arr as $key => $value) {
        // custom handling for arrays
        if (is_array($value)) {
            if ($is_list) {
                $parts[] = ft_convert_to_json($value);
            } else {
                $parts[] = '"' . $key . '":' . ft_convert_to_json($value);
            }
        } else {
            $str = '';
            if (!$is_list) {
                $str = '"' . $key . '":';
            }
            // custom handling for multiple data types
            if (is_numeric($value)) {
                $str .= $value;
            } elseif ($value === false) {
                $str .= 'false';
            } elseif ($value === true) {
                $str .= 'true';
            } else {
                $json_replacements = array(array('\\', '/', "\n", "\t", "\r", "\\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\\"'));
                $value = str_replace($json_replacements[0], $json_replacements[1], $value);
                $str .= '"' . $value . '"';
            }
            $parts[] = $str;
        }
    }
    $json = implode(',', $parts);
    if ($is_list) {
        return '[' . $json . ']';
    }
    return '{' . $json . '}';
}