Bug 19926: Add the Koha::Object->unblessed_all_relateds method
[koha-ffzg.git] / Koha / ItemType.pm
index ca9302d..24be0d2 100644 (file)
@@ -51,13 +51,23 @@ sub image_location {
 
 sub translated_description {
     my ( $self, $lang ) = @_;
+    if ( my $translated_description = eval { $self->get_column('translated_description') } ) {
+        # If the value has already been fetched (eg. from sarch_with_localization),
+        # do not search for it again
+        # Note: This is a bit hacky but should be fast
+        return $translated_description
+             ? $translated_description
+             : $self->description;
+    }
     $lang ||= C4::Languages::getlanguage;
     my $translated_description = Koha::Localizations->search({
         code => $self->itemtype,
         entity => 'itemtypes',
         lang => $lang
     })->next;
-    return $translated_description || $self->description;
+    return $translated_description
+         ? $translated_description->translation
+         : $self->description;
 }
 
 =head3 translated_descriptions
@@ -79,11 +89,28 @@ sub translated_descriptions {
     } @translated_descriptions ];
 }
 
+
+
+=head3 can_be_deleted
+
+my $can_be_deleted = Koha::ItemType->can_be_deleted();
+
+Counts up the number of biblioitems and items with itemtype (code) and hands back the combined number of biblioitems and items with the itemtype
+
+=cut
+
+sub can_be_deleted {
+    my ($self) = @_;
+    my $nb_items = Koha::Items->search( { itype => $self->itemtype } )->count;
+    my $nb_biblioitems = Koha::Biblioitems->search( { itemtype => $self->itemtype } )->count;
+    return $nb_items + $nb_biblioitems == 0 ? 1 : 0;
+}
+
 =head3 type
 
 =cut
 
-sub type {
+sub _type {
     return 'Itemtype';
 }