Bug 30477: Add new UNIMARC installer translation files
[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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20
21 use C4::Auth qw( haspermission );
22
23 use Koha::Patrons;
24 use Koha::Database;
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::Exceptions;
27 use Koha::Virtualshelfshare;
28 use Koha::Virtualshelfshares;
29 use Koha::Virtualshelfcontent;
30 use Koha::Virtualshelfcontents;
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 sub store {
45     my ( $self ) = @_;
46
47     unless ( $self->owner ) {
48         Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
49     }
50
51     unless ( $self->is_shelfname_valid ) {
52         Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
53     }
54
55     $self->allow_change_from_owner( 1 )
56         unless defined $self->allow_change_from_owner;
57     $self->allow_change_from_others( 0 )
58         unless defined $self->allow_change_from_others;
59     $self->allow_change_from_staff( 0 )
60         unless defined $self->allow_change_from_staff;
61
62     $self->created_on( dt_from_string )
63         unless defined $self->created_on;
64
65     return $self->SUPER::store( $self );
66 }
67
68 sub is_public {
69     my ( $self ) = @_;
70     return $self->public;
71 }
72
73 sub is_private {
74     my ( $self ) = @_;
75     return !$self->public;
76 }
77
78 sub is_shelfname_valid {
79     my ( $self ) = @_;
80
81     my $conditions = {
82         shelfname => $self->shelfname,
83         ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
84     };
85
86     if ( $self->is_private and defined $self->owner ) {
87         $conditions->{-or} = {
88             "virtualshelfshares.borrowernumber" => $self->owner,
89             "me.owner" => $self->owner,
90         };
91         $conditions->{public} = 0;
92     }
93     elsif ( $self->is_private and not defined $self->owner ) {
94         $conditions->{owner} = undef;
95         $conditions->{public} = 0;
96     }
97     else {
98         $conditions->{public} = 1;
99     }
100
101     my $count = Koha::Virtualshelves->search(
102         $conditions,
103         {
104             join => 'virtualshelfshares',
105         }
106     )->count;
107     return $count ? 0 : 1;
108 }
109
110 sub get_shares {
111     my ( $self ) = @_;
112     my $rs = $self->{_result}->virtualshelfshares;
113     my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
114     return $shares;
115 }
116
117 sub get_contents {
118     my ( $self ) = @_;
119     my $rs = $self->{_result}->virtualshelfcontents;
120     my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
121     return $contents;
122 }
123
124 sub share {
125     my ( $self, $key ) = @_;
126     unless ( $key ) {
127         Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
128     }
129     Koha::Virtualshelfshare->new(
130         {
131             shelfnumber => $self->shelfnumber,
132             invitekey => $key,
133             sharedate => dt_from_string,
134         }
135     )->store;
136 }
137
138 sub is_shared {
139     my ( $self ) = @_;
140     return  $self->get_shares->search(
141         {
142             borrowernumber => { '!=' => undef },
143         }
144     )->count;
145 }
146
147 sub is_shared_with {
148     my ( $self, $borrowernumber ) = @_;
149     return unless $borrowernumber;
150     return  $self->get_shares->search(
151         {
152             borrowernumber => $borrowernumber,
153         }
154     )->count;
155 }
156
157 sub remove_share {
158     my ( $self, $borrowernumber ) = @_;
159     my $shelves = Koha::Virtualshelfshares->search(
160         {
161             shelfnumber => $self->shelfnumber,
162             borrowernumber => $borrowernumber,
163         }
164     );
165     return 0 unless $shelves->count;
166
167     # Only 1 share with 1 patron can exist
168     return $shelves->next->delete;
169 }
170
171 sub add_biblio {
172     my ( $self, $biblionumber, $borrowernumber ) = @_;
173     return unless $biblionumber;
174     my $already_exists = $self->get_contents->search(
175         {
176             biblionumber => $biblionumber,
177         }
178     )->count;
179     return if $already_exists;
180
181     # Check permissions
182     my $patron = Koha::Patrons->find( $borrowernumber ) or return 0;
183     return 0 unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) || ( $self->allow_change_from_staff && $patron->can_patron_change_staff_only_lists ) || $self->allow_change_from_others;
184
185     my $content = Koha::Virtualshelfcontent->new(
186         {
187             shelfnumber => $self->shelfnumber,
188             biblionumber => $biblionumber,
189             borrowernumber => $borrowernumber,
190         }
191     )->store;
192     $self->lastmodified(dt_from_string);
193     $self->store;
194
195     return $content;
196 }
197
198 sub remove_biblios {
199     my ( $self, $params ) = @_;
200     my $biblionumbers = $params->{biblionumbers} || [];
201     my $borrowernumber = $params->{borrowernumber};
202     return unless @$biblionumbers;
203
204     my $number_removed = 0;
205     my $patron = Koha::Patrons->find( $borrowernumber ) or return 0;
206     if( ( $self->owner == $borrowernumber && $self->allow_change_from_owner )
207       || ( $self->allow_change_from_staff && $patron->can_patron_change_staff_only_lists )
208       || $self->allow_change_from_others ) {
209         $number_removed += $self->get_contents->search({
210             biblionumber => $biblionumbers,
211         })->delete;
212     }
213     return $number_removed;
214 }
215
216 sub can_be_viewed {
217     my ( $self, $borrowernumber ) = @_;
218     return 1 if $self->is_public;
219     return 0 unless $borrowernumber;
220     return 1 if $self->owner == $borrowernumber;
221     return $self->get_shares->search(
222         {
223             borrowernumber => $borrowernumber,
224         }
225     )->count;
226 }
227
228 sub can_be_deleted {
229     my ( $self, $borrowernumber ) = @_;
230
231     return 0 unless $borrowernumber;
232     return 1 if $self->owner == $borrowernumber;
233
234     my $patron = Koha::Patrons->find( $borrowernumber ) or return 0;
235
236     return 1 if $self->is_public and haspermission( $patron->userid, { lists => 'delete_public_lists' } );
237
238     return 0;
239 }
240
241 sub can_be_managed {
242     my ( $self, $borrowernumber ) = @_;
243     return 1
244       if $borrowernumber and $self->owner == $borrowernumber;
245
246     my $patron = Koha::Patrons->find( $borrowernumber ) or return 0;
247     return 1
248       if $self->is_public and haspermission( $patron->userid, { lists => 'edit_public_lists' } );
249     return 0;
250 }
251
252 sub can_biblios_be_added {
253     my ( $self, $borrowernumber ) = @_;
254
255     my $patron = Koha::Patrons->find( $borrowernumber ) or return 0;
256     return 1
257       if $borrowernumber
258       and ( ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) or ( $self->allow_change_from_staff && $patron->can_patron_change_staff_only_lists ) or $self->allow_change_from_others );
259     return 0;
260 }
261
262 sub can_biblios_be_removed {
263     my ( $self, $borrowernumber ) = @_;
264     return $self->can_biblios_be_added( $borrowernumber );
265     # Same answer since bug 18228
266 }
267
268 =head2 Internal methods
269
270 =head3 _type
271
272 =cut
273
274 sub _type {
275     return 'Virtualshelve';
276 }
277
278 1;