Bug 2047: adding images to arbitrary authorized values
authorAndrew Moore <andrew.moore@liblime.com>
Tue, 29 Apr 2008 23:20:26 +0000 (18:20 -0500)
committerJoshua Ferraro <jmf@liblime.com>
Thu, 1 May 2008 01:10:43 +0000 (20:10 -0500)
I've refactored the subs I added in the previous commit so that they make a little
more sense and are in better places in the code base. I was really hoping to make use
of existing subs, but they all seemed so specific to particular uses.

The icons now show up on the OPAC item details page.

TODO: The icons still don't show up in the OPAC search results page.

Signed-off-by: Joshua Ferraro <jmf@liblime.com>
C4/Biblio.pm
C4/Items.pm
C4/Search.pm
koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tmpl
koha-tmpl/opac-tmpl/prog/en/modules/opac-detail.tmpl
opac/opac-detail.pl

index a6f0a28..08c17c3 100755 (executable)
@@ -3075,6 +3075,66 @@ sub set_service_options {
     return $serviceOptions;
 }
 
+=head3 get_biblio_authorised_values
+
+  find the types and values for all authorised values assigned to this biblio.
+
+  parameters:
+    biblionumber
+
+  returns: a hashref malling the authorised value to the value set for this biblionumber
+
+      $authorised_values = {
+                             'Scent'     => 'flowery',
+                             'Audience'  => 'Young Adult',
+                             'itemtypes' => 'SER',
+                           };
+
+  Notes: forlibrarian should probably be passed in, and called something different.
+
+
+=cut
+
+sub get_biblio_authorised_values {
+    my $biblionumber = shift;
+    
+    my $forlibrarian = 1; # are we in staff or opac?
+    my $frameworkcode = GetFrameworkCode( $biblionumber );
+
+    my $authorised_values;
+
+    my $record  = GetMarcBiblio( $biblionumber );
+    my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode );
+
+    # assume that these entries in the authorised_value table are bibliolevel.
+    # ones that start with 'item%' are item level.
+    my $query = q(SELECT distinct authorised_value, kohafield
+                    FROM marc_subfield_structure
+                    WHERE authorised_value !=''
+                      AND (kohafield like 'biblio%'
+                       OR  kohafield like '') );
+    my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
+    
+    foreach my $tag ( keys( %$tagslib ) ) {
+        foreach my $subfield ( keys( %{$tagslib->{ $tag }} ) ) {
+            # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
+            if ( 'HASH' eq ref $tagslib->{ $tag }{ $subfield } ) {
+                if ( exists $tagslib->{ $tag }{ $subfield }{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } ) {
+                    if ( defined $record->field( $tag ) ) {
+                        my $this_subfield_value = $record->field( $tag )->subfield( $subfield );
+                        if ( defined $this_subfield_value ) {
+                            $authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } = $this_subfield_value;
+                        }
+                    }
+                }
+            }
+        }
+    }
+    # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
+    return $authorised_values;
+}
+
+
 1;
 
 __END__
index 825667f..685d85e 100644 (file)
@@ -1315,6 +1315,106 @@ sub GetItemnumberFromBarcode {
     return ($result);
 }
 
