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