diff options
author | Perl Tidy <perltidy@bugzilla.org> | 2018-12-05 21:38:52 +0100 |
---|---|---|
committer | Dylan William Hardison <dylan@hardison.net> | 2018-12-05 23:49:08 +0100 |
commit | 8ec8da0491ad89604700b3e29a227966f6d84ba1 (patch) | |
tree | 9d270f173330ca19700e0ba9f2ee931300646de1 /Bugzilla/Field | |
parent | a7bb5a65b71644d9efce5fed783ed545b9336548 (diff) | |
download | bugzilla-8ec8da0491ad89604700b3e29a227966f6d84ba1.tar.gz bugzilla-8ec8da0491ad89604700b3e29a227966f6d84ba1.tar.xz |
no bug - reformat all the code using the new perltidy rules
Diffstat (limited to 'Bugzilla/Field')
-rw-r--r-- | Bugzilla/Field/Choice.pm | 304 | ||||
-rw-r--r-- | Bugzilla/Field/ChoiceInterface.pm | 227 |
2 files changed, 272 insertions, 259 deletions
diff --git a/Bugzilla/Field/Choice.pm b/Bugzilla/Field/Choice.pm index 10f8f38e6..eab2c20f3 100644 --- a/Bugzilla/Field/Choice.pm +++ b/Bugzilla/Field/Choice.pm @@ -28,42 +28,42 @@ use Scalar::Util qw(blessed); use constant IS_CONFIG => 1; use constant DB_COLUMNS => qw( - id - value - sortkey - isactive - visibility_value_id + id + value + sortkey + isactive + visibility_value_id ); use constant UPDATE_COLUMNS => qw( - value - sortkey - isactive - visibility_value_id + value + sortkey + isactive + visibility_value_id ); use constant NAME_FIELD => 'value'; use constant LIST_ORDER => 'sortkey, value'; use constant VALIDATORS => { - value => \&_check_value, - sortkey => \&_check_sortkey, - visibility_value_id => \&_check_visibility_value_id, - isactive => \&_check_isactive, + value => \&_check_value, + sortkey => \&_check_sortkey, + visibility_value_id => \&_check_visibility_value_id, + isactive => \&_check_isactive, }; use constant CLASS_MAP => { - bug_status => 'Bugzilla::Status', - classification => 'Bugzilla::Classification', - component => 'Bugzilla::Component', - product => 'Bugzilla::Product', + bug_status => 'Bugzilla::Status', + classification => 'Bugzilla::Classification', + component => 'Bugzilla::Component', + product => 'Bugzilla::Product', }; use constant DEFAULT_MAP => { - op_sys => 'defaultopsys', - rep_platform => 'defaultplatform', - priority => 'defaultpriority', - bug_severity => 'defaultseverity', + op_sys => 'defaultopsys', + rep_platform => 'defaultplatform', + priority => 'defaultpriority', + bug_severity => 'defaultseverity', }; ################# @@ -76,32 +76,32 @@ use constant DEFAULT_MAP => { # are Bugzilla::Status objects. sub type { - my ($class, $field) = @_; - my $field_obj = blessed $field ? $field : Bugzilla::Field->check($field); - my $field_name = $field_obj->name; - - if ($class->CLASS_MAP->{$field_name}) { - return $class->CLASS_MAP->{$field_name}; - } - - # For generic classes, we use a lowercase class name, so as - # not to interfere with any real subclasses we might make some day. - my $package = "Bugzilla::Field::Choice::$field_name"; - Bugzilla->request_cache->{"field_$package"} = $field_obj; - - # This package only needs to be created once. We check if the DB_TABLE - # glob for this package already exists, which tells us whether or not - # we need to create the package (this works even under mod_perl, where - # this package definition will persist across requests)). - if (!defined *{"${package}::DB_TABLE"}) { - eval <<EOC; + my ($class, $field) = @_; + my $field_obj = blessed $field ? $field : Bugzilla::Field->check($field); + my $field_name = $field_obj->name; + + if ($class->CLASS_MAP->{$field_name}) { + return $class->CLASS_MAP->{$field_name}; + } + + # For generic classes, we use a lowercase class name, so as + # not to interfere with any real subclasses we might make some day. + my $package = "Bugzilla::Field::Choice::$field_name"; + Bugzilla->request_cache->{"field_$package"} = $field_obj; + + # This package only needs to be created once. We check if the DB_TABLE + # glob for this package already exists, which tells us whether or not + # we need to create the package (this works even under mod_perl, where + # this package definition will persist across requests)). + if (!defined *{"${package}::DB_TABLE"}) { + eval <<EOC; package $package; use base qw(Bugzilla::Field::Choice); use constant DB_TABLE => '$field_name'; EOC - } + } - return $package; + return $package; } ################ @@ -112,11 +112,11 @@ EOC # the understanding that you can't use Bugzilla::Field::Choice # without calling type(). sub new { - my $class = shift; - if ($class eq 'Bugzilla::Field::Choice') { - ThrowCodeError('field_choice_must_use_type'); - } - $class->SUPER::new(@_); + my $class = shift; + if ($class eq 'Bugzilla::Field::Choice') { + ThrowCodeError('field_choice_must_use_type'); + } + $class->SUPER::new(@_); } ######################### @@ -128,64 +128,66 @@ sub new { # columns. (Normally Bugzilla::Object dies if you pass arguments # that aren't valid columns.) sub create { - my $class = shift; - my ($params) = @_; - foreach my $key (keys %$params) { - if (!grep {$_ eq $key} $class->_get_db_columns) { - delete $params->{$key}; - } + my $class = shift; + my ($params) = @_; + foreach my $key (keys %$params) { + if (!grep { $_ eq $key } $class->_get_db_columns) { + delete $params->{$key}; } - return $class->SUPER::create(@_); + } + return $class->SUPER::create(@_); } sub update { - my $self = shift; - my $dbh = Bugzilla->dbh; - my $fname = $self->field->name; - - $dbh->bz_start_transaction(); - - my ($changes, $old_self) = $self->SUPER::update(@_); - if (exists $changes->{value}) { - my ($old, $new) = @{ $changes->{value} }; - if ($self->field->type == FIELD_TYPE_MULTI_SELECT) { - $dbh->do("UPDATE bug_$fname SET value = ? WHERE value = ?", - undef, $new, $old); - } - else { - $dbh->do("UPDATE bugs SET $fname = ? WHERE $fname = ?", - undef, $new, $old); - } + my $self = shift; + my $dbh = Bugzilla->dbh; + my $fname = $self->field->name; - if ($old_self->is_default) { - my $param = $self->DEFAULT_MAP->{$self->field->name}; - SetParam($param, $self->name); - write_params(); - } + $dbh->bz_start_transaction(); + + my ($changes, $old_self) = $self->SUPER::update(@_); + if (exists $changes->{value}) { + my ($old, $new) = @{$changes->{value}}; + if ($self->field->type == FIELD_TYPE_MULTI_SELECT) { + $dbh->do("UPDATE bug_$fname SET value = ? WHERE value = ?", undef, $new, $old); + } + else { + $dbh->do("UPDATE bugs SET $fname = ? WHERE $fname = ?", undef, $new, $old); } - $dbh->bz_commit_transaction(); - return wantarray ? ($changes, $old_self) : $changes; + if ($old_self->is_default) { + my $param = $self->DEFAULT_MAP->{$self->field->name}; + SetParam($param, $self->name); + write_params(); + } + } + + $dbh->bz_commit_transaction(); + return wantarray ? ($changes, $old_self) : $changes; } sub remove_from_db { - my $self = shift; - if ($self->is_default) { - ThrowUserError('fieldvalue_is_default', - { field => $self->field, value => $self, - param_name => $self->DEFAULT_MAP->{$self->field->name}, - }); - } - if ($self->is_static) { - ThrowUserError('fieldvalue_not_deletable', - { field => $self->field, value => $self }); - } - if ($self->bug_count) { - ThrowUserError("fieldvalue_still_has_bugs", - { field => $self->field, value => $self }); - } - $self->_check_if_controller(); # From ChoiceInterface. - $self->SUPER::remove_from_db(); + my $self = shift; + if ($self->is_default) { + ThrowUserError( + 'fieldvalue_is_default', + { + field => $self->field, + value => $self, + param_name => $self->DEFAULT_MAP->{$self->field->name}, + } + ); + } + if ($self->is_static) { + ThrowUserError('fieldvalue_not_deletable', + {field => $self->field, value => $self}); + } + if ($self->bug_count) { + ThrowUserError("fieldvalue_still_has_bugs", + {field => $self->field, value => $self}); + } + $self->_check_if_controller(); # From ChoiceInterface. + $self->SUPER::remove_from_db(); } ############ @@ -193,12 +195,13 @@ sub remove_from_db { ############ sub set_is_active { $_[0]->set('isactive', $_[1]); } -sub set_name { $_[0]->set('value', $_[1]); } -sub set_sortkey { $_[0]->set('sortkey', $_[1]); } +sub set_name { $_[0]->set('value', $_[1]); } +sub set_sortkey { $_[0]->set('sortkey', $_[1]); } + sub set_visibility_value { - my ($self, $value) = @_; - $self->set('visibility_value_id', $value); - delete $self->{visibility_value}; + my ($self, $value) = @_; + $self->set('visibility_value_id', $value); + delete $self->{visibility_value}; } ############## @@ -206,73 +209,74 @@ sub set_visibility_value { ############## sub _check_isactive { - my ($invocant, $value) = @_; - $value = Bugzilla::Object::check_boolean($invocant, $value); - if (!$value and ref $invocant) { - if ($invocant->is_default) { - my $field = $invocant->field; - ThrowUserError('fieldvalue_is_default', - { value => $invocant, field => $field, - param_name => $invocant->DEFAULT_MAP->{$field->name} - }); - } - if ($invocant->is_static) { - ThrowUserError('fieldvalue_not_deletable', - { value => $invocant, field => $invocant->field }); + my ($invocant, $value) = @_; + $value = Bugzilla::Object::check_boolean($invocant, $value); + if (!$value and ref $invocant) { + if ($invocant->is_default) { + my $field = $invocant->field; + ThrowUserError( + 'fieldvalue_is_default', + { + value => $invocant, + field => $field, + param_name => $invocant->DEFAULT_MAP->{$field->name} } + ); + } + if ($invocant->is_static) { + ThrowUserError('fieldvalue_not_deletable', + {value => $invocant, field => $invocant->field}); } - return $value; + } + return $value; } sub _check_value { - my ($invocant, $value) = @_; + my ($invocant, $value) = @_; - my $field = $invocant->field; + my $field = $invocant->field; - $value = trim($value); + $value = trim($value); - # Make sure people don't rename static values - if (blessed($invocant) && $value ne $invocant->name - && $invocant->is_static) - { - ThrowUserError('fieldvalue_not_editable', - { field => $field, old_value => $invocant }); - } + # Make sure people don't rename static values + if (blessed($invocant) && $value ne $invocant->name && $invocant->is_static) { + ThrowUserError('fieldvalue_not_editable', + {field => $field, old_value => $invocant}); + } - ThrowUserError('fieldvalue_undefined') if !defined $value || $value eq ""; - ThrowUserError('fieldvalue_name_too_long', { value => $value }) - if length($value) > MAX_FIELD_VALUE_SIZE; + ThrowUserError('fieldvalue_undefined') if !defined $value || $value eq ""; + ThrowUserError('fieldvalue_name_too_long', {value => $value}) + if length($value) > MAX_FIELD_VALUE_SIZE; - my $exists = $invocant->type($field)->new({ name => $value }); - if ($exists && (!blessed($invocant) || $invocant->id != $exists->id)) { - ThrowUserError('fieldvalue_already_exists', - { field => $field, value => $exists }); - } + my $exists = $invocant->type($field)->new({name => $value}); + if ($exists && (!blessed($invocant) || $invocant->id != $exists->id)) { + ThrowUserError('fieldvalue_already_exists', + {field => $field, value => $exists}); + } - return $value; + return $value; } sub _check_sortkey { - my ($invocant, $value) = @_; - $value = trim($value); - return 0 if !$value; - # Store for the error message in case detaint_natural clears it. - my $orig_value = $value; - detaint_natural($value) - || ThrowUserError('fieldvalue_sortkey_invalid', - { sortkey => $orig_value, - field => $invocant->field }); - return $value; + my ($invocant, $value) = @_; + $value = trim($value); + return 0 if !$value; + + # Store for the error message in case detaint_natural clears it. + my $orig_value = $value; + detaint_natural($value) + || ThrowUserError('fieldvalue_sortkey_invalid', + {sortkey => $orig_value, field => $invocant->field}); + return $value; } sub _check_visibility_value_id { - my ($invocant, $value_id) = @_; - $value_id = trim($value_id); - my $field = $invocant->field->value_field; - return undef if !$field || !$value_id; - my $value_obj = Bugzilla::Field::Choice->type($field) - ->check({ id => $value_id }); - return $value_obj->id; + my ($invocant, $value_id) = @_; + $value_id = trim($value_id); + my $field = $invocant->field->value_field; + return undef if !$field || !$value_id; + my $value_obj = Bugzilla::Field::Choice->type($field)->check({id => $value_id}); + return $value_obj->id; } 1; diff --git a/Bugzilla/Field/ChoiceInterface.pm b/Bugzilla/Field/ChoiceInterface.pm index bcfd75578..207ac9cb5 100644 --- a/Bugzilla/Field/ChoiceInterface.pm +++ b/Bugzilla/Field/ChoiceInterface.pm @@ -26,14 +26,19 @@ sub FIELD_NAME { return $_[0]->DB_TABLE; } #################### sub _check_if_controller { - my $self = shift; - my $vis_fields = $self->controls_visibility_of_fields; - my $values = $self->controlled_values_array; - if (@$vis_fields || @$values) { - ThrowUserError('fieldvalue_is_controller', - { value => $self, fields => [map($_->name, @$vis_fields)], - vals => $self->controlled_values }); - } + my $self = shift; + my $vis_fields = $self->controls_visibility_of_fields; + my $values = $self->controlled_values_array; + if (@$vis_fields || @$values) { + ThrowUserError( + 'fieldvalue_is_controller', + { + value => $self, + fields => [map($_->name, @$vis_fields)], + vals => $self->controlled_values + } + ); + } } @@ -42,145 +47,149 @@ sub _check_if_controller { ############# sub is_active { return $_[0]->{'isactive'}; } -sub sortkey { return $_[0]->{'sortkey'}; } +sub sortkey { return $_[0]->{'sortkey'}; } sub bug_count { - my $self = shift; - return $self->{bug_count} if defined $self->{bug_count}; - my $dbh = Bugzilla->dbh; - my $fname = $self->field->name; - my $count; - if ($self->field->type == FIELD_TYPE_MULTI_SELECT) { - $count = $dbh->selectrow_array("SELECT COUNT(*) FROM bug_$fname - WHERE value = ?", undef, $self->name); - } - else { - $count = $dbh->selectrow_array("SELECT COUNT(*) FROM bugs - WHERE $fname = ?", - undef, $self->name); - } - $self->{bug_count} = $count; - return $count; + my $self = shift; + return $self->{bug_count} if defined $self->{bug_count}; + my $dbh = Bugzilla->dbh; + my $fname = $self->field->name; + my $count; + if ($self->field->type == FIELD_TYPE_MULTI_SELECT) { + $count = $dbh->selectrow_array( + "SELECT COUNT(*) FROM bug_$fname + WHERE value = ?", undef, $self->name + ); + } + else { + $count = $dbh->selectrow_array( + "SELECT COUNT(*) FROM bugs + WHERE $fname = ?", undef, $self->name + ); + } + $self->{bug_count} = $count; + return $count; } sub field { - my $invocant = shift; - my $class = ref $invocant || $invocant; - my $cache = Bugzilla->request_cache; - # This is just to make life easier for subclasses. Our auto-generated - # subclasses from Bugzilla::Field::Choice->type() already have this set. - $cache->{"field_$class"} ||= - new Bugzilla::Field({ name => $class->FIELD_NAME }); - return $cache->{"field_$class"}; + my $invocant = shift; + my $class = ref $invocant || $invocant; + my $cache = Bugzilla->request_cache; + + # This is just to make life easier for subclasses. Our auto-generated + # subclasses from Bugzilla::Field::Choice->type() already have this set. + $cache->{"field_$class"} ||= new Bugzilla::Field({name => $class->FIELD_NAME}); + return $cache->{"field_$class"}; } sub is_default { - my $self = shift; - my $name = $self->DEFAULT_MAP->{$self->field->name}; - # If it doesn't exist in DEFAULT_MAP, then there is no parameter - # related to this field. - return 0 unless $name; - return ($self->name eq Bugzilla->params->{$name}) ? 1 : 0; + my $self = shift; + my $name = $self->DEFAULT_MAP->{$self->field->name}; + + # If it doesn't exist in DEFAULT_MAP, then there is no parameter + # related to this field. + return 0 unless $name; + return ($self->name eq Bugzilla->params->{$name}) ? 1 : 0; } sub is_static { - my $self = shift; - # If we need to special-case Resolution for *anything* else, it should - # get its own subclass. - if ($self->field->name eq 'resolution') { - return grep($_ eq $self->name, ('', 'FIXED', 'DUPLICATE')) - ? 1 : 0; - } - elsif ($self->field->custom) { - return $self->name eq '---' ? 1 : 0; - } - return 0; + my $self = shift; + + # If we need to special-case Resolution for *anything* else, it should + # get its own subclass. + if ($self->field->name eq 'resolution') { + return grep($_ eq $self->name, ('', 'FIXED', 'DUPLICATE')) ? 1 : 0; + } + elsif ($self->field->custom) { + return $self->name eq '---' ? 1 : 0; + } + return 0; } sub controls_visibility_of_fields { - my $self = shift; - my $dbh = Bugzilla->dbh; + my $self = shift; + my $dbh = Bugzilla->dbh; - if (!$self->{controls_visibility_of_fields}) { - my $ids = $dbh->selectcol_arrayref( - "SELECT id FROM fielddefs + if (!$self->{controls_visibility_of_fields}) { + my $ids = $dbh->selectcol_arrayref( + "SELECT id FROM fielddefs INNER JOIN field_visibility ON fielddefs.id = field_visibility.field_id - WHERE value_id = ? AND visibility_field_id = ?", undef, - $self->id, $self->field->id); + WHERE value_id = ? AND visibility_field_id = ?", undef, $self->id, + $self->field->id + ); - $self->{controls_visibility_of_fields} = - Bugzilla::Field->new_from_list($ids); - } + $self->{controls_visibility_of_fields} = Bugzilla::Field->new_from_list($ids); + } - return $self->{controls_visibility_of_fields}; + return $self->{controls_visibility_of_fields}; } sub visibility_value { - my $self = shift; - if ($self->{visibility_value_id}) { - require Bugzilla::Field::Choice; - $self->{visibility_value} ||= - Bugzilla::Field::Choice->type($self->field->value_field)->new( - $self->{visibility_value_id}); - } - return $self->{visibility_value}; + my $self = shift; + if ($self->{visibility_value_id}) { + require Bugzilla::Field::Choice; + $self->{visibility_value} + ||= Bugzilla::Field::Choice->type($self->field->value_field) + ->new($self->{visibility_value_id}); + } + return $self->{visibility_value}; } sub controlled_values { - my $self = shift; - return $self->{controlled_values} if defined $self->{controlled_values}; - my $fields = $self->field->controls_values_of; - my %controlled_values; - require Bugzilla::Field::Choice; - foreach my $field (@$fields) { - $controlled_values{$field->name} = - Bugzilla::Field::Choice->type($field) - ->match({ visibility_value_id => $self->id }); - } - $self->{controlled_values} = \%controlled_values; - return $self->{controlled_values}; + my $self = shift; + return $self->{controlled_values} if defined $self->{controlled_values}; + my $fields = $self->field->controls_values_of; + my %controlled_values; + require Bugzilla::Field::Choice; + foreach my $field (@$fields) { + $controlled_values{$field->name} = Bugzilla::Field::Choice->type($field) + ->match({visibility_value_id => $self->id}); + } + $self->{controlled_values} = \%controlled_values; + return $self->{controlled_values}; } sub controlled_values_array { - my ($self) = @_; - my $values = $self->controlled_values; - return [map { @{ $values->{$_} } } keys %$values]; + my ($self) = @_; + my $values = $self->controlled_values; + return [map { @{$values->{$_}} } keys %$values]; } sub is_visible_on_bug { - my ($self, $bug) = @_; + my ($self, $bug) = @_; - # Values currently set on the bug are always shown. - return 1 if $self->is_set_on_bug($bug); + # Values currently set on the bug are always shown. + return 1 if $self->is_set_on_bug($bug); - # Inactive values are, otherwise, never shown. - return 0 if !$self->is_active; + # Inactive values are, otherwise, never shown. + return 0 if !$self->is_active; - # Values without a visibility value are, otherwise, always shown. - my $visibility_value = $self->visibility_value; - return 1 if !$visibility_value; + # Values without a visibility value are, otherwise, always shown. + my $visibility_value = $self->visibility_value; + return 1 if !$visibility_value; - # Values with a visibility value are only shown if the visibility - # value is set on the bug. - return $visibility_value->is_set_on_bug($bug); + # Values with a visibility value are only shown if the visibility + # value is set on the bug. + return $visibility_value->is_set_on_bug($bug); } sub is_set_on_bug { - my ($self, $bug) = @_; - my $field_name = $self->FIELD_NAME; - # This allows bug/create/create.html.tmpl to pass in a hashref that - # looks like a bug object. - my $value = blessed($bug) ? $bug->$field_name : $bug->{$field_name}; - $value = $value->name if blessed($value); - return 0 if !defined $value; - - if ($self->field->type == FIELD_TYPE_BUG_URLS - or $self->field->type == FIELD_TYPE_MULTI_SELECT) - { - return grep($_ eq $self->name, @$value) ? 1 : 0; - } - return $value eq $self->name ? 1 : 0; + my ($self, $bug) = @_; + my $field_name = $self->FIELD_NAME; + + # This allows bug/create/create.html.tmpl to pass in a hashref that + # looks like a bug object. + my $value = blessed($bug) ? $bug->$field_name : $bug->{$field_name}; + $value = $value->name if blessed($value); + return 0 if !defined $value; + + if ( $self->field->type == FIELD_TYPE_BUG_URLS + or $self->field->type == FIELD_TYPE_MULTI_SELECT) + { + return grep($_ eq $self->name, @$value) ? 1 : 0; + } + return $value eq $self->name ? 1 : 0; } 1; |