Classification.pm 5.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::WebService::Classification;

use 5.10.1;
use strict;
12
use warnings;
13

14
use parent qw (Bugzilla::WebService);
15 16 17 18 19 20 21 22 23

use Bugzilla::Classification;
use Bugzilla::Error;
use Bugzilla::WebService::Util qw(filter validate params_to_objects);

use constant READ_ONLY => qw(
    get
);

24 25 26 27
use constant PUBLIC_METHODS => qw(
    get
);

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
sub get {
    my ($self, $params) = validate(@_, 'names', 'ids');

    defined $params->{names} || defined $params->{ids}
        || ThrowCodeError('params_required', { function => 'Classification.get',
                                               params => ['names', 'ids'] });

    my $user = Bugzilla->user;

    Bugzilla->params->{'useclassification'}
      || $user->in_group('editclassifications')
      || ThrowUserError('auth_classification_not_enabled');

    Bugzilla->switch_to_shadow_db;

    my @classification_objs = @{ params_to_objects($params, 'Bugzilla::Classification') };
    unless ($user->in_group('editclassifications')) {
        my %selectable_class = map { $_->id => 1 } @{$user->get_selectable_classifications};
        @classification_objs = grep { $selectable_class{$_->id} } @classification_objs;
    }

49
    my @classifications = map { $self->_classification_to_hash($_, $params) } @classification_objs;
50 51 52 53 54

    return { classifications => \@classifications };
}

sub _classification_to_hash {
55
    my ($self, $classification, $params) = @_;
56 57 58 59 60 61

    my $user = Bugzilla->user;
    return unless (Bugzilla->params->{'useclassification'} || $user->in_group('editclassifications'));

    my $products = $user->in_group('editclassifications') ?
                     $classification->products : $user->get_selectable_products($classification->id);
62 63

    return filter $params, {
64 65 66 67
        id          => $self->type('int',    $classification->id),
        name        => $self->type('string', $classification->name),
        description => $self->type('string', $classification->description),
        sort_key    => $self->type('int',    $classification->sortkey),
68 69
        products    => [ map { $self->_product_to_hash($_, $params) } @$products ],
    };
70 71 72
}

sub _product_to_hash {
73 74 75
    my ($self, $product, $params) = @_;

    return filter $params, {
76 77 78
       id          => $self->type('int', $product->id),
       name        => $self->type('string', $product->name),
       description => $self->type('string', $product->description),
79
   }, undef, 'products';
80 81 82 83 84 85 86 87 88 89 90 91
}

1;

__END__

=head1 NAME

Bugzilla::Webservice::Classification - The Classification API

=head1 DESCRIPTION

92
This part of the Bugzilla API allows you to deal with the available Classifications.
93 94 95 96 97 98 99
You will be able to get information about them as well as manipulate them.

=head1 METHODS

See L<Bugzilla::WebService> for a description of how parameters are passed,
and what B<STABLE>, B<UNSTABLE>, and B<EXPERIMENTAL> mean.

100 101 102 103
Although the data input and output is the same for JSONRPC, XMLRPC and REST,
the directions for how to access the data via REST is noted in each method
where applicable.

104 105 106 107 108 109 110 111 112 113 114 115
=head1 Classification Retrieval

=head2 get

B<EXPERIMENTAL>

=over

=item B<Description>

Returns a hash containing information about a set of classifications.

116 117 118 119
=item B<REST>

To return information on a single classification:

120
GET /rest/classification/<classification_id_or_name>
121 122 123

The returned data format will be the same as below.

124 125 126 127 128 129
=item B<Params>

In addition to the parameters below, this method also accepts the
standard L<include_fields|Bugzilla::WebService/include_fields> and
L<exclude_fields|Bugzilla::WebService/exclude_fields> arguments.

130
You could get classifications info by supplying their names and/or ids.
131 132 133 134 135 136 137 138 139 140 141 142 143 144
So, this method accepts the following parameters:

=over

=item C<ids>

An array of classification ids.

=item C<names>

An array of classification names.

=back

145
=item B<Returns>
146 147

A hash with the key C<classifications> and an array of hashes as the corresponding value.
148
Each element of the array represents a classification that the user is authorized to see
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
and has the following keys:

=over

=item C<id>

C<int> The id of the classification.

=item C<name>

C<string> The name of the classification.

=item C<description>

C<string> The description of the classificaion.

=item C<sort_key>

C<int> The value which determines the order the classification is sorted.

=item C<products>

An array of hashes. The array contains the products the user is authorized to
access within the classification. Each hash has the following keys:

=over

=item C<name>

C<string> The name of the product.

=item C<id>

C<int> The id of the product.

=item C<description>

C<string> The description of the product.

=back

=back

=item B<Errors>

=over

=item 900 (Classification not enabled)

Classification is not enabled on this installation.

=back

=item B<History>

=over

=item Added in Bugzilla B<4.4>.

208 209
=item REST API call added in Bugzilla B<5.0>.

210 211 212 213
=back

=back