+=head2 GetBibShelves
+
+This finds all the public lists that this bib record is in.
+
+=cut
+
+sub GetBibliosShelves {
+ my ( $biblionumber ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare('
+ SELECT vs.shelfname, vs.shelfnumber
+ FROM virtualshelves vs
+ JOIN virtualshelfcontents vc ON (vs.shelfnumber= vc.shelfnumber)
+ WHERE vs.category != 1
+ AND vc.biblionumber= ?
+ ');
+ $sth->execute( $biblionumber );
+ return $sth->fetchall_arrayref({});
+}
+
+=head2 RefreshShelvesSummary
+
+ ($total, $pubshelves, $barshelves) = RefreshShelvesSummary($sessionID, $loggedinuser, $row_count);
+
+Updates the current session and userenv with the most recent shelves
+
+Returns the total number of shelves stored in the session/userenv along with two references each to an
+array of hashes, one containing the C<$loggedinuser>'s private shelves and one containing all public/open shelves.
+
+This function is used in conjunction with the 'Lists' button in masthead.inc.
+
+=cut
+
+sub RefreshShelvesSummary ($$$) {
+ require C4::Auth;
+ my ($sessionID, $loggedinuser, $row_count) = @_;
+ my $session = C4::Auth::get_session($sessionID);
+ my ($total, $totshelves, $barshelves, $pubshelves);
+
+ ($barshelves, $totshelves) = GetRecentShelves(1, $row_count, $loggedinuser);
+ $total->{'bartotal'} = $totshelves;
+ ($pubshelves, $totshelves) = GetRecentShelves(2, $row_count, undef);
+ $total->{'pubtotal'} = $totshelves;
+
+ # Update the current session with the latest shelves...
+ $session->param('barshelves', $barshelves);
+ $session->param('pubshelves', $pubshelves);
+ $session->param('totshelves', $total);
+
+ # likewise the userenv...
+ C4::Context->set_shelves_userenv('bar',$barshelves);
+ C4::Context->set_shelves_userenv('pub',$pubshelves);
+ C4::Context::set_shelves_userenv('tot',$total);
+
+ return ($total, $pubshelves, $barshelves);
+}
+
+# internal subs
+
+sub _shelf_count ($$) {
+ my (@params) = @_;
+ # Find out how many shelves total meet the submitted criteria...
+ my $query = "SELECT count(*) FROM virtualshelves";
+ $query .= ($params[1] > 1) ? " WHERE category >= ?" : " WHERE owner=? AND category=?";
+ shift @params if $params[1] > 1;
+ my $sth = $dbh->prepare($query);
+ $sth->execute(@params);
+ my $total = $sth->fetchrow;
+ return $total;
+}
+
+sub _biblionumber_sth {
+ my ($shelf) = @_;
+ my $query = 'select biblionumber from virtualshelfcontents where shelfnumber = ?';
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare($query)
+ or die $dbh->errstr;
+ $sth->execute( $shelf )
+ or die $sth->errstr;
+ $sth;
+}
+
+sub each_biblionumbers (&$) {
+ my ($code,$shelf) = @_;
+ my $ref = _biblionumber_sth($shelf)->fetchall_arrayref;
+ map {
+ $_=$$_[0];
+ $code->();
+ } @$ref;
+}
+