+=head3 get_item_authorised_values
+
+  find the types and values for all authorised values assigned to this item.
+
+  parameters:
+    itemnumber
+
+  returns: a hashref malling the authorised value to the value set for this itemnumber
+
+    $authorised_values = {
+             'CCODE'      => undef,
+             'DAMAGED'    => '0',
+             'LOC'        => '3',
+             'LOST'       => '0'
+             'NOT_LOAN'   => '0',
+             'RESTRICTED' => undef,
+             'STACK'      => undef,
+             'WITHDRAWN'  => '0',
+             'branches'   => 'CPL',
+             'cn_source'  => undef,
+             'itemtypes'  => 'SER',
+           };
+
+   Notes: see C4::Biblio::get_biblio_authorised_values for a similar method at the biblio level.
+
+=cut
+
+sub get_item_authorised_values {
+    my $itemnumber = shift;
+
+    # assume that these entries in the authorised_value table are item level.
+    my $query = q(SELECT distinct authorised_value, kohafield
+                    FROM marc_subfield_structure
+                    WHERE kohafield like 'item%'
+                      AND authorised_value != '' );
+
+    my $itemlevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
+    my $iteminfo = GetItem( $itemnumber );
+    # warn( Data::Dumper->Dump( [ $itemlevel_authorised_values ], [ 'itemlevel_authorised_values' ] ) );
+    my $return;
+    foreach my $this_authorised_value ( keys %$itemlevel_authorised_values ) {
+        my $field = $itemlevel_authorised_values->{ $this_authorised_value }->{'kohafield'};
+        $field =~ s/^items\.//;
+        if ( exists $iteminfo->{ $field } ) {
+            $return->{ $this_authorised_value } = $iteminfo->{ $field };
+        }
+    }
+    # warn( Data::Dumper->Dump( [ $return ], [ 'return' ] ) );
+    return $return;
+}
+
+=head3 get_authorised_value_images
+
+  find a list of icons that are appropriate for display based on the
+  authorised values for a biblio.
+
+  parameters: listref of authorised values, such as comes from
+    get_item_ahtorised_values or
+    from C4::Biblio::get_biblio_authorised_values
+
+  returns: listref of hashrefs for each image. Each hashref looks like
+    this:
+
+      { imageurl => '/intranet-tmpl/prog/img/itemtypeimg/npl/WEB.gif',
+        label    => '',
+        category => '',
+        value    => '', }
+
+  Notes: Currently, I put on the full path to the images on the staff
+  side. This should either be configurable or not done at all. Since I
+  have to deal with 'intranet' or 'opac' in
+  get_biblio_authorised_values, perhaps I should be passing it in.
+
+=cut
+
+sub get_authorised_value_images {
+    my $authorised_values = shift;
+
+    my @imagelist;
+
+    my $authorised_value_list = GetAuthorisedValues();
+    # warn ( Data::Dumper->Dump( [ $authorised_value_list ], [ 'authorised_value_list' ] ) );
+    foreach my $this_authorised_value ( @$authorised_value_list ) {
+        if ( exists $authorised_values->{ $this_authorised_value->{'category'} }
+             && $authorised_values->{ $this_authorised_value->{'category'} } eq $this_authorised_value->{'authorised_value'} ) {
+            # warn ( Data::Dumper->Dump( [ $this_authorised_value ], [ 'this_authorised_value' ] ) );
+            if ( defined $this_authorised_value->{'imageurl'} ) {
+                push @imagelist, { imageurl => C4::Koha::getitemtypeimagesrc( 'intranet' ) . '/' . $this_authorised_value->{'imageurl'},
+                                   label    => $this_authorised_value->{'lib'},
+                                   category => $this_authorised_value->{'category'},
+                                   value    => $this_authorised_value->{'authorised_value'}, };
+            }
+        }
+    }
+
+    # warn ( Data::Dumper->Dump( [ \@imagelist ], [ 'imagelist' ] ) );
+    return \@imagelist;
+
+}
+
 =head1 LIMITED USE FUNCTIONS
 
 The following functions, while part of the public API,
index bab365e..4b597eb 100644 (file)
@@ -1481,6 +1481,7 @@ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
         $oldbiblio->{orderedcount}         = $ordered_count;
         $oldbiblio->{isbn} =~
           s/-//g;    # deleting - in isbn to enable amazon content
+        $oldbiblio->{'authorised_value_images'}  = C4::Items::get_authorised_value_images( C4::Biblio::get_biblio_authorised_values( $oldbiblio->{'biblionumber'} ) );
         push( @newresults, $oldbiblio );
     }
     return @newresults;
index 990a562..61a2d96 100644 (file)
@@ -298,6 +298,15 @@ $(window).load(function() {
                                         <!-- TMPL_IF name="cn_class" -->[<a href="/cgi-bin/koha/opac-search.pl?q=callnum:<!-- TMPL_VAR NAME="cn_class" ESCAPE="URL" -->"><!-- TMPL_VAR NAME="cn_class" --></a>]<!-- /TMPL_IF -->
                                     </p>
                                     <p class="searchhighlightblob"><!-- TMPL_VAR NAME="searchhighlightblob" --></p>
+
+                                    <!-- TMPL_IF NAME="authorised_value_images" -->
+                                <p>
+                                  <!-- TMPL_LOOP NAME="authorised_value_images" -->
+                                  <img src="<!-- TMPL_VAR name="imageurl" -->" />
+                                  <!-- /TMPL_LOOP -->
+                                </p>
+                                <!-- /TMPL_IF -->
+                                
                                 <!-- /TMPL_IF -->
                                   <!-- TMPL_IF NAME="norequests" -->
                                         No holds allowed
index 7f4d51f..11c5731 100755 (executable)
     <!-- /TMPL_IF -->
 <!-- /TMPL_IF -->
 
+        <!-- TMPL_IF NAME="authorised_value_images" -->
+        <div class="authorised_value_images">
+          <!-- TMPL_LOOP NAME="authorised_value_images" -->
+          <img src="<!-- TMPL_VAR name="imageurl" -->" alt="<!-- TMPL_VAR name="label" -->" title="<!-- TMPL_VAR name="label" -->">
+          <!-- /TMPL_LOOP -->
+        </div>
+        <!-- /TMPL_IF -->
+
        <!-- TMPL_IF NAME="TagsShowOnDetail" -->
         <div class="results_summary">
         <!-- TMPL_IF NAME="TagLoop" -->
                             <!-- TMPL_VAR NAME="notforloan" -->
                         <!-- TMPL_ELSE -->
                             <!-- TMPL_IF name="itemlost"-->
-                                Item lost
+                                <!-- TMPL_IF name="lostimageurl"--><img src="<!-- TMPL_VAR NAME="lostimageurl" -->" alt="<!-- TMPL_VAR NAME="lostimagelabel" -->" title="<!-- TMPL_VAR NAME="lostimagelabel" -->"><!-- TMPL_ELSE -->Item lost<!-- /TMPL_IF -->
                             <!-- TMPL_ELSE -->
                                 <!-- TMPL_IF NAME="wthdrawn" -->
                                     Item Cancelled
index 792d204..d881bba 100755 (executable)
@@ -118,6 +118,8 @@ if (C4::Context->preference("RequestOnOpac")) {
     $RequestOnOpac = 1;
 }
 
+my $biblio_authorised_value_images = C4::Items::get_authorised_value_images( C4::Biblio::get_biblio_authorised_values( $biblionumber ) );
+
 my $norequests = 1;
 my %itemfields;
 for my $itm (@items) {
@@ -139,29 +141,41 @@ for my $itm (@items) {
        $itemfields{ccode} = 1 if($itm->{ccode});
        $itemfields{enumchron} = 1 if($itm->{enumchron});
        $itemfields{copynumber} = 1 if($itm->{copynumber});
+
+     # walk through the item-level authorised values and populate some images
+     my $item_authorised_value_images = C4::Items::get_authorised_value_images( C4::Items::get_item_authorised_values( $itm->{'itemnumber'} ) );
+     # warn( Data::Dumper->Dump( [ $item_authorised_value_images ], [ 'item_authorised_value_images' ] ) );
+
+     if ( $itm->{'itemlost'} ) {
+         my $lostimageinfo = List::Util::first { $_->{'category'} eq 'LOST' } @$item_authorised_value_images;
+         $itm->{'lostimageurl'}   = $lostimageinfo->{ 'imageurl' };
+         $itm->{'lostimagelabel'} = $lostimageinfo->{ 'label' };
+     }
+
 }
 
 ## get notes and subjects from MARC record
-    my $dbh              = C4::Context->dbh;
-    my $marcflavour      = C4::Context->preference("marcflavour");
-    my $record           = GetMarcBiblio($biblionumber);
-    my $marcnotesarray   = GetMarcNotes   ($record,$marcflavour);
-    my $marcauthorsarray = GetMarcAuthors ($record,$marcflavour);
-    my $marcsubjctsarray = GetMarcSubjects($record,$marcflavour);
-    my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
-    my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
+my $dbh              = C4::Context->dbh;
+my $marcflavour      = C4::Context->preference("marcflavour");
+my $record           = GetMarcBiblio($biblionumber);
+my $marcnotesarray   = GetMarcNotes   ($record,$marcflavour);
+my $marcauthorsarray = GetMarcAuthors ($record,$marcflavour);
+my $marcsubjctsarray = GetMarcSubjects($record,$marcflavour);
+my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
+my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
 
     $template->param(
-        MARCNOTES   => $marcnotesarray,
-        MARCSUBJCTS => $marcsubjctsarray,
-        MARCAUTHORS => $marcauthorsarray,
-        MARCSERIES  => $marcseriesarray,
-        MARCURLS    => $marcurlsarray,
-               norequests => $norequests,
-               RequestOnOpac=>$RequestOnOpac,
-               itemdata_ccode => $itemfields{ccode},
-               itemdata_enumchron => $itemfields{enumchron},
-               itemdata_copynumber => $itemfields{copynumber},
+                     MARCNOTES               => $marcnotesarray,
+                     MARCSUBJCTS             => $marcsubjctsarray,
+                     MARCAUTHORS             => $marcauthorsarray,
+                     MARCSERIES              => $marcseriesarray,
+                     MARCURLS                => $marcurlsarray,
+                     norequests              => $norequests,
+                     RequestOnOpac           => $RequestOnOpac,
+                     itemdata_ccode          => $itemfields{ccode},
+                     itemdata_enumchron      => $itemfields{enumchron},
+                     itemdata_copynumber     => $itemfields{copynumber},
+                     authorised_value_images => $biblio_authorised_value_images,
     );
 
 foreach ( keys %{$dat} ) {