Bug 14544: Make the OPAC 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     my $shares = $self->{_result}->virtualshelfshares;
105     return $shares;
106 }
107
108 sub get_contents {
109     my ( $self ) = @_;
110     my $contents = $self->{_result}->virtualshelfcontents;
111     return $contents;
112 }
113
114 sub share {
115     my ( $self, $key ) = @_;
116     unless ( $key ) {
117         Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
118     }
119     Koha::Virtualshelfshare->new(
120         {
121             shelfnumber => $self->shelfnumber,
122             invitekey => $key,
123             sharedate => dt_from_string,
124         }
125     )->store;
126 }
127
128 sub is_shared {
129     my ( $self ) = @_;
130     return  $self->get_shares->search(
131         {
132             borrowernumber => { '!=' => undef },
133         }
134     )->count;
135 }
136
137 sub is_shared_with {
138     my ( $self, $borrowernumber ) = @_;
139     return unless $borrowernumber;
140     return  $self->get_shares->search(
141         {
142             borrowernumber => $borrowernumber,
143         }
144     )->count;
145 }
146
147 sub remove_share {
148     my ( $self, $borrowernumber ) = @_;
149     my $shelves = Koha::Virtualshelfshares->search(
150         {
151             shelfnumber => $self->shelfnumber,
152             borrowernumber => $borrowernumber,
153         }
154     );
155     return 0 unless $shelves->count;
156
157     # Only 1 share with 1 patron can exist
158     return $shelves->next->delete;
159 }
160
161 sub add_biblio {
162     my ( $self, $biblionumber, $borrowernumber ) = @_;
163     return unless $biblionumber;
164     my $already_exists = $self->get_contents->search(
165         {
166             biblionumber => $biblionumber,
167         }
168     )->count;
169     return if $already_exists;
170     my $content = Koha::Virtualshelfcontent->new(
171         {
172             shelfnumber => $self->shelfnumber,
173             biblionumber => $biblionumber,
174             borrowernumber => $borrowernumber,
175         }
176     )->store;
177     $self->lastmodified(dt_from_string);
178     $self->store;
179
180     return $content;
181 }
182
183 sub remove_biblios {
184     my ( $self, $params ) = @_;
185     my $biblionumbers = $params->{biblionumbers} || [];
186     my $borrowernumber = $params->{borrowernumber};
187     return unless @$biblionumbers;
188
189     my $number_removed = 0;
190     for my $biblionumber ( @$biblionumbers ) {
191         if ( $self->owner == $borrowernumber or $self->allow_delete_own ) {
192             $number_removed += $self->get_contents->search(
193                 {
194                     biblionumber => $biblionumber,
195                     borrowernumber => $borrowernumber,
196                 }
197             )->delete;
198         }
199         if ( $self->allow_delete_other ) {
200             $number_removed += $self->get_contents->search(
201                 {
202                     biblionumber => $biblionumber,
203                     # FIXME
204                     # This does not make sense, but it's has been backported from DelFromShelf.
205                     # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
206                     borrowernumber => {
207                         -or => {
208                             '!=' => $borrowernumber,
209                             '=' => undef
210                         }
211                     },
212                 }
213             )->delete;
214         }
215     }
216     return $number_removed;
217 }
218
219 sub can_be_viewed {
220     my ( $self, $borrowernumber ) = @_;
221     return 1 if $self->category == $PUBLIC;
222     return 0 unless $borrowernumber;
223     return 1 if $self->owner == $borrowernumber;
224     return $self->get_shares->search(
225         {
226             borrowernumber => $borrowernumber,
227         }
228     )->count;
229 }
230
231 sub can_be_deleted {
232     my ( $self, $borrowernumber ) = @_;
233
234     return 0 unless $borrowernumber;
235     return 1 if $self->owner == $borrowernumber;
236
237     my $patron = Koha::Borrowers->find( $borrowernumber );
238
239     return 1 if $self->category == $PUBLIC and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
240
241     return 0;
242 }
243
244 sub can_be_managed {
245     my ( $self, $borrowernumber ) = @_;
246     return 1
247       if $borrowernumber and $self->owner == $borrowernumber;
248     return 0;
249 }
250
251 sub can_biblios_be_added {
252     my ( $self, $borrowernumber ) = @_;
253
254     return 1
255       if $borrowernumber
256       and ( $self->owner == $borrowernumber
257          or $self->allow_add );
258     return 0;
259 }
260
261 sub can_biblios_be_removed {
262     my ( $self, $borrowernumber ) = @_;
263
264     return 1
265       if $borrowernumber
266       and (  $self->owner == $borrowernumber
267           or $self->allow_delete_own
268           or $self->allow_delete_other );
269     return 0;
270 }
271
272 sub type {
273     return 'Virtualshelve';
274 }
275
276 1;