[Koha-cvs] koha bookshelves/addbookbybiblionumber.pl books... [rel_3_0]

Antoine Farnault antoine at koha-fr.org
Wed Aug 30 17:59:14 CEST 2006


CVSROOT:	/sources/koha
Module name:	koha
Branch:		rel_3_0
Changes by:	Antoine Farnault <toins>	06/08/30 15:59:14

Modified files:
	bookshelves    : addbookbybiblionumber.pl shelves.pl 
	C4             : BookShelves.pm 

Log message:
	Code cleaned according to coding guide lines.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/koha/bookshelves/addbookbybiblionumber.pl?cvsroot=koha&only_with_tag=rel_3_0&r1=1.4&r2=1.4.2.1
http://cvs.savannah.gnu.org/viewcvs/koha/bookshelves/shelves.pl?cvsroot=koha&only_with_tag=rel_3_0&r1=1.9&r2=1.9.2.1
http://cvs.savannah.gnu.org/viewcvs/koha/C4/BookShelves.pm?cvsroot=koha&only_with_tag=rel_3_0&r1=1.15&r2=1.15.8.1

Patches:
Index: bookshelves/addbookbybiblionumber.pl
===================================================================
RCS file: /sources/koha/koha/bookshelves/addbookbybiblionumber.pl,v
retrieving revision 1.4
retrieving revision 1.4.2.1
diff -u -b -r1.4 -r1.4.2.1
--- bookshelves/addbookbybiblionumber.pl	4 Jul 2006 14:36:51 -0000	1.4
+++ bookshelves/addbookbybiblionumber.pl	30 Aug 2006 15:59:14 -0000	1.4.2.1
@@ -1,8 +1,7 @@
 #!/usr/bin/perl
+
 #script to provide bookshelf management
-# WARNING: This file uses 4-character tabs!
 #
-# $Header: /sources/koha/koha/bookshelves/addbookbybiblionumber.pl,v 1.4 2006/07/04 14:36:51 toins Exp $
 #
 # Copyright 2000-2002 Katipo Communications
 #
@@ -21,6 +20,41 @@
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
+# $Id: addbookbybiblionumber.pl,v 1.4.2.1 2006/08/30 15:59:14 toins Exp $
+
+=head1 NAME
+
+    addbookbybiblionumber.pl
+
+=head1 DESCRIPTION
+
+    This script allow to add a book in a virtual shelf from a biblionumber.
+
+=head1 CGI PARAMETERS
+
+=over 4
+
+=item biblionumber
+
+    The biblionumber
+
+=item shelfnumber
+
+    the shelfnumber where to add the book.
+
+=item newbookshelf
+
+    if this parameter exists, then it must be equals to the name of the shelf
+    to add.
+
+=item category
+
+    if this script has to add a shelf, it add one with this category.
+
+=back
+
+=cut
+
 use strict;
 use C4::Search;
 use C4::Biblio;
@@ -32,7 +66,10 @@
 use C4::Interface::CGI::Output;
 use HTML::Template;
 
-my $env;
+#use it only to debug !
+use CGI::Carp qw/fatalsToBrowser/;
+use warnings;
+
 my $query = new CGI;
 my $biblionumber = $query->param('biblionumber');
 my $shelfnumber = $query->param('shelfnumber');
@@ -47,18 +84,16 @@
 							flagsrequired => {catalogue => 1},
 						});
 
-my $x; # for trash
-($x,$x,$shelfnumber) = AddShelf('',$newbookshelf,$loggedinuser,$category) if $newbookshelf;
+$shelfnumber = AddShelf($newbookshelf,$loggedinuser,$category) if $newbookshelf;
 
-if ($shelfnumber) {
-	&AddToShelfFromBiblio($env, $biblionumber, $shelfnumber);
+if ($shelfnumber && ($shelfnumber != -1)) { # this shelf doesn't already exist.
+    &AddToShelfFromBiblio($biblionumber, $shelfnumber);
 	print "Content-Type: text/html\n\n<html><body onload=\"window.close()\"></body></html>";
 	exit;
-} else {
-
+} else {    # this shelf already exist.
 	my  ( $bibliocount, @biblios )  = getbiblio($biblionumber);
 
-	my ($shelflist) = GetShelfList($loggedinuser,3);
+    my ($shelflist) = GetShelves($loggedinuser,3);
 	my @shelvesloop;
 	my %shelvesloop;
 	foreach my $element (sort keys %$shelflist) {
@@ -66,14 +101,16 @@
 			$shelvesloop{$element} = $shelflist->{$element}->{'shelfname'};
 	}
 
-	my $CGIbookshelves=CGI::scrolling_list( -name     => 'shelfnumber',
+    my $CGIbookshelves=CGI::scrolling_list(
+                -name     => 'shelfnumber',
 				-values   => \@shelvesloop,
 				-labels   => \%shelvesloop,
 				-size     => 1,
 	 			-tabindex=>'',
 				-multiple => 0 );
 
-	$template->param(biblionumber => $biblionumber,
+    $template->param(
+                biblionumber => $biblionumber,
 						title => $biblios[0]->{'title'},
 						author => $biblios[0]->{'author'},
 						CGIbookshelves => $CGIbookshelves,
@@ -84,7 +121,11 @@
 
 	output_html_with_http_headers $query, $cookie, $template->output;
 }
+
 # $Log: addbookbybiblionumber.pl,v $
+# Revision 1.4.2.1  2006/08/30 15:59:14  toins
+# Code cleaned according to coding guide lines.
+#
 # Revision 1.4  2006/07/04 14:36:51  toins
 # Head & rel_2_2 merged
 #
@@ -98,23 +139,6 @@
 # Revision 1.3.2.2  2006/02/05 21:45:25  kados
 # Adds support for intranetstylesheet system pref in Koha scripts
 #
-# Revision 1.3.2.1  2006/02/04 21:26:47  kados
-# Adds support for intranetcolorstylesheet
-#
-# Revision 1.3  2004/12/15 17:28:22  tipaul
-# adding bookshelf features :
-# * create bookshelf on the fly
-# * modify a bookshelf (this being not finished, will commit the rest soon)
-#
-# Revision 1.2  2004/11/19 16:31:30  tipaul
-# bugfix for bookshelves not in official CVS
-#
-# Revision 1.1.2.2  2004/03/10 15:08:18  tipaul
-# modifying shelves : introducing category of shelf : private, public, free for all
-#
-# Revision 1.1.2.1  2004/02/19 10:14:36  tipaul
-# new feature : adding book to bookshelf from biblio detail screen.
-#
 
 # Local Variables:
 # tab-width: 4

Index: bookshelves/shelves.pl
===================================================================
RCS file: /sources/koha/koha/bookshelves/shelves.pl,v
retrieving revision 1.9
retrieving revision 1.9.2.1
diff -u -b -r1.9 -r1.9.2.1
--- bookshelves/shelves.pl	4 Jul 2006 14:36:51 -0000	1.9
+++ bookshelves/shelves.pl	30 Aug 2006 15:59:14 -0000	1.9.2.1
@@ -1,8 +1,5 @@
 #!/usr/bin/perl
-#script to provide bookshelf management
-# WARNING: This file uses 4-character tabs!
-#
-# $Header: /sources/koha/koha/bookshelves/shelves.pl,v 1.9 2006/07/04 14:36:51 toins Exp $
+
 #
 # Copyright 2000-2002 Katipo Communications
 #
@@ -21,6 +18,52 @@
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
+
+=head1 NAME
+
+    shelves.pl
+
+=head1 DESCRIPTION
+
+    this script is used to script to provide bookshelf management
+
+=head1 CGI PARAMETERS
+
+=over 4
+
+=item C<modifyshelfcontents>
+
+    if this script has to modify the shelve content.
+
+=item C<shelfnumber>
+
+    to know on which shelve this script has to work.
+
+=item C<addbarcode>
+
+=item C<op>
+
+    op can be equals to:
+        * modifsave to save change on the shelves
+        * modif to change the template to allow to modify the shelves.
+
+=item C<viewshelf>
+
+    to load the template with 'viewshelves param' which allow to read the shelves information.
+
+=item C<shelves>
+
+    if equals to 1. then call the function shelves which add
+    or delete a shelf.
+
+=item C<addshelf>
+
+	if the param shelves = 1 then addshelf must be equals to the name of the shelf to add.
+
+=back
+
+=cut
+
 use strict;
 use C4::Search;
 use CGI;
@@ -31,13 +74,8 @@
 use C4::Interface::CGI::Output;
 use HTML::Template;
 
-my $env;
 my $query = new CGI;
-my $headerbackgroundcolor='#663266';
-my $circbackgroundcolor='#555555';
-my $circbackgroundcolor='#550000';
-my $linecolor1='#bbbbbb';
-my $linecolor2='#dddddd';
+
 my ($template, $loggedinuser, $cookie)
     = get_template_and_user({template_name => "bookshelves/shelves.tmpl",
 							query => $query,
@@ -49,28 +87,30 @@
 if ($query->param('modifyshelfcontents')) {
 	my $shelfnumber=$query->param('shelfnumber');
 	my $barcode=$query->param('addbarcode');
-	my ($item) = getiteminformation($env, 0, $barcode);
+	my ($item) = getiteminformation(0, $barcode);
 	if (ShelfPossibleAction($loggedinuser,$shelfnumber,'manage')) {
-		AddToShelf($env, $item->{'itemnumber'}, $shelfnumber);
+		AddToShelf($item->{'itemnumber'}, $shelfnumber);
 		foreach ($query->param) {
 			if (/REM-(\d*)/) {
 				my $itemnumber=$1;
-				RemoveFromShelf($env, $itemnumber, $shelfnumber);
+				DelFromShelf($itemnumber, $shelfnumber);
 			}
 		}
 	}
 }
-my ($shelflist) = GetShelfList($loggedinuser,2);
 
-$template->param({	loggedinuser => $loggedinuser,
-					headerbackgroundcolor => $headerbackgroundcolor,
-					circbackgroundcolor => $circbackgroundcolor });
+# getting the Shelves list
+my $shelflist = GetShelves($loggedinuser,2);
+
+$template->param({loggedinuser => $loggedinuser});
+my $op = $query->param('op');
+
 SWITCH: {
-	if ($query->param('op') eq 'modifsave') {
-		ModifShelf($query->param('shelfnumber'),$query->param('shelfname'),$loggedinuser,$query->param('category'));
+	if ( $op && ($op eq 'modifsave')) {
+		ModShelf($query->param('shelfnumber'),$query->param('shelfname'),$loggedinuser,$query->param('category'));
 		last SWITCH;
 	}
-	if ($query->param('op') eq 'modif') {
+	if ( $op && ($op eq 'modif')) {
 		my ($shelfnumber,$shelfname,$owner,$category) = GetShelf($query->param('shelf'));
 		$template->param(edit => 1,
 						shelfnumber => $shelfnumber,
@@ -89,7 +129,7 @@
 	}
 }
 
-($shelflist) = GetShelfList($loggedinuser,2); # rebuild shelflist in case a shelf has been added
+($shelflist) = GetShelves($loggedinuser,2); # rebuild shelflist in case a shelf has been added
 
 my $color='';
 my @shelvesloop;
@@ -105,10 +145,11 @@
 		$line{'canmanage'} = ShelfPossibleAction($loggedinuser,$element,'manage');
 		$line{'firstname'}=$shelflist->{$element}->{'firstname'} unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
 		$line{'surname'}=$shelflist->{$element}->{'surname'} unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
-;
+
 		push (@shelvesloop, \%line);
 		}
-$template->param(shelvesloop => \@shelvesloop,
+$template->param(
+        shelvesloop => \@shelvesloop,
 		intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
 		intranetstylesheet => C4::Context->preference("intranetstylesheet"),
 		IntranetNav => C4::Context->preference("IntranetNav"),
@@ -116,19 +157,16 @@
 
 output_html_with_http_headers $query, $cookie, $template->output;
 
-# sub editshelf {
-# 	my ($shelfnumber) = @_;
-# 	my ($shelfnumber,$shelfname,$owner,$category) = GetShelf($shelfnumber);
-# 	$template->param(edit => 1,
-# 					shelfnumber => $shelfnumber,
-# 					shelfname => $shelfname,
-# 					"category$category" => 1);
-# }
+
 sub shelves {
 	if (my $newshelf=$query->param('addshelf')) {
-		my ($status, $string) = AddShelf($env,$newshelf,$query->param('owner'),$query->param('category'));
-		if ($status) {
-			$template->param(status1 => $status, string1 => $string);
+		my $shelfnumber = AddShelf($newshelf,$query->param('owner'),$query->param('category'));
+
+		if ($shelfnumber == -1) { #shelf already exists.
+			$template->param({
+                shelfnumber =>$shelfnumber,
+                already => 1
+            });
 		}
 	}
 	my @paramsloop;
@@ -136,17 +174,17 @@
 		my %line;
 		if (/DEL-(\d+)/) {
 			my $delshelf=$1;
-			my ($status, $string) = RemoveShelf($env,$delshelf);
+			my ($status, $count) = DelShelf($delshelf);
 			if ($status) {
 				$line{'status'}=$status;
-				$line{'string'} = $string;
+				$line{'count'} = $count;
 			}
 		}
 		#if the shelf is not deleted, %line points on null
 		push(@paramsloop,\%line);
 	}
 	$template->param(paramsloop => \@paramsloop);
-	my ($shelflist) = GetShelfList($loggedinuser,2);
+	my ($shelflist) = GetShelves($loggedinuser,2);
 	my $color='';
 	my @shelvesloop;
 	foreach my $element (sort keys %$shelflist) {
@@ -158,7 +196,8 @@
 		$line{'shelfbookcount'}=$shelflist->{$element}->{'count'} ;
 		push(@shelvesloop, \%line);
 	}
-	$template->param(shelvesloop=>\@shelvesloop,
+	$template->param(
+	        shelvesloop=>\@shelvesloop,
 							shelves => 1,
 						);
 }
@@ -167,7 +206,7 @@
 	my $shelfnumber=shift;
 	#check that the user can view the shelf
 	return unless (ShelfPossibleAction($loggedinuser,$shelfnumber,'view'));
-	my ($itemlist) = GetShelfContents($env, $shelfnumber);
+	my ($itemlist) = GetShelfContents($shelfnumber);
 	my $item='';
 	my $color='';
 	my @itemsloop;
@@ -194,6 +233,9 @@
 
 #
 # $Log: shelves.pl,v $
+# Revision 1.9.2.1  2006/08/30 15:59:14  toins
+# Code cleaned according to coding guide lines.
+#
 # Revision 1.9  2006/07/04 14:36:51  toins
 # Head & rel_2_2 merged
 #
@@ -201,93 +243,3 @@
 # Adds script support for IntranetNav ... see mail to koha-devel for
 # details
 #
-# Revision 1.5.2.4  2006/02/05 21:45:25  kados
-# Adds support for intranetstylesheet system pref in Koha scripts
-#
-# Revision 1.5.2.3  2006/02/04 21:26:47  kados
-# Adds support for intranetcolorstylesheet
-#
-# Revision 1.5.2.2  2005/04/27 18:15:27  oleonard
-# Left out some instances in the previous update
-#
-# Revision 1.5.2.1  2005/04/27 16:55:38  oleonard
-# Moving alternating row colors to the template, adding publicationyear and itemtype variables
-#
-# Revision 1.5  2004/12/16 11:30:57  tipaul
-# adding bookshelf features :
-# * create bookshelf on the fly
-# * modify a bookshelf name & status
-#
-# Revision 1.4  2004/12/15 17:28:23  tipaul
-# adding bookshelf features :
-# * create bookshelf on the fly
-# * modify a bookshelf (this being not finished, will commit the rest soon)
-#
-# Revision 1.3  2004/12/02 16:38:50  tipaul
-# improvement in book shelves
-#
-# Revision 1.2  2004/11/19 16:31:30  tipaul
-# bugfix for bookshelves not in official CVS
-#
-# Revision 1.1.2.1  2004/03/10 15:08:18  tipaul
-# modifying shelves : introducing category of shelf : private, public, free for all
-#
-# Revision 1.13  2004/02/11 08:35:31  tipaul
-# synch'ing 2.0.0 branch and head
-#
-# Revision 1.12.2.1  2004/02/06 14:22:19  tipaul
-# fixing bugs in bookshelves management.
-#
-# Revision 1.12  2003/02/05 10:04:14  acli
-# Worked around weirdness with HTML::Template; without the {}, it complains
-# of being passed an odd number of arguments even though we are not
-#
-# Revision 1.11  2003/02/05 09:23:03  acli
-# Fixed a few minor errors to make it run
-# Noted correct tab size
-#
-# Revision 1.10  2003/02/02 07:18:37  acli
-# Moved C4/Charset.pm to C4/Interface/CGI/Output.pm
-#
-# Create output_html_with_http_headers function to contain the "print $query
-# ->header(-type => guesstype...),..." call. This is in preparation for
-# non-HTML output (e.g., text/xml) and charset conversion before output in
-# the future.
-#
-# Created C4/Interface/CGI/Template.pm to hold convenience functions specific
-# to the CGI interface using HTML::Template
-#
-# Modified moremembers.pl to make the "sex" field localizable for languages
-# where M and F doesn't make sense
-#
-# Revision 1.9  2002/12/19 18:55:40  hdl
-# Templating reservereport et shelves.
-#
-# Revision 1.9  2002/08/14 18:12:51  hdl
-# Templating files
-#
-# Revision 1.8  2002/08/14 18:12:51  tonnesen
-# Added copyright statement to all .pl and .pm files
-#
-# Revision 1.7  2002/07/05 05:03:37  tonnesen
-# Minor changes to authentication routines.
-#
-# Revision 1.5  2002/07/04 19:42:48  tonnesen
-# Minor changes
-#
-# Revision 1.4  2002/07/04 19:21:29  tonnesen
-# Beginning of authentication api.  Applied to shelves.pl for now as a test case.
-#
-# Revision 1.2.2.1  2002/06/26 20:28:15  tonnesen
-# Some udpates that I made here locally a while ago.  Still won't be useful, but
-# should be functional
-#
-#
-#
-
-
-
-
-# Local Variables:
-# tab-width: 4
-# End:

Index: C4/BookShelves.pm
===================================================================
RCS file: /sources/koha/koha/C4/BookShelves.pm,v
retrieving revision 1.15
retrieving revision 1.15.8.1
diff -u -b -r1.15 -r1.15.8.1
--- C4/BookShelves.pm	16 Dec 2004 11:30:58 -0000	1.15
+++ C4/BookShelves.pm	30 Aug 2006 15:59:14 -0000	1.15.8.1
@@ -3,7 +3,7 @@
 
 package C4::BookShelves;
 
-# $Id: BookShelves.pm,v 1.15 2004/12/16 11:30:58 tipaul Exp $
+# $Id: BookShelves.pm,v 1.15.8.1 2006/08/30 15:59:14 toins Exp $
 
 # Copyright 2000-2002 Katipo Communications
 #
@@ -30,7 +30,7 @@
 use vars qw($VERSION @ISA @EXPORT);
 
 # set the version for version checking
-$VERSION = 0.01;
+$VERSION = do { my @v = '$Revision: 1.15.8.1 $' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
 
 =head1 NAME
 
@@ -53,41 +53,21 @@
 =cut
 
 @ISA = qw(Exporter);
- at EXPORT = qw(&GetShelfList		&GetShelfContents		&GetShelf
-			&AddToShelf			&AddToShelfFromBiblio
-			&RemoveFromShelf	&AddShelf				&ModifShelf 
-			&RemoveShelf		&ShelfPossibleAction
-				);
-
-my $dbh = C4::Context->dbh;
-
-=item ShelfPossibleAction
-
-=over 4
-
-=item C<$loggedinuser,$shelfnumber,$action>
+ at EXPORT = qw(
+        &GetShelves &GetShelfContents &GetShelf
 
-$action can be "view" or "manage".
+        &AddToShelf &AddToShelfFromBiblio &AddShelf
 
-Returns 1 if the user can do the $action in the $shelfnumber shelf.
-Returns 0 otherwise.
-
-=back
+        &ModShelf
+        &ShelfPossibleAction
+        &DelFromShelf &DelShelf
+);
 
-=cut
-sub ShelfPossibleAction {
-	my ($loggedinuser,$shelfnumber,$action)= @_;
-	my $sth = $dbh->prepare("select owner,category from bookshelf where shelfnumber=?");
-	$sth->execute($shelfnumber);
-	my ($owner,$category) = $sth->fetchrow;
-	return 1 if (($category>=3 or $owner eq $loggedinuser) && $action eq 'manage');
-	return 1 if (($category>= 2 or $owner eq $loggedinuser) && $action eq 'view');
-	return 0;
-}
+my $dbh = C4::Context->dbh;
 
-=item GetShelfList
+=item GetShelves
 
-  $shelflist = &GetShelfList();
+  $shelflist = &GetShelves($owner, $mincategory);
   ($shelfnumber, $shelfhash) = each %{$shelflist};
 
 Looks up the virtual bookshelves, and returns a summary. C<$shelflist>
@@ -95,6 +75,9 @@
 (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 bookshelf for adding a book".
+bookshelves of the owner are always selected, whatever the category
+
 =over 4
 
 =item C<$shelfhash-E<gt>{shelfname}>
@@ -108,185 +91,322 @@
 =back
 
 =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 GetShelfList {
-	my ($owner,$mincategory) = @_;
-	# mincategory : 2 if the list is for "look". 3 if the list is for "Select bookshelf for adding a book".
-	# bookshelves of the owner are always selected, whatever the category
-	my $sth=$dbh->prepare("SELECT		bookshelf.shelfnumber, bookshelf.shelfname,owner,surname,firstname,category,
+
+sub GetShelves {
+    my ( $owner, $mincategory ) = @_;
+
+    my $query = qq(
+        SELECT bookshelf.shelfnumber, bookshelf.shelfname,owner,surname,firstname,category,
 							count(shelfcontents.itemnumber) as count
 								FROM		bookshelf
-								LEFT JOIN	shelfcontents
-								ON		bookshelf.shelfnumber = shelfcontents.shelfnumber
-								left join borrowers on bookshelf.owner = borrowers.borrowernumber
-								where owner=? or category>=?
-								GROUP BY	bookshelf.shelfnumber order by shelfname");
-    $sth->execute($owner,$mincategory);
+            LEFT JOIN   shelfcontents ON bookshelf.shelfnumber = shelfcontents.shelfnumber
+            LEFT JOIN   borrowers ON bookshelf.owner = borrowers.borrowernumber
+        WHERE  owner=? OR category>=?
+        GROUP BY bookshelf.shelfnumber
+        ORDER BY shelfname
+    );
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $owner, $mincategory );
     my %shelflist;
-    while (my ($shelfnumber, $shelfname,$owner,$surname,$firstname,$category,$count) = $sth->fetchrow) {
-	$shelflist{$shelfnumber}->{'shelfname'}=$shelfname;
-	$shelflist{$shelfnumber}->{'count'}=$count;
-	$shelflist{$shelfnumber}->{'category'}=$category;
-	$shelflist{$shelfnumber}->{'owner'}=$owner;
+    while (
+        my (
+            $shelfnumber, $shelfname, $owner, $surname,
+            $firstname,   $category,  $count
+        )
+        = $sth->fetchrow
+      )
+    {
+        $shelflist{$shelfnumber}->{'shelfname'} = $shelfname;
+        $shelflist{$shelfnumber}->{'count'}     = $count;
+        $shelflist{$shelfnumber}->{'category'}  = $category;
+        $shelflist{$shelfnumber}->{'owner'}     = $owner;
 	$shelflist{$shelfnumber}->{surname} = $surname;
 	$shelflist{$shelfnumber}->{firstname} = $firstname;
     }
-    return(\%shelflist);
+    return ( \%shelflist );
 }
 
+=item GetShef
+
+  (shelfnumber,shelfname,owner,category) = &GetShelf($shelfnumber);
+
+Looks up information about the contents of virtual bookshelf number
+C<$shelfnumber>
+
+Returns the database's information on 'bookshelf' table.
+
+=cut
+
 sub GetShelf {
 	my ($shelfnumber) = @_;
-	my $sth=$dbh->prepare("select shelfnumber,shelfname,owner,category from bookshelf where shelfnumber=?");
+    my $query = qq(
+        SELECT shelfnumber,shelfname,owner,category
+        FROM   bookshelf
+        WHERE  shelfnumber=?
+    );
+    my $sth = $dbh->prepare($query);
 	$sth->execute($shelfnumber);
 	return $sth->fetchrow;
 }
+
 =item GetShelfContents
 
-  $itemlist = &GetShelfContents($env, $shelfnumber);
+  $itemlist = &GetShelfContents($shelfnumber);
 
 Looks up information about the contents of virtual bookshelf number
 C<$shelfnumber>.
 
 Returns a reference-to-array, whose elements are references-to-hash,
-as returned by C<&getiteminformation>.
-
-I don't know what C<$env> is.
+as returned by C<C4::Circ2::getiteminformation>.
 
 =cut
+
 #'
 sub GetShelfContents {
-    my ($env, $shelfnumber) = @_;
+    my ( $shelfnumber ) = @_;
     my @itemlist;
-    my $sth=$dbh->prepare("select itemnumber from shelfcontents where shelfnumber=? order by itemnumber");
+    my $query = qq(
+        SELECT itemnumber
+        FROM   shelfcontents
+        WHERE  shelfnumber=?
+        ORDER BY itemnumber
+    );
+    my $sth = $dbh->prepare($query);
     $sth->execute($shelfnumber);
-    while (my ($itemnumber) = $sth->fetchrow) {
-	my ($item) = getiteminformation($env, $itemnumber, 0);
-	push (@itemlist, $item);
+    while ( my ($itemnumber) = $sth->fetchrow ) {
+        my ($item) = getiteminformation( $itemnumber, 0 );
+        push( @itemlist, $item );
     }
-    return (\@itemlist);
+    return ( \@itemlist );
 }
 
+=item AddShelf
+
+  $shelfnumber = &AddShelf( $shelfname, $owner, $category);
+
+Creates a new virtual bookshelf with name C<$shelfname>, owner C<$owner> and category
+C<$category>.
+
+Returns a code to know what's happen.
+    * -1 : if this bookshelf already exist.
+    * $shelfnumber : if success.
+
+=cut
+
+sub AddShelf {
+    my ( $shelfname, $owner, $category ) = @_;
+    my $query = qq(
+        SELECT *
+        FROM   bookshelf
+        WHERE  shelfname=?
+    );
+    my $sth = $dbh->prepare($query);
+    $sth->execute($shelfname);
+    if ( $sth->rows ) {
+        return (-1);
+    }
+    else {
+        my $query = qq(
+            INSERT INTO bookshelf
+                (shelfname,owner,category)
+            VALUES (?,?,?)
+        );
+        $sth = $dbh->prepare($query);
+        $sth->execute( $shelfname, $owner, $category );
+        my $shelfnumber = $dbh->{'mysql_insertid'};
+        return ($shelfnumber);
+    }
+}
+
+
 =item AddToShelf
 
-  &AddToShelf($env, $itemnumber, $shelfnumber);
+  &AddToShelf($itemnumber, $shelfnumber);
 
 Adds item number C<$itemnumber> to virtual bookshelf number
 C<$shelfnumber>, unless that item is already on that shelf.
 
-C<$env> is ignored.
-
 =cut
+
 #'
 sub AddToShelf {
-	my ($env, $itemnumber, $shelfnumber) = @_;
+    my ( $itemnumber, $shelfnumber ) = @_;
 	return unless $itemnumber;
-	my $sth=$dbh->prepare("select * from shelfcontents where shelfnumber=? and itemnumber=?");
+    my $query = qq(
+        SELECT *
+        FROM   shelfcontents
+        WHERE  shelfnumber=? AND itemnumber=?
+    );
+    my $sth = $dbh->prepare($query);
 
-	$sth->execute($shelfnumber, $itemnumber);
-	if ($sth->rows) {
-# already on shelf
-	} else {
-		$sth=$dbh->prepare("insert into shelfcontents (shelfnumber, itemnumber, flags) values (?, ?, 0)");
-		$sth->execute($shelfnumber, $itemnumber);
+    $sth->execute( $shelfnumber, $itemnumber );
+    unless ( $sth->rows ) {
+        # already on shelf
+        my $query = qq(
+            INSERT INTO shelfcontents
+                (shelfnumber, itemnumber, flags)
+            VALUES
+                (?, ?, 0)
+        );
+        $sth = $dbh->prepare($query);
+        $sth->execute( $shelfnumber, $itemnumber );
 	}
 }
+
+=item AddToShelfFromBiblio
+ 
+    &AddToShelfFromBiblio($biblionumber, $shelfnumber)
+
+    this function allow to add a book into the shelf number $shelfnumber
+    from biblionumber.
+
+=cut
+
 sub AddToShelfFromBiblio {
-	my ($env, $biblionumber, $shelfnumber) = @_;
+    my ( $biblionumber, $shelfnumber ) = @_;
 	return unless $biblionumber;
-	my $sth = $dbh->prepare("select itemnumber from items where biblionumber=?");
+    my $query = qq(
+        SELECT itemnumber
+        FROM   items
+        WHERE  biblionumber=?
+    );
+    my $sth = $dbh->prepare($query);
 	$sth->execute($biblionumber);
 	my ($itemnumber) = $sth->fetchrow;
-	$sth=$dbh->prepare("select * from shelfcontents where shelfnumber=? and itemnumber=?");
-	$sth->execute($shelfnumber, $itemnumber);
-	if ($sth->rows) {
-# already on shelf
-	} else {
-		$sth=$dbh->prepare("insert into shelfcontents (shelfnumber, itemnumber, flags) values (?, ?, 0)");
-		$sth->execute($shelfnumber, $itemnumber);
+    my $query = qq(
+        SELECT *
+        FROM   shelfcontents
+        WHERE  shelfnumber=? AND itemnumber=?
+    );
+    $sth = $dbh->prepare($query);
+    $sth->execute( $shelfnumber, $itemnumber );
+    unless ( $sth->rows ) {
+        # already on shelf
+        my $query =qq(
+            INSERT INTO shelfcontents
+                (shelfnumber, itemnumber, flags)
+            VALUES
+                (?, ?, 0)
+        );
+        $sth = $dbh->prepare($query);
+        $sth->execute( $shelfnumber, $itemnumber );
 	}
 }
 
-=item RemoveFromShelf
+=item ModShelf
 
-  &RemoveFromShelf($env, $itemnumber, $shelfnumber);
+ModShelf($shelfnumber, $shelfname, $owner, $category )
 
-Removes item number C<$itemnumber> from virtual bookshelf number
-C<$shelfnumber>. If the item wasn't on that bookshelf to begin with,
-nothing happens.
-
-C<$env> is ignored.
+Modify the value into bookshelf table with values given on input arg.
 
 =cut
-#'
-sub RemoveFromShelf {
-    my ($env, $itemnumber, $shelfnumber) = @_;
-    my $sth=$dbh->prepare("delete from shelfcontents where shelfnumber=? and itemnumber=?");
-    $sth->execute($shelfnumber,$itemnumber);
+
+sub ModShelf {
+    my ( $shelfnumber, $shelfname, $owner, $category ) = @_;
+    my $query = qq(
+        UPDATE bookshelf
+        SET    shelfname=?,owner=?,category=?
+        WHERE  shelfnumber=?
+    );
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $shelfname, $owner, $category, $shelfnumber );
 }
 
-=item AddShelf
+=item DelShelf
 
-  ($status, $msg) = &AddShelf($env, $shelfname);
+  ($status) = &DelShelf($shelfnumber);
 
-Creates a new virtual bookshelf with name C<$shelfname>.
+Deletes virtual bookshelf number C<$shelfnumber>. The bookshelf must
+be empty.
 
 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.
 
-C<$env> is ignored.
-
 =cut
 
-sub AddShelf {
-    my ($env, $shelfname, $owner, $category) = @_;
-    my $sth=$dbh->prepare("select * from bookshelf where shelfname=?");
-	$sth->execute($shelfname);
-    if ($sth->rows) {
-		return(1, "Shelf \"$shelfname\" already exists");
-    } else {
-		$sth=$dbh->prepare("insert into bookshelf (shelfname,owner,category) values (?,?,?)");
-		$sth->execute($shelfname,$owner,$category);
-		my $shelfnumber = $dbh->{'mysql_insertid'};
-		return (0, "Done",$shelfnumber);
-    }
-}
 
-sub ModifShelf {
-	my ($shelfnumber, $shelfname, $owner, $category) = @_;
-	my $sth = $dbh->prepare("update bookshelf set shelfname=?,owner=?,category=? where shelfnumber=?");
-	$sth->execute($shelfname,$owner,$category,$shelfnumber);
-}
+=item ShelfPossibleAction
 
-=item RemoveShelf
+=over 4
 
-  ($status, $msg) = &RemoveShelf($env, $shelfnumber);
+ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
 
-Deletes virtual bookshelf number C<$shelfnumber>. The bookshelf must
-be empty.
+=item C<$loggedinuser,$shelfnumber,$action>
 
-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.
+$action can be "view" or "manage".
 
-C<$env> is ignored.
+Returns 1 if the user can do the $action in the $shelfnumber shelf.
+Returns 0 otherwise.
+
+=back
 
 =cut
+
+sub ShelfPossibleAction {
+    my ( $loggedinuser, $shelfnumber, $action ) = @_;
+    my $query = qq(
+        SELECT owner,category
+        FROM   bookshelf
+        WHERE  shelfnumber=?
+    );
+    my $sth = $dbh->prepare($query);
+    $sth->execute($shelfnumber);
+    my ( $owner, $category ) = $sth->fetchrow;
+    return 1 if (($category >= 3 or $owner eq $loggedinuser) && $action eq 'manage' );
+    return 1 if (($category >= 2 or $owner eq $loggedinuser) && $action eq 'view' );
+    return 0;
+}
+
+=item DelFromShelf
+
+  &DelFromShelf( $itemnumber, $shelfnumber);
+
+Removes item number C<$itemnumber> from virtual bookshelf number
+C<$shelfnumber>. If the item wasn't on that bookshelf to begin with,
+nothing happens.
+
+=cut
+
+#'
+sub DelFromShelf {
+    my ( $itemnumber, $shelfnumber ) = @_;
+    my $query = qq(
+        DELETE FROM shelfcontents
+        WHERE  shelfnumber=? AND itemnumber=?
+    );
+    my $sth =
+      $dbh->prepare($query);
+    $sth->execute( $shelfnumber, $itemnumber );
+}
+
 #'
-sub RemoveShelf {
-    my ($env, $shelfnumber) = @_;
-    my $sth=$dbh->prepare("select count(*) from shelfcontents where shelfnumber=?");
+sub DelShelf {
+    my ( $shelfnumber ) = @_;
+    my $query =qq(
+        SELECT count(*)
+        FROM   shelfcontents
+        WHERE  shelfnumber=?
+    );
+    my $sth = $dbh->prepare($query);
 	$sth->execute($shelfnumber);
-    my ($count)=$sth->fetchrow;
+    my ($count) = $sth->fetchrow;
     if ($count) {
-	return (1, "Shelf has $count items on it.  Please remove all items before deleting this shelf.");
-    } else {
-	$sth=$dbh->prepare("delete from bookshelf where shelfnumber=?");
+        return (1,$count);
+    }
+    else {
+        my $query = qq(
+            DELETE FROM bookshelf
+            WHERE       shelfnumber=?
+        );
+        $sth = $dbh->prepare($query);
 	$sth->execute($shelfnumber);
-	return (0, "Done");
+        return ( 0, $count);
     }
 }
 
@@ -296,6 +416,9 @@
 
 #
 # $Log: BookShelves.pm,v $
+# Revision 1.15.8.1  2006/08/30 15:59:14  toins
+# Code cleaned according to coding guide lines.
+#
 # Revision 1.15  2004/12/16 11:30:58  tipaul
 # adding bookshelf features :
 # * create bookshelf on the fly
@@ -305,56 +428,6 @@
 # adding bookshelf features :
 # * create bookshelf on the fly
 # * modify a bookshelf (this being not finished, will commit the rest soon)
-#
-# Revision 1.13  2004/03/11 16:06:20  tipaul
-# *** empty log message ***
-#
-# Revision 1.11.2.2  2004/02/19 10:15:41  tipaul
-# new feature : adding book to bookshelf from biblio detail screen.
-#
-# Revision 1.11.2.1  2004/02/06 14:16:55  tipaul
-# fixing bugs in bookshelves management.
-#
-# Revision 1.11  2003/12/15 10:57:08  slef
-# DBI call fix for bug 662
-#
-# Revision 1.10  2003/02/05 10:05:02  acli
-# Converted a few SQL statements to use ? to fix a few strange SQL errors
-# Noted correct tab size
-#
-# Revision 1.9  2002/10/13 08:29:18  arensb
-# Deleted unused variables.
-# Removed trailing whitespace.
-#
-# Revision 1.8  2002/10/10 04:32:44  arensb
-# Simplified references.
-#
-# Revision 1.7  2002/10/05 09:50:10  arensb
-# Merged with arensb-context branch: use C4::Context->dbh instead of
-# &C4Connect, and generally prefer C4::Context over C4::Database.
-#
-# Revision 1.6.2.1  2002/10/04 02:24:43  arensb
-# Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
-# C4Connect.
-#
-# Revision 1.6  2002/09/23 13:50:30  arensb
-# Fixed missing bit in POD.
-#
-# Revision 1.5  2002/09/22 17:29:17  arensb
-# Added POD.
-# Added some FIXME comments.
-# Removed useless trailing whitespace.
-#
-# Revision 1.4  2002/08/14 18:12:51  tonnesen
-# Added copyright statement to all .pl and .pm files
-#
-# Revision 1.3  2002/07/02 17:48:06  tonnesen
-# Merged in updates from rel-1-2
-#
-# Revision 1.2.2.1  2002/06/26 20:46:48  tonnesen
-# Inserting some changes I made locally a while ago.
-#
-#
 
 __END__
 





More information about the Koha-cvs mailing list