Bug 5260: simplify script and error handling
[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 use Koha::Virtualshelfcontents;
32
33 use base qw(Koha::Object);
34
35 =head1 NAME
36
37 Koha::Virtualshelf - Koha Virtualshelf Object class
38
39 =head1 API
40
41 =head2 Class Methods
42
43 =cut
44
45 =head3 type
46
47 =cut
48
49 our $PRIVATE = 1;
50 our $PUBLIC = 2;
51
52 sub store {
53     my ( $self ) = @_;
54
55     unless ( $self->owner ) {
56         Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
57     }
58
59     unless ( $self->is_shelfname_valid ) {
60         Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
61     }
62
63     $self->allow_add( 0 )
64         unless defined $self->allow_add;
65     $self->allow_delete_own( 1 )
66         unless defined $self->allow_delete_own;
67     $self->allow_delete_other( 0 )
68         unless defined $self->allow_delete_other;
69
70     $self->created_on( dt_from_string );
71
72     return $self->SUPER::store( $self );
73 }
74
75 sub is_shelfname_valid {
76     my ( $self ) = @_;
77
78     my $conditions = {
79         shelfname => $self->shelfname,
80         ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
81     };
82
83     if ( $self->category == $PRIVATE and defined $self->owner ) {
84         $conditions->{-or} = {
85             "virtualshelfshares.borrowernumber" => $self->owner,
86             "me.owner" => $self->owner,
87         };
88         $conditions->{category} = $PRIVATE;
89     }
90     elsif ( $self->category == $PRIVATE and not defined $self->owner ) {
91         $conditions->{owner} = undef;
92         $conditions->{category} = $PRIVATE;
93     }
94     else {
95         $conditions->{category} = $PUBLIC;
96     }
97
98     my $count = Koha::Virtualshelves->search(
99         $conditions,
100         {
101             join => 'virtualshelfshares',
102         }
103     )->count;
104     return $count ? 0 : 1;
105 }
106
107 sub get_shares {
108     my ( $self ) = @_;
109     my $rs = $self->{_result}->virtualshelfshares;
110     my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
111     return $shares;
112 }
113
114 sub get_contents {
115     my ( $self ) = @_;
116     my $rs = $self->{_result}->virtualshelfcontents;
117     my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
118     return $contents;
119 }
120
121 sub share {
122     my ( $self, $key ) = @_;
123     unless ( $key ) {
124         Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
125     }
126     Koha::Virtualshelfshare->new(
127         {
128             shelfnumber => $self->shelfnumber,
129             invitekey => $key,
130             sharedate => dt_from_string,
131         }
132     )->store;
133 }
134
135 sub is_shared {
136     my ( $self ) = @_;
137     return  $self->get_shares->search(
138         {
139             borrowernumber => { '!=' => undef },
140         }
141     )->count;
142 }
143
144 sub is_shared_with {
145     my ( $self, $borrowernumber ) = @_;
146     return unless $borrowernumber;
147     return  $self->get_shares->search(
148         {
149             borrowernumber => $borrowernumber,
150         }
151     )->count;
152 }
153
154 sub remove_share {
155     my ( $self, $borrowernumber ) = @_;
156     my $shelves = Koha::Virtualshelfshares->search(
157         {
158             shelfnumber => $self->shelfnumber,
159             borrowernumber => $borrowernumber,
160         }
161     );
162     return 0 unless $shelves->count;
163
164     # Only 1 share with 1 patron can exist
165     return $shelves->next->delete;
166 }
167
168 sub add_biblio {
169     my ( $self, $biblionumber, $borrowernumber ) = @_;
170     return unless $biblionumber;
171     my $already_exists = $self->get_contents->search(
172         {
173             biblionumber => $biblionumber,
174         }
175     )->count;
176     return if $already_exists;
177     my $content = Koha::Virtualshelfcontent->new(
178         {
179             shelfnumber => $self->shelfnumber,
180             biblionumber => $biblionumber,
181             borrowernumber => $borrowernumber,
182         }
183     )->store;
184     $self->lastmodified(dt_from_string);
185     $self->store;
186
187     return $content;
188 }
189
190 sub remove_biblios {
191     my ( $self, $params ) = @_;
192     my $biblionumbers = $params->{biblionumbers} || [];
193     my $borrowernumber = $params->{borrowernumber};
194     return unless @$biblionumbers;
195
196     my $number_removed = 0;
197     for my $biblionumber ( @$biblionumbers ) {
198         if ( $self->owner == $borrowernumber or $self->allow_delete_own ) {
199             $number_removed += $self->get_contents->search(
200                 {
201                     biblionumber => $biblionumber,
202                     borrowernumber => $borrowernumber,
203                 }
204             )->delete;
205         }
206         if ( $self->allow_delete_other ) {
207             $number_removed += $self->get_contents->search(
208                 {
209                     biblionumber => $biblionumber,
210                     # FIXME
211                     # This does not make sense, but it's has been backported from DelFromShelf.
212                     # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
213                     borrowernumber => {
214                         -or => {
215                             '!=' => $borrowernumber,
216                             '=' => undef
217                         }
218                     },
219                 }
220             )->delete;
221         }
222     }
223     return $number_removed;
224 }
225
226 sub can_be_viewed {
227     my ( $self, $borrowernumber ) = @_;
228     return 1 if $self->category == $PUBLIC;
229     return 0 unless $borrowernumber;
230     return 1 if $self->owner == $borrowernumber;
231     return $self->get_shares->search(
232         {
233             borrowernumber => $borrowernumber,
234         }
235     )->count;
236 }
237
238 sub can_be_deleted {
239     my ( $self, $borrowernumber ) = @_;
240
241     return 0 unless $borrowernumber;
242     return 1 if $self->owner == $borrowernumber;
243
244     my $patron = Koha::Patrons->find( $borrowernumber );
245
246     return 1 if $self->category == $PUBLIC and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
247
248     return 0;
249 }
250
251 sub can_be_managed {
252     my ( $self, $borrowernumber ) = @_;
253     return 1
254       if $borrowernumber and $self->owner == $borrowernumber;
255     return 0;
256 }
257
258 sub can_biblios_be_added {
259     my ( $self, $borrowernumber ) = @_;
260
261     return 1
262       if $borrowernumber
263       and ( $self->owner == $borrowernumber
264          or $self->allow_add );
265     return 0;
266 }
267
268 sub can_biblios_be_removed {
269     my ( $self, $borrowernumber ) = @_;
270
271     return 1
272       if $borrowernumber
273       and (  $self->owner == $borrowernumber
274           or $self->allow_delete_own
275           or $self->allow_delete_other );
276     return 0;
277 }
278
279 sub _type {
280     return 'Virtualshelve';
281 }
282
283 1;