function custom_field_set_value($p_field_id, $p_bug_id, $p_value) { $c_field_id = db_prepare_int($p_field_id); $c_bug_id = db_prepare_int($p_bug_id); custom_field_ensure_exists($p_field_id); $t_custom_field_table = config_get('mantis_custom_field_table'); $query = "SELECT name, type, possible_values, valid_regexp,\r\n\t\t\t\t access_level_rw, length_min, length_max, default_value\r\n\t\t\t\t FROM {$t_custom_field_table}\r\n\t\t\t\t WHERE id='{$c_field_id}'"; $result = db_query($query); $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_access_level_rw = $row['access_level_rw']; $t_length_min = $row['length_min']; $t_length_max = $row['length_max']; $t_default_value = $row['default_value']; $c_value = db_prepare_string(custom_field_value_to_database($p_value, $t_type)); # check for valid value if (!is_blank($t_valid_regexp)) { if (!ereg($t_valid_regexp, $p_value)) { return false; } } if (strlen($p_value) < $t_length_min) { return false; } if (0 != $t_length_max && strlen($p_value) > $t_length_max) { return false; } if (!custom_field_has_write_access($p_field_id, $p_bug_id, auth_get_current_user_id())) { return false; } $t_custom_field_string_table = config_get('mantis_custom_field_string_table'); # do I need to update or insert this value? $query = "SELECT value\r\n\t\t\t\t FROM {$t_custom_field_string_table}\r\n\t\t\t\t WHERE field_id='{$c_field_id}' AND\r\n\t\t\t\t \t\tbug_id='{$c_bug_id}'"; $result = db_query($query); if (db_num_rows($result) > 0) { $query = "UPDATE {$t_custom_field_string_table}\r\n\t\t\t\t\t SET value='{$c_value}'\r\n\t\t\t\t\t WHERE field_id='{$c_field_id}' AND\r\n\t\t\t\t\t \t\tbug_id='{$c_bug_id}'"; db_query($query); $row = db_fetch_array($result); history_log_event_direct($c_bug_id, $t_name, custom_field_database_to_value($row['value'], $t_type), $p_value); } else { # Always store the value, even if it's the dafault value # This is important, as the definitions might change but the # values stored with a bug must not change $query = "INSERT INTO {$t_custom_field_string_table}\r\n\t\t\t\t\t\t( field_id, bug_id, value )\r\n\t\t\t\t\t VALUES\r\n\t\t\t\t\t\t( '{$c_field_id}', '{$c_bug_id}', '{$c_value}' )"; db_query($query); history_log_event_direct($c_bug_id, $t_name, '', $p_value); } custom_field_clear_cache($p_field_id); #db_query() errors on failure so: return true; }
/** * Set the value of a custom field for a given bug * return true on success, false on failure * @param integer $p_field_id Custom field identifier. * @param integer $p_bug_id A bug identifier. * @param mixed $p_value New custom field value. * @param boolean $p_log_insert Create history logs for new values. * @return boolean * @access public */ function custom_field_set_value($p_field_id, $p_bug_id, $p_value, $p_log_insert = true) { custom_field_ensure_exists($p_field_id); if (!custom_field_validate($p_field_id, $p_value)) { return false; } $t_name = custom_field_get_field($p_field_id, 'name'); $t_type = custom_field_get_field($p_field_id, 'type'); $t_value_field = $t_type == CUSTOM_FIELD_TYPE_TEXTAREA ? 'text' : 'value'; # Determine whether an existing value needs to be updated or a new value inserted $t_query = 'SELECT ' . $t_value_field . ' FROM {custom_field_string} WHERE field_id=' . db_param() . ' AND bug_id=' . db_param(); $t_result = db_query($t_query, array($p_field_id, $p_bug_id)); if ($t_row = db_fetch_array($t_result)) { $t_query = 'UPDATE {custom_field_string} SET ' . $t_value_field . '=' . db_param() . ' WHERE field_id=' . db_param() . ' AND bug_id=' . db_param(); $t_params = array(custom_field_value_to_database($p_value, $t_type), (int) $p_field_id, (int) $p_bug_id); db_query($t_query, $t_params); history_log_event_direct($p_bug_id, $t_name, custom_field_database_to_value($t_row[$t_value_field], $t_type), $p_value); } else { $t_query = 'INSERT INTO {custom_field_string} ( field_id, bug_id, ' . $t_value_field . ' ) VALUES ( ' . db_param() . ', ' . db_param() . ', ' . db_param() . ')'; $t_params = array((int) $p_field_id, (int) $p_bug_id, custom_field_value_to_database($p_value, $t_type)); db_query($t_query, $t_params); # Don't log history events for new bug reports or on other special occasions if ($p_log_insert) { history_log_event_direct($p_bug_id, $t_name, '', $p_value); } } custom_field_clear_cache($p_field_id); # db_query() errors on failure so: return true; }
/** * Set the value of a custom field for a given bug * return true on success, false on failure * @param int $p_field_id custom field id * @param int $p_bug_id bug id * @param mixed $p_value * @param boolean $p_log create history logs for new values * @return bool * @access public */ function custom_field_set_value($p_field_id, $p_bug_id, $p_value, $p_log_insert = true) { $c_field_id = db_prepare_int($p_field_id); $c_bug_id = db_prepare_int($p_bug_id); custom_field_ensure_exists($p_field_id); if (!custom_field_validate($p_field_id, $p_value)) { return false; } $t_name = custom_field_get_field($p_field_id, 'name'); $t_type = custom_field_get_field($p_field_id, 'type'); $t_custom_field_string_table = db_get_table('custom_field_string'); $t_value_field = $t_type == CUSTOM_FIELD_TYPE_TEXTAREA ? 'text' : 'value'; # Determine whether an existing value needs to be updated or a new value inserted $query = "SELECT {$t_value_field}\n\t\t\t\t FROM {$t_custom_field_string_table}\n\t\t\t\t WHERE field_id=" . db_param() . " AND\n\t\t\t\t \t\tbug_id=" . db_param(); $result = db_query_bound($query, array($c_field_id, $c_bug_id)); if (db_num_rows($result) > 0) { $query = "UPDATE {$t_custom_field_string_table}\n\t\t\t\t\t SET {$t_value_field}=" . db_param() . "\n\t\t\t\t\t WHERE field_id=" . db_param() . " AND\n\t\t\t\t\t \t\tbug_id=" . db_param(); db_query_bound($query, array(custom_field_value_to_database($p_value, $t_type), $c_field_id, $c_bug_id)); $row = db_fetch_array($result); history_log_event_direct($c_bug_id, $t_name, custom_field_database_to_value($row[$t_value_field], $t_type), $p_value); } else { $query = "INSERT INTO {$t_custom_field_string_table}\n\t\t\t\t\t\t( field_id, bug_id, {$t_value_field} )\n\t\t\t\t\t VALUES\n\t\t\t\t\t\t( " . db_param() . ', ' . db_param() . ', ' . db_param() . ')'; db_query_bound($query, array($c_field_id, $c_bug_id, custom_field_value_to_database($p_value, $t_type))); # Don't log history events for new bug reports or on other special occasions if ($p_log_insert) { history_log_event_direct($c_bug_id, $t_name, '', $p_value); } } custom_field_clear_cache($p_field_id); # db_query errors on failure so: return true; }