Bug 14544: Make the intranet side independent of Page.pm
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 5 Aug 2015 15:37:02 +0000 (16:37 +0100)
committerTomas Cohen Arazi <tomascohen@theke.io>
Thu, 5 Nov 2015 12:58:01 +0000 (09:58 -0300)
Signed-off-by: Alex Arnaud <alex.arnaud@biblibre.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

13 files changed:
C4/Output.pm
C4/Utils/DataTables/VirtualShelves.pm
C4/VirtualShelves/Page.pm
Koha/Virtualshelf.pm
koha-tmpl/intranet-tmpl/prog/en/includes/virtualshelves-toolbar.inc
koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/downloadshelf.tt
koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tt
koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/tables/shelves_results.tt
t/db_dependent/Virtualshelves.t
virtualshelves/addbybiblionumber.pl
virtualshelves/downloadshelf.pl
virtualshelves/shelves.pl

index 90249db..3ae4c6c 100644 (file)
@@ -89,6 +89,7 @@ sub pagination_bar {
     my $nb_pages       = (@_) ? shift : 1;
     my $current_page   = (@_) ? shift : undef; # delay default until later
     my $startfrom_name = (@_) ? shift : 'page';
+    my $additional_parameters = shift || {};
 
     # how many pages to show before and after the current page?
     my $pages_around = 2;
@@ -105,6 +106,10 @@ sub pagination_bar {
        $base_url =~ s/$delim$//;               # remove trailing delim
 
     my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
+    my $url_suffix;
+    while ( my ( $k, $v ) = each %$additional_parameters ) {
+        $url_suffix .= '&amp;' . $k . '=' . $v;
+    }
     my $pagination_bar = '';
 
     # navigation bar useful only if more than one page to display !
@@ -116,7 +121,9 @@ sub pagination_bar {
                 "\n" . '&nbsp;'
               . '<a href="'
               . $url
-              . '1" rel="start">'
+              . '1'
+              . $url_suffix
+              . '"rel="start">'
               . '&lt;&lt;' . '</a>';
         }
         else {
@@ -133,6 +140,7 @@ sub pagination_bar {
               . '<a href="'
               . $url
               . $previous
+              . $url_suffix
               . '" rel="prev">' . '&lt;' . '</a>';
         }
         else {
@@ -171,7 +179,9 @@ sub pagination_bar {
                         "\n" . '&nbsp;'
                       . '<a href="'
                       . $url
-                      . $page_number . '">'
+                      . $page_number
+                      . $url_suffix
+                      . '">'
                       . $page_number . '</a>';
                 }
                 $last_displayed_page = $page_number;
@@ -186,6 +196,7 @@ sub pagination_bar {
               . '&nbsp;<a href="'
               . $url
               . $next
+              . $url_suffix
               . '" rel="next">' . '&gt;' . '</a>';
         }
         else {
@@ -199,6 +210,7 @@ sub pagination_bar {
               . '&nbsp;<a href="'
               . $url
               . $nb_pages
+              . $url_suffix
               . '" rel="last">'
               . '&gt;&gt;' . '</a>';
         }
index e331447..bfda353 100644 (file)
@@ -5,7 +5,7 @@ use C4::Branch qw/onlymine/;
 use C4::Context;
 use C4::Members qw/GetMemberIssuesAndFines/;
 use C4::Utils::DataTables;
-use C4::VirtualShelves;
+use Koha::Virtualshelves;
 
 sub search {
     my ( $params ) = @_;
@@ -121,8 +121,9 @@ sub search {
     ( $iTotalRecords ) = $dbh->selectrow_array( $query, undef, @args );
 
     for my $shelf ( @$shelves ) {
-        $shelf->{can_manage_shelf} = C4::VirtualShelves::ShelfPossibleAction( $loggedinuser, $shelf->{shelfnumber}, 'manage' );
-        $shelf->{can_delete_shelf} = C4::VirtualShelves::ShelfPossibleAction( $loggedinuser, $shelf->{shelfnumber}, 'delete_shelf' );
+        my $s = Koha::Virtualshelves->find( $shelf->{shelfnumber} );
+        $shelf->{can_manage_shelf} = $s->can_be_managed( $loggedinuser );
+        $shelf->{can_delete_shelf} = $s->can_be_deleted( $loggedinuser );
     }
     return {
         iTotalRecords => $iTotalRecords,
index f22e014..69a85c1 100644 (file)
@@ -68,7 +68,6 @@ sub shelfpage {
     loggedinuser => $loggedinuser,
     OpacAllowPublicListCreation => C4::Context->preference('OpacAllowPublicListCreation'),
     );
-    my $edit;
     my $shelves;
     my @paramsloop;
     my $totitems;
@@ -209,7 +208,6 @@ sub shelfpage {
                 my $shelf = Koha::Virtualshelves->find( $shelfnumber );
                 my $member = GetMember( 'borrowernumber' => $shelf->owner );
                 my $ownername = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
-                $edit = 1;
                 $template->param(
                     edit                => 1,
                     display             => $displaymode,
@@ -231,9 +229,6 @@ sub shelfpage {
         #View a shelf
         if ( $shelfnumber = $query->param('viewshelf') ) {
             my $shelf = Koha::Virtualshelves->find( $shelfnumber );
-            $template->param(
-                'DisplayMultiPlaceHold' => C4::Context->preference('DisplayMultiPlaceHold'),
-            );
             if (C4::Context->preference('TagsEnabled')) {
                 $template->param(TagsEnabled => 1);
                     foreach (qw(TagsShowOnList TagsInputOnList)) {
@@ -494,20 +489,6 @@ sub shelfpage {
         csv_profiles                                                       => GetCsvProfilesLoop('marc')
     );
 
-    unless( $shelfnumber or $shelves or $edit ) {
-        # Only used for intranet
-        $template->param( op => 'list' );
-    }
-
-    if ($shelves or    # note: this part looks duplicative, but is intentional
-        $edit
-      ) {
-        $template->param( seflag => 1 );
-        #This hack is just another argument for refactoring this script one day
-        #At this point you are adding or editing a list; if you add, then you add a private list (by default) with permissions as below; if you edit, do not pass these permissions, they must come from the database
-        $template->param( allow_add => 0, allow_delete_own => 1, allow_delete_other => 0) unless $shelfnumber;
-    }
-
 #Next call updates the shelves for the Lists button.
 #May not always be needed (when nothing changed), but doesn't take much.
     my ($total, $pubshelves, $barshelves) = C4::VirtualShelves::GetSomeShelfNames($loggedinuser, 'MASTHEAD');
index 7edacaa..3cd257c 100644 (file)
@@ -19,6 +19,9 @@ use Modern::Perl;
 
 use Carp;
 
+use C4::Auth;
+
+use Koha::Borrowers;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Exceptions;
@@ -201,6 +204,59 @@ sub remove_biblios {
     return $number_removed;
 }
 
+sub can_be_viewed {
+    my ( $self, $borrowernumber ) = @_;
+    return 1 if $self->category == $PUBLIC;
+    return 0 unless $borrowernumber;
+    return 1 if $self->owner == $borrowernumber;
+    return $self->get_shares->search(
+        {
+            borrowernumber => $borrowernumber,
+        }
+    )->count;
+}
+
+sub can_be_deleted {
+    my ( $self, $borrowernumber ) = @_;
+
+    return 0 unless $borrowernumber;
+    return 1 if $self->owner == $borrowernumber;
+
+    my $patron = Koha::Borrowers->find( $borrowernumber );
+
+    return 1 if $self->category == $PUBLIC and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
+
+    return 0;
+}
+
+sub can_be_managed {
+    my ( $self, $borrowernumber ) = @_;
+    return 1
+      if $borrowernumber and $self->owner == $borrowernumber;
+    return 0;
+}
+
+sub can_biblios_be_added {
+    my ( $self, $borrowernumber ) = @_;
+
+    return 1
+      if $borrowernumber
+      and ( $self->owner == $borrowernumber
+         or $self->allow_add );
+    return 0;
+}
+
+sub can_biblios_be_removed {
+    my ( $self, $borrowernumber ) = @_;
+
+    return 1
+      if $borrowernumber
+      and (  $self->owner == $borrowernumber
+          or $self->allow_delete_own
+          or $self->allow_delete_other );
+    return 0;
+}
+
 sub type {
     return 'Virtualshelve';
 }
index aacf6c8..6ad8c64 100644 (file)
@@ -1,7 +1,7 @@
 <script type="text/javascript">
 //<![CDATA[
 
-[% IF ( viewshelf ) %]
+[% IF op == 'view' %]
 
     function sendList(){
         open(CGIBIN+'virtualshelves/sendshelf.pl?shelfid=[% shelfnumber %]','win_form','scrollbars=no,resizable=no,height=300,width=450,top=50,left=100');
@@ -13,7 +13,7 @@
             return false;
         });
         $("#deleteshelf").click(function(e){
-            if(confirmDelete(_("Are you sure you want to delete this list?"))){
+            if(confirm(_("Are you sure you want to delete this list?"))){
                 return true;
             } else {
                 e.preventDefault();
 </script>
 
 <div id="toolbar" class="btn-toolbar">
-    <div class="btn-group"><a id="newshelf" class="btn btn-small" href="/cgi-bin/koha/virtualshelves/shelves.pl?shelves=1"><i class="fa fa-plus"></i> New list</a></div>
+    <div class="btn-group"><a id="newshelf" class="btn btn-small" href="/cgi-bin/koha/virtualshelves/shelves.pl?op=add_form&amp;referer=[% op %]"><i class="fa fa-plus"></i> New list</a></div>
 
-    [% IF ( viewshelf ) %]
-        [% IF ( manageshelf ) %]
+    [% IF op == 'view' %]
+        [% IF can_manage_shelf %]
         <div class="btn-group">
             <button class="btn btn-small dropdown-toggle" data-toggle="dropdown"><i class="fa fa-pencil"></i> Edit <span class="caret"></span></button>
-                <ul class="dropdown-menu">
-                    <li><a href="/cgi-bin/koha/virtualshelves/shelves.pl?shelfnumber=[% shelfnumber %]&amp;op=modif&amp;display=viewshelf">Edit list</a></li>
-                    [% IF ( showprivateshelves ) %]
-                        <li><a id="deleteshelf" href="/cgi-bin/koha/virtualshelves/shelves.pl?display=privateshelves&amp;shelves=1&amp;DEL-[% shelfnumber %]=1">Delete list</a></li>
-                    [% ELSE %]
-                        <li><a id="deleteshelf" href="/cgi-bin/koha/virtualshelves/shelves.pl?shelves=1&amp;DEL-[% shelfnumber %]=1">Delete list</a></li>
-                    [% END %]
-                </ul>
+            <ul class="dropdown-menu">
+                <li><a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=edit_form&amp;shelfnumber=[% shelf.shelfnumber %]&amp;referer=[% op %]">Edit list</a></li>
+                <li><a id="deleteshelf" href="/cgi-bin/koha/virtualshelves/shelves.pl?op=delete&amp;shelfnumber=[% shelf.shelfnumber %]">Delete list</a></li>
+            </ul>
         </div>
         [% END %]
 
@@ -56,6 +52,6 @@
                 </ul>
         </div>
         <div class="btn-group"><a class="btn btn-small" href="#" id="sendlist"><i class="fa fa-envelope"></i> Send list</a></div>
-        <div class="btn-group"><a class="btn btn-small" id="printlist" target="_blank" href="/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=[% shelfnumber %]&amp;print=1"><i class="fa fa-print"></i> Print list</a></div>
+        <div class="btn-group"><a class="btn btn-small" id="printlist" target="_blank" href="/cgi-bin/koha/virtualshelves/shelves.pl?op=view&amp;shelfnumber=[% shelf.shelfnumber %]&amp;print=1"><i class="fa fa-print"></i> Print list</a></div>
     [% END %]
 </div>
index 15cf03a..7a78ae9 100644 (file)
@@ -360,7 +360,7 @@ function verify_images() {
         [% IF ( GetShelves ) %]
             <span class="results_summary"><span class="label">Lists that include this title: </span>
             [% FOREACH GetShelve IN GetShelves %]
-                <a href="/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=[% GetShelve.shelfnumber %]">[% GetShelve.shelfname %]</a>
+                <a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=view&amp;shelfnumber=[% GetShelve.shelfnumber %]">[% GetShelve.shelfname %]</a>
                 [% IF ( loop.last ) %][% ELSE %]|[% END %]
             [% END %]
             </span>
@@ -525,7 +525,7 @@ function verify_images() {
                <li><strong>Lists that include this title: </strong>
                <ul>
                [% FOREACH GetShelve IN GetShelves %]
-                       <li><a href="/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=[% GetShelve.shelfnumber %]">[% GetShelve.shelfname %]</a></li>
+                <li><a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=view&amp;shelfnumber=[% GetShelve.shelfnumber %]">[% GetShelve.shelfname %]</a></li>
                [% END %]
                </ul>
                </li>
index 00cb335..96fa994 100644 (file)
@@ -3,6 +3,22 @@
 [% INCLUDE 'doc-head-close.inc' %]
 </head>
 <body id="lists_downloadshelf" class="lists" style="padding:1em;">
+
+[% FOR m IN messages %]
+    <div class="dialog [% m.type %]">
+        [% SWITCH m.code %]
+        [% CASE 'unauthorized' %]
+            You do not have permission to view this list.
+        [% CASE 'does_not_exist' %]
+            This list does not exist.
+        [% CASE %]
+            [% m.code %]
+        [% END %]
+    </div>
+[% END %]
+
+
+
 [% IF ( format ) %]
     <p>Your download should begin automatically.</p>
 [% ELSE %]
index ea5812a..b039ba5 100644 (file)
@@ -1,5 +1,9 @@
+[% USE Koha %]
+[% USE KohaDates %]
+[% SET PRIVATE = 1 %]
+[% SET PUBLIC = 2 %]
 [% INCLUDE 'doc-head-open.inc' %]
-<title>Koha &rsaquo; [% IF ( viewshelf ) %]Lists &rsaquo; Contents of [% shelfname | html %][% ELSE %]Lists[% END %][% IF ( shelves ) %] &rsaquo; Create new list[% END %][% IF ( edit ) %] &rsaquo; Edit list [% shelfname | html %][% END %]</title>
+<title>Koha &rsaquo; [% IF op == 'view' %]Lists &rsaquo; Contents of [% shelf.shelfname | html %][% ELSE %]Lists[% END %][% IF op == 'add_form' %] &rsaquo; Create new list[% END %][% IF op == 'edit_form' %] &rsaquo; Edit list [% shelf.shelfname | html %][% END %]</title>
 [% INCLUDE 'doc-head-close.inc' %]
 <link rel="stylesheet" type="text/css" href="[% themelang %]/css/datatables.css" />
 [% INCLUDE 'datatables.inc' %]
@@ -14,7 +18,7 @@
 </script>
 [% END %]
 
-[% IF ( viewshelf ) %]
+[% IF op == 'view' %]
     <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.checkboxes.min.js"></script>
     <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.fixFloat.js"></script>
 [% END %]
@@ -27,7 +31,11 @@ var MSG_CONFIRM_DELETE_LIST = _("Are you sure you want to remove this list?");
 
 [% IF op == 'list' %]
 $(document).ready(function(){
-    var type = 1;
+    [% IF category == PUBLIC %]
+        var type = [% PUBLIC %];
+    [% ELSE %]
+        var type = [% PRIVATE %];
+    [% END %]
     var dtListResults = $("#listresultst").dataTable($.extend(true, {}, dataTablesDefaults, {
         'bServerSide': true,
         'sAjaxSource': "/cgi-bin/koha/svc/virtualshelves/search",
@@ -81,13 +89,18 @@ $(document).ready(function(){
     dtListResults.fnAddFilters("filter", 750);
 
     var tabs = $("#tabs").tabs({
+        [% IF category == PUBLIC %]
+            active: 1,
+        [% ELSE %]
+            active: 0,
+        [% END %]
         activate: function(e, ui) {
             var active = tabs.tabs("option", "active" );
             if ( active == 0 ) {
-                type = 1; // private
+                type = [% PRIVATE %];
                 dtListResults.fnDraw();
             } else if ( active == 1 ) {
-                type = 2; // public
+                type = [% PUBLIC %];
                 dtListResults.fnDraw();
             }
         }
@@ -95,7 +108,7 @@ $(document).ready(function(){
 });
 [% END %]
 
-[% IF ( viewshelf ) %]
+[% IF op == 'view' %]
 $(document).ready(function(){
     [% IF ( itemsloop ) %]$('#searchheader').fixFloat();[% END %]
     $("span.clearall").html("<a id=\"CheckNone\" href=\"/cgi-bin/koha/shelves.pl\">"+_("Clear all")+"<\/a>");
@@ -173,14 +186,6 @@ $(document).ready(function(){
 });
 [% END %]
 
-       function confirmDelete(message){
-               if (window.confirm(message)) {
-                   location.href="/cgi-bin/koha/virtualshelves/shelves.pl?[% IF ( showprivateshelves ) %]display=privateshelves&[% END %]shelves=1&DEL-[% shelfnumber %]=1&shelfoff=[% shelfoff %]";
-               } else { 
-                       return false;
-               }
-       }
-
     /**
      * This function checks if the adequate number of records are checked for merging
      */
@@ -272,35 +277,28 @@ function placeHold () {
 [% INCLUDE 'header.inc' %]
 [% INCLUDE 'cat-search.inc' %]
 
-[% BLOCK list_permissions %]
-    <li>
-        <label for="permissions">Permissions: </label>
-        <select name="allow_add" id="allow_add">
-            [% IF allow_add %]<option value="0">Do not allow</option>[% ELSE %]<option value="0" selected="selected">Do not allow</option>[% END %]
-            [% IF allow_add %]<option value="1" selected="selected">Allow</option>[% ELSE %]<option value="1">Allow</option>[% END %]
-        </select>
-        &nbsp;<span>anyone else to add entries.</span>
-    </li>
-    <li>
-        <label>&nbsp;</label>
-        <select name="allow_delete_own" id="allow_delete_own">
-            [% IF allow_delete_own %]<option value="0">Do not allow</option>[% ELSE %]<option value="0" selected="selected">Do not allow</option>[% END %]
-            [% IF allow_delete_own %]<option value="1" selected="selected">Allow</option>[% ELSE %]<option value="1">Allow</option>[% END %]
-        </select>
-        &nbsp;<span>anyone to remove his own contributed entries.</span>
-    </li>
-    <li>
-        <label>&nbsp;</label>
-        <select name="allow_delete_other" id="allow_delete_other">
-            [% IF allow_delete_other %]<option value="0">Do not allow</option>[% ELSE %]<option value="0" selected="selected">Do not allow</option>[% END %]
-            [% IF allow_delete_other %]<option value="1" selected="selected">Allow</option>[% ELSE %]<option value="1">Allow</option>[% END %]
-        </select>
-        &nbsp;<span>anyone to remove other contributed entries.</span>
-    </li>
-[% END %]
-<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/virtualshelves/shelves.pl">Lists</a> [% IF ( category1 ) %] &rsaquo; [% IF ( viewshelf || edit ) %] <a href="/cgi-bin/koha/virtualshelves/shelves.pl?display=privateshelves">Your lists</a> [% ELSE %] Your lists [% END %] [% ELSIF ( category2 ) %] &rsaquo; [% IF ( viewshelf ) %] <a href="/cgi-bin/koha/virtualshelves/shelves.pl?display=publicshelves">Public lists</a> [% ELSE %] Public lists [% END %] [% ELSIF ( showprivateshelves ) %] &rsaquo; [% IF ( viewshelf || edit ) %] <a href="/cgi-bin/koha/virtualshelves/shelves.pl?display=privateshelves">Your lists</a> [% ELSE %] Your lists [% END %] [% ELSIF ( showpublicshelves ) %] &rsaquo; [% IF ( viewshelf || edit ) %] <a href="/cgi-bin/koha/virtualshelves/shelves.pl?display=publicshelves">Public lists</a> [% ELSE %] Public lists [% END %] [% END %]
+<div id="breadcrumbs">
+    <a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo;
+    [% IF op != 'list' %]
+        <a href="/cgi-bin/koha/virtualshelves/shelves.pl">Lists</a>
+    [% ELSE %]
+        Lists
+    [% END %]
+    [% IF shelf AND shelf.category == PRIVATE %] &rsaquo;
+        [% IF op == 'view' OR op == 'edit_form' %]
+            <a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=list&amp;category=[% PRIVATE %]">Your lists</a>
+        [% ELSE %]
+            Your lists
+        [% END %]
+    [% ELSIF shelf AND shelf.category == PUBLIC %] &rsaquo;
+        [% IF op == 'view' %]
+            <a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=list&amp;category=[% PUBLIC %]">Public lists</a>
+        [% ELSE %]
+            Public lists
+        [% END %]
+    [% END %]
 
-[% IF ( viewshelf ) %]&rsaquo; Contents of <i>[% shelfname | html %]</i>[% END %][% IF ( shelves ) %] &rsaquo; Create new list[% END %][% IF ( edit ) %] &rsaquo; Edit list <i>[% shelfname | html %]</i>[% END %]</div>
+[% IF op == 'view' %]&rsaquo; Contents of <i>[% shelf.shelfname | html %]</i>[% END %][% IF op == 'add_form' %] &rsaquo; Create new list[% END %][% IF op == 'edit_form' %] &rsaquo; Edit list <i>[% shelf.shelfname | html %]</i>[% END %]</div>
 
 <div id="doc3" class="yui-t2">
 <div id="bd">
@@ -318,70 +316,53 @@ function placeHold () {
         [% CASE 'error_on_insert' %]
             An error occurred when inserting this list. Perhaps the name already exists.
         [% CASE 'error_on_delete' %]
-            An error occurred when deleteing this list. Check the logs.
+            An error occurred when deleting this list. Check the logs.
+        [% CASE 'error_on_add_biblio' %]
+            The item has not been added to the list. Please check it's not in this list yet.
         [% CASE 'success_on_update' %]
             List updated with success.
         [% CASE 'success_on_insert' %]
             List inserted with success.
         [% CASE 'success_on_delete' %]
             List deleted with success.
-        [% CASE 'Koha::Exception::DuplicateObject' %]
+        [% CASE 'success_on_add_biblio' %]
+            The item has been added to the list.
+        [% CASE 'success_on_remove_biblios' %]
+            The item has been removed from the list.
+        [% CASE 'does_not_exist' %]
+            This list does not exist.
+        [% CASE 'item_does_not_exist' %]
+            This item does not exist.
+        [% CASE 'unauthorized_on_view' %]
+            You do not have permission to view this list.
+        [% CASE 'unauthorized_on_update' %]
+            You do not have permission to update this list.
+        [% CASE 'unauthorized_on_delete' %]
+            You do not have permission to delete this list.
+        [% CASE 'unauthorized_on_add_biblio' %]
+            You do not have permission to add a biblio to this list.
+        [% CASE 'no_biblio_removed' %]
+            No biblio has been removed.
+        [% CASE 'Koha::Exceptions::Virtualshelves::DuplicateObject' %]
             An error occurred when inserting this list. The name already [% shelfname %] exists.
+        [% CASE 'DBIx::Class::Exception' %]
+            [% m.msg %]
         [% CASE %]
             [% m.code %]
         [% END %]
     </div>
 [% END %]
 
-[% IF ( paramsloop ) %]
-[% FOREACH paramsloo IN paramsloop %]
-<div class="yui-ge">
-    <div class="yui-u first">
-               [% IF ( paramsloo.status ) %]<div class="dialog alert">[% paramsloo.string %]</div>[% END %]
-               [% IF ( paramsloo.nobarcode ) %]<div class="dialog alert">ERROR: No barcode given.</div>[% END %] 
-        [% IF ( paramsloo.noshelfnumber ) %]<div class="dialog alert">ERROR: No list number given.</div>[% END %]
-               [% IF ( paramsloo.need_confirm ) %]
-               <div class="dialog alert">The list <i>[% paramsloo.need_confirm %]</i> is not empty.
-            [% IF ( paramsloo.single ) %]
-                <br />It has <b>[% paramsloo.count %]</b> entry.
-            [% ELSE %]
-                <br />It has <b>[% paramsloo.count %]</b> entries.
-            [% END %]
-                       <br />Use the "Confirm" button below to confirm deletion.
-               </div>
-               [% END %]
-               [% IF ( paramsloo.nopermission ) %]
-               <div class="dialog alert">ERROR: You do not have adequate permission for that action on list [% paramsloo.nopermission %].</div>
-               [% END %]
-               [% IF ( paramsloo.failgetitem ) %]
-               <div class="dialog alert">ERROR: No item found with barcode [% paramsloo.failgetitem %].</div>
-               [% END %] 
-               [% IF ( paramsloo.duplicatebiblio ) %]
-               <div class="dialog alert">A record matching barcode <b>[% paramsloo.duplicatebiblio %]</b> has already been added.</div>
-               [% END %]
-                [% IF ( paramsloo.nothingdeleted) %]
-                      <div class="dialog message">Warning: You could not delete any of the selected items from this list.</div>
-                [% END %]
-                [% IF ( paramsloo.somedeleted) %]
-                      <div class="dialog message">Warning: You could not delete all selected items from this list.</div>
-                [% END %]
-                [% IF ( paramsloo.modifyfailure) %]
-                      <div class="dialog message">ERROR: List could not be modified.</div>
-                [% END %]
-       </div>
-</div>
-[% END %]
-[% END %] 
-
-[% IF ( viewshelf ) %]
+[% IF op == 'view' %]
    <div class="yui-g">
-    [% IF ( itemsloop ) %]
+    [% IF itemsloop %]
 
-    <h3>Contents of <i>[% shelfname | html %]</i></h3>
+    <h3>Contents of <i>[% shelf.shelfname | html %]</i></h3>
     <div class="pages">[% pagination_bar %]</div>
     <form action="/cgi-bin/koha/virtualshelves/shelves.pl" id="listform" method="post" class="checkboxed">
-        <input type="hidden" name="viewshelf" value="[% shelfnumber %]" />
-        <input type="hidden" name="modifyshelfcontents" value="1" />
+        <input type="hidden" name="op" value="remove_biblios" />
+        <input type="hidden" name="referer" value="view" />
+        <input type="hidden" name="shelfnumber" value="[% shelf.shelfnumber %]" />
 
 [% IF direction == 'asc' %]
     [% SET new_direction = 'desc' %]
@@ -391,16 +372,16 @@ function placeHold () {
 [% END %]
 
 <div id="searchheader" class="noprint">
-    [% IF ( itemsloop ) %]
+    [% IF itemsloop %]
         <div id="selection_ops"><span class="checkall"></span> |
         <span class="clearall"></span>
 
         <span class="addto">| </span>
         &nbsp;
-        [% IF ( CAN_user_reserveforothers && DisplayMultiPlaceHold ) %]
+        [% IF CAN_user_reserveforothers && Koha.Preference('DisplayMultiPlaceHold') %]
             <div class="btn-group"><button class="btn btn-mini placehold"><i class="fa fa-sticky-note-o"></i> Place hold</button></div>
         [% END %]
-        [% IF ( allowremovingitems ) %]
+        [% IF can_remove_biblios %]
             <div class="btn-group"><button type="submit" class="btn btn-mini list-remove"><i class="fa fa-times-circle"></i> Remove selected</button></div>
         [% END %]
         [% IF ( CAN_user_editcatalogue_edit_catalogue ) %]<div class="btn-group"><button type="submit" class="btn btn-mini merge-items"><i class="fa fa-compress"></i> Merge selected</button></div>[% END %]
@@ -414,16 +395,16 @@ function placeHold () {
 
                 [% UNLESS ( item_level_itypes ) %]<th>Item type</th>[% END %]
                 <th>
-                    <a href="/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=[% shelfnumber %]&amp;sort=title&amp;direction=[% IF sort != 'title' %]asc[% ELSE %][% new_direction %][% END %]">Title</a>
-                    [% IF sort == 'title' %]
+                    <a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=view&amp;shelfnumber=[% shelf.shelfnumber %]&amp;sortfield=title&amp;direction=[% IF sortfield != 'title' %]asc[% ELSE %][% new_direction %][% END %]">Title</a>
+                    [% IF sortfield == 'title' %]
                         <img src="[% interface %]/[% theme %]/img/[% direction %].gif" alt="[% direction %] sort" />
                     [% ELSE %]
                         <img src="[% interface %]/[% theme %]/img/ascdesc.gif" alt="" />
                     [% END %]
                 </th>
                 <th>
-                    <a href="/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=[% shelfnumber %]&amp;sort=author&amp;direction=[% IF sort != 'author' %]asc[% ELSE %][% new_direction %][% END %]">Author</a>
-                   [% IF sort == 'author' %]
+                    <a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=view&amp;shelfnumber=[% shelf.shelfnumber %]&amp;sortfield=author&amp;direction=[% IF sortfield != 'author' %]asc[% ELSE %][% new_direction %][% END %]">Author</a>
+                   [% IF sortfield == 'author' %]
                         <img src="[% interface %]/[% theme %]/img/[% direction %].gif" alt="[% direction %] sort" />
                    [% ELSE %]
                        <img src="[% interface %]/[% theme %]/img/ascdesc.gif" alt="" />
@@ -431,30 +412,25 @@ function placeHold () {
                  </th>
                 <th>Date added</th>
                 <th>
-                    <a href="/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=[% shelfnumber %]&amp;sort=itemcallnumber&amp;direction=[% IF sort != 'itemcallnumber' %]asc[% ELSE %][% new_direction %][% END %]">Call number</a>
-                   [% IF sort == 'itemcallnumber' %]
+                    <a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=view&amp;shelfnumber=[% shelf.shelfnumber %]&amp;sortfield=itemcallnumber&amp;direction=[% IF sortfield != 'itemcallnumber' %]asc[% ELSE %][% new_direction %][% END %]">Call number</a>
+                   [% IF sortfield == 'itemcallnumber' %]
                         <img src="[% interface %]/[% theme %]/img/[% direction %].gif" alt="[% direction %] sort" />
                    [% ELSE %]
                        <img src="[% interface %]/[% theme %]/img/ascdesc.gif" alt="" />
                    [% END %]
                  </th>
             </tr>
-               [% FOREACH itemsloo IN itemsloop %]
-                       [% UNLESS ( loop.odd ) %]
-                       <tr class="highlight">
-                       [% ELSE %]
-                       <tr>
-                       [% END %]
-                       [% IF ( itemsloop ) %]
-                       <td>
-                               [% IF ( itemsloo.confirm ) %]
-                               <input type="hidden"   name="CONFIRM-[% itemsloo.confirm %]" />
-                               <input type="checkbox" class="selection" value="[% itemsloo.biblionumber %]" name="REM-[% itemsloo.biblionumber %]" checked />
-                               [% ELSE %]
-                               <input type="checkbox" class="selection" value="[% itemsloo.biblionumber %]" name="REM-[% itemsloo.biblionumber %]" />
-                               [% END %]
-                       </td>
-                       [% END %]
+        [% FOREACH itemsloo IN itemsloop %]
+            [% UNLESS ( loop.odd ) %]
+                <tr class="highlight">
+            [% ELSE %]
+                <tr>
+            [% END %]
+            [% IF itemsloop %]
+                <td>
+                    <input type="checkbox" class="selection" value="[% itemsloo.biblionumber %]" name="biblionumber" />
+                </td>
+            [% END %]
                        [% UNLESS ( item_level_itypes ) %]<td>
                 [% UNLESS ( noItemTypeImages || !itemsloo.imageurl ) %]<img src="[% itemsloo.imageurl %]" alt="[% itemsloo.description %]" title="[% itemsloo.description %]" />[% END %][% itemsloo.description %]
                        </td>[% END %]
@@ -483,10 +459,10 @@ function placeHold () {
                             | <a href="/cgi-bin/koha/cataloguing/additem.pl?biblionumber=[% itemsloo.biblionumber %]">Edit items</a>
                         [% END %]
                     </p>
-                       </td>
-                       <td>[% itemsloo.author %]</td>
-                       <td>[% itemsloo.dateadded %]</td>
-                       <td>
+            </td>
+            <td>[% itemsloo.author %]</td>
+            <td>[% itemsloo.dateadded | $KohaDates%]</td>
+            <td>
                 <ul>
                 [% FOREACH result IN itemsloo.ITEM_RESULTS %]
                     <li>[% result.holdingbranch %] [% IF ( result.location_intranet ) %] ([% result.location_intranet %]) [% END %]
@@ -505,88 +481,111 @@ function placeHold () {
 
        [% END %]
    </div>
-[% END %]<!-- /viewshelf -->
+[% END %]
 
-[% IF ( debug ) %]
-  [% IF ( edit ) %]<div>Edit is on ([% shelfname | html %])</div>[% END %]
-  [% IF ( seflag ) %]<div>seflag is on ([% seflag %])</div>[% END %]
+[% IF can_add_biblios %]
+<div class="yui-g">
+<form action="/cgi-bin/koha/virtualshelves/shelves.pl" method="post">
+ <fieldset class="brief noprint">
+    <legend>Add an item to <i>[% shelfname | html %]</i></legend>
+        <ol>
+            <li>
+                <label for="barcode">Barcode:</label>
+                <input name="barcode" type="text" id="barcode" size="14" />
+                <input type="hidden" name="op" value="add_biblio" />
+                <input type="hidden" name="referer" value="view" />
+                <input type="hidden" name="shelfnumber" value="[% shelf.shelfnumber %]" />
+                <input type="submit" value="Add" />
+            </li>
+        </ol>
+ </fieldset>
+</form>
+</div>
 [% END %]
 
-[% IF ( seflag ) %]
+[% IF op == 'add_form' OR op == 'edit_form' %]
 <div class="yui-ge">
     <div class="yui-u first">
     <form method="post" action="/cgi-bin/koha/virtualshelves/shelves.pl" class="validated">
         <fieldset class="rows">
 
-    [% IF ( shelves ) %]
-        <legend>Create a new list</legend>
-        <input type="hidden" name="shelves" value="1" />
+        [% IF op == 'add_form' %]
+            <legend>Create a new list</legend>
+            <input type="hidden" name="op" value="add" />
+        [% ELSE %]
+            <legend>Edit list <i>[% shelf.shelfname | html %]</i></legend>
+            <input type="hidden" name="op" value="edit" />
+        [% END %]
+        <input type="hidden" name="referer" value="[% referer %]" />
+        <input type="hidden" name="shelfnumber" value="[% shelf.shelfnumber %]" />
         <ol>
-        <li><label class="required" for="addshelf">List name:</label><input id="addshelf" type="text" name="addshelf" size="25" required="required" class="required" />
+            <li>
+                <label for="shelfname" class="required">Name: </label><input type="text" id="shelfname" name="shelfname" size="25" value="[% shelf.shelfname |html %]" required="required" class="required" />
             <span class="required">Required</span>
-        </li>
-        <li><span class="label">Owner: </span><input type="hidden" name="owner" id="owner" value="[% loggedinuser %]" />[% loggedinusername %]</li>
-        <li><label for="sortfield" >Sort this list by: </label>
-        <select name="sortfield" id="sortfield">
-            <option value="title">Title</option>
-            <option value="author">Author</option>
-            <option value="copyrightdate">Copyrightdate</option>
-            <option value="itemcallnumber">Call number</option>
-        </select></li>
-        <li><label for="category">Category: </label>
-            <select name="category" id="category">
-                  <option value="1">Private</option>
-                  <option value="2">Public</option>
-                     </select></li>
-            [% INCLUDE list_permissions %]
-        </ol>
-    [% END %]
-
-    [% IF ( edit ) %]
-        <legend>Edit list <i>[% shelfname | html %]</i></legend>
-        <input type="hidden" name="display" value="[% display %]" />
-        <input type="hidden" name="op" value="modifsave" />
-        [% IF ( showprivateshelves ) %]<input type="hidden" name="display" value="privateshelves" />[% END %]
-        <input type="hidden" name="shelfnumber" value="[% shelfnumber %]" />
-               <ol>
-            <li><label for="shelfname" class="required">Name: </label><input type="text" id="shelfname" name="shelfname" size="25" value="[% shelfname |html %]" required="required" class="required" />
-            <span class="required">Required</span>
-        </li>
-               <li><label for="owner">Owner: </label><input type="hidden" id="owner" name="owner" value="[% IF ( owner ) %][% ownername %][% ELSE %][% loggedinusername %][% END %]" />[% IF ( owner ) %][% ownername %][% ELSE %][% loggedinusername %][% END %]</li>
-               <li><label for="sortfield" >Sort this list by: </label>
-               <select name="sortfield">
-        [% IF ( sortfield == "title" ) %]<option value="title" selected="selected">Title</option>[% ELSE %]<option value="title">Title</option>[% END %]
-        [% IF ( sortfield == "author" ) %]<option value="author" selected="selected">Author</option>[% ELSE %]<option value="author">Author</option>[% END %]
-        [% IF ( sortfield == "copyrightdate" ) %]<option value="copyrightdate" selected="selected">Copyrightdate</option>[% ELSE %]<option value="copyrightdate">Copyrightdate</option>[% END %]
-        [% IF ( sortfield == "itemcallnumber" ) %]<option value="itemcallnumber" selected="selected">Call number</option>[% ELSE %]<option value="itemcallnumber">Call number</option>[% END %]
-               </select></li>
-               <li><label for="category">Category: </label>
-                       <select id="category" name="category">
-                       [% IF ( category1 ) %]
-                               <option value="1" selected="selected">Private</option>
-                       [% ELSE %]
-                               <option value="1">Private</option>
-                       [% END %]
-                       [% IF ( category2 ) %]
-                               <option value="2" selected="selected">Public</option>
-                       [% ELSE %]
-                               <option value="2">Public</option>
-                       [% END %]
-                       </select></li>
-            [% INCLUDE list_permissions %]
-            </ol>
-       [% END %]
+            </li>
+            <li>
+                <span class="label">Owner: </span>
+                [% IF op == 'add_form' %]
+                    <input type="hidden" name="owner" id="owner" value="[% loggedinusernumber %]" />[% loggedinusername %]</li>
+                [% ELSE %]
+                    [% IF owner %]
+                        <input type="hidden" id="owner" name="owner" value="[% owner.borrowernumber %]" />[% owner.firstname _ ' ' _ owner.surname %]
+                    [% ELSE %]
+                        <input type="hidden" id="owner" name="owner" value="[% loggedinusernumber %]" />[% loggedinusername %]
+                    [% END %]
+                [% END %]
+            </li>
+            <li><label for="sortfield" >Sort this list by: </label>
+            <select name="sortfield">
+            [% IF shelf.sortfield == "title" %]<option value="title" selected="selected">Title</option>[% ELSE %]<option value="title">Title</option>[% END %]
+            [% IF shelf.sortfield == "author" %]<option value="author" selected="selected">Author</option>[% ELSE %]<option value="author">Author</option>[% END %]
+            [% IF shelf.sortfield == "copyrightdate" %]<option value="copyrightdate" selected="selected">Copyrightdate</option>[% ELSE %]<option value="copyrightdate">Copyrightdate</option>[% END %]
+            [% IF shelf.sortfield == "itemcallnumber" %]<option value="itemcallnumber" selected="selected">Call number</option>[% ELSE %]<option value="itemcallnumber">Call number</option>[% END %]
+            </select></li>
+            <li><label for="category">Category: </label>
+                <select id="category" name="category">
+                [% IF shelf.category == PRIVATE %]
+                    <option value="1" selected="selected">Private</option>
+                [% ELSE %]
+                    <option value="1">Private</option>
+                [% END %]
+                [% IF shelf.category == PUBLIC %]
+                    <option value="2" selected="selected">Public</option>
+                [% ELSE %]
+                    <option value="2">Public</option>
+                [% END %]
+                           </select></li>
 
-               </fieldset>
+            [% FOR permission IN ['allow_add', 'allow_delete_own', 'allow_delete_other'] %]
+                <li>
+                    [% IF loop.first %]
+                        <label for="permissions">Permissions: </label>
+                    [% ELSE %]
+                        <label>&nbsp;</label>
+                    [% END %]
+                    <select name="[% permission %]" id="[% permission %]">
+                        [% IF shelf.$permission %]<option value="0">Do not allow</option>[% ELSE %]<option value="0" selected="selected">Do not allow</option>[% END %]
+                        [% IF shelf.$permission %]<option value="1" selected="selected">Allow</option>[% ELSE %]<option value="1">Allow</option>[% END %]
+                    </select>
+                    [% SWITCH permission %]
+                        [% CASE 'allow_add' %]&nbsp;<span>anyone else to add entries.</span>
+                        [% CASE 'allow_delete_own' %]&nbsp;<span>anyone to remove his own contributed entries.</span>
+                        [% CASE 'allow_delete_other' %]&nbsp;<span>anyone to remove other contributed entries.</span>
+                    [% END %]
+                </li>
+            [% END %]
+        </ol>
+    </fieldset>
 
-    <fieldset class="action"><input type="submit" value="Save" class="submit" />
-        [% IF ( showprivateshelves ) %]
-            <a href="/cgi-bin/koha/virtualshelves/shelves.pl?display=privateshelves" class="cancel">Cancel</a>
-        [% ELSE %]
-            [% IF ( display == "viewshelf" ) %]
-               <a href="/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=[% shelfnumber %]" class="cancel">Cancel</a>
+    <fieldset class="action">
+        <input type="submit" value="Save" class="submit" />
+        [% IF referer == 'view' %]
+           <a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=view&amp;shelfnumber=[% shelf.shelfnumber %]" class="cancel">Cancel</a>
+       [% ELSE %]
+            [% IF category == PUBLIC %]
+                <a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=list&amp;category=[% PUBLIC %]" class="cancel">Cancel</a>
             [% ELSE %]
-                <a href="/cgi-bin/koha/virtualshelves/shelves.pl" class="cancel">Cancel</a>
+                <a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=list&amp;category=[% PRIVATE %]" class="cancel">Cancel</a>
             [% END %]
         [% END %]
     </fieldset>
@@ -601,7 +600,7 @@ function placeHold () {
         </div>
     </div>
 </div>
-[% END %]<!-- /seflag -->
+[% END %]
 
 [% IF op == 'list' %]
     <h2>Lists</h2>
index 646b3b4..08d75a6 100644 (file)
@@ -10,7 +10,7 @@
                 "dt_type":
                     "[% data.type %]",
                 "dt_shelfname":
-                    "<a href='/cgi-bin/koha/virtualshelves/shelves.pl?viewshelf=[% data.shelfnumber %]'>[% data.shelfname | html%]</a>",
+                    "<a href='/cgi-bin/koha/virtualshelves/shelves.pl?op=view&shelfnumber=[% data.shelfnumber %]'>[% data.shelfname | html%]</a>",
                 "dt_count":
                     "[% data.count %] item(s)",
                 "dt_owner":
@@ -34,7 +34,8 @@
     [%~ IF can_manage_shelf ~%]
         [%~ action_block =                '<form action="shelves.pl" method="get">' ~%]
         [%~ action_block = action_block _ '<input type="hidden" name="shelfnumber" value="' _ shelfnumber  _ '" />' ~%]
-        [%~ action_block = action_block _ '<input type="hidden" name="op" value="modif" />' ~%]
+        [%~ action_block = action_block _ '<input type="hidden" name="op" value="edit_form" />' ~%]
+        [%~ action_block = action_block _ '<input type="hidden" name="category" value="' _ type _ '" />' ~%]
         [%~ action_block = action_block _ '<input type="submit" class="editshelf" value="Edit" />' ~%]
         [%~ action_block = action_block _ '</form>' ~%]
     [%~ END ~%]
         [%~ action_block = action_block _ '<form action="shelves.pl" method="post">' ~%]
         [%~ action_block = action_block _ '<input type="hidden" name="shelfoff" value="' _ shelfoff _ '" />' ~%]
         [%~ action_block = action_block _ '<input type="hidden" name="shelves" value="1" />' ~%]
-        [%~ action_block = action_block _ '<input type="hidden" name="DEL-' _ shelfnumber _ '" value="1" />' ~%]
-        [%~ action_block = action_block _ '<input type="hidden" name="CONFIRM-' _ shelfnumber _ '" value="1" />' ~%]
-        [%~ IF type == 1 ~%]
-            [%~ action_block = action_block _ '<input type="hidden" name="display" value="privateshelves" />' ~%]
-        [%~ ELSE ~%]
-            [%~ action_block = action_block _ '<input type="hidden" name="display" value="publicshelves" />' ~%]
-        [%~ END ~%]
+        [%~ action_block = action_block _ '<input type="hidden" name="op" value="delete" />' ~%]
+        [%~ action_block = action_block _ '<input type="hidden" name="shelfnumber" value="' _ shelfnumber  _ '" />' ~%]
+        [%~ action_block = action_block _ '<input type="hidden" name="category" value="' _ type _ '" />' ~%]
         [%~ action_block = action_block _ '<input type="submit" class="deleteshelf" onclick="return confirmDelete(MSG_CONFIRM_DELETE_LIST)" value="Delete" />' ~%]
         [%~ action_block = action_block _ '</form>' ~%]
     [%~ END ~%]
index 36e6188..aa8b323 100644 (file)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 
 use Modern::Perl;
-use Test::More tests => 3;
+use Test::More tests => 4;
 use DateTime::Duration;
 
 use C4::Context;
@@ -236,3 +236,108 @@ subtest 'Shelf content' => sub {
     $number_of_contents = Koha::Virtualshelfcontents->search->count;
     is( $number_of_contents, 1, 'The biblio should have been deleted to the shelf by the patron 2, even if it is not his own content (allow_delete_other=1)' );
 };
+
+subtest 'Shelf permissions' => sub {
+
+    plan tests => 40;
+    my $patron1 = $builder->build( { source => 'Borrower', value => { flags => '2096766' } } ); # 2096766 is everything checked but not superlibrarian
+    my $patron2 = $builder->build( { source => 'Borrower', value => { flags => '1048190' } } ); # 1048190 is everything checked but not superlibrarian and delete_public_lists
+    my $biblio1 = $builder->build( { source => 'Biblio', } );
+    my $biblio2 = $builder->build( { source => 'Biblio', } );
+    my $biblio3 = $builder->build( { source => 'Biblio', } );
+    my $biblio4 = $builder->build( { source => 'Biblio', } );
+
+
+    my $public_shelf = Koha::Virtualshelf->new(
+        {   shelfname    => "my first shelf",
+            owner        => $patron1->{borrowernumber},
+            category     => 2,
+            allow_add          => 0,
+            allow_delete_own   => 0,
+            allow_delete_other => 0,
+        }
+    )->store;
+
+    is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
+    is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
+
+    is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
+    is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
+
+    is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
+    is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
+
+    is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
+    is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (add) by someone else' );
+
+    is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
+    is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Public list should not be modified (remove) by someone else' );
+
+
+    $public_shelf->allow_add(1);
+    $public_shelf->allow_delete_own(1);
+    $public_shelf->allow_delete_other(1);
+    $public_shelf->store;
+
+    is( $public_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his public list' );
+    is( $public_shelf->can_be_viewed( $patron2->{borrowernumber} ), 1, 'Public list should be viewed by someone else' );
+
+    is( $public_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
+    is( $public_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Public list should not be deleted by someone else' );
+
+    is( $public_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
+    is( $public_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Public list should not be managed by someone else' );
+
+    is( $public_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
+    is( $public_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Public list should not be modified (add) by someone else' );
+
+    is( $public_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
+    is( $public_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Public list should not be modified (remove) by someone else' );
+
+
+    my $private_shelf = Koha::Virtualshelf->new(
+        {   shelfname    => "my first shelf",
+            owner        => $patron1->{borrowernumber},
+            category     => 1,
+            allow_add          => 0,
+            allow_delete_own   => 0,
+            allow_delete_other => 0,
+        }
+    )->store;
+
+    is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
+    is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
+
+    is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
+    is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
+
+    is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
+    is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
+
+    is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
+    is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (add) by someone else' );
+
+    is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
+    is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 0, 'Private list should not be modified (remove) by someone else' );
+
+
+    $private_shelf->allow_add(1);
+    $private_shelf->allow_delete_own(1);
+    $private_shelf->allow_delete_other(1);
+    $private_shelf->store;
+
+    is( $private_shelf->can_be_viewed( $patron1->{borrowernumber} ), 1, 'The owner should be able to view his list' );
+    is( $private_shelf->can_be_viewed( $patron2->{borrowernumber} ), 0, 'Private list should not be viewed by someone else' );
+
+    is( $private_shelf->can_be_deleted( $patron1->{borrowernumber} ), 1, 'The owner should be able to delete his list' );
+    is( $private_shelf->can_be_deleted( $patron2->{borrowernumber} ), 0, 'Private list should not be deleted by someone else' );
+
+    is( $private_shelf->can_be_managed( $patron1->{borrowernumber} ), 1, 'The owner should be able to manage his list' );
+    is( $private_shelf->can_be_managed( $patron2->{borrowernumber} ), 0, 'Private list should not be managed by someone else' );
+
+    is( $private_shelf->can_biblios_be_added( $patron1->{borrowernumber} ), 1, 'The owner should be able to add biblios to his list' );
+    is( $private_shelf->can_biblios_be_added( $patron2->{borrowernumber} ), 1, 'Private list could be modified (add) by someone else # individual check done later' );
+
+    is( $private_shelf->can_biblios_be_removed( $patron1->{borrowernumber} ), 1, 'The owner should be able to remove biblios to his list' );
+    is( $private_shelf->can_biblios_be_removed( $patron2->{borrowernumber} ), 1, 'Private list could be modified (remove) by someone else # individual check done later' );
+};
index 37d300e..e6a1630 100755 (executable)
@@ -155,7 +155,8 @@ sub HandleNewVirtualShelf {
 }
 
 sub HandleShelfNumber {
-    if($authorized= ShelfPossibleAction($loggedinuser, $shelfnumber, 'add')) {
+    my $shelf = Koha::Virtualshelves->find( $shelfnumber );
+    if($authorized = $shelf->can_biblios_be_added( $loggedinuser ) ) {
     AddBibliosToShelf($shelfnumber, @biblionumber);
     #Close this page and return
     print $query->header;
@@ -167,9 +168,9 @@ sub HandleShelfNumber {
 }
 
 sub HandleSelectedShelf {
-    if($authorized= ShelfPossibleAction( $loggedinuser, $shelfnumber, 'add')){
+    my $shelf = Koha::Virtualshelves->find( $shelfnumber );
+    if($authorized = $shelf->can_biblios_be_added( $loggedinuser ) ) {
         #confirm adding to specific shelf
-        my $shelf = Koha::Virtualshelves->find( $shelfnumber );
         $template->param(
         singleshelf               => 1,
         shelfnumber               => $shelf->shelfnumber,
index 0f90340..156e261 100755 (executable)
@@ -31,10 +31,13 @@ use C4::VirtualShelves;
 use C4::Record;
 use C4::Ris;
 use C4::Csv;
+
+use Koha::Virtualshelves;
+
 use utf8;
 my $query = new CGI;
 
-my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
+my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
     {
         template_name   => "virtualshelves/downloadshelf.tt",
         query           => $query,
@@ -47,6 +50,7 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
 my $shelfid = $query->param('shelfid');
 my $format  = $query->param('format');
 my $dbh     = C4::Context->dbh;
+my @messages;
 
 if ($shelfid && $format) {
 
@@ -54,41 +58,52 @@ if ($shelfid && $format) {
     my $marcflavour         = C4::Context->preference('marcflavour');
     my $output='';
 
-    # CSV 
-    if ($format =~ /^\d+$/) {
-       my @biblios;
-       foreach (@$items) {
-           push @biblios, $_->{biblionumber};
-       }
-       $output = marc2csv(\@biblios, $format);
-    }
-    else { #Other formats
-        foreach my $biblio (@$items) {
-            my $biblionumber = $biblio->{biblionumber};
-            my $record = GetMarcBiblio($biblionumber, 1);
-            if ($format eq 'iso2709') {
-                $output .= $record->as_usmarc();
-            }
-            elsif ($format eq 'ris') {
-                $output .= marc2ris($record);
+    my $shelf = Koha::Virtualshelves->find($shelfid);
+    if ( $shelf ) {
+        if ( $shelf->can_be_viewed( $loggedinuser ) ) {
+
+            # CSV
+            if ($format =~ /^\d+$/) {
+                my @biblios;
+                foreach (@$items) {
+                    push @biblios, $_->{biblionumber};
+                }
+                $output = marc2csv(\@biblios, $format);
             }
-            elsif ($format eq 'bibtex') {
-                $output .= marc2bibtex($record, $biblionumber);
+            else { #Other formats
+                foreach my $biblio (@$items) {
+                    my $biblionumber = $biblio->{biblionumber};
+                    my $record = GetMarcBiblio($biblionumber, 1);
+                    if ($format eq 'iso2709') {
+                        $output .= $record->as_usmarc();
+                    }
+                    elsif ($format eq 'ris') {
+                        $output .= marc2ris($record);
+                    }
+                    elsif ($format eq 'bibtex') {
+                        $output .= marc2bibtex($record, $biblionumber);
+                    }
+                }
             }
+            print $query->header(
+            -type => 'application/octet-stream',
+            -'Content-Transfer-Encoding' => 'binary',
+            -attachment=>"shelf.$format");
+            print $output;
+            exit;
+        } else {
+            push @messages, { type => 'error', code => 'unauthorized' };
         }
+    } else {
+        push @messages, { type => 'error', code => 'does_not_exist' };
     }
 
     # If it was a CSV export we change the format after the export so the file extension is fine
     $format = "csv" if ($format =~ m/^\d+$/);
-    
-    print $query->header(
-       -type => 'application/octet-stream',
-       -'Content-Transfer-Encoding' => 'binary',
-       -attachment=>"shelf.$format");
-    print $output;
 }
 else {
     $template->param(csv_profiles => GetCsvProfilesLoop('marc'));
     $template->param(shelfid => $shelfid); 
-    output_html_with_http_headers $query, $cookie, $template->output;
 }
+$template->param( messages => \@messages );
+output_html_with_http_headers $query, $cookie, $template->output;
index 43ae72a..8e7ecc0 100755 (executable)
@@ -1,7 +1,6 @@
 #!/usr/bin/perl
 
-#
-# Copyright 2000-2002 Katipo Communications
+# Copyright 2015 Koha Team
 #
 # This file is part of Koha.
 #
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 use CGI qw ( -utf8 );
-use C4::VirtualShelves::Page;
+use C4::VirtualShelves;
 use C4::Auth;
+use C4::Biblio;
+use C4::Csv;
+use C4::Koha;
+use C4::Items;
+use C4::Members;
+use C4::Output;
+use C4::XSLT;
+use Koha::Virtualshelves;
 
 my $query = new CGI;
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
-    {
-        template_name   => "virtualshelves/shelves.tt",
+    {   template_name   => "virtualshelves/shelves.tt",
         query           => $query,
         type            => "intranet",
         authnotrequired => 0,
         flagsrequired   => { catalogue => 1 },
     }
 );
-$template->param( print => $query->param('print') );
-shelfpage('intranet', $query, $template, $loggedinuser, $cookie);
+
+my $op       = $query->param('op')       || 'list';
+my $referer  = $query->param('referer')  || $op;
+my $category = $query->param('category') || 1;
+my ( $shelf, $shelfnumber, @messages );
+
+if ( $op eq 'add_form' ) {
+    # Nothing to do
+} 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 );
+        $template->param( owner => $patron, );
+        unless ( $shelf->can_be_managed( $loggedinuser ) ) {
+            push @messages, { type => 'error', code => 'unauthorized_on_update' };
+            $op = 'list';
+        }
+    } else {
+        push @messages, { type => 'error', code => 'does_not_exist' };
+    }
+} elsif ( $op eq 'add' ) {
+    eval {
+        $shelf = Koha::Virtualshelf->new(
+            {   shelfname          => $query->param('shelfname'),
+                sortfield          => $query->param('sortfield'),
+                category           => $query->param('category'),
+                allow_add          => $query->param('allow_add'),
+                allow_delete_own   => $query->param('allow_delete_own'),
+                allow_delete_other => $query->param('allow_delete_other'),
+                owner              => $query->param('owner'),
+            }
+        );
+        $shelf->store;
+        $shelfnumber = $shelf->shelfnumber;
+    };
+    if ($@) {
+        push @messages, { type => 'error', code => ref($@), msg => $@ };
+    } elsif ( not $shelf ) {
+        push @messages, { type => 'error', code => 'error_on_insert' };
+    } else {
+        push @messages, { type => 'message', code => 'success_on_insert' };
+        $op = 'view';
+    }
+} elsif ( $op eq 'edit' ) {
+    $shelfnumber = $query->param('shelfnumber');
+    $shelf       = Koha::Virtualshelves->find($shelfnumber);
+
+    if ( $shelf ) {
+        $op = $referer;
+        if ( $shelf->can_be_managed( $loggedinuser ) ) {
+            $shelf->shelfname( $query->param('shelfname') );
+            $shelf->sortfield( $query->param('sortfield') );
+            $shelf->allow_add( $query->param('allow_add') );
+            $shelf->allow_delete_own( $query->param('allow_delete_own') );
+            $shelf->allow_delete_other( $query->param('allow_delete_other') );
+            $shelf->category( $query->param('category') );
+            eval { $shelf->store };
+
+            if ($@) {
+                push @messages, { type => 'error', code => 'error_on_update' };
+                $op = 'edit_form';
+            } else {
+                push @messages, { type => 'message', code => 'success_on_update' };
+            }
+        } else {
+            push @messages, { type => 'error', code => 'unauthorized_on_update' };
+        }
+    } else {
+        push @messages, { type => 'error', code => 'does_not_exist' };
+    }
+} elsif ( $op eq 'delete' ) {
+    $shelfnumber = $query->param('shelfnumber');
+    $shelf       = Koha::Virtualshelves->find($shelfnumber);
+    if ($shelf) {
+        if ( $shelf->can_be_deleted( $loggedinuser ) ) {
+            eval { $shelf->delete; };
+            if ($@) {
+                push @messages, { type => 'error', code => ref($@), msg => $@ };
+            } else {
+                push @messages, { type => 'message', code => 'success_on_delete' };
+            }
+        } else {
+            push @messages, { type => 'error', code => 'unauthorized_on_delete' };
+        }
+    } else {
+        push @messages, { type => 'error', code => 'does_not_exist' };
+    }
+    $op = 'list';
+} elsif ( $op eq 'add_biblio' ) {
+    $shelfnumber = $query->param('shelfnumber');
+    $shelf = Koha::Virtualshelves->find($shelfnumber);
+    if ($shelf) {
+        if( my $barcode = $query->param('barcode') ) {
+            my $item = GetItem( 0, $barcode);
+            if (defined $item && $item->{itemnumber}) {
+                my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
+                if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
+                    my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
+                    if ($@) {
+                        push @messages, { type => 'error', code => ref($@), msg => $@ };
+                    } elsif ( $added ) {
+                        push @messages, { type => 'message', code => 'success_on_add_biblio' };
+                    } else {
+                        push @messages, { type => 'message', code => 'error_on_add_biblio' };
+                    }
+                } else {
+                    push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
+                }
+            } else {
+                push @messages, { type => 'error', code => 'item_does_not_exist' };
+            }
+        }
+    } else {
+        push @messages, { type => 'error', code => 'does_not_exist' };
+    }
+    $op = $referer;
+} elsif ( $op eq 'remove_biblios' ) {
+    $shelfnumber = $query->param('shelfnumber');
+    $shelf = Koha::Virtualshelves->find($shelfnumber);
+    my @biblionumbers = $query->param('biblionumber');
+    if ($shelf) {
+        if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
+            my $number_of_biblios_removed = eval {
+                $shelf->remove_biblios(
+                    {
+                        biblionumbers => \@biblionumbers,
+                        borrowernumber => $loggedinuser,
+                    }
+                );
+            };
+            if ($@) {
+                push @messages, { type => 'error', code => ref($@), msg => $@ };
+            } elsif ( $number_of_biblios_removed ) {
+                push @messages, { type => 'message', code => 'success_on_remove_biblios' };
+            } else {
+                push @messages, { type => 'error', code => 'no_biblio_removed' };
+            }
+        } else {
+            push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
+        }
+    } else {
+        push @messages, { type => 'error', code => 'does_not_exist' };
+    }
+    $op = $referer;
+}
+
+if ( $op eq 'view' ) {
+    $shelfnumber ||= $query->param('shelfnumber');
+    $shelf = Koha::Virtualshelves->find($shelfnumber);
+    if ( $shelf ) {
+        if ( $shelf->can_be_viewed( $loggedinuser ) ) {
+            my $sortfield = $query->param('sortfield') || $shelf->sortfield;    # Passed in sorting overrides default sorting
+            my $direction = $query->param('direction') || 'asc';
+            my ( $shelflimit, $shelfoffset, $itemoff );
+            unless ( $query->param('print') ) {
+                $shelflimit = C4::Context->preference('numSearchResults') || 20;
+                $itemoff = ( $query->param('itemoff') ? $query->param('itemoff') : 1 );
+                $shelfoffset = ( $itemoff - 1 ) * $shelflimit;    # Sets the offset to begin retrieving items at
+            }
+            my ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset, $sortfield, $direction );
+
+            my $borrower = GetMember( borrowernumber => $loggedinuser );
+
+            for my $this_item (@$items) {
+                my $biblionumber = $this_item->{biblionumber};
+                my $record       = GetMarcBiblio($biblionumber);
+
+                if ( C4::Context->preference("XSLTResultsDisplay") ) {
+                    $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTResultsDisplay" );
+                }
+
+                my $marcflavour = C4::Context->preference("marcflavour");
+                $this_item->{'imageurl'}        = getitemtypeinfo( $this_item->{'itemtype'}, 'intranet' )->{'imageurl'};
+                $this_item->{'coins'}           = GetCOinSBiblio($record);
+                $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $this_item->{'biblionumber'} ) );
+                $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
+                $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
+                $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
+                $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
+
+                unless ( defined $this_item->{size} ) {
+
+                    #TT has problems with size
+                    $this_item->{size} = q||;
+                }
+
+                # Getting items infos for location display
+                my @items_infos = &GetItemsLocationInfo( $this_item->{'biblionumber'} );
+                $this_item->{'ITEM_RESULTS'} = \@items_infos;
+            }
+
+            # Build drop-down list for 'Add To:' menu...
+            my ( $totalref, $pubshelves, $barshelves ) = C4::VirtualShelves::GetSomeShelfNames( $loggedinuser, 'COMBO', 1 );
+            $template->param(
+                addbarshelves      => $totalref->{bartotal},
+                addbarshelvesloop  => $barshelves,
+                addpubshelves      => $totalref->{pubtotal},
+                addpubshelvesloop  => $pubshelves,
+                can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
+                can_remove_shelf   => $shelf->can_be_deleted($loggedinuser),
+                can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
+                can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
+                sortfield          => $sortfield,
+                itemsloop          => $items,
+                sortfield          => $sortfield,
+                direction          => $direction,
+            );
+            if ($shelflimit) {
+                $template->param(
+                    pagination_bar => pagination_bar(
+                        q||, ( int( $totitems / $shelflimit ) ) + ( ( $totitems % $shelflimit ) > 0 ? 1 : 0 ),
+                        $itemoff, "itemoff", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
+                    ),
+                );
+            }
+        } else {
+            push @messages, { type => 'error', code => 'unauthorized_on_view' };
+        }
+    } else {
+        push @messages, { type => 'error', code => 'does_not_exist' };
+    }
+}
+
+$template->param(
+    op       => $op,
+    referer  => $referer,
+    shelf    => $shelf,
+    messages => \@messages,
+    category => $category,
+    print    => $query->param('print') || 0,
+    csv_profiles => GetCsvProfilesLoop('marc'),
+);
+
+output_html_with_http_headers $query, $cookie, $template->output;