+sub GetAuthvalueDropbox {
+ my ( $authcat, $default ) = @_;
+ my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
+ my $dbh = C4::Context->dbh;
+
+ my $query = qq{
+ SELECT *
+ FROM authorised_values
+ };
+ $query .= qq{
+ LEFT JOIN authorised_values_branches ON ( id = av_id )
+ } if $branch_limit;
+ $query .= qq{
+ WHERE category = ?
+ };
+ $query .= " AND ( branchcode = ? OR branchcode IS NULL )" if $branch_limit;
+ $query .= " GROUP BY lib ORDER BY category, lib, lib_opac";
+ my $sth = $dbh->prepare($query);
+ $sth->execute( $authcat, $branch_limit ? $branch_limit : () );
+
+
+ my $option_list = [];
+ my @authorised_values = ( q{} );
+ while (my $av = $sth->fetchrow_hashref) {
+ push @{$option_list}, {
+ value => $av->{authorised_value},
+ label => $av->{lib},
+ default => ($default eq $av->{authorised_value}),
+ };
+ }
+
+ if ( @{$option_list} ) {
+ return $option_list;
+ }
+ return;
+}
+
+
+=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];
+ # chose a random id within that range if there is more than one quote
+ my $offset = int(rand($range));
+ # grab it
+ $query = 'SELECT * FROM quotes ORDER BY id LIMIT 1 OFFSET ?';
+ $sth = C4::Context->dbh->prepare($query);
+ # see http://www.perlmonks.org/?node_id=837422 for why
+ # we're being verbose and using bind_param
+ $sth->bind_param(1, $offset, SQL_INTEGER);
+ $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( dt_from_string() ),
+ $quote->{'id'}
+ );
+ }
+ return $quote;
+}
+