Bug 5917 : Fixing a bug with signedin users
[koha_gimpoz] / C4 / VirtualShelves.pm
index ec55a65..77ae394 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.
 #
 # 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 strict;
+use warnings;
+
 use Carp;
 use C4::Context;
 use C4::Circulation;
 use C4::Debug;
 use C4::Members;
 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);
 
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
 
@@ -36,21 +39,19 @@ BEGIN {
        require Exporter;
        @ISA    = qw(Exporter);
        @EXPORT = qw(
        require Exporter;
        @ISA    = qw(Exporter);
        @EXPORT = qw(
-        &GetShelves &GetShelfContents &GetShelf
-               &GetRecentShelves &GetShelvesSummary
-
-        &AddToShelf &AddToShelfFromBiblio &AddShelf
-
-               &SetShelvesLimit
-               &RefreshShelvesSummary
-
-        &ModShelf
-        &ShelfPossibleAction
-        &DelFromShelf &DelShelf
+            &GetShelves &GetShelfContents &GetShelf
+            &AddToShelf &AddShelf
+            &ModShelf
+            &ShelfPossibleAction
+            &DelFromShelf &DelShelf
+            &GetBibliosShelves
        );
        );
+        @EXPORT_OK = qw(
+            &GetShelvesSummary &GetRecentShelves
+            &RefreshShelvesSummary &SetShelvesLimit
+        );
 }
 
 }
 
-use C4::Auth qw(get_session);
 
 my $dbh = C4::Context->dbh;
 
 
 my $dbh = C4::Context->dbh;
 
@@ -66,13 +67,11 @@ C4::VirtualShelves - Functions for manipulating Koha virtual virtualshelves
 
 This module provides functions for manipulating virtual virtualshelves,
 including creating and deleting virtualshelves, and adding and removing
 
 This module provides functions for manipulating virtual virtualshelves,
 including creating and deleting virtualshelves, and adding and removing
-items to and from virtualshelves.
+bibs to and from virtualshelves.
 
 =head1 FUNCTIONS
 
 
 =head1 FUNCTIONS
 
-=over 2
-
-=item GetShelves
+=head2 GetShelves
 
   ($shelflist, $totshelves) = &GetShelves($mincategory, $row_count, $offset, $owner);
   ($shelfnumber, $shelfhash) = each %{$shelflist};
 
   ($shelflist, $totshelves) = &GetShelves($mincategory, $row_count, $offset, $owner);
   ($shelfnumber, $shelfhash) = each %{$shelflist};
@@ -84,7 +83,7 @@ When C<$mincategory> is 2 or 3, supply undef as argument for C<$owner>.
 C<$shelflist>is a reference-to-hash. The keys are the virtualshelves numbers (C<$shelfnumber>, above),
 and the values (C<$shelfhash>, above) are themselves references-to-hash, with the following keys:
 
 C<$shelflist>is a reference-to-hash. The keys are the virtualshelves numbers (C<$shelfnumber>, above),
 and the values (C<$shelfhash>, above) are themselves references-to-hash, with the following keys:
 
-=over 4
+=over
 
 =item C<$shelfhash-E<gt>{shelfname}>
 
 
 =item C<$shelfhash-E<gt>{shelfname}>
 
@@ -117,8 +116,7 @@ sub GetShelves ($$$$) {
     $query .= ($mincategory == 1) ? "WHERE  owner=? AND category=?" : "WHERE category>=?";
        $query .= qq(
         GROUP BY virtualshelves.shelfnumber
     $query .= ($mincategory == 1) ? "WHERE  owner=? AND category=?" : "WHERE category>=?";
        $query .= qq(
         GROUP BY virtualshelves.shelfnumber
-        ORDER BY virtualshelves.category
-               DESC 
+        ORDER BY virtualshelves.shelfname
                LIMIT ?, ?);
     my $sth2 = $dbh->prepare($query);
     $sth2->execute(@params);
                LIMIT ?, ?);
     my $sth2 = $dbh->prepare($query);
     $sth2->execute(@params);
@@ -127,6 +125,7 @@ sub GetShelves ($$$$) {
                $firstname,   $category,  $sortfield, $count ) = $sth2->fetchrow ) {
         $shelflist{$shelfnumber}->{'shelfname'} = $shelfname;
         $shelflist{$shelfnumber}->{'count'}     = $count;
                $firstname,   $category,  $sortfield, $count ) = $sth2->fetchrow ) {
         $shelflist{$shelfnumber}->{'shelfname'} = $shelfname;
         $shelflist{$shelfnumber}->{'count'}     = $count;
+        if($count eq 1){ $shelflist{$shelfnumber}->{'single'} = 1; }
         $shelflist{$shelfnumber}->{'sortfield'} = $sortfield;
         $shelflist{$shelfnumber}->{'category'}  = $category;
         $shelflist{$shelfnumber}->{'owner'}     = $owner;
         $shelflist{$shelfnumber}->{'sortfield'} = $sortfield;
         $shelflist{$shelfnumber}->{'category'}  = $category;
         $shelflist{$shelfnumber}->{'owner'}     = $owner;
@@ -136,7 +135,7 @@ sub GetShelves ($$$$) {
     return ( \%shelflist, $total );
 }
 
     return ( \%shelflist, $total );
 }
 
-=item GetShelvesSummary
+=head2 GetShelvesSummary
 
        ($shelves, $total) = GetShelvesSummary($mincategory, $row_count, $offset, $owner)
 
 
        ($shelves, $total) = GetShelvesSummary($mincategory, $row_count, $offset, $owner)
 
@@ -185,7 +184,7 @@ sub GetShelvesSummary ($$$$) {
        # 2|6|Josh Ferraro|51|en_fuego|106
 }
 
        # 2|6|Josh Ferraro|51|en_fuego|106
 }
 
-=item GetRecentShelves
+=head2 GetRecentShelves
 
        ($shelflist) = GetRecentShelves(1, $limit, $owner)
 
 
        ($shelflist) = GetRecentShelves(1, $limit, $owner)
 
@@ -203,9 +202,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
     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";
        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);
        $query .= " ORDER BY lastmodified DESC LIMIT ?, ?";
        my $sth = $dbh->prepare($query);
        $sth->execute(@params);
@@ -213,7 +212,7 @@ sub GetRecentShelves ($$$) {
        return ( \@shelflist, $total );
 }
 
        return ( \@shelflist, $total );
 }
 
-=item GetShelf
+=head2 GetShelf
 
   (shelfnumber,shelfname,owner,category,sortfield) = &GetShelf($shelfnumber);
 
 
   (shelfnumber,shelfname,owner,category,sortfield) = &GetShelf($shelfnumber);
 
@@ -236,9 +235,9 @@ sub GetShelf ($) {
     return $sth->fetchrow;
 }
 
     return $sth->fetchrow;
 }
 
-=item GetShelfContents
+=head2 GetShelfContents
 
 
-  $itemlist = &GetShelfContents($shelfnumber);
+  $biblist = &GetShelfContents($shelfnumber);
 
 Looks up information about the contents of virtual virtualshelves number
 C<$shelfnumber>.  Sorted by a field in the biblio table.  copyrightdate 
 
 Looks up information about the contents of virtual virtualshelves number
 C<$shelfnumber>.  Sorted by a field in the biblio table.  copyrightdate 
@@ -254,7 +253,7 @@ from C4::Circulation.
 
 =cut
 
 
 =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 = ?");
     my ($shelfnumber, $row_count, $offset, $sortfield) = @_;
     my $dbh=C4::Context->dbh();
        my $sth1 = $dbh->prepare("SELECT count(*) FROM virtualshelfcontents WHERE shelfnumber = ?");
