Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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_change_from_owner( 1 )
64         unless defined $self->allow_change_from_owner;
65     $self->allow_change_from_others( 0 )
66         unless defined $self->allow_change_from_others;
67
68     $self->created_on( dt_from_string )
69         unless defined $self->created_on;
70
71     return $self->SUPER::store( $self );
72 }
73
74 sub is_public {
75     my ( $self ) = @_;
76     return $self->category == $PUBLIC;
77 }
78
79 sub is_private {
80     my ( $self ) = @_;
81     return $self->category == $PRIVATE;
82 }
83
84 sub is_shelfname_valid {
85     my ( $self ) = @_;
86
87     my $conditions = {
88         shelfname => $self->shelfname,
89         ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
90     };
91
92     if ( $self->is_private and defined $self->owner ) {
93         $conditions->{-or} = {
94             "virtualshelfshares.borrowernumber" => $self->owner,
95             "me.owner" => $self->owner,
96         };
97         $conditions->{category} = $PRIVATE;
98     }
99     elsif ( $self->is_private and not defined $self->owner ) {
100         $conditions->{owner} = undef;
101         $conditions->{category} = $PRIVATE;
102     }
103     else {
104         $conditions->{category} = $PUBLIC;
105     }
106
107     my $count = Koha::Virtualshelves->search(
108         $conditions,
109         {
110             join => 'virtualshelfshares',
111         }
112     )->count;
113     return $count ? 0 : 1;
114 }
115
116 sub get_shares {
117     my ( $self ) = @_;
118     my $rs = $self->{_result}->virtualshelfshares;
119     my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
120     return $shares;
121 }
122
123 sub get_contents {
124     my ( $self ) = @_;
125     my $rs = $self->{_result}->virtualshelfcontents;
126     my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
127     return $contents;
128 }
129
130 sub share {
131     my ( $self, $key ) = @_;
132     unless ( $key ) {
133         Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
134     }
135     Koha::Virtualshelfshare->new(
136         {
137             shelfnumber => $self->shelfnumber,
138             invitekey => $key,
139             sharedate => dt_from_string,
140         }
141     )->store;
142 }
143
144 sub is_shared {
145     my ( $self ) = @_;
146     return  $self->get_shares->search(
147         {
148             borrowernumber => { '!=' => undef },
149         }
150     )->count;
151 }
152
153 sub is_shared_with {
154     my ( $self, $borrowernumber ) = @_;
155     return unless $borrowernumber;
156     return  $self->get_shares->search(
157         {
158             borrowernumber => $borrowernumber,
159         }
160     )->count;
161 }
162
163 sub remove_share {
164     my ( $self, $borrowernumber ) = @_;
165     my $shelves = Koha::Virtualshelfshares->search(
166         {
167             shelfnumber => $self->shelfnumber,
168             borrowernumber => $borrowernumber,
169         }
170     );
171     return 0 unless $shelves->count;
172
173     # Only 1 share with 1 patron can exist
174     return $shelves->next->delete;
175 }
176
177 sub add_biblio {
178     my ( $self, $biblionumber, $borrowernumber ) = @_;
179     return unless $biblionumber;
180     my $already_exists = $self->get_contents->search(
181         {
182             biblionumber => $biblionumber,
183         }
184     )->count;
185     return if $already_exists;
186
187     # Check permissions
188     return unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) || $self->allow_change_from_others;
189
190     my $content = Koha::Virtualshelfcontent->new(
191         {
192             shelfnumber => $self->shelfnumber,
193             biblionumber => $biblionumber,
194             borrowernumber => $borrowernumber,
195         }
196     )->store;
197     $self->lastmodified(dt_from_string);
198     $self->store;
199
200     return $content;
201 }
202
203 sub remove_biblios {
204     my ( $self, $params ) = @_;
205     my $biblionumbers = $params->{biblionumbers} || [];
206     my $borrowernumber = $params->{borrowernumber};
207     return unless @$biblionumbers;
208
209     my $number_removed = 0;
210     if( ( $self->owner == $borrowernumber && $self->allow_change_from_owner )
211       || $self->allow_change_from_others ) {
212         $number_removed += $self->get_contents->search({
213             biblionumber => $biblionumbers,
214         })->delete;
215     }
216     return $number_removed;
217 }
218
219 sub can_be_viewed {
220     my ( $self, $borrowernumber ) = @_;
221     return 1 if $self->is_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::Patrons->find( $borrowernumber );
238
239     return 1 if $self->is_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 && $self->allow_change_from_owner ) or $self->allow_change_from_others );
257     return 0;
258 }
259
260 sub can_biblios_be_removed {
261     my ( $self, $borrowernumber ) = @_;
262     return $self->can_biblios_be_added( $borrowernumber );
263     # Same answer since bug 18228
264 }
265
266 sub _type {
267     return 'Virtualshelve';
268 }
269
270 1;