From 4dd427ea99673391d923db9682836d344f178b54 Mon Sep 17 00:00:00 2001 From: "lpsolit%gmail.com" <> Date: Mon, 8 Sep 2008 21:21:24 +0000 Subject: Bug 453743: Decrease the number of calls to the DB about flags when viewing a bug - Patch by Frédéric Buclin r/a=mkanat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bugzilla/Flag.pm | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'Bugzilla/Flag.pm') diff --git a/Bugzilla/Flag.pm b/Bugzilla/Flag.pm index 8201a907d..ea3e940d8 100644 --- a/Bugzilla/Flag.pm +++ b/Bugzilla/Flag.pm @@ -1158,6 +1158,44 @@ sub CancelRequests { \@old_summaries, \@new_summaries); } +# This is an internal function used by $bug->flag_types +# and $attachment->flag_types to collect data about available +# flag types and existing flags set on them. You should never +# call this function directly. +sub _flag_types { + my $vars = shift; + + my $target_type = $vars->{target_type}; + my $flags; + + # Retrieve all existing flags for this bug/attachment. + if ($target_type eq 'bug') { + my $bug_id = delete $vars->{bug_id}; + $flags = Bugzilla::Flag->match({target_type => 'bug', bug_id => $bug_id}); + } + elsif ($target_type eq 'attachment') { + my $attach_id = delete $vars->{attach_id}; + $flags = Bugzilla::Flag->match({attach_id => $attach_id}); + } + else { + ThrowCodeError('bad_arg', {argument => 'target_type', + function => 'Bugzilla::Flag::_flag_types'}); + } + + # Get all available flag types for the given product and component. + my $flag_types = Bugzilla::FlagType::match($vars); + + $_->{flags} = [] foreach @$flag_types; + my %flagtypes = map { $_->id => $_ } @$flag_types; + + # Group existing flags per type. + # Call the internal 'type_id' variable instead of the method + # to not create a flagtype object. + push(@{$flagtypes{$_->{type_id}}->{flags}}, $_) foreach @$flags; + + return [sort {$a->sortkey <=> $b->sortkey || $a->name cmp $b->name} values %flagtypes]; +} + =head1 SEE ALSO =over -- cgit v1.2.3-24-g4f1b