Backend for "Session" Shelves in toolbar. Affects Auth and Context, so please test.
authorJoe Atzberger <joe.atzberger@liblime.com>
Thu, 24 Jan 2008 03:16:18 +0000 (21:16 -0600)
committerJoshua Ferraro <jmf@liblime.com>
Thu, 24 Jan 2008 03:31:20 +0000 (21:31 -0600)
Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
C4/Auth.pm
C4/Context.pm
C4/VirtualShelves.pm
koha-tmpl/opac-tmpl/prog/en/includes/masthead.inc

index 43ded3e..d5b14d7 100755 (executable)
@@ -31,12 +31,13 @@ use C4::Output;    # to get the template
 use C4::Members;
 use C4::Koha;
 use C4::Branch; # GetBranches
+use C4::VirtualShelves 3.02 qw(GetShelvesSummary);
 
 # use utf8;
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug $ldap);
 
 BEGIN {
-    $VERSION = 3.01;        # set version for version checking
+    $VERSION = 3.02;        # set version for version checking
     $debug = $ENV{DEBUG} || 0 ;
     @ISA   = qw(Exporter);
     @EXPORT    = qw(&checkauth &get_template_and_user);
@@ -145,6 +146,11 @@ sub get_template_and_user {
         # user info
         $template->param( loggedinusername => $user );
         $template->param( sessionID        => $sessionID );
+               my $shelves;
+               if ($shelves = C4::Context->get_shelves_userenv()) {
+               $template->param( barshelves     => scalar (@$shelves));
+               $template->param( barshelvesloop => $shelves);
+               }
 
         $borrowernumber = getborrowernumber($user);
         my ( $borr, $alternativeflags ) =
@@ -316,6 +322,7 @@ sub get_template_and_user {
             'item-level_itypes' => C4::Context->preference('item-level_itypes'),
         );
     }
+       $template->param(listloop=>[{shelfname=>"Freelist", shelfnumber=>110}]);
     return ( $template, $borrowernumber, $cookie, $flags);
 }
 
@@ -427,7 +434,7 @@ sub _session_log {
 
 sub checkauth {
     my $query = shift;
-  # warn "Checking Auth";
+       $debug and warn "Checking Auth";
     # $authnotrequired will be set for scripts which will run without authentication
     my $authnotrequired = shift;
     my $flagsrequired   = shift;
@@ -446,7 +453,7 @@ sub checkauth {
     # state variables
     my $loggedin = 0;
     my %info;
-    my ( $userid, $cookie, $sessionID, $flags );
+    my ( $userid, $cookie, $sessionID, $flags, $shelves );
     my $logout = $query->param('logout.x');
     if ( $userid = $ENV{'REMOTE_USER'} ) {
         # Using Basic Authentication, no cookies required
@@ -469,6 +476,7 @@ sub checkauth {
                 $session->param('branchname'),   $session->param('flags'),
                 $session->param('emailaddress'), $session->param('branchprinter')
             );
+            C4::Context::set_shelves_userenv($session->param('shelves'));
             $debug and printf STDERR "AUTH_SESSION: (%s)\t%s %s - %s\n", map {$session->param($_)} qw(cardnumber firstname surname branch) ;
             $ip       = $session->param('ip');
             $lasttime = $session->param('lasttime');
@@ -532,11 +540,11 @@ sub checkauth {
                 $info{'nopermission'} = 1;
                 C4::Context->_unset_userenv($sessionID);
             }
+
+                       my ($borrowernumber, $firstname, $surname, $userflags,
+                               $branchcode, $branchname, $branchprinter, $emailaddress);
+
             if ( $return == 1 ) {
-                my (
-                   $borrowernumber, $firstname, $surname, $userflags,
-                   $branchcode, $branchname, $branchprinter, $emailaddress
-                );
                 my $select = "
                 SELECT borrowernumber, firstname, surname, flags, borrowers.branchcode, 
                         branches.branchname    as branchname, 
@@ -610,10 +618,11 @@ sub checkauth {
                 $session->param('emailaddress',$emailaddress);
                 $session->param('ip',$session->remote_addr());
                 $session->param('lasttime',time());
-                $debug and printf STDERR "AUTH_3: (%s)\t%s %s - %s\n", map {$session->param($_)} qw(cardnumber firstname surname branch) ;
+                $debug and printf STDERR "AUTH_4: (%s)\t%s %s - %s\n", map {$session->param($_)} qw(cardnumber firstname surname branch) ;
             }
             elsif ( $return == 2 ) {
                 #We suppose the user is the superlibrarian
+                               $borrowernumber = 0;
                 $session->param('number',0);
                 $session->param('id',C4::Context->config('user'));
                 $session->param('cardnumber',C4::Context->config('user'));
@@ -633,6 +642,9 @@ sub checkauth {
                 $session->param('branchname'),   $session->param('flags'),
                 $session->param('emailaddress'), $session->param('branchprinter')
             );
+                       $shelves = GetShelvesSummary($borrowernumber,2,10);
+                       $session->param('shelves', $shelves);
+                       C4::Context::set_shelves_userenv($shelves);
         }
         else {
             if ($userid) {
index ca7316b..361b6dc 100644 (file)
@@ -295,6 +295,7 @@ sub new {
     $self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield
     $self->{"userenv"} = undef;        # User env
     $self->{"activeuser"} = undef;        # current active user
+    $self->{"shelves"} = undef;
 
     bless $self, $class;
     return $self;
@@ -874,6 +875,22 @@ sub set_userenv{
     return $cell;
 }
 
+sub set_shelves_userenv ($) {
+       my $lists = shift or return undef;
+       my $activeuser = $context->{activeuser} or return undef;
+       $context->{userenv}->{$activeuser}->{shelves} = $lists;
+       # die "set_shelves_userenv: $lists";
+}
+sub get_shelves_userenv () {
+       my $active;
+       unless ($active = $context->{userenv}->{$context->{activeuser}}) {
+               warn "get_shelves_userenv cannot retrieve context->{userenv}->{context->{activeuser}}";
+               return undef;
+       }
+       my $lists = $active->{shelves} or return undef;#  die "get_shelves_userenv: activeenv has no ->{shelves}";
+       return $lists;
+}
+
 =item _new_userenv
 
   C4::Context->_new_userenv($session);
index 1af3603..f075c6f 100644 (file)
@@ -25,11 +25,11 @@ use strict;
 use Carp;
 use C4::Context;
 use C4::Circulation;
-use vars qw($VERSION @ISA @EXPORT);
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
 
 BEGIN {
        # set the version for version checking
-       $VERSION = 3.01;
+       $VERSION = 3.02;
        require Exporter;
        @ISA    = qw(Exporter);
        @EXPORT = qw(
@@ -41,6 +41,7 @@ BEGIN {
         &ShelfPossibleAction
         &DelFromShelf &DelShelf
        );
+       @EXPORT_OK = qw(&GetShelvesSummary);
 }
 
 my $dbh = C4::Context->dbh;
@@ -65,7 +66,9 @@ items to and from virtualshelves.
 
 =item GetShelves
 
+  $shelflist = &GetShelves($owner);
   $shelflist = &GetShelves($owner, $mincategory);
+  $shelflist = &GetShelves($owner, $mincategory, $limit);
   ($shelfnumber, $shelfhash) = each %{$shelflist};
 
 Looks up the virtual virtualshelves, and returns a summary. C<$shelflist>
@@ -73,7 +76,7 @@ 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<mincategory> : 2 if the list is for "look". 3 if the list is for "Select virtualshelves for adding a virtual".
+C<mincategory> : 2 if the list is for "Public", 3 for "Open".
 virtualshelves of the owner are always selected, whatever the category
 
 =over 4
@@ -90,14 +93,10 @@ The number of virtuals on that virtualshelves.
 
 =cut
 
-#'
-# FIXME - Wouldn't it be more intuitive to return a list, rather than
-# a reference-to-hash? The shelf number can be just another key in the
-# hash.
-
 sub GetShelves {
-    my ( $owner, $mincategory ) = @_;
-
+    my ($owner, $mincategory, $limit) = @_;
+       ($mincategory and $mincategory =~ /^\d+$/) or $mincategory = 2;
+       (      $limit and       $limit =~ /^\d+$/) or $limit = undef;
     my $query = qq(
         SELECT virtualshelves.shelfnumber, virtualshelves.shelfname,owner,surname,firstname,virtualshelves.category,virtualshelves.sortfield,
                count(virtualshelfcontents.biblionumber) as count
@@ -108,6 +107,7 @@ sub GetShelves {
         GROUP BY virtualshelves.shelfnumber
         ORDER BY virtualshelves.category, virtualshelves.shelfname, borrowers.firstname, borrowers.surname
     );
+       $limit and $query .= " LIMIT $limit ";
     my $sth = $dbh->prepare($query);
     $sth->execute( $owner, $mincategory );
     my %shelflist;
@@ -121,15 +121,44 @@ sub GetShelves {
     {
         $shelflist{$shelfnumber}->{'shelfname'} = $shelfname;
         $shelflist{$shelfnumber}->{'count'}     = $count;
-        $shelflist{$shelfnumber}->{'sortfield'}     = $sortfield;
+        $shelflist{$shelfnumber}->{'sortfield'} = $sortfield;
         $shelflist{$shelfnumber}->{'category'}  = $category;
         $shelflist{$shelfnumber}->{'owner'}     = $owner;
-        $shelflist{$shelfnumber}->{'surname'}     = $surname;
-        $shelflist{$shelfnumber}->{'firstname'}   = $firstname;
+        $shelflist{$shelfnumber}->{'surname'}   = $surname;
+        $shelflist{$shelfnumber}->{'firstname'} = $firstname;
     }
     return ( \%shelflist );
 }
 
+sub GetShelvesSummary {
+    my ($owner, $mincategory, $limit) = @_;
+       ($mincategory and $mincategory =~ /^\d+$/) or $mincategory = 2;
+       (      $limit and       $limit =~ /^\d+$/) or $limit = 10;
+    my $query = qq(
+               SELECT
+                       virtualshelves.shelfnumber,
+                       virtualshelves.shelfname,
+                       owner,
+                       CONCAT(firstname, ' ', surname) AS name,
+                       virtualshelves.category,
+                       count(virtualshelfcontents.biblionumber) AS count
+               FROM   virtualshelves
+                       LEFT JOIN  virtualshelfcontents ON virtualshelves.shelfnumber = virtualshelfcontents.shelfnumber
+                       LEFT JOIN             borrowers ON virtualshelves.owner = borrowers.borrowernumber
+               WHERE  owner=? OR category>=?
+               GROUP BY virtualshelves.shelfnumber
+               ORDER BY virtualshelves.category, borrowers.surname, borrowers.firstname, virtualshelves.shelfname
+               LIMIT ?
+       );
+       my $sth = $dbh->prepare($query);
+       $sth->execute($owner,$mincategory,$limit);
+    return $sth->fetchall_arrayref({});
+       # Probably NOT the final implementation since it is still bulky (repeated hash keys).
+       # might like an array of rows of delimited values:
+       # 1|2||0|blacklist|112
+       # 2|6|Josh Ferraro|51|en_fuego|106
+}
+
 =item GetShelf
 
   (shelfnumber,shelfname,owner,category) = &GetShelf($shelfnumber);
index f6365b8..d8432a9 100644 (file)
@@ -9,7 +9,25 @@
 
 <div id="members">
        <ul>
-       <!--TMPL_IF NAME="virtualshelves" --><li><a href="/cgi-bin/koha/opac-shelves.pl">Lists</a></li><!-- /TMPL_IF --><!-- TMPL_IF NAME="loggedinusername" --><li><a href="/cgi-bin/koha/opac-user.pl">Your Account</a></li><!-- /TMPL_IF --><!-- TMPL_IF NAME="loggedinusername" --><li><span class="members">Logged in as <span class="loggedinusername"><!-- TMPL_LOOP NAME="USER_INFO" --><!-- TMPL_VAR NAME="title" --> <!-- TMPL_VAR NAME="firstname" --> <!-- TMPL_VAR NAME="surname" --><!-- /TMPL_LOOP --></span></span></li><li><a class="logout" href="/cgi-bin/koha/opac-main.pl?logout.x=1">Log Out</a></li><!-- TMPL_ELSE --><li><a href="/cgi-bin/koha/opac-user.pl">Your Account</a></li><!-- /TMPL_IF -->
+       <!-- TMPL_IF NAME="virtualshelves" -->
+               <li><a href="/cgi-bin/koha/opac-shelves.pl">Lists</a>
+               <!-- TMPL_IF NAME="FIXME_barshelves" --><--! FIXME: Owen, you can take it from here!  Make it "barshelves" for proof/demo. -->
+                       <ul>
+                       <!-- TMPL_LOOP NAME="barshelvesloop" -->
+                       <li><a href="/cgi-bin/koha/opac-shelves.pl?viewshelf=<!-- TMPL_VAR NAME="shelfnumber" -->">
+                               <!-- TMPL_VAR NAME="shelfname" -->
+                       </a></li>
+                       <!-- /TMPL_LOOP -->
+                       </ul>
+               <!-- /TMPL_IF -->
+               </li>
+       <!-- /TMPL_IF -->
+       <!-- TMPL_IF NAME="loggedinusername" --><li><a href="/cgi-bin/koha/opac-user.pl">Your Account</a></li><!-- /TMPL_IF -->
+       <!-- TMPL_IF NAME="loggedinusername" -->
+       <li><span class="members">Logged in as <span class="loggedinusername"><!-- TMPL_LOOP NAME="USER_INFO" --><!-- TMPL_VAR NAME="title" --> <!-- TMPL_VAR NAME="firstname" --> <!-- TMPL_VAR NAME="surname" --><!-- /TMPL_LOOP --></span></span></li>
+       <li><a class="logout" href="/cgi-bin/koha/opac-main.pl?logout.x=1">Log Out</a></li>
+       <!-- TMPL_ELSE --><li><a href="/cgi-bin/koha/opac-user.pl">Your Account</a></li>
+       <!-- /TMPL_IF -->
        </ul>   
 </div>