X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FKoha.pm;h=c23a3b0c31420cc128dfe59e031e085715b4b281;hb=1e8423167ec7bcffe9851d237230c27cd2f32a2c;hp=95244bd91efe7969f082ddbd78f933b2302bab08;hpb=f098258e8cf2d616c6bbc35e44c6a1df47be8517;p=koha_fer diff --git a/C4/Koha.pm b/C4/Koha.pm index 95244bd91e..c23a3b0c31 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -22,16 +22,18 @@ package C4::Koha; use strict; #use warnings; FIXME - Bug 2505 + use C4::Context; -use C4::Output; -use URI::Split qw(uri_split); + use Memoize; -use Business::ISBN; +use DateTime; +use DateTime::Format::MySQL; +use autouse 'Data::Dumper' => qw(Dumper); -use vars qw($VERSION @ISA @EXPORT $DEBUG); +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( @@ -70,6 +72,7 @@ BEGIN { $DEBUG ); $DEBUG = 0; +@EXPORT_OK = qw( GetDailyQuote ); } # expensive functions @@ -455,7 +458,7 @@ sub getitemtypeimagedir { if ($src eq 'intranet') { return C4::Context->config('intrahtdocs') . '/' .C4::Context->preference('template') . '/img/itemtypeimg'; } else { - return C4::Context->config('opachtdocs') . '/' . C4::Context->preference('template') . '/itemtypeimg'; + return C4::Context->config('opachtdocs') . '/' . C4::Context->preference('opacthemes') . '/itemtypeimg'; } } @@ -464,7 +467,7 @@ sub getitemtypeimagesrc { if ($src eq 'intranet') { return '/intranet-tmpl' . '/' . C4::Context->preference('template') . '/img/itemtypeimg'; } else { - return '/opac-tmpl' . '/' . C4::Context->preference('template') . '/itemtypeimg'; + return '/opac-tmpl' . '/' . C4::Context->preference('opacthemes') . '/itemtypeimg'; } } @@ -472,8 +475,9 @@ sub getitemtypeimagelocation($$) { my ( $src, $image ) = @_; return '' if ( !$image ); + require URI::Split; - my $scheme = ( uri_split( $image ) )[0]; + my $scheme = ( URI::Split::uri_split( $image ) )[0]; return $image if ( $scheme ); @@ -573,9 +577,8 @@ sub getImageSets { my @imagesets = (); # list of hasrefs of image set data to pass to template my @subdirectories = _getSubdirectoryNames( $paths->{'staff'}{'filesystem'} ); -warn $paths->{'staff'}{'filesystem'}; foreach my $imagesubdir ( @subdirectories ) { - warn $imagesubdir; + warn $imagesubdir if $DEBUG; my @imagelist = (); # hashrefs of image info my @imagenames = _getImagesFromDirectory( File::Spec->catfile( $paths->{'staff'}{'filesystem'}, $imagesubdir ) ); my $imagesetactive = 0; @@ -671,6 +674,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; @@ -681,95 +685,95 @@ sub getFacets { if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) { $facets = [ { - link_value => 'su-to', - label_value => 'Topics', - tags => - [ '600', '601', '602', '603', '604', '605', '606', '610' ], - subfield => 'a', + idx => 'su-to', + label => 'Topics', + tags => [ qw/ 600a 601a 602a 603a 604a 605a 606ax 610a/ ], + sep => ' - ', }, { - link_value => 'su-geo', - label_value => 'Places', - tags => ['651'], - subfield => 'a', + idx => 'su-geo', + label => 'Places', + tags => [ qw/ 651a / ], + sep => ' - ', }, { - link_value => 'su-ut', - label_value => 'Titles', - tags => [ '500', '501', '502', '503', '504', ], - subfield => 'a', + idx => 'su-ut', + label => 'Titles', + tags => [ qw/ 500a 501a 502a 503a 504a / ], + sep => ', ', }, { - link_value => 'au', - label_value => 'Authors', - tags => [ '700', '701', '702', ], - subfield => 'a', + idx => 'au', + label => 'Authors', + tags => [ qw/ 700ab 701ab 702ab / ], + sep => ', ', }, { - link_value => 'se', - label_value => 'Series', - tags => ['225'], - subfield => 'a', + idx => 'se', + label => 'Series', + tags => [ qw/ 225a / ], + sep => ', ', }, - ]; - - my $library_facet; - - $library_facet = { - link_value => 'branch', - label_value => 'Libraries', - tags => [ '995', ], - subfield => 'b', - expanded => '1', - }; - push @$facets, $library_facet unless C4::Context->preference("singleBranchMode"); + ]; + my $library_facet = { + idx => 'branch', + label => 'Libraries', + tags => [ qw/ 995b / ], + expanded => '1', + }; + push @$facets, $library_facet unless C4::Context->preference("singleBranchMode"); } else { $facets = [ { - link_value => 'su-to', - label_value => 'Topics', - tags => ['650'], - subfield => 'a', + idx => 'su-to', + label => 'Topics', + tags => [ qw/ 650a / ], + sep => '--', }, - # { - # link_value => 'su-na', - # label_value => 'People and Organizations', - # tags => ['600', '610', '611'], - # subfield => 'a', + # idx => 'su-na', + # label => 'People and Organizations', + # tags => [ qw/ 600a 610a 611a / ], + # sep => 'a', # }, { - link_value => 'su-geo', - label_value => 'Places', - tags => ['651'], - subfield => 'a', + idx => 'su-geo', + label => 'Places', + tags => [ qw/ 651a / ], + sep => '--', }, { - link_value => 'su-ut', - label_value => 'Titles', - tags => ['630'], - subfield => 'a', + idx => 'su-ut', + label => 'Titles', + tags => [ qw/ 630a / ], + sep => '--', }, { - link_value => 'au', - label_value => 'Authors', - tags => [ '100', '110', '700', ], - subfield => 'a', + idx => 'au', + label => 'Authors', + tags => [ qw/ 100a 110a 700a / ], + sep => ', ', }, { - link_value => 'se', - label_value => 'Series', - tags => [ '440', '490', ], - subfield => 'a', + idx => 'se', + label => 'Series', + tags => [ qw/ 440a 490a / ], + sep => ', ', + }, + { + idx => 'itype', + label => 'ItemTypes', + tags => [ qw/ 952y 942c / ], + sep => ', ', }, ]; my $library_facet; $library_facet = { - link_value => 'branch', - label_value => 'Libraries', - tags => [ '952', ], - subfield => 'b', + idx => 'branch', + label => 'Libraries', + tags => [ qw/ 952b / ], + sep => ', ', expanded => '1', }; push @$facets, $library_facet unless C4::Context->preference("singleBranchMode"); @@ -1017,22 +1021,25 @@ C<$opac> If set to a true value, displays OPAC descriptions rather than normal o sub GetAuthorisedValues { my ($category,$selected,$opac) = @_; - my @results; + 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 $sth = $dbh->prepare($query); $sth->execute; - while (my $data=$sth->fetchrow_hashref) { - if ($selected && $selected eq $data->{'authorised_value'} ) { - $data->{'selected'} = 1; - } - if ($opac && $data->{'lib_opac'}) { - $data->{'lib'} = $data->{'lib_opac'}; - } - push @results, $data; - } + while (my $data=$sth->fetchrow_hashref) { + if ( (defined($selected)) && ($selected eq $data->{'authorised_value'}) ) { + $data->{'selected'} = 1; + } + else { + $data->{'selected'} = 0; + } + if ($opac && $data->{'lib_opac'}) { + $data->{'lib'} = $data->{'lib_opac'}; + } + push @results, $data; + } #my $data = $sth->fetchall_arrayref({}); return \@results; #$data; } @@ -1067,13 +1074,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 }; } } @@ -1203,21 +1211,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; } } @@ -1225,7 +1234,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; @@ -1237,10 +1246,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 { @@ -1248,10 +1257,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 { @@ -1259,28 +1268,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; } } @@ -1290,7 +1298,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'); @@ -1302,8 +1313,78 @@ sub GetNormalizedOCLCNumber { } } } - else { # TODO: add UNIMARC fields +} + +=head2 GetDailyQuote($opts) + +Takes a hashref of options + +Currently supported options are: + +'id' An exact quote id +'random' Select a random quote +noop When no option is passed in, this sub will return the quote timestamped for the current day + +The function returns an anonymous hash following this format: + + { + 'source' => 'source-of-quote', + 'timestamp' => 'timestamp-value', + 'text' => 'text-of-quote', + 'id' => 'quote-id' + }; + +=cut + +# This is definitely a candidate for some sort of caching once we finally settle caching/persistence issues... +# at least for default option + +sub GetDailyQuote { + my %opts = @_; + my $dbh = C4::Context->dbh; + my $query = ''; + my $sth = undef; + my $quote = undef; + if ($opts{'id'}) { + $query = 'SELECT * FROM quotes WHERE id = ?'; + $sth = $dbh->prepare($query); + $sth->execute($opts{'id'}); + $quote = $sth->fetchrow_hashref(); + } + elsif ($opts{'random'}) { + # Fall through... we also return a random quote as a catch-all if all else fails + } + else { + $query = 'SELECT * FROM quotes WHERE timestamp LIKE CONCAT(CURRENT_DATE,\'%\') ORDER BY timestamp DESC LIMIT 0,1'; + $sth = $dbh->prepare($query); + $sth->execute(); + $quote = $sth->fetchrow_hashref(); + } + unless ($quote) { # if there are not matches, choose a random quote + # get a list of all available quote ids + $sth = C4::Context->dbh->prepare('SELECT count(*) FROM quotes;'); + $sth->execute; + my $range = ($sth->fetchrow_array)[0]; + if ($range > 1) { + # chose a random id within that range if there is more than one quote + my $id = int(rand($range)); + # grab it + $query = 'SELECT * FROM quotes WHERE id = ?;'; + $sth = C4::Context->dbh->prepare($query); + $sth->execute($id); + } + else { + $query = 'SELECT * FROM quotes;'; + $sth = C4::Context->dbh->prepare($query); + $sth->execute(); + } + $quote = $sth->fetchrow_hashref(); + # update the timestamp for that quote + $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?'; + $sth = C4::Context->dbh->prepare($query); + $sth->execute(DateTime::Format::MySQL->format_datetime(DateTime->now), $quote->{'id'}); } + return $quote; } sub _normalize_match_point { @@ -1315,6 +1396,7 @@ sub _normalize_match_point { } sub _isbn_cleanup { + require Business::ISBN; my $isbn = Business::ISBN->new( $_[0] ); if ( $isbn ) { $isbn = $isbn->as_isbn10 if $isbn->type eq 'ISBN13';