Bug 8508 - Holds to Pull : Library dropdown options are erroneously concatenated...
[koha.git] / C4 / Koha.pm
index ebb3eed..6869ab0 100644 (file)
@@ -24,7 +24,7 @@ use strict;
 #use warnings; FIXME - Bug 2505
 
 use C4::Context;
-
+use C4::Branch qw(GetBranchesCount);
 use Memoize;
 use DateTime;
 use DateTime::Format::MySQL;
@@ -33,7 +33,7 @@ use autouse 'Data::Dumper' => qw(Dumper);
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEBUG);
 
 BEGIN {
-       $VERSION = 3.01;
+    $VERSION = 3.07.00.049;
        require Exporter;
        @ISA    = qw(Exporter);
        @EXPORT = qw(
@@ -63,6 +63,7 @@ BEGIN {
     &GetAuthorisedValueByCode
     &GetKohaImageurlFromAuthorisedValues
                &GetAuthValCode
+        &AddAuthorisedValue
                &GetNormalizedUPC
                &GetNormalizedISBN
                &GetNormalizedEAN
@@ -112,7 +113,7 @@ sub slashifyDate {
 }
 
 # FIXME.. this should be moved to a MARC-specific module
-sub subfield_is_koha_internal_p ($) {
+sub subfield_is_koha_internal_p {
     my ($subfield) = @_;
 
     # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
@@ -425,20 +426,22 @@ sub getframeworkinfo {
 
 =head2 getitemtypeinfo
 
-  $itemtype = &getitemtype($itemtype);
+  $itemtype = &getitemtypeinfo($itemtype, [$interface]);
 
-Returns information about an itemtype.
+Returns information about an itemtype. The optional $interface argument
+sets which interface ('opac' or 'intranet') to return the imageurl for.
+Defaults to intranet.
 
 =cut
 
 sub getitemtypeinfo {
-    my ($itemtype) = @_;
+    my ($itemtype, $interface) = @_;
     my $dbh        = C4::Context->dbh;
     my $sth        = $dbh->prepare("select * from itemtypes where itemtype=?");
     $sth->execute($itemtype);
     my $res = $sth->fetchrow_hashref;
 
-    $res->{imageurl} = getitemtypeimagelocation( 'intranet', $res->{imageurl} );
+    $res->{imageurl} = getitemtypeimagelocation( ( ( defined $interface && $interface eq 'opac' ) ? 'opac' : 'intranet' ), $res->{imageurl} );
 
     return $res;
 }
@@ -471,7 +474,7 @@ sub getitemtypeimagesrc {
        }
 }
 
-sub getitemtypeimagelocation($$) {
+sub getitemtypeimagelocation {
        my ( $src, $image ) = @_;
 
        return '' if ( !$image );
@@ -630,7 +633,7 @@ sub GetPrinters {
 
 =cut
 
-sub GetPrinter ($$) {
+sub GetPrinter {
     my ( $query, $printers ) = @_;    # get printer for this query from printers
     my $printer = $query->param('printer');
     my %cookie = $query->cookie('userenv');
@@ -674,6 +677,7 @@ sub getallthemes {
     opendir D, "$htdocs";
     my @dirlist = readdir D;
     foreach my $directory (@dirlist) {
+        next if $directory eq 'lib';
         -d "$htdocs/$directory/en" and push @themes, $directory;
     }
     return @themes;
@@ -686,26 +690,26 @@ sub getFacets {
             {
                 idx   => 'su-to',
                 label => 'Topics',
-                tags  => [ qw/ 600a 601a 602a 603a 604a 605a 606ax 610a/ ],
+                tags  => [ qw/ 600ab 601ab 602a 604at 605a 606ax 610a / ],
                 sep   => ' - ',
             },
             {
                 idx   => 'su-geo',
                 label => 'Places',
-                tags  => [ qw/ 651a / ],
+                tags  => [ qw/ 607a / ],
                 sep   => ' - ',
             },
             {
                 idx   => 'su-ut',
                 label => 'Titles',
-                tags  => [ qw/ 500a 501a 502a 503a 504a / ],
+                tags  => [ qw/ 500a 501a 503a / ],
                 sep   => ', ',
             },
             {
                 idx   => 'au',
                 label => 'Authors',
                 tags  => [ qw/ 700ab 701ab 702ab / ],
-                sep   => ', ',
+                sep   => C4::Context->preference("UNIMARCAuthorsFacetsSeparator"),
             },
             {
                 idx   => 'se',
@@ -713,14 +717,23 @@ sub getFacets {
                 tags  => [ qw/ 225a / ],
                 sep   => ', ',
             },
-        ];
-        my $library_facet = {
-            idx   => 'branch',
-            label => 'Libraries',
-            tags  => [ qw/ 995b / ],
-            expanded => '1',
-        };
-        push @$facets, $library_facet unless C4::Context->preference("singleBranchMode");
+            ];
+
+            my $library_facet;
+            unless ( C4::Context->preference("singleBranchMode") || GetBranchesCount() == 1 ) {
+                $library_facet = {
+                    idx  => 'branch',
+                    label => 'Libraries',
+                    tags        => [ qw/ 995b / ],
+                };
+            } else {
+                $library_facet = {
+                    idx  => 'location',
+                    label => 'Location',
+                    tags        => [ qw/ 995c / ],
+                };
+            }
+            push( @$facets, $library_facet );
     }
     else {
         $facets = [
@@ -760,16 +773,29 @@ sub getFacets {
                 tags  => [ qw/ 440a 490a / ],
                 sep   => ', ',
             },
+            {
+                idx   => 'itype',
+                label => 'ItemTypes',
+                tags  => [ qw/ 952y 942c / ],
+                sep   => ', ',
+            },
             ];
+
             my $library_facet;
-            $library_facet = {
-                idx   => 'branch',
-                label => 'Libraries',
-                tags  => [ qw/ 952b / ],
-                sep   => ', ',
-                expanded    => '1',
-            };
-            push @$facets, $library_facet unless C4::Context->preference("singleBranchMode");
+            unless ( C4::Context->preference("singleBranchMode") || GetBranchesCount() == 1 ) {
+                $library_facet = {
+                    idx  => 'branch',
+                    label => 'Libraries',
+                    tags        => [ qw / 952b / ],
+                };
+            } else {
+                $library_facet = {
+                    idx => 'location',
+                    label => 'Location',
+                    tags => [ qw / 952c / ],
+                };
+            }
+            push( @$facets, $library_facet );
     }
     return $facets;
 }
@@ -886,13 +912,13 @@ like:
 
     {
       'checked'    => 'checked',
-      'encoding'   => 'MARC-8'
+      'encoding'   => 'utf8',
       'icon'       => undef,
       'id'         => 'LIBRARY OF CONGRESS',
       'label'      => '',
       'name'       => 'server',
       'opensearch' => '',
-      'value'      => 'z3950.loc.gov:7090/',
+      'value'      => 'lx2.loc.gov:210/',
       'zed'        => 1,
     },
 
@@ -1013,28 +1039,50 @@ C<$opac> If set to a true value, displays OPAC descriptions rather than normal o
 =cut
 
 sub GetAuthorisedValues {
-    my ($category,$selected,$opac) = @_;
+    my ( $category, $selected, $opac ) = @_;
+    my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
     my @results;
     my $dbh      = C4::Context->dbh;
-    my $query    = "SELECT * FROM authorised_values";
-    $query .= " WHERE category = '" . $category . "'" if $category;
-    $query .= " ORDER BY category, lib, lib_opac";
+    my $query = qq{
+        SELECT *
+        FROM authorised_values
+    };
+    $query .= qq{
+          LEFT JOIN authorised_values_branches ON ( id = av_id )
+    } if $branch_limit;
+    my @where_strings;
+    my @where_args;
+    if($category) {
+        push @where_strings, "category = ?";
+        push @where_args, $category;
+    }
+    if($branch_limit) {
+        push @where_strings, "( branchcode = ? OR branchcode IS NULL )";
+        push @where_args, $branch_limit;
+    }
+    if(@where_strings > 0) {
+        $query .= " WHERE " . join(" AND ", @where_strings);
+    }
+    $query .= " GROUP BY lib ORDER BY category, lib, lib_opac";
+
     my $sth = $dbh->prepare($query);
-    $sth->execute;
+
+    $sth->execute( @where_args );
     while (my $data=$sth->fetchrow_hashref) {
-        if ( (defined($selected)) && ($selected eq $data->{'authorised_value'}) ) {
-            $data->{'selected'} = 1;
+        if ( defined $selected and $selected eq $data->{authorised_value} ) {
+            $data->{selected} = 1;
         }
         else {
-            $data->{'selected'} = 0;
+            $data->{selected} = 0;
         }
-        if ($opac && $data->{'lib_opac'}) {
-            $data->{'lib'} = $data->{'lib_opac'};
+
+        if ($opac && $data->{lib_opac}) {
+            $data->{lib} = $data->{lib_opac};
         }
         push @results, $data;
     }
-    #my $data = $sth->fetchall_arrayref({});
-    return \@results; #$data;
+    $sth->finish;
+    return \@results;
 }
 
 =head2 GetAuthorisedValueCategories
@@ -1067,13 +1115,14 @@ by the passed category and code
 =cut
 
 sub GetAuthorisedValueByCode {
-    my ( $category, $authvalcode ) = @_;
+    my ( $category, $authvalcode, $opac ) = @_;
 
+    my $field = $opac ? 'lib_opac' : 'lib';
     my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("SELECT lib FROM authorised_values WHERE category=? AND authorised_value =?");
+    my $sth = $dbh->prepare("SELECT $field FROM authorised_values WHERE category=? AND authorised_value =?");
     $sth->execute( $category, $authvalcode );
     while ( my $data = $sth->fetchrow_hashref ) {
-        return $data->{'lib'};
+        return $data->{ $field };
     }
 }
 
@@ -1103,7 +1152,7 @@ sub GetKohaAuthorisedValues {
        }
        return \%values;
   } else {
-       return undef;
+       return;
   }
 }
 
@@ -1134,7 +1183,7 @@ sub GetKohaAuthorisedValuesFromField {
        }
        return \%values;
   } else {
-       return undef;
+       return;
   }
 }
 
@@ -1178,6 +1227,26 @@ sub GetKohaAuthorisedValueLib {
   return $value;
 }
 
+=head2 AddAuthorisedValue
+
+    AddAuthorisedValue($category, $authorised_value, $lib, $lib_opac, $imageurl);
+
+Create a new authorised value.
+
+=cut
+
+sub AddAuthorisedValue {
+    my ($category, $authorised_value, $lib, $lib_opac, $imageurl) = @_;
+
+    my $dbh = C4::Context->dbh;
+    my $query = qq{
+        INSERT INTO authorised_values (category, authorised_value, lib, lib_opac, imageurl)
+        VALUES (?,?,?,?,?)
+    };
+    my $sth = $dbh->prepare($query);
+    $sth->execute($category, $authorised_value, $lib, $lib_opac, $imageurl);
+}
+
 =head2 display_marc_indicators
 
   my $display_form = C4::Koha::display_marc_indicators($field);
@@ -1226,7 +1295,7 @@ sub GetNormalizedUPC {
 }
 
 # Normalizes and returns the first valid ISBN found in the record
-# ISBN13 are converted into ISBN10. This is required to get Amazon cover book.
+# ISBN13 are converted into ISBN10. This is required to get some book cover images.
 sub GetNormalizedISBN {
     my ($isbn,$record,$marcflavour) = @_;
     my @fields;
@@ -1236,7 +1305,7 @@ sub GetNormalizedISBN {
         $isbn =~ s/(.*)( \| )(.*)/$1/;
         return _isbn_cleanup($isbn);
     }
-    return undef unless $record;
+    return unless $record;
 
     if ($marcflavour eq 'UNIMARC') {
         @fields = $record->field('010');
@@ -1245,7 +1314,7 @@ sub GetNormalizedISBN {
             if ($isbn) {
                 return _isbn_cleanup($isbn);
             } else {
-                return undef;
+                return;
             }
         }
     }
@@ -1256,7 +1325,7 @@ sub GetNormalizedISBN {
             if ($isbn) {
                 return _isbn_cleanup($isbn);
             } else {
-                return undef;
+                return;
             }
         }
     }
@@ -1301,7 +1370,7 @@ sub GetNormalizedOCLCNumber {
                 $oclc =~ s/\(OCoLC\)//;
                 return $oclc;
             } else {
-                return undef;
+                return;
             }
         }
     }