Bug 16794: Group the 2 action buttons into the same column
[koha.git] / Koha / Virtualshelf.pm
1 package Koha::Virtualshelf;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Carp;
21
22 use C4::Auth;
23
24 use Koha::Patrons;
25 use Koha::Database;
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Exceptions;
28 use Koha::Virtualshelfshare;
29 use Koha::Virtualshelfshares;
30 use Koha::Virtualshelfcontent;
31
32 use base qw(Koha::Object);
33
34 =head1 NAME
35
36 Koha::Virtualshelf - Koha Virtualshelf Object class
37
38 =head1 API
39
40 =head2 Class Methods
41
42 =cut
43
44 =head3 type
45
46 =cut
47
48 our $PRIVATE = 1;
49 our $PUBLIC = 2;
50
51 sub store {
52     my ( $self ) = @_;
53
54     unless ( $self->owner ) {
55         Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
56     }
57
58     unless ( $self->is_shelfname_valid ) {
59         Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
60     }
61
62     $self->allow_add( 0 )
63         unless defined $self->allow_add;
64     $self->allow_delete_own( 1 )
65         unless defined $self->allow_delete_own;
66     $self->allow_delete_other( 0 )
67         unless defined $self->allow_delete_other;
68
69     $self->created_on( dt_from_string );
70
71     return $self->SUPER::store( $self );
72 }
73
74 sub is_shelfname_valid {
75     my ( $self ) = @_;
76
77     my $conditions = {
78         shelfname => $self->shelfname,
79         ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
80     };
81
82     if ( $self->category == $PRIVATE and defined $self->owner ) {
83         $conditions->{-or} = {
84             "virtualshelfshares.borrowernumber" => $self->owner,
85             "me.owner" => $self->owner,
86         };
87         $conditions->{category} = $PRIVATE;
88     }
89     elsif ( $self->category == $PRIVATE and not defined $self->owner ) {
90         $conditions->{owner} = undef;
91         $conditions->{category} = $PRIVATE;
92     }
93     else {
94         $conditions->{category} = $PUBLIC;
95     }
96
97     my $count = Koha::Virtualshelves->search(
98         $conditions,
99         {
100             join => 'virtualshelfshares',
101         }
102     )->count;
103     return $count ? 0 : 1;
104 }
105
106 sub get_shares {
107     my ( $self ) = @_;
108     my $shares = $self->{_result}->virtualshelfshares;
109     return $shares;
110 }
111
112 sub get_contents {
113     my ( $self ) = @_;
114     my $contents = $self->{_result}->virtualshelfcontents;
115     return $contents;
116 }
117
118 sub share {
119     my ( $self, $key ) = @_;
120     unless ( $key ) {
121         Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
122     }
123     Koha::Virtualshelfshare->new(
124         {
125             shelfnumber => $self->shelfnumber,
126             invitekey => $key,
127             sharedate => dt_from_string,
128         }
129     )->store;
130 }
131
132 sub is_shared {
133     my ( $self ) = @_;
134     return  $self->get_shares->search(
135         {
136             borrowernumber => { '!=' => undef },
137         }
138     )->count;
139 }
140
141 sub is_shared_with {
142     my ( $self, $borrowernumber ) = @_;
143     return unless $borrowernumber;
144     return  $self->get_shares->search(
145         {
146             borrowernumber => $borrowernumber,
147         }
148     )->count;
149 }
150
151 sub remove_share {
152     my ( $self, $borrowernumber ) = @_;
153     my $shelves = Koha::Virtualshelfshares->search(
154         {
155             shelfnumber => $self->shelfnumber,
156             borrowernumber => $borrowernumber,
157         }
158     );
159     return 0 unless $shelves->count;
160
161     # Only 1 share with 1 patron can exist
162     return $shelves->next->delete;
163 }
164
165 sub add_biblio {
166     my ( $self, $biblionumber, $borrowernumber ) = @_;
167     return unless $biblionumber;
168     my $already_exists = $self->get_contents->search(
169         {
170             biblionumber => $biblionumber,
171         }
172     )->count;
173     return if $already_exists;
174     my $content = Koha::Virtualshelfcontent->new(
175         {
176             shelfnumber => $self->shelfnumber,
177             biblionumber => $biblionumber,
178             borrowernumber => $borrowernumber,
179         }
180     )->store;
181     $self->lastmodified(dt_from_string);
182     $self->store;
183
184     return $content;
185 }
186
187 sub remove_biblios {
188     my ( $self, $params ) = @_;
189     my $biblionumbers = $params->{biblionumbers} || [];
190     my $borrowernumber = $params->{borrowernumber};
191     return unless @$biblionumbers;
192
193     my $number_removed = 0;
194     for my $biblionumber ( @$biblionumbers ) {
195         if ( $self->owner == $borrowernumber or $self->allow_delete_own ) {
196             $number_removed += $self->get_contents->search(
197                 {
198                     biblionumber => $biblionumber,
199                     borrowernumber => $borrowernumber,
200                 }
201             )->delete;
202         }
203         if ( $self->allow_delete_other ) {
204             $number_removed += $self->get_contents->search(
205                 {
206                     biblionumber => $biblionumber,
207                     # FIXME
208                     # This does not make sense, but it's has been backported from DelFromShelf.
209                     # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
210                     borrowernumber => {
211                         -or => {
212                             '!=' => $borrowernumber,
213                             '=' => undef
214                         }
215                     },
216                 }
217             )->delete;
218         }
219     }
220     return $number_removed;
221 }
222
223 sub can_be_viewed {
224     my ( $self, $borrowernumber ) = @_;
225     return 1 if $self->category == $PUBLIC;
226     return 0 unless $borrowernumber;
227     return 1 if $self->owner == $borrowernumber;
228     return $self->get_shares->search(
229         {
230             borrowernumber => $borrowernumber,
231         }
232     )->count;
233 }
234
235 sub can_be_deleted {
236     my ( $self, $borrowernumber ) = @_;
237
238     return 0 unless $borrowernumber;
239     return 1 if $self->owner == $borrowernumber;
240
241     my $patron = Koha::Patrons->find( $borrowernumber );
242
243     return 1 if $self->category == $PUBLIC and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
244
245     return 0;
246 }
247
248 sub can_be_managed {
249     my ( $self, $borrowernumber ) = @_;
250     return 1
251       if $borrowernumber and $self->owner == $borrowernumber;
252     return 0;
253 }
254
255 sub can_biblios_be_added {
256     my ( $self, $borrowernumber ) = @_;
257
258     return 1
259       if $borrowernumber
260       and ( $self->owner == $borrowernumber
261          or $self->allow_add );
262     return 0;
263 }
264
265 sub can_biblios_be_removed {
266     my ( $self, $borrowernumber ) = @_;
267
268     return 1
269       if $borrowernumber
270       and (  $self->owner == $borrowernumber
271           or $self->allow_delete_own
272           or $self->allow_delete_other );
273     return 0;
274 }
275
276 sub _type {
277     return 'Virtualshelve';
278 }
279
280 1;