use Memoize because Memcache::Memoize is slow for me
[koha.git] / C4 / Koha.pm
index 2bdcc28..06b2ec5 100644 (file)
@@ -2,6 +2,7 @@ package C4::Koha;
 
 # Copyright 2000-2002 Katipo Communications
 # Parts Copyright 2010 Nelsonville Public Library
+# Parts copyright 2010 BibLibre
 #
 # This file is part of Koha.
 #
@@ -57,6 +58,8 @@ BEGIN {
                &GetKohaAuthorisedValues
                &GetKohaAuthorisedValuesFromField
     &GetKohaAuthorisedValueLib
+    &GetAuthorisedValueByCode
+    &GetKohaImageurlFromAuthorisedValues
                &GetAuthValCode
                &GetNormalizedUPC
                &GetNormalizedISBN
@@ -941,6 +944,25 @@ sub displayServers {
     return \@primaryserverloop;
 }
 
+
+=head2 GetKohaImageurlFromAuthorisedValues
+
+$authhorised_value = GetKohaImageurlFromAuthorisedValues( $category, $authvalcode );
+
+Return the first url of the authorised value image represented by $lib.
+
+=cut
+
+sub GetKohaImageurlFromAuthorisedValues {
+    my ( $category, $lib ) = @_;
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare("SELECT imageurl FROM authorised_values WHERE category=? AND lib =?");
+    $sth->execute( $category, $lib );
+    while ( my $data = $sth->fetchrow_hashref ) {
+        return $data->{'imageurl'};
+    }
+}
+
 =head2 GetAuthValCode
 
   $authvalcode = GetAuthValCode($kohafield,$frameworkcode);
@@ -1029,12 +1051,32 @@ sub GetAuthorisedValueCategories {
     my $sth = $dbh->prepare("SELECT DISTINCT category FROM authorised_values ORDER BY category");
     $sth->execute;
     my @results;
-    while (my $category = $sth->fetchrow_array) {
+    while (defined (my $category  = $sth->fetchrow_array) ) {
         push @results, $category;
     }
     return \@results;
 }
 
+=head2 GetAuthorisedValueByCode
+
+$authhorised_value = GetAuthorisedValueByCode( $category, $authvalcode );
+
+Return the lib attribute from authorised_values from the row identified
+by the passed category and code
+
+=cut
+
+sub GetAuthorisedValueByCode {
+    my ( $category, $authvalcode ) = @_;
+
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare("SELECT lib FROM authorised_values WHERE category=? AND authorised_value =?");
+    $sth->execute( $category, $authvalcode );
+    while ( my $data = $sth->fetchrow_hashref ) {
+        return $data->{'lib'};
+    }
+}
+
 =head2 GetKohaAuthorisedValues
 
 Takes $kohafield, $fwcode as parameters.
@@ -1161,21 +1203,22 @@ sub GetNormalizedUPC {
  my ($record,$marcflavour) = @_;
     my (@fields,$upc);
 
-    if ($marcflavour eq 'MARC21') {
-        @fields = $record->field('024');
+    if ($marcflavour eq 'UNIMARC') {
+        @fields = $record->field('072');
         foreach my $field (@fields) {
-            my $indicator = $field->indicator(1);
             my $upc = _normalize_match_point($field->subfield('a'));
-            if ($indicator == 1 and $upc ne '') {
+            if ($upc ne '') {
                 return $upc;
             }
         }
+
     }
-    else { # assume unimarc if not marc21
-        @fields = $record->field('072');
+    else { # assume marc21 if not unimarc
+        @fields = $record->field('024');
         foreach my $field (@fields) {
+            my $indicator = $field->indicator(1);
             my $upc = _normalize_match_point($field->subfield('a'));
-            if ($upc ne '') {
+            if ($indicator == 1 and $upc ne '') {
                 return $upc;
             }
         }
@@ -1195,10 +1238,10 @@ sub GetNormalizedISBN {
     }
     return undef unless $record;
 
-    if ($marcflavour eq 'MARC21') {
-        @fields = $record->field('020');
+    if ($marcflavour eq 'UNIMARC') {
+        @fields = $record->field('010');
         foreach my $field (@fields) {
-            $isbn = $field->subfield('a');
+            my $isbn = $field->subfield('a');
             if ($isbn) {
                 return _isbn_cleanup($isbn);
             } else {
@@ -1206,10 +1249,10 @@ sub GetNormalizedISBN {
             }
         }
     }
-    else { # assume unimarc if not marc21
-        @fields = $record->field('010');
+    else { # assume marc21 if not unimarc
+        @fields = $record->field('020');
         foreach my $field (@fields) {
-            my $isbn = $field->subfield('a');
+            $isbn = $field->subfield('a');
             if ($isbn) {
                 return _isbn_cleanup($isbn);
             } else {
@@ -1217,28 +1260,27 @@ sub GetNormalizedISBN {
             }
         }
     }
-
 }
 
 sub GetNormalizedEAN {
     my ($record,$marcflavour) = @_;
     my (@fields,$ean);
 
-    if ($marcflavour eq 'MARC21') {
-        @fields = $record->field('024');
+    if ($marcflavour eq 'UNIMARC') {
+        @fields = $record->field('073');
         foreach my $field (@fields) {
-            my $indicator = $field->indicator(1);
             $ean = _normalize_match_point($field->subfield('a'));
-            if ($indicator == 3 and $ean ne '') {
+            if ($ean ne '') {
                 return $ean;
             }
         }
     }
-    else { # assume unimarc if not marc21
-        @fields = $record->field('073');
+    else { # assume marc21 if not unimarc
+        @fields = $record->field('024');
         foreach my $field (@fields) {
+            my $indicator = $field->indicator(1);
             $ean = _normalize_match_point($field->subfield('a'));
-            if ($ean ne '') {
+            if ($indicator == 3 and $ean ne '') {
                 return $ean;
             }
         }
@@ -1248,7 +1290,10 @@ sub GetNormalizedOCLCNumber {
     my ($record,$marcflavour) = @_;
     my (@fields,$oclc);
 
-    if ($marcflavour eq 'MARC21') {
+    if ($marcflavour eq 'UNIMARC') {
+        # TODO: add UNIMARC fields
+    }
+    else { # assume marc21 if not unimarc
         @fields = $record->field('035');
         foreach my $field (@fields) {
             $oclc = $field->subfield('a');
@@ -1260,8 +1305,6 @@ sub GetNormalizedOCLCNumber {
             }
         }
     }
-    else { # TODO: add UNIMARC fields
-    }
 }
 
 sub _normalize_match_point {