start of release notes for 3.2
[koha.git] / C4 / VirtualShelves.pm
index eaebb54..fc11fc9 100644 (file)
@@ -17,16 +17,19 @@ package C4::VirtualShelves;
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 use strict;
+use warnings;
+
 use Carp;
 use C4::Context;
 use C4::Circulation;
 use C4::Debug;
 use C4::Members;
+require C4::Auth;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
 
@@ -41,6 +44,7 @@ BEGIN {
             &ModShelf
             &ShelfPossibleAction
             &DelFromShelf &DelShelf
+            &GetBibliosShelves
        );
         @EXPORT_OK = qw(
             &GetShelvesSummary &GetRecentShelves
@@ -48,7 +52,6 @@ BEGIN {
         );
 }
 
-use C4::Auth qw(get_session);
 
 my $dbh = C4::Context->dbh;
 
@@ -201,9 +204,9 @@ sub GetRecentShelves ($$$) {
     my (@shelflist);
        my $total = _shelf_count($owner, $mincategory);
        my @params = ($owner, $mincategory, 0, $row_count);      #FIXME: offset is hardcoded here, but could be passed in for enhancements
-       shift @params if !$owner;
+       shift @params if (not defined $owner);
        my $query = "SELECT * FROM virtualshelves";
-       $query .= ($owner ? " WHERE owner = ? AND category = ?" : " WHERE category >= ? ");
+       $query .= ((defined $owner) ? " WHERE owner = ? AND category = ?" : " WHERE category >= ? ");
        $query .= " ORDER BY lastmodified DESC LIMIT ?, ?";
        my $sth = $dbh->prepare($query);
        $sth->execute(@params);
@@ -252,7 +255,7 @@ from C4::Circulation.
 
 =cut
 
-sub GetShelfContents ($$;$$) {
+sub GetShelfContents ($;$$$) {
     my ($shelfnumber, $row_count, $offset, $sortfield) = @_;
     my $dbh=C4::Context->dbh();
        my $sth1 = $dbh->prepare("SELECT count(*) FROM virtualshelfcontents WHERE shelfnumber = ?");
@@ -264,8 +267,8 @@ sub GetShelfContents ($$;$$) {
                ($sortfield) = $sth2->fetchrow_array;
        }
     my $query =
-       " SELECT vc.biblionumber, vc.shelfnumber, vc.dateadded,
-                               biblio.*, biblioitems.itemtype, itemtypes.*
+       " SELECT vc.biblionumber, vc.shelfnumber, vc.dateadded, itemtypes.*,
+                               biblio.*, biblioitems.itemtype, biblioitems.publicationyear
          FROM   virtualshelfcontents vc
                 LEFT JOIN biblio      ON      vc.biblionumber =      biblio.biblionumber
                 LEFT JOIN biblioitems ON  biblio.biblionumber = biblioitems.biblionumber
@@ -273,13 +276,14 @@ sub GetShelfContents ($$;$$) {
          WHERE  vc.shelfnumber=? ";
        my @params = ($shelfnumber);
        if($sortfield) {
-               $query .= " ORDER BY ? ";
+               $query .= " ORDER BY " . $sortfield;
                $query .= " DESC " if ($sortfield eq 'copyrightdate');
-               push (@params, $sortfield);
        }
-       $query .= " LIMIT ?, ? ";
-       push (@params, ($offset ? $offset : 0));
-       push (@params, $row_count);
+    if($row_count){
+          $query .= " LIMIT ?, ? ";
+          push (@params, ($offset ? $offset : 0));
+          push (@params, $row_count);
+    }
     my $sth3 = $dbh->prepare($query);
        $sth3->execute(@params);
        return ($sth3->fetchall_arrayref({}), $total);
@@ -471,6 +475,7 @@ sub ShelfPossibleAction {
     $sth->execute($shelfnumber);
     my ( $owner, $category ) = $sth->fetchrow;
        my $borrower = GetMemberDetails($user);
+       return 0 if not defined($user);
        return 1 if ( $category >= 3);                                                  # open list
     return 1 if (($category >= 2) and
                                defined($action) and $action eq 'view');        # public list, anybody can view
@@ -528,6 +533,26 @@ sub DelShelf {
        return $sth->execute(shift);
 }
 
+=item 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({});
+}
+
 =item RefreshShelvesSummary
 
        ($total, $pubshelves, $barshelves) = RefreshShelvesSummary($sessionID, $loggedinuser, $row_count);
@@ -544,7 +569,7 @@ This function is used in conjunction with the 'Lists' button in masthead.inc.
 sub RefreshShelvesSummary ($$$) {
        
        my ($sessionID, $loggedinuser, $row_count) = @_;
-       my $session = get_session($sessionID);
+       my $session = C4::Auth::get_session($sessionID);
        my ($total, $totshelves, $barshelves, $pubshelves);
 
        ($barshelves, $totshelves) = GetRecentShelves(1, $row_count, $loggedinuser);
@@ -579,6 +604,26 @@ sub _shelf_count ($$) {
        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;
+}
+
 1;
 
 __END__