View | Details | Raw Unified | Return to bug 14544
Collapse All | Expand All

(-)a/C4/VirtualShelves.pm (-102 lines)
Lines 41-47 BEGIN { Link Here
41
    @ISA    = qw(Exporter);
41
    @ISA    = qw(Exporter);
42
    @EXPORT = qw(
42
    @EXPORT = qw(
43
            &GetShelfContents
43
            &GetShelfContents
44
            &ShelfPossibleAction
45
    );
44
    );
46
        @EXPORT_OK = qw(
45
        @EXPORT_OK = qw(
47
            &ShelvesMax
46
            &ShelvesMax
Lines 163-269 sub GetShelfContents { Link Here
163
    # or newer, for your version of DBI.
162
    # or newer, for your version of DBI.
164
}
163
}
165
164
166
=head2 ShelfPossibleAction
167
168
ShelfPossibleAction($loggedinuser, $shelfnumber, $action);
169
170
C<$loggedinuser,$shelfnumber,$action>
171
172
$action can be "view", "add", "delete", "manage", "new_public", "new_private".
173
New additional actions are: invite, acceptshare.
174
Note that add/delete here refers to adding/deleting entries from the list. Deleting the list itself falls under manage.
175
new_public and new_private refers to creating a new public or private list.
176
The distinction between deleting your own entries from the list or entries from
177
others is made when deleting a content from the shelf.
178
179
Returns 1 if the user can do the $action in the $shelfnumber shelf.
180
Returns 0 otherwise.
181
For the actions invite and acceptshare a second errorcode is returned if the
182
result is false. See opac-shareshelf.pl
183
184
=cut
185
186
sub ShelfPossibleAction {
187
    my ( $user, $shelfnumber, $action ) = @_;
188
    $action= 'view' unless $action;
189
    $user=0 unless $user;
190
191
    if($action =~ /^new/) { #no shelfnumber needed
192
        if($action eq 'new_private') {
193
            return $user>0;
194
        }
195
        elsif($action eq 'new_public') {
196
            return $user>0 && C4::Context->preference('OpacAllowPublicListCreation');
197
        }
198
        return 0;
199
    }
200
201
    return 0 unless defined($shelfnumber);
202
203
    if ( $user > 0 and $action eq 'delete_shelf' ) {
204
        my $borrower = C4::Members::GetMember( borrowernumber => $user );
205
        require C4::Auth;
206
        return 1
207
            if C4::Auth::haspermission( $borrower->{userid}, { lists => 'delete_public_lists' } );
208
    }
209
210
    my $dbh = C4::Context->dbh;
211
    my $query = q{
212
        SELECT COALESCE(owner,0) AS owner, category, allow_add, allow_delete_own, allow_delete_other, COALESCE(sh.borrowernumber,0) AS borrowernumber
213
        FROM virtualshelves vs
214
        LEFT JOIN virtualshelfshares sh ON sh.shelfnumber=vs.shelfnumber
215
        AND sh.borrowernumber=?
216
        WHERE vs.shelfnumber=?
217
        };
218
    my $sth = $dbh->prepare($query);
219
    $sth->execute($user, $shelfnumber);
220
    my $shelf= $sth->fetchrow_hashref;
221
222
    return 0 unless $shelf && ($shelf->{category}==2 || $shelf->{owner}==$user || ($user && $shelf->{borrowernumber}==$user));
223
    if($action eq 'view') {
224
        #already handled in the above condition
225
        return 1;
226
    }
227
    elsif($action eq 'add') {
228
        return 0 if $user<=0; #should be logged in
229
        return 1 if $shelf->{allow_add}==1 || $shelf->{owner}==$user;
230
        #owner may always add
231
    }
232
    elsif($action eq 'delete') {
233
        #this answer is just diplomatic: it says that you may be able to delete
234
        #some items from that shelf
235
        #it does not answer the question about a specific biblio
236
        #Koha::Virtualshelf->remove_biblios checks the situation per biblio
237
        return 1 if $user>0 && ($shelf->{allow_delete_own}==1 || $shelf->{allow_delete_other}==1);
238
    }
239
    elsif($action eq 'invite') {
240
        #for sharing you must be the owner and the list must be private
241
        if( $shelf->{category}==1 ) {
242
            return 1 if $shelf->{owner}==$user;
243
            return (0, 4); # code 4: should be owner
244
        }
245
        else {
246
            return (0, 5); # code 5: should be private list
247
        }
248
    }
249
    elsif($action eq 'acceptshare') {
250
        #the key for accepting is checked later in Koha::Virtualshelf->share
251
        #you must not be the owner, list must be private
252
        if( $shelf->{category}==1 ) {
253
            return (0, 8) if $shelf->{owner}==$user;
254
                #code 8: should not be owner
255
            return 1;
256
        }
257
        else {
258
            return (0, 5); # code 5: should be private list
259
        }
260
    }
261
    elsif($action eq 'manage' or $action eq 'delete_shelf') {
262
        return 1 if $user && $shelf->{owner}==$user;
263
    }
264
    return 0;
265
}
266
267
=head2 ShelvesMax
165
=head2 ShelvesMax
268
166
269
    $howmany= ShelvesMax($context);
167
    $howmany= ShelvesMax($context);
(-)a/opac/opac-addbybiblionumber.pl (-28 / +38 lines)
Lines 87-133 sub AddBibliosToShelf { Link Here
87
}
87
}
88
88
89
sub HandleNewVirtualShelf {
89
sub HandleNewVirtualShelf {
90
    if($authorized= ShelfPossibleAction($loggedinuser, undef, $category==1? 'new_private': 'new_public')) {
90
    if ( $loggedinuser > 0 and
91
    my $shelf = eval {
91
        (
92
        Koha::Virtualshelf->new(
92
            $category == 1
93
            {
93
                or $category == 2 and $loggedinuser>0 && C4::Context->preference('OpacAllowPublicListCreation')
94
                shelfname => $newvirtualshelf,
94
        )
95
                category => $category,
95
    ) {
96
                owner => $loggedinuser,
96
        my $shelf = eval {
97
            }
97
            Koha::Virtualshelf->new(
98
        );
98
                {
99
    };
99
                    shelfname => $newvirtualshelf,
100
    if ( $@ or not $shelf ) {
100
                    category => $category,
101
        $authorized=0;
101
                    owner => $loggedinuser,
102
        $errcode=1;
102
                }
103
        return;
103
            );
104
    }
104
        };
105
    AddBibliosToShelf($shelfnumber, @biblionumber);
105
        if ( $@ or not $shelf ) {
106
    #Reload the page where you came from
106
            $authorized = 0;
107
    print $query->header;
107
            $errcode = 1;
108
    print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
108
            return;
109
        }
110
        AddBibliosToShelf($shelfnumber, @biblionumber);
111
        #Reload the page where you came from
112
        print $query->header;
113
        print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"window.opener.location.reload(true);self.close();\"></body></html>";
109
    }
114
    }
110
}
115
}
111
116
112
sub HandleShelfNumber {
117
sub HandleShelfNumber {
113
    if($authorized= ShelfPossibleAction($loggedinuser, $shelfnumber, 'add')) {
118
    my $shelfnumber = $query->param('shelfnumber');
114
    AddBibliosToShelf($shelfnumber,@biblionumber);
119
    my $shelf = Koha::Virtualshelves->find( $shelfnumber );
115
    #Close this page and return
120
    if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
116
    print $query->header;
121
        AddBibliosToShelf($shelfnumber,@biblionumber);
117
    print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
122
        #Close this page and return
123
        print $query->header;
124
        print "<html><meta http-equiv=\"refresh\" content=\"0\" /><body onload=\"self.close();\"></body></html>";
125
    } else {
126
        # TODO
118
    }
127
    }
119
}
128
}
120
129
121
sub HandleSelectedShelf {
130
sub HandleSelectedShelf {
122
    if($authorized= ShelfPossibleAction( $loggedinuser, $selectedshelf, 'add')){
131
    my $shelfnumber = $query->param('selectedshelf');
123
        #adding to specific shelf
132
    my $shelf = Koha::Virtualshelves->find( $shelfnumber );
124
        my $shelfnumber = $query->param('selectedshelf');
133
    if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
125
        my $shelf = Koha::Virtualshelves->find( $shelfnumber );
126
        $template->param(
134
        $template->param(
127
            singleshelf               => 1,
135
            singleshelf               => 1,
128
            shelfnumber               => $shelf->shelfnumber,
136
            shelfnumber               => $shelf->shelfnumber,
129
            shelfname                 => $shelf->shelfname,
137
            shelfname                 => $shelf->shelfname,
130
        );
138
        );
139
    } else {
140
        # TODO
131
    }
141
    }
132
}
142
}
133
143
(-)a/opac/opac-downloadshelf.pl (-2 lines)
Lines 107-114 if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) { Link Here
107
107
108
    } else {
108
    } else {
109
109
110
        my $shelf = Koha::Virtualshelves->find( $shelfid );
111
112
        # if modal context is passed set a variable so that page markup can be different
110
        # if modal context is passed set a variable so that page markup can be different
113
        if($context eq "modal"){
111
        if($context eq "modal"){
114
            $template->param(modal => 1);
112
            $template->param(modal => 1);
(-)a/opac/opac-sendshelf.pl (-1 / +2 lines)
Lines 52-58 my $email = $query->param('email'); Link Here
52
52
53
my $dbh          = C4::Context->dbh;
53
my $dbh          = C4::Context->dbh;
54
54
55
if ( ShelfPossibleAction( (defined($borrowernumber) ? $borrowernumber : -1), $shelfid, 'view' ) ) {
55
my $shelf = Koha::Virtualshelves->find( $shelfid );
56
if ( $shelf->can_be_viewed( $borrowernumber ) ) {
56
57
57
if ( $email ) {
58
if ( $email ) {
58
    my $message = Koha::Email->new();
59
    my $message = Koha::Email->new();
(-)a/opac/opac-shareshelf.pl (-7 / +7 lines)
Lines 115-127 sub confirm_invite { Link Here
115
sub show_accept {
115
sub show_accept {
116
    my ($param) = @_;
116
    my ($param) = @_;
117
117
118
    my @rv = ShelfPossibleAction( $param->{loggedinuser},
118
    my $shelfnumber = $param->{shelfnumber};
119
        $param->{shelfnumber}, 'acceptshare' );
119
    my $shelf = Koha::Virtualshelves->find( $shelfnumber );
120
    $param->{errcode} = $rv[1] if !$rv[0];
121
    return if $param->{errcode};
122
120
123
    #errorcode 5: should be private list
121
    # The key for accepting is checked later in Koha::Virtualshelf->share
124
    #errorcode 8: should not be owner
122
    # You must not be the owner and the list must be private
123
    if ( $shelf->category == 2 or $shelf->owner == $param->{loggedinuser} ) {
124
        return;
125
    }
125
126
126
    # We could have used ->find with the share id, but we don't want to change
127
    # We could have used ->find with the share id, but we don't want to change
127
    # the url sent to the patron
128
    # the url sent to the patron
128
- 

Return to bug 14544