X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FKoha.pm;h=4f386e469d5cc7a6123ef742f1f360b54d97bb93;hb=3ce542ce2668770a5669d2fb98b666f42a5bd388;hp=a028937b1a08424bc83891071fac46352418f476;hpb=9ec7ac5b8e036ad824636df5acd4e1ec2ebb45c8;p=srvgit diff --git a/C4/Koha.pm b/C4/Koha.pm index a028937b1a..4f386e469d 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -22,13 +22,18 @@ package C4::Koha; use strict; #use warnings; FIXME - Bug 2505 + use C4::Context; + use Memoize; +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( @@ -67,6 +72,7 @@ BEGIN { $DEBUG ); $DEBUG = 0; +@EXPORT_OK = qw( GetDailyQuote ); } # expensive functions @@ -754,6 +760,12 @@ sub getFacets { tags => [ qw/ 440a 490a / ], sep => ', ', }, + { + idx => 'itype', + label => 'ItemTypes', + tags => [ qw/ 952y 942c / ], + sep => ', ', + }, ]; my $library_facet; $library_facet = { @@ -1301,6 +1313,78 @@ sub GetNormalizedOCLCNumber { } } +=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 { my $match_point = shift; (my $normalized_match_point) = $match_point =~ /([\d-]*[X]*)/;