From 931bd3b651ccca832a618f7b28bbc35e503665d4 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Fri, 3 Oct 2008 06:28:31 +0000 Subject: Bug 455632: Add Bugzilla::Field::Choice->create and have editvalues.cgi use it Patch By Max Kanat-Alexander r=bbaetz, a=mkanat --- Bugzilla/Bug.pm | 8 - Bugzilla/Constants.pm | 4 + Bugzilla/Field.pm | 4 +- Bugzilla/Field/Choice.pm | 237 +++++++++++++++--------- Bugzilla/Status.pm | 55 +++++- editvalues.cgi | 55 +----- template/en/default/global/code-error.html.tmpl | 6 + template/en/default/global/user-error.html.tmpl | 21 ++- 8 files changed, 231 insertions(+), 159 deletions(-) diff --git a/Bugzilla/Bug.pm b/Bugzilla/Bug.pm index 1d12ee2cf..d620f222c 100644 --- a/Bugzilla/Bug.pm +++ b/Bugzilla/Bug.pm @@ -54,7 +54,6 @@ use base qw(Bugzilla::Object Exporter); RemoveVotes CheckIfVotedConfirmed LogActivityEntry editable_bug_fields - SPECIAL_STATUS_WORKFLOW_ACTIONS ); ##################################################################### @@ -234,13 +233,6 @@ use constant MAX_LINE_LENGTH => 254; # Used in _check_comment(). Gives the max length allowed for a comment. use constant MAX_COMMENT_LENGTH => 65535; -use constant SPECIAL_STATUS_WORKFLOW_ACTIONS => qw( - none - duplicate - change_resolution - clearresolution -); - ##################################################################### sub new { diff --git a/Bugzilla/Constants.pm b/Bugzilla/Constants.pm index abe1fe248..601ea52b2 100644 --- a/Bugzilla/Constants.pm +++ b/Bugzilla/Constants.pm @@ -151,6 +151,7 @@ use File::Basename; MAX_PRODUCT_SIZE MAX_MILESTONE_SIZE MAX_COMPONENT_SIZE + MAX_FIELD_VALUE_SIZE MAX_FREETEXT_LENGTH ); @@ -427,6 +428,9 @@ use constant MAX_MILESTONE_SIZE => 20; # The longest component name allowed. use constant MAX_COMPONENT_SIZE => 64; +# The maximum length for values of -type field. my $field = new Bugzilla::Field({name => 'bug_status'}); - my $choice = new Bugzilla::Field::Choice({ field => $field, id => 1 }); - my $choice = new Bugzilla::Field::Choice({ field => $field, name => 'NEW' }); + my $choice = new Bugzilla::Field::Choice->type($field)->new(1); - my $choices = Bugzilla::Field::Choice->new_from_list([1,2,3], - { field => $field}); - my $choices = Bugzilla::Field::Choice->get_all({ field => $field }); - my $choices = Bugzilla::Field::Choice->match({ sortkey => 10, - field => $field }); + my $choices = Bugzilla::Field::Choice->type($field)->new_from_list([1,2,3]); + my $choices = Bugzilla::Field::Choice->type($field)->get_all(); + my $choices = Bugzilla::Field::Choice->type($field->match({ sortkey => 10 }); =head1 DESCRIPTION This is an implementation of L, but with a twist. -All the class methods require that you specify an additional C -argument, which is a L object that represents the -field whose value this is. +You can't call any class methods (such as C, C, etc.) +directly on C itself. Instead, you have to +call Ctype($field)> to get the class +you're going to instantiate, and then you call the methods on that. + +We do that because each field has its own database table for its values, so +each value type needs its own class. -You can look at the L to see where this extra C -argument goes in each function. +See the L for examples of how this works. =head1 METHODS +=head2 Class Factory + +In object-oriented design, a "class factory" is a method that picks +and returns the right class for you, based on an argument that you pass. + +=over + +=item C + +Takes a single argument, which is either the name of a field from the +C table, or a L object representing a field. + +Returns an appropriate subclass of C that you +can now call class methods on (like C, C, C, etc.) + +B: YOU CANNOT CALL CLASS METHODS ON C. You +must call C to get a class you can call methods on. + +=back + =head2 Accessors These are in addition to the standard L accessors. diff --git a/Bugzilla/Status.pm b/Bugzilla/Status.pm index f8b77331c..5f37974e7 100644 --- a/Bugzilla/Status.pm +++ b/Bugzilla/Status.pm @@ -22,13 +22,28 @@ use strict; package Bugzilla::Status; -use base qw(Bugzilla::Object Exporter); -@Bugzilla::Status::EXPORT = qw(BUG_STATE_OPEN is_open_state closed_bug_statuses); +use Bugzilla::Error; + +use base qw(Bugzilla::Field::Choice Exporter); +@Bugzilla::Status::EXPORT = qw( + BUG_STATE_OPEN + SPECIAL_STATUS_WORKFLOW_ACTIONS + + is_open_state + closed_bug_statuses +); ################################ ##### Initialization ##### ################################ +use constant SPECIAL_STATUS_WORKFLOW_ACTIONS => qw( + none + duplicate + change_resolution + clearresolution +); + use constant DB_TABLE => 'bug_status'; use constant DB_COLUMNS => qw( @@ -39,8 +54,24 @@ use constant DB_COLUMNS => qw( is_open ); -use constant NAME_FIELD => 'value'; -use constant LIST_ORDER => 'sortkey, value'; +sub VALIDATORS { + my $invocant = shift; + my $validators = $invocant->SUPER::VALIDATORS; + $validators->{is_open} = \&Bugzilla::Object::check_boolean; + $validators->{value} = \&_check_value; + return $validators; +} + +######################### +# Database Manipulation # +######################### + +sub create { + my $class = shift; + my $self = $class->SUPER::create(@_); + add_missing_bug_status_transitions(); + return $self; +} ############################### ##### Accessors #### @@ -51,6 +82,22 @@ sub sortkey { return $_[0]->{'sortkey'}; } sub is_active { return $_[0]->{'isactive'}; } sub is_open { return $_[0]->{'is_open'}; } +############## +# Validators # +############## + +sub _check_value { + my $invocant = shift; + my $value = $invocant->SUPER::_check_value(@_); + + if (grep { lc($value) eq lc($_) } SPECIAL_STATUS_WORKFLOW_ACTIONS) { + ThrowUserError('fieldvalue_reserved_word', + { field => $invocant->field, value => $value }); + } + return $value; +} + + ############################### ##### Methods #### ############################### diff --git a/editvalues.cgi b/editvalues.cgi index ffb7ce576..4525774e1 100755 --- a/editvalues.cgi +++ b/editvalues.cgi @@ -29,7 +29,7 @@ use Bugzilla::Config qw(:admin); use Bugzilla::Token; use Bugzilla::Field; use Bugzilla::Bug; -use Bugzilla::Status; +use Bugzilla::Status qw(SPECIAL_STATUS_WORKFLOW_ACTIONS); # List of different tables that contain the changeable field values # (the old "enums.") Keep them in alphabetical order by their @@ -172,12 +172,8 @@ trick_taint($field); sub display_field_values { my $template = Bugzilla->template; my $field = $vars->{'field'}->name; - my $fieldvalues = - Bugzilla->dbh->selectall_arrayref("SELECT value AS name, sortkey" - . " FROM $field ORDER BY sortkey, value", - {Slice =>{}}); - $vars->{'values'} = $fieldvalues; + $vars->{'values'} = $vars->{'field'}->legal_values; $vars->{'default'} = Bugzilla->params->{$defaults{$field}} if defined $defaults{$field}; $vars->{'static'} = $static{$field} if exists $static{$field}; @@ -212,53 +208,14 @@ if ($action eq 'add') { if ($action eq 'new') { check_token_data($token, 'add_field_value'); - # Cleanups and validity checks - $value || ThrowUserError('fieldvalue_undefined'); - - if (length($value) > 60) { - ThrowUserError('fieldvalue_name_too_long', - {'value' => $value}); - } - # Need to store in case detaint_natural() clears the sortkey - my $stored_sortkey = $sortkey; - if (!detaint_natural($sortkey)) { - ThrowUserError('fieldvalue_sortkey_invalid', - {'name' => $field, - 'sortkey' => $stored_sortkey}); - } - if (ValueExists($field, $value)) { - ThrowUserError('fieldvalue_already_exists', - {'field' => $field_obj, - 'value' => $value}); - } - if ($field eq 'bug_status' - && (grep { lc($value) eq $_ } SPECIAL_STATUS_WORKFLOW_ACTIONS)) - { - $vars->{'value'} = $value; - ThrowUserError('fieldvalue_reserved_word', $vars); - } - - # Value is only used in a SELECT placeholder and through the HTML filter. - trick_taint($value); - - # Add the new field value. - $dbh->do("INSERT INTO $field (value, sortkey) VALUES (?, ?)", - undef, ($value, $sortkey)); - - if ($field eq 'bug_status') { - unless ($cgi->param('is_open')) { - # The bug status is a closed state, but they are open by default. - $dbh->do('UPDATE bug_status SET is_open = 0 WHERE value = ?', undef, $value); - } - # Allow the transition from this new bug status to the one used - # by the 'duplicate_or_move_bug_status' parameter. - Bugzilla::Status::add_missing_bug_status_transitions(); - } + my $created_value = Bugzilla::Field::Choice->type($field_obj)->create( + { value => $value, sortkey => $sortkey, + is_open => scalar $cgi->param('is_open') }); delete_token($token); $vars->{'message'} = 'field_value_created'; - $vars->{'value'} = $value; + $vars->{'value'} = $created_value->name; display_field_values(); } diff --git a/template/en/default/global/code-error.html.tmpl b/template/en/default/global/code-error.html.tmpl index b93b92efd..f85375849 100644 --- a/template/en/default/global/code-error.html.tmpl +++ b/template/en/default/global/code-error.html.tmpl @@ -145,6 +145,12 @@ in the database for '[% username FILTER html %]', but your account source says that '[% extern_user FILTER html %]' has that ID. + [% ELSIF error == "field_choice_must_use_type" %] + When you call a class method on Bugzilla::Field::Choice, + you must call Bugzilla::Field::Choice->type('some_field') + to generate the right class (you can't call class methods directly + on Bugzilla::Field::Choice). + [% ELSIF error == "field_type_mismatch" %] Cannot seem to handle [% field FILTER html %] and [% type FILTER html %] together. diff --git a/template/en/default/global/user-error.html.tmpl b/template/en/default/global/user-error.html.tmpl index c61a1f42a..bcb6b6630 100644 --- a/template/en/default/global/user-error.html.tmpl +++ b/template/en/default/global/user-error.html.tmpl @@ -432,7 +432,7 @@ [% ELSIF error == "fieldvalue_already_exists" %] [% title = "Field Value Already Exists" %] The value '[% value FILTER html %]' already exists for the - '[%- field.description FILTER html %]' field. + [%+ field.description FILTER html %] field. [% ELSIF error == "fieldvalue_doesnt_exist" %] [% title = "Specified Field Value Does Not Exist" %] @@ -450,8 +450,9 @@ [% ELSIF error == "fieldvalue_name_too_long" %] [% title = "Field Value Is Too Long" %] - The value of a field is limited to 60 characters. - '[% value FILTER html %]' is too long ([% value.length %] characters). + The value of a field is limited to [% constants.FIELD_VALUE_MAX_SIZE %] + characters. '[% value FILTER html %]' is too long ([% value.length %] + characters). [% ELSIF error == "fieldvalue_not_editable" %] [% title = "Field Value Not Editable" %] @@ -475,8 +476,9 @@ [% ELSIF error == "fieldvalue_sortkey_invalid" %] [% title = "Invalid Field Value Sortkey" %] - The sortkey '[% sortkey FILTER html %]' for the '[% name FILTER html %]' - field is not a valid (positive) number. + The sortkey '[% sortkey FILTER html %]' for the + [%+ field.description FILTER html %] field is not a valid + (positive) number. [% ELSIF error == "fieldvalue_still_has_bugs" %] [% title = "You Cannot Delete This Field Value" %] @@ -1178,13 +1180,13 @@ is less than the minimum allowable value of '[% min_num FILTER html %]'. [% ELSIF error == "object_name_not_specified" %] - [% type = BLOCK %][% PROCESS object_name %][% END %] + [% type = BLOCK %][% INCLUDE object_name class = class %][% END %] [% title = BLOCK %][% type FILTER ucfirst FILTER html %] Not Specified[% END %] You must select/enter a [% type FILTER html %]. [% ELSIF error == "object_does_not_exist" %] - [% type = BLOCK %][% PROCESS object_name %][% END %] + [% type = BLOCK %][% INCLUDE object_name class = class %][% END %] [% title = BLOCK %]Invalid [% type FILTER ucfirst FILTER html %][% END %] There is no [% type FILTER html %] named '[% name FILTER html %]' [% IF product.defined %] @@ -1672,5 +1674,10 @@ milestone [% ELSIF class == "Bugzilla::Status" %] status + [% ELSIF class == "Bugzilla::Field" %] + field + [% ELSIF ( matches = class.match('^Bugzilla::Field::Choice::(.+)') ) %] + [% SET field_name = matches.0 %] + [% field_descs.$field_name %] [% END %] [% END %] -- cgit v1.2.3-24-g4f1b