Ejemplo n.º 1
0
function cfdef_prepare_list_distinct_values($p_field_def)
{
    $t_custom_field_table = db_get_table('custom_field');
    $query = "SELECT possible_values\n\t\t\t  FROM {$t_custom_field_table}\n\t\t\t  WHERE id=" . db_param();
    $result = db_query_bound($query, array($p_field_def['id']));
    $t_row_count = db_num_rows($result);
    if (0 == $t_row_count) {
        return false;
    }
    $row = db_fetch_array($result);
    $t_possible_values = custom_field_prepare_possible_values($row['possible_values']);
    $t_values_arr = explode('|', $t_possible_values);
    $t_return_arr = array();
    foreach ($t_values_arr as $t_option) {
        array_push($t_return_arr, $t_option);
    }
    return $t_return_arr;
}
Ejemplo n.º 2
0
/**
 * Prepare possible values for option list
 * @param array $p_field_def Custom field definition.
 * @return array|boolean
 */
function cfdef_prepare_list_distinct_values(array $p_field_def)
{
    $t_query = 'SELECT possible_values FROM {custom_field} WHERE id=' . db_param();
    $t_result = db_query($t_query, array($p_field_def['id']));
    $t_row = db_fetch_array($t_result);
    if (!$t_row) {
        return false;
    }
    $t_possible_values = custom_field_prepare_possible_values($t_row['possible_values']);
    $t_values_arr = explode('|', $t_possible_values);
    $t_return_arr = array();
    foreach ($t_values_arr as $t_option) {
        array_push($t_return_arr, $t_option);
    }
    return $t_return_arr;
}
Ejemplo n.º 3
0
function print_custom_field_input($p_field_def, $p_bug_id = null)
{
    $t_id = $p_field_def['id'];
    if (null === $p_bug_id) {
        $t_custom_field_value = $p_field_def['default_value'];
    } else {
        $t_custom_field_value = custom_field_get_value($t_id, $p_bug_id);
    }
    $t_custom_field_value = string_attribute($t_custom_field_value);
    switch ($p_field_def['type']) {
        case CUSTOM_FIELD_TYPE_ENUM:
        case CUSTOM_FIELD_TYPE_LIST:
        case CUSTOM_FIELD_TYPE_MULTILIST:
            $t_values = explode('|', custom_field_prepare_possible_values($p_field_def['possible_values']));
            $t_list_size = $t_possible_values_count = count($t_values);
            if ($t_possible_values_count > 5) {
                $t_list_size = 5;
            }
            if ($p_field_def['type'] == CUSTOM_FIELD_TYPE_ENUM) {
                $t_list_size = 0;
                # for enums the size is 0
            }
            if ($p_field_def['type'] == CUSTOM_FIELD_TYPE_MULTILIST) {
                echo '<select ', helper_get_tab_index(), ' name="custom_field_' . $t_id . '[]" size="' . $t_list_size . '" multiple="multiple">';
            } else {
                echo '<select ', helper_get_tab_index(), ' name="custom_field_' . $t_id . '" size="' . $t_list_size . '">';
            }
            $t_selected_values = explode('|', $t_custom_field_value);
            foreach ($t_values as $t_option) {
                if (in_array($t_option, $t_selected_values, true)) {
                    echo '<option value="' . $t_option . '" selected="selected"> ' . $t_option . '</option>';
                } else {
                    echo '<option value="' . $t_option . '">' . $t_option . '</option>';
                }
            }
            echo '</select>';
            break;
        case CUSTOM_FIELD_TYPE_CHECKBOX:
            $t_values = explode('|', custom_field_prepare_possible_values($p_field_def['possible_values']));
            $t_checked_values = explode('|', $t_custom_field_value);
            foreach ($t_values as $t_option) {
                echo '<input ', helper_get_tab_index(), ' type="checkbox" name="custom_field_' . $t_id . '[]"';
                if (in_array($t_option, $t_checked_values, true)) {
                    echo ' value="' . $t_option . '" checked="checked">&nbsp;' . $t_option . '&nbsp;&nbsp;';
                } else {
                    echo ' value="' . $t_option . '">&nbsp;' . $t_option . '&nbsp;&nbsp;';
                }
            }
            break;
        case CUSTOM_FIELD_TYPE_NUMERIC:
        case CUSTOM_FIELD_TYPE_FLOAT:
        case CUSTOM_FIELD_TYPE_EMAIL:
        case CUSTOM_FIELD_TYPE_STRING:
            echo '<input ', helper_get_tab_index(), ' type="text" name="custom_field_' . $t_id . '" size="80"';
            if (0 < $p_field_def['length_max']) {
                echo ' maxlength="' . $p_field_def['length_max'] . '"';
            } else {
                echo ' maxlength="255"';
            }
            echo ' value="' . $t_custom_field_value . '"></input>';
            break;
        case CUSTOM_FIELD_TYPE_DATE:
            print_date_selection_set("custom_field_" . $t_id, config_get('short_date_format'), $t_custom_field_value, false, true);
            break;
    }
}
Ejemplo n.º 4
0
/**
 * Allows the validation of a custom field value without setting it
 * or needing a bug to exist.
 * @param integer $p_field_id Custom field identifier.
 * @param string  $p_value    Custom field value.
 * @return boolean
 * @access public
 */
function custom_field_validate($p_field_id, $p_value)
{
    custom_field_ensure_exists($p_field_id);
    $t_query = 'SELECT name, type, possible_values, valid_regexp,
				  		 access_level_rw, length_min, length_max, default_value
				  FROM {custom_field}
				  WHERE id=' . db_param();
    $t_result = db_query($t_query, array($p_field_id));
    $t_row = db_fetch_array($t_result);
    $t_name = $t_row['name'];
    $t_type = $t_row['type'];
    $t_possible_values = $t_row['possible_values'];
    $t_valid_regexp = $t_row['valid_regexp'];
    $t_length_min = $t_row['length_min'];
    $t_length_max = $t_row['length_max'];
    $t_default_value = $t_row['default_value'];
    $t_valid = true;
    $t_length = utf8_strlen($p_value);
    switch ($t_type) {
        case CUSTOM_FIELD_TYPE_STRING:
            # Empty fields are valid
            if ($t_length == 0) {
                break;
            }
            # Regular expression string validation
            if (!is_blank($t_valid_regexp)) {
                $t_valid &= preg_match('/' . $t_valid_regexp . '/', $p_value);
            }
            # Check the length of the string
            $t_valid &= 0 == $t_length_min || $t_length >= $t_length_min;
            $t_valid &= 0 == $t_length_max || $t_length <= $t_length_max;
            break;
        case CUSTOM_FIELD_TYPE_NUMERIC:
            # Empty fields are valid
            if ($t_length == 0) {
                break;
            }
            $t_valid &= is_numeric($p_value);
            # Check the length of the number
            $t_valid &= 0 == $t_length_min || $t_length >= $t_length_min;
            $t_valid &= 0 == $t_length_max || $t_length <= $t_length_max;
            break;
        case CUSTOM_FIELD_TYPE_FLOAT:
            # Empty fields are valid
            if ($t_length == 0) {
                break;
            }
            # Allow both integer and float numbers
            $t_valid &= is_numeric($p_value) || is_float($p_value);
            # Check the length of the number
            $t_valid &= 0 == $t_length_min || $t_length >= $t_length_min;
            $t_valid &= 0 == $t_length_max || $t_length <= $t_length_max;
            break;
        case CUSTOM_FIELD_TYPE_DATE:
            # gpc_get_cf for date returns the value from strtotime
            # For 32 bit systems, supported range will be 13 Dec 1901 20:45:54 UTC to 19 Jan 2038 03:14:07 UTC
            $t_valid &= $p_value !== false;
            break;
        case CUSTOM_FIELD_TYPE_CHECKBOX:
        case CUSTOM_FIELD_TYPE_MULTILIST:
            # Checkbox fields can hold a null value (when no checkboxes are ticked)
            if ($p_value === '') {
                break;
            }
            # If checkbox field value is not null then we need to validate it
            $t_values = explode('|', $p_value);
            $t_possible_values = custom_field_prepare_possible_values($t_row['possible_values']);
            $t_possible_values = explode('|', $t_possible_values);
            $t_invalid_values = array_diff($t_values, $t_possible_values);
            $t_valid &= count($t_invalid_values) == 0;
            break;
        case CUSTOM_FIELD_TYPE_ENUM:
        case CUSTOM_FIELD_TYPE_LIST:
        case CUSTOM_FIELD_TYPE_RADIO:
            # List fields can be empty (when they are not shown on the
            # form, or shown with no default values and never clicked)
            if (is_blank($p_value)) {
                break;
            }
            # If list field value is not empty then we need to validate it
            $t_possible_values = custom_field_prepare_possible_values($t_row['possible_values']);
            $t_values_arr = explode('|', $t_possible_values);
            $t_valid &= in_array($p_value, $t_values_arr);
            break;
        case CUSTOM_FIELD_TYPE_EMAIL:
            if ($p_value !== '') {
                $t_valid &= email_is_valid($p_value);
            }
            break;
        default:
            break;
    }
    return (bool) $t_valid;
}
Ejemplo n.º 5
0
/**
 * Allows the validation of a custom field value without setting it
 * or needing a bug to exist.
 * @param int $p_field_id custom field id
 * @param string $p_value custom field value
 * @return bool
 * @access public
 */
function custom_field_validate($p_field_id, $p_value)
{
    $c_field_id = db_prepare_int($p_field_id);
    custom_field_ensure_exists($p_field_id);
    $t_custom_field_table = db_get_table('custom_field');
    $query = "SELECT name, type, possible_values, valid_regexp,\n\t\t\t\t  \t\t access_level_rw, length_min, length_max, default_value\n\t\t\t\t  FROM {$t_custom_field_table}\n\t\t\t\t  WHERE id=" . db_param();
    $result = db_query_bound($query, array($c_field_id));
    $row = db_fetch_array($result);
    $t_name = $row['name'];
    $t_type = $row['type'];
    $t_possible_values = $row['possible_values'];
    $t_valid_regexp = $row['valid_regexp'];
    $t_length_min = $row['length_min'];
    $t_length_max = $row['length_max'];
    $t_default_value = $row['default_value'];
    $t_valid = true;
    $t_length = utf8_strlen($p_value);
    switch ($t_type) {
        case CUSTOM_FIELD_TYPE_STRING:
            # Regular expression string validation
            if (!is_blank($t_valid_regexp) && !is_blank($p_value)) {
                $t_valid &= preg_match("/{$t_valid_regexp}/", $p_value);
            }
            # Check the length of the string
            $t_valid &= 0 == $t_length_min || $t_length >= $t_length_min;
            $t_valid &= 0 == $t_length_max || $t_length <= $t_length_max;
            break;
        case CUSTOM_FIELD_TYPE_NUMERIC:
            $t_valid &= $t_length == 0 || is_numeric($p_value);
            break;
        case CUSTOM_FIELD_TYPE_FLOAT:
            # Allow both integer and float numbers
            $t_valid &= $t_length == 0 || is_numeric($p_value) || is_float($p_value);
            break;
        case CUSTOM_FIELD_TYPE_DATE:
            # gpc_get_cf for date returns the value from strftime
            # Either false (php >= 5.1) or -1 (php < 5.1) for failure
            $t_valid &= $p_value == null || $p_value !== false && $p_value > 0;
            break;
        case CUSTOM_FIELD_TYPE_CHECKBOX:
            # Checkbox fields can hold a null value (when no checkboxes are ticked)
            if ($p_value === '') {
                break;
            }
            # If checkbox field value is not null then we need to validate it... (note: no "break" statement here!)
        # If checkbox field value is not null then we need to validate it... (note: no "break" statement here!)
        case CUSTOM_FIELD_TYPE_MULTILIST:
            $t_values = explode('|', $p_value);
            $t_possible_values = custom_field_prepare_possible_values($row['possible_values']);
            $t_possible_values = explode('|', $t_possible_values);
            $t_invalid_values = array_diff($t_values, $t_possible_values);
            $t_valid &= count($t_invalid_values) == 0;
            break;
        case CUSTOM_FIELD_TYPE_ENUM:
        case CUSTOM_FIELD_TYPE_LIST:
        case CUSTOM_FIELD_TYPE_RADIO:
            $t_possible_values = custom_field_prepare_possible_values($row['possible_values']);
            $t_values_arr = explode('|', $t_possible_values);
            $t_valid &= in_array($p_value, $t_values_arr);
            break;
        case CUSTOM_FIELD_TYPE_EMAIL:
            if ($p_value !== '') {
                $t_valid &= email_is_valid($p_value);
            }
            break;
        default:
            break;
    }
    return (bool) $t_valid;
}