Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / virtualshelves / shelves.pl
index 06d93d2..aeb65c9 100755 (executable)
@@ -29,9 +29,14 @@ use C4::XSLT;
 
 use Koha::Biblios;
 use Koha::Biblioitems;
+use Koha::Items;
+use Koha::ItemTypes;
 use Koha::CsvProfiles;
+use Koha::Patrons;
 use Koha::Virtualshelves;
 
+use constant ANYONE => 2;
+
 my $query = new CGI;
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
@@ -49,14 +54,15 @@ my $category = $query->param('category') || 1;
 my ( $shelf, $shelfnumber, @messages );
 
 if ( $op eq 'add_form' ) {
-    # Nothing to do
+    # Only pass default
+    $shelf = { allow_change_from_owner => 1 };
 } elsif ( $op eq 'edit_form' ) {
     $shelfnumber = $query->param('shelfnumber');
     $shelf       = Koha::Virtualshelves->find($shelfnumber);
 
     if ( $shelf ) {
         $category = $shelf->category;
-        my $patron = GetMember( 'borrowernumber' => $shelf->owner );
+        my $patron = Koha::Patrons->find( $shelf->owner )->unblessed;
         $template->param( owner => $patron, );
         unless ( $shelf->can_be_managed( $loggedinuser ) ) {
             push @messages, { type => 'alert', code => 'unauthorized_on_update' };
@@ -66,14 +72,14 @@ if ( $op eq 'add_form' ) {
         push @messages, { type => 'alert', code => 'does_not_exist' };
     }
 } elsif ( $op eq 'add' ) {
+    my $allow_changes_from = $query->param('allow_changes_from');
     eval {
         $shelf = Koha::Virtualshelf->new(
             {   shelfname          => scalar $query->param('shelfname'),
                 sortfield          => scalar $query->param('sortfield'),
                 category           => scalar $query->param('category'),
-                allow_add          => scalar $query->param('allow_add'),
-                allow_delete_own   => scalar $query->param('allow_delete_own'),
-                allow_delete_other => scalar $query->param('allow_delete_other'),
+                allow_change_from_owner => $allow_changes_from > 0,
+                allow_change_from_others => $allow_changes_from == ANYONE,
                 owner              => scalar $query->param('owner'),
             }
         );
@@ -96,13 +102,13 @@ if ( $op eq 'add_form' ) {
     if ( $shelf ) {
         $op = $referer;
         my $sortfield = $query->param('sortfield');
-        $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
+        $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
         if ( $shelf->can_be_managed( $loggedinuser ) ) {
             $shelf->shelfname( scalar $query->param('shelfname') );
             $shelf->sortfield( $sortfield );
-            $shelf->allow_add( scalar $query->param('allow_add') );
-            $shelf->allow_delete_own( scalar $query->param('allow_delete_own') );
-            $shelf->allow_delete_other( scalar $query->param('allow_delete_other') );
+            my $allow_changes_from = $query->param('allow_changes_from');
+            $shelf->allow_change_from_owner( $allow_changes_from > 0 );
+            $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
             $shelf->category( scalar $query->param('category') );
             eval { $shelf->store };
 
@@ -146,10 +152,9 @@ if ( $op eq 'add_form' ) {
                 foreach my $barcode (@barcodes){
                     $barcode =~ s/\r$//; # strip any naughty return chars
                     next if $barcode eq '';
-                    my $item = GetItem( 0, $barcode);
-                    if (defined $item && $item->{itemnumber}) {
-                        my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
-                        my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
+                    my $item = Koha::Items->find({barcode => $barcode});
+                    if ( $item ) {
+                        my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
                         if ($@) {
                             push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
                         } elsif ( $added ) {
@@ -165,6 +170,30 @@ if ( $op eq 'add_form' ) {
                 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
             }
         }
+        if ( my $biblionumbers = $query->param('biblionumbers') ) {
+            if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
+                my @biblionumbers = split /\n/, $biblionumbers;
+                foreach my $biblionumber (@biblionumbers) {
+                    $biblionumber =~ s/\r$//; # strip any naughty return chars
+                    next if $biblionumber eq '';
+                    my $biblio = Koha::Biblios->find($biblionumber);
+                    if (defined $biblio) {
+                        my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
+                        if ($@) {
+                            push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
+                        } elsif ( $added ) {
+                            push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
+                        } else {
+                            push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
+                        }
+                    } else {
+                        push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
+                    }
+                }
+            } else {
+                push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
+            }
+        }
     } else {
         push @messages, { type => 'alert', code => 'does_not_exist' };
     }
@@ -205,7 +234,7 @@ if ( $op eq 'view' ) {
     if ( $shelf ) {
         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
             my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title';    # Passed in sorting overrides default sorting
-            $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
+            $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
             my $direction = $query->param('direction') || 'asc';
             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
             my ( $rows, $page );
@@ -214,7 +243,7 @@ if ( $op eq 'view' ) {
                 $page = ( $query->param('page') ? $query->param('page') : 1 );
             }
 
-            my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
+            my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
             my $contents = $shelf->get_contents->search(
                 {},
                 {
@@ -225,8 +254,6 @@ if ( $op eq 'view' ) {
                 }
             );
 
-            my $borrower = GetMember( borrowernumber => $loggedinuser );
-
             my $xslfile = C4::Context->preference('XSLTListsDisplay');
             my $lang   = $xslfile ? C4::Languages::getlanguage()  : undef;
             my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
@@ -235,7 +262,7 @@ if ( $op eq 'view' ) {
             while ( my $content = $contents->next ) {
                 my $this_item;
                 my $biblionumber = $content->biblionumber;
-                my $record       = GetMarcBiblio($biblionumber);
+                my $record       = GetMarcBiblio({ biblionumber => $biblionumber });
 
                 if ( $xslfile ) {
                     $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
@@ -244,16 +271,19 @@ if ( $op eq 'view' ) {
 
                 my $marcflavour = C4::Context->preference("marcflavour");
                 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
-                my $itemtypeinfo = getitemtypeinfo( $itemtype, 'intranet' );
+                $itemtype = Koha::ItemTypes->find( $itemtype );
                 my $biblio = Koha::Biblios->find( $content->biblionumber );
                 $this_item->{title}             = $biblio->title;
+                $this_item->{subtitle}          = $biblio->subtitle;
+                $this_item->{medium}            = $biblio->medium;
+                $this_item->{part_number}       = $biblio->part_number;
+                $this_item->{part_name}         = $biblio->part_name;
                 $this_item->{author}            = $biblio->author;
                 $this_item->{dateadded}         = $content->dateadded;
-                $this_item->{imageurl}          = $itemtypeinfo->{imageurl};
-                $this_item->{description}       = $itemtypeinfo->{description};
-                $this_item->{notforloan}        = $itemtypeinfo->{notforloan};
-                $this_item->{'coins'}           = GetCOinSBiblio($record);
-                $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
+                $this_item->{imageurl}          = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
+                $this_item->{description}       = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
+                $this_item->{notforloan}        = $itemtype->notforloan if $itemtype;
+                $this_item->{'coins'}           = $biblio->get_coins;
                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );