]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 373440: Make "check" into a generic function in Bugzilla::Object
authormkanat%bugzilla.org <>
Fri, 24 Aug 2007 02:31:19 +0000 (02:31 +0000)
committermkanat%bugzilla.org <>
Fri, 24 Aug 2007 02:31:19 +0000 (02:31 +0000)
Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=LpSolit, a=LpSolit

Bugzilla/Bug.pm
Bugzilla/Milestone.pm
Bugzilla/Object.pm
Bugzilla/User.pm
Bugzilla/Version.pm
editmilestones.cgi
editversions.cgi
template/en/default/global/user-error.html.tmpl

index 0a2770a65cf5998c2695ab1915d58feb7a50b0e2..33bec5f71d4e70308e95a7a7c0e7343be7701566 100755 (executable)
@@ -1282,7 +1282,7 @@ sub add_cc {
     my ($self, $user_or_name, $product) = @_;
     return if !$user_or_name;
     my $user = ref $user_or_name ? $user_or_name
-                                 : Bugzilla::User::check($user_or_name);
+                                 : Bugzilla::User->check($user_or_name);
 
     my $product_id = $product ? $product->id : $self->{product_id};
     if (Bugzilla->params->{strict_isolation}
@@ -1301,7 +1301,7 @@ sub add_cc {
 sub remove_cc {
     my ($self, $user_or_name) = @_;
     my $user = ref $user_or_name ? $user_or_name
-                                 : Bugzilla::User::check($user_or_name);
+                                 : Bugzilla::User->check($user_or_name);
     my $cc_users = $self->cc_users;
     @$cc_users = grep { $_->id != $user->id } @$cc_users;
 }
index 2e70b4e2d1f1ef4989ee7a0b861eb19781eb3ee1..596c34bd84226184bcb2eca70a7a71af522e6523 100644 (file)
@@ -96,23 +96,6 @@ sub sortkey    { return $_[0]->{'sortkey'};    }
 #####     Subroutines      #####
 ################################
 
-sub check_milestone {
-    my ($product, $milestone_name) = @_;
-
-    unless ($milestone_name) {
-        ThrowUserError('milestone_not_specified');
-    }
-
-    my $milestone = new Bugzilla::Milestone({ product => $product,
-                                              name    => $milestone_name });
-    unless ($milestone) {
-        ThrowUserError('milestone_not_valid',
-                       {'product' => $product->name,
-                        'milestone' => $milestone_name});
-    }
-    return $milestone;
-}
-
 sub check_sort_key {
     my ($milestone_name, $sortkey) = @_;
     # Keep a copy in case detaint_signed() clears the sortkey
@@ -174,21 +157,3 @@ Milestone.pm represents a Product Milestone object.
  Returns:     Integer with the number of bugs.
 
 =back
-
-=head1 SUBROUTINES
-
-=over
-
-=item C<check_milestone($product, $milestone_name)>
-
- Description: Checks if a milestone name was passed in
-              and if it is a valid milestone.
-
- Params:      $product - Bugzilla::Product object.
-              $milestone_name - String with a milestone name.
-
- Returns:     Bugzilla::Milestone object.
-
-=back
-
-=cut
index 4d54b04e1602b244bc2ce7ee4647013f41931b4b..9afad763348f0365c3aa5dfbf2bcbfa5f1af6400 100644 (file)
@@ -104,6 +104,24 @@ sub _init {
     return $object;
 }
 
+sub check {
+    my ($invocant, $param) = @_;
+    my $class = ref($invocant) || $invocant;
+    # If we were just passed a name, then just use the name.
+    if (!ref $param) {
+        $param = { name => $param };
+    }
+    # Don't allow empty names.
+    if (exists $param->{name}) {
+        $param->{name} = trim($param->{name});
+        $param->{name} || ThrowUserError('object_name_not_specified',
+                                          { class => $class });
+    }
+    my $obj = $class->new($param)
+        || ThrowUserError('object_does_not_exist', {%$param, class => $class});
+    return $obj;
+}
+
 sub new_from_list {
     my $invocant = shift;
     my $class = ref($invocant) || $invocant;
@@ -463,7 +481,7 @@ during these comparisons.
 
 =over
 
-=item C<new($param)>
+=item C<new>
 
 =over
 
@@ -478,7 +496,7 @@ If you pass an integer, the integer is the id of the object,
 from the database, that we  want to read in. (id is defined
 as the value in the L</ID_FIELD> column).
 
-If you pass in a hash, you can pass a C<name> key. The 
+If you pass in a hashref, you can pass a C<name> key. The 
 value of the C<name> key is the case-insensitive name of the object 
 (from L</NAME_FIELD>) in the DB.
 
@@ -503,7 +521,35 @@ are intended B<only> for use by subclasses.
 
 =item B<Returns>
 
-A fully-initialized object.
+A fully-initialized object, or C<undef> if there is no object in the
+database matching the parameters you passed in.
+
+=back
+
+=item C<check>
+
+=over
+
+=item B<Description>
+
+Checks if there is an object in the database with the specified name, and
+throws an error if you specified an empty name, or if there is no object
+in the database with that name.
+
+=item B<Params>
+
+The parameters are the same as for L</new>, except that if you don't pass
+a hashref, the single argument is the I<name> of the object, not the id.
+
+=item B<Returns>
+
+A fully initialized object, guaranteed.
+
+=item B<Notes For Implementors>
+
+If you implement this in your subclass, make sure that you also update
+the C<object_name> block at the bottom of the F<global/user-error.html.tmpl>
+template.
 
 =back
 
index cf8de0274ce0588815ec231f6563a2d46c10951c..8ccd15a63929f95c31b23c150e7540c4d17a3074 100644 (file)
@@ -131,14 +131,6 @@ sub new {
     return $class->SUPER::new(@_);
 }
 
-sub check {
-    my ($username) = @_;
-    $username = trim($username);
-    my $user = new Bugzilla::User({ name => $username })
-        || ThrowUserError('invalid_username', { name => $username });
-    return $user;
-}
-
 sub update {
     my $self = shift;
     my $changes = $self->SUPER::update(@_);
index 69eee3752925f0557d90a9f468748c42dae82a69..a2ef6b01e41a7b0e30242fdaf493c8e5f5c38ad5 100644 (file)
@@ -150,20 +150,6 @@ sub product_id { return $_[0]->{'product_id'}; }
 #####     Subroutines       ###
 ###############################
 
-sub check_version {
-    my ($product, $version_name) = @_;
-
-    $version_name || ThrowUserError('version_not_specified');
-    my $version = new Bugzilla::Version(
-        { product => $product, name => $version_name });
-    unless ($version) {
-        ThrowUserError('version_not_valid',
-                       {'product' => $product->name,
-                        'version' => $version_name});
-    }
-    return $version;
-}
-
 sub create {
     my ($name, $product) = @_;
     my $dbh = Bugzilla->dbh;
@@ -212,9 +198,6 @@ Bugzilla::Version - Bugzilla product version class.
 
     my $version = $hash_ref->{'version_value'};
 
-    my $version = Bugzilla::Version::check_version($product_obj,
-                                                   'acme_version');
-
     my $version = Bugzilla::Version::create($version_name, $product);
 
 =head1 DESCRIPTION
@@ -266,15 +249,6 @@ Version.pm represents a Product Version object.
 
 =over
 
-=item C<check_version($product, $version_name)>
-
- Description: Checks if the version name exists for the product name.
-
- Params:      $product - A Bugzilla::Product object.
-              $version_name - String with a version name.
-
- Returns:     Bugzilla::Version object.
-
 =item C<create($version_name, $product)>
 
  Description: Create a new version for the given product.
index 17733bdb1a0e9f2e927d703236ac65da78050706..880e1d4a794bde7af8f0eb76ca634620ac88a77f 100755 (executable)
@@ -168,8 +168,8 @@ if ($action eq 'new') {
 #
 
 if ($action eq 'del') {
-    my $milestone = Bugzilla::Milestone::check_milestone($product,
-                                                         $milestone_name);
+    my $milestone = Bugzilla::Milestone->check({ product => $product,
+                                                 name    => $milestone_name });
     
     $vars->{'milestone'} = $milestone;
     $vars->{'product'} = $product;
@@ -193,9 +193,8 @@ if ($action eq 'del') {
 
 if ($action eq 'delete') {
     check_token_data($token, 'delete_milestone');
-    my $milestone =
-        Bugzilla::Milestone::check_milestone($product,
-                                             $milestone_name);
+    my $milestone = Bugzilla::Milestone->check({ product => $product,
+                                                 name    => $milestone_name });
     $vars->{'milestone'} = $milestone;
     $vars->{'product'} = $product;
 
@@ -245,9 +244,8 @@ if ($action eq 'delete') {
 
 if ($action eq 'edit') {
 
-    my $milestone =
-        Bugzilla::Milestone::check_milestone($product,
-                                             $milestone_name);
+    my $milestone = Bugzilla::Milestone->check({ product => $product,
+                                                 name    => $milestone_name });
 
     $vars->{'milestone'} = $milestone;
     $vars->{'product'} = $product;
@@ -269,9 +267,8 @@ if ($action eq 'edit') {
 if ($action eq 'update') {
     check_token_data($token, 'edit_milestone');
     my $milestone_old_name = trim($cgi->param('milestoneold') || '');
-    my $milestone_old =
-        Bugzilla::Milestone::check_milestone($product,
-                                             $milestone_old_name);
+    my $milestone_old = Bugzilla::Milestone->check(
+        { product => $product, name =>  $milestone_old_name });
 
     if (length($milestone_name) > 20) {
         ThrowUserError('milestone_name_too_long',
@@ -343,9 +340,8 @@ if ($action eq 'update') {
 
     $dbh->bz_unlock_tables();
 
-    my $milestone =
-        Bugzilla::Milestone::check_milestone($product,
-                                             $milestone_name);
+    my $milestone = Bugzilla::Milestone->check({ product => $product,
+                                                 name    => $milestone_name });
     delete_token($token);
 
     $vars->{'milestone'} = $milestone;
index 7bda6215dc03c62283b937960d088a1b7e5c576d..54f87457beddc8c5563452ef35706dd0d44a0c4e 100755 (executable)
@@ -147,9 +147,8 @@ if ($action eq 'new') {
 #
 
 if ($action eq 'del') {
-
-    my $version = Bugzilla::Version::check_version($product, $version_name);
-
+    my $version = Bugzilla::Version->check({ product => $product,
+                                             name    => $version_name });
     $vars->{'version'} = $version;
     $vars->{'product'} = $product;
     $vars->{'token'} = issue_session_token('delete_version');
@@ -167,7 +166,8 @@ if ($action eq 'del') {
 
 if ($action eq 'delete') {
     check_token_data($token, 'delete_version');
-    my $version = Bugzilla::Version::check_version($product, $version_name);
+    my $version = Bugzilla::Version->check({ product => $product, 
+                                             name    => $version_name });
     $version->remove_from_db;
     delete_token($token);
 
@@ -189,9 +189,8 @@ if ($action eq 'delete') {
 #
 
 if ($action eq 'edit') {
-
-    my $version = Bugzilla::Version::check_version($product, $version_name);
-
+    my $version = Bugzilla::Version->check({ product => $product,
+                                             name    => $version_name });
     $vars->{'version'} = $version;
     $vars->{'product'} = $product;
     $vars->{'token'} = issue_session_token('edit_version');
@@ -211,8 +210,8 @@ if ($action eq 'edit') {
 if ($action eq 'update') {
     check_token_data($token, 'edit_version');
     my $version_old_name = trim($cgi->param('versionold') || '');
-    my $version =
-        Bugzilla::Version::check_version($product, $version_old_name);
+    my $version = Bugzilla::Version->check({ product => $product,
+                                             name   => $version_old_name });
 
     $dbh->bz_lock_tables('bugs WRITE', 'versions WRITE');
 
index b5b7bf098cbfe89ff5073572b905232a8c44b09e..5cebb8166aa99e086d26dd1a1b9971cecc66efde 100644 (file)
     The name of a milestone is limited to 20 characters. 
     '[% name FILTER html %]' is too long ([% name.length %] characters).
 
-  [% ELSIF error == "milestone_not_specified" %]
-    [% title = "No Milestone Specified" %]
-    No milestone specified when trying to edit milestones.
-
   [% ELSIF error == "milestone_not_valid" %]
     [% title = "Specified Milestone Does Not Exist" %]
     The milestone '[% milestone FILTER html %]' for product 
     in the <em>[% field_descs.$field FILTER html %]</em> field 
     is less than the minimum allowable value of '[% min_num FILTER html %]'.
 
+  [% ELSIF error == "object_name_not_specified" %]
+    [% type = BLOCK %][% PROCESS object_name %][% 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 %]
+    [% title = BLOCK %]Invalid [% type FILTER ucfirst FILTER html %][% END %]
+    There is no [% type FILTER html %] named '[% name FILTER html %]'
+    [% IF product.defined %]
+      in the '[% product.name FILTER html %]' product
+    [% END %].
+    [% IF class == "Bugzilla::User" %]
+      Either you mis-typed the name or that user has not yet registered
+      for a [% terms.Bugzilla %] account.
+    [% END %]
+
   [% ELSIF error == "old_password_incorrect" %]
     [% title = "Incorrect Old Password" %]
     You did not enter your old password correctly.
 [% END %]            
 
 [% PROCESS global/footer.html.tmpl %]
+
+[% BLOCK object_name %]
+  [% IF class == "Bugzilla::User" %]
+    user
+  [% ELSIF class == "Bugzilla::Version" %]
+    version
+  [% ELSIF class == "Bugzilla::Milestone" %]
+    milestone
+  [% END %]
+[% END %]