@@ -266,8 +265,8 @@ sub GetShelfContents ($$;$$) {
                ($sortfield) = $sth2->fetchrow_array;
        }
     my $query =
                ($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, biblioitems.publishercode, biblioitems.place, biblioitems.size, biblioitems.pages
          FROM   virtualshelfcontents vc
                 LEFT JOIN biblio      ON      vc.biblionumber =      biblio.biblionumber
                 LEFT JOIN biblioitems ON  biblio.biblionumber = biblioitems.biblionumber
          FROM   virtualshelfcontents vc
                 LEFT JOIN biblio      ON      vc.biblionumber =      biblio.biblionumber
                 LEFT JOIN biblioitems ON  biblio.biblionumber = biblioitems.biblionumber
@@ -275,13 +274,14 @@ sub GetShelfContents ($$;$$) {
          WHERE  vc.shelfnumber=? ";
        my @params = ($shelfnumber);
        if($sortfield) {
          WHERE  vc.shelfnumber=? ";
        my @params = ($shelfnumber);
        if($sortfield) {
-               $query .= " ORDER BY ? ";
+               $query .= " ORDER BY " . $sortfield;
                $query .= " DESC " if ($sortfield eq 'copyrightdate');
                $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);
     my $sth3 = $dbh->prepare($query);
        $sth3->execute(@params);
        return ($sth3->fetchall_arrayref({}), $total);
@@ -293,7 +293,7 @@ sub GetShelfContents ($$;$$) {
        # or newer, for your version of DBI.
 }
 
        # or newer, for your version of DBI.
 }
 
-=item AddShelf
+=head2 AddShelf
 
   $shelfnumber = &AddShelf( $shelfname, $owner, $category);
 
 
   $shelfnumber = &AddShelf( $shelfname, $owner, $category);
 
@@ -327,12 +327,12 @@ sub AddShelf {
     return ($shelfnumber);
 }
 
     return ($shelfnumber);
 }
 
-=item AddToShelf
+=head2 AddToShelf
 
   &AddToShelf($biblionumber, $shelfnumber);
 
 
   &AddToShelf($biblionumber, $shelfnumber);
 
-Adds item number C<$biblionumber> to virtual virtualshelves number
-C<$shelfnumber>, unless that item is already on that shelf.
+Adds bib number C<$biblionumber> to virtual virtualshelves number
+C<$shelfnumber>, unless that bib is already on that shelf.
 
 =cut
 
 
 =cut
 
@@ -364,43 +364,7 @@ sub AddToShelf {
        $sth->execute( $shelfnumber );
 }
 
        $sth->execute( $shelfnumber );
 }
 
-=item AddToShelfFromBiblio
-    &AddToShelfFromBiblio($biblionumber, $shelfnumber)
-
-    this function allow to add a virtual into the shelf number $shelfnumber
-    from biblionumber.
-
-=cut
-
-sub AddToShelfFromBiblio {
-    my ( $biblionumber, $shelfnumber ) = @_;
-    return unless $biblionumber;
-    my $query = qq(
-        SELECT *
-        FROM   virtualshelfcontents
-        WHERE  shelfnumber=? AND biblionumber=?
-    );
-    my $sth = $dbh->prepare($query);
-    $sth->execute( $shelfnumber, $biblionumber );
-    unless ( $sth->rows ) {
-        my $query =qq(
-            INSERT INTO virtualshelfcontents
-                (shelfnumber, biblionumber, flags)
-            VALUES
-                (?, ?, 0)
-        );
-        $sth = $dbh->prepare($query);
-        $sth->execute( $shelfnumber, $biblionumber );
-               $query = qq(UPDATE virtualshelves
-                                       SET lastmodified = CURRENT_TIMESTAMP
-                                       WHERE shelfnumber = ?);
-               $sth = $dbh->prepare($query);
-               $sth->execute( $shelfnumber );
-    }
-}
-
-=item ModShelf
+=head2 ModShelf
 
 ModShelf($shelfnumber, $hashref)
 
 
 ModShelf($shelfnumber, $hashref)
 
@@ -449,7 +413,7 @@ sub ModShelf {
        $sth->execute( @bind_params );
 }
 
        $sth->execute( @bind_params );
 }
 
-=item ShelfPossibleAction
+=head2 ShelfPossibleAction
 
 ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
 
 
 ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
 
@@ -473,6 +437,7 @@ sub ShelfPossibleAction {
     $sth->execute($shelfnumber);
     my ( $owner, $category ) = $sth->fetchrow;
        my $borrower = GetMemberDetails($user);
     $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
        return 1 if ( $category >= 3);                                                  # open list
     return 1 if (($category >= 2) and
                                defined($action) and $action eq 'view');        # public list, anybody can view
@@ -481,12 +446,12 @@ sub ShelfPossibleAction {
     return 0;
 }
 
     return 0;
 }
 
-=item DelFromShelf
+=head2 DelFromShelf
 
   &DelFromShelf( $biblionumber, $shelfnumber);
 
 
   &DelFromShelf( $biblionumber, $shelfnumber);
 
-Removes item number C<$biblionumber> from virtual virtualshelves number
-C<$shelfnumber>. If the item wasn't on that virtualshelves to begin with,
+Removes bib number C<$biblionumber> from virtual virtualshelves number
+C<$shelfnumber>. If the bib wasn't on that virtualshelves to begin with,
 nothing happens.
 
 =cut
 nothing happens.
 
 =cut
@@ -502,7 +467,7 @@ sub DelFromShelf {
     $sth->execute( $shelfnumber, $biblionumber );
 }
 
     $sth->execute( $shelfnumber, $biblionumber );
 }
 
-=item DelShelf (old version)
+=head2 DelShelf (old version)
 
   ($status, $msg) = &DelShelf($shelfnumber);
 
 
   ($status, $msg) = &DelShelf($shelfnumber);
 
@@ -513,7 +478,7 @@ Returns a two-element array, where C<$status> is 0 if the operation
 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
 success, or an error message giving the reason for failure.
 
 was successful, or non-zero otherwise. C<$msg> is "Done" in case of
 success, or an error message giving the reason for failure.
 
-=item DelShelf (current version)
+=head2 DelShelf (current version)
 
   $Number = DelShelf($shelfnumber);
 
 
   $Number = DelShelf($shelfnumber);
 
@@ -530,7 +495,27 @@ sub DelShelf {
        return $sth->execute(shift);
 }
 
        return $sth->execute(shift);
 }
 
-=item RefreshShelvesSummary
+=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);
 
 
        ($total, $pubshelves, $barshelves) = RefreshShelvesSummary($sessionID, $loggedinuser, $row_count);
 
@@ -546,7 +531,7 @@ This function is used in conjunction with the 'Lists' button in masthead.inc.
 sub RefreshShelvesSummary ($$$) {
        
        my ($sessionID, $loggedinuser, $row_count) = @_;
 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);
        my ($total, $totshelves, $barshelves, $pubshelves);
 
        ($barshelves, $totshelves) = GetRecentShelves(1, $row_count, $loggedinuser);
@@ -555,13 +540,13 @@ sub RefreshShelvesSummary ($$$) {
        $total->{'pubtotal'} = $totshelves;
 
        # Update the current session with the latest shelves...
        $total->{'pubtotal'} = $totshelves;
 
        # Update the current session with the latest shelves...
-       $session->param('barshelves', ${@$barshelves}[0]);
-       $session->param('pubshelves', ${@$pubshelves}[0]);
+       $session->param('barshelves', $barshelves->[0]);
+       $session->param('pubshelves', $pubshelves->[0]);
        $session->param('totshelves', $total);
 
        # likewise the userenv...
        $session->param('totshelves', $total);
 
        # likewise the userenv...
-       C4::Context->set_shelves_userenv('bar',${@$barshelves}[0]);
-       C4::Context->set_shelves_userenv('pub',${@$pubshelves}[0]);
+       C4::Context->set_shelves_userenv('bar',$barshelves->[0]);
+       C4::Context->set_shelves_userenv('pub',$pubshelves->[0]);
        C4::Context::set_shelves_userenv('tot',$total);
 
        return ($total, $pubshelves, $barshelves);
        C4::Context::set_shelves_userenv('tot',$total);
 
        return ($total, $pubshelves, $barshelves);
@@ -581,15 +566,33 @@ sub _shelf_count ($$) {
        return $total;
 }
 
        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__
 
 1;
 
 __END__
 
-=back
-
 =head1 AUTHOR
 
 =head1 AUTHOR
 
-Koha Developement team <info@koha.org>
+Koha Development Team <http://koha-community.org/>
 
 =head1 SEE ALSO
 
 
 =head1 SEE ALSO