Report.pm 2.79 KB
Newer Older
1 2 3 4 5 6 7 8 9
# 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::Report;

10 11
use 5.10.1;
use strict;
12
use warnings;
13

14
use base qw(Bugzilla::Object);
15 16 17 18 19 20 21 22 23 24 25 26 27 28

use Bugzilla::CGI;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Util;

use constant DB_TABLE => 'reports';

# Do not track reports saved by users.
use constant AUDIT_CREATES => 0;
use constant AUDIT_UPDATES => 0;
use constant AUDIT_REMOVES => 0;

use constant DB_COLUMNS => qw(
29 30 31 32
  id
  user_id
  name
  query
33 34 35
);

use constant UPDATE_COLUMNS => qw(
36 37
  name
  query
38 39
);

40
use constant VALIDATORS => {name => \&_check_name, query => \&_check_query,};
41 42 43 44 45 46

##############
# Validators #
##############

sub _check_name {
47 48 49 50 51 52 53 54
  my ($invocant, $name) = @_;
  $name = clean_text($name);
  $name || ThrowUserError("report_name_missing");
  $name !~ /[<>&]/ || ThrowUserError("illegal_query_name");
  if (length($name) > MAX_LEN_QUERY_NAME) {
    ThrowUserError("query_name_too_long");
  }
  return $name;
55 56 57
}

sub _check_query {
58 59 60 61 62
  my ($invocant, $query) = @_;
  $query || ThrowUserError("buglist_parameters_required");
  my $cgi = new Bugzilla::CGI($query);
  $cgi->clean_search_url;
  return $cgi->query_string;
63 64 65 66 67 68 69 70
}

#############
# Accessors #
#############

sub query { return $_[0]->{'query'}; }

71
sub set_name  { $_[0]->set('name',  $_[1]); }
72 73 74 75 76 77 78
sub set_query { $_[0]->set('query', $_[1]); }

###########
# Methods #
###########

sub create {
79 80
  my $class = shift;
  my $param = shift;
81

82 83
  Bugzilla->login(LOGIN_REQUIRED);
  $param->{'user_id'} = Bugzilla->user->id;
84

85 86
  unshift @_, $param;
  my $self = $class->SUPER::create(@_);
87 88 89
}

sub check {
90 91 92 93 94 95 96 97 98
  my $class  = shift;
  my $report = $class->SUPER::check(@_);
  my $user   = Bugzilla->user;
  if (grep($_->id eq $report->id, @{$user->reports})) {
    return $report;
  }
  else {
    ThrowUserError('report_access_denied');
  }
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

1;

__END__

=head1 NAME

Bugzilla::Report - Bugzilla report class.

=head1 SYNOPSIS

    use Bugzilla::Report;

    my $report = new Bugzilla::Report(1);

    my $report = Bugzilla::Report->check({id => $id});

    my $name = $report->name;
    my $query = $report->query;

    my $report = Bugzilla::Report->create({ name => $name, query => $query });

    $report->set_name($new_name);
    $report->set_query($new_query);
    $report->update();

    $report->remove_from_db;

=head1 DESCRIPTION

Report.pm represents a Report object. It is an implementation
of L<Bugzilla::Object>, and thus provides all methods that
L<Bugzilla::Object> provides.

=cut
135 136 137 138 139 140 141 142 143 144 145 146 147 148

=head1 B<Methods in need of POD>

=over

=item create

=item query

=item set_query

=item set_name

=back