Bug 17652: opac-account.pl does not include login branchcode
[koha.git] / Koha / Virtualshelf.pm
blob4e45c3166c9e57a266630a0f724cec86f08465c1
1 package Koha::Virtualshelf;
3 # This file is part of Koha.
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.
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.
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.
18 use Modern::Perl;
20 use Carp;
22 use C4::Auth;
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;
33 use base qw(Koha::Object);
35 =head1 NAME
37 Koha::Virtualshelf - Koha Virtualshelf Object class
39 =head1 API
41 =head2 Class Methods
43 =cut
45 =head3 type
47 =cut
49 our $PRIVATE = 1;
50 our $PUBLIC = 2;
52 sub store {
53 my ( $self ) = @_;
55 unless ( $self->owner ) {
56 Koha::Exceptions::Virtualshelves::UseDbAdminAccount->throw;
59 unless ( $self->is_shelfname_valid ) {
60 Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
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;
70 $self->created_on( dt_from_string );
72 return $self->SUPER::store( $self );
75 sub is_shelfname_valid {
76 my ( $self ) = @_;
78 my $conditions = {
79 shelfname => $self->shelfname,
80 ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
83 if ( $self->category == $PRIVATE and defined $self->owner ) {
84 $conditions->{-or} = {
85 "virtualshelfshares.borrowernumber" => $self->owner,
86 "me.owner" => $self->owner,
88 $conditions->{category} = $PRIVATE;
90 elsif ( $self->category == $PRIVATE and not defined $self->owner ) {
91 $conditions->{owner} = undef;
92 $conditions->{category} = $PRIVATE;
94 else {
95 $conditions->{category} = $PUBLIC;
98 my $count = Koha::Virtualshelves->search(
99 $conditions,
101 join => 'virtualshelfshares',
103 )->count;
104 return $count ? 0 : 1;
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;
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;
121 sub share {
122 my ( $self, $key ) = @_;
123 unless ( $key ) {
124 Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
126 Koha::Virtualshelfshare->new(
128 shelfnumber => $self->shelfnumber,
129 invitekey => $key,
130 sharedate => dt_from_string,
132 )->store;
135 sub is_shared {
136 my ( $self ) = @_;
137 return $self->get_shares->search(
139 borrowernumber => { '!=' => undef },
141 )->count;
144 sub is_shared_with {
145 my ( $self, $borrowernumber ) = @_;
146 return unless $borrowernumber;
147 return $self->get_shares->search(
149 borrowernumber => $borrowernumber,
151 )->count;
154 sub remove_share {
155 my ( $self, $borrowernumber ) = @_;
156 my $shelves = Koha::Virtualshelfshares->search(
158 shelfnumber => $self->shelfnumber,
159 borrowernumber => $borrowernumber,
162 return 0 unless $shelves->count;
164 # Only 1 share with 1 patron can exist
165 return $shelves->next->delete;
168 sub add_biblio {
169 my ( $self, $biblionumber, $borrowernumber ) = @_;
170 return unless $biblionumber;
171 my $already_exists = $self->get_contents->search(
173 biblionumber => $biblionumber,
175 )->count;
176 return if $already_exists;
177 my $content = Koha::Virtualshelfcontent->new(
179 shelfnumber => $self->shelfnumber,
180 biblionumber => $biblionumber,
181 borrowernumber => $borrowernumber,
183 )->store;
184 $self->lastmodified(dt_from_string);
185 $self->store;
187 return $content;
190 sub remove_biblios {
191 my ( $self, $params ) = @_;
192 my $biblionumbers = $params->{biblionumbers} || [];
193 my $borrowernumber = $params->{borrowernumber};
194 return unless @$biblionumbers;
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(
201 biblionumber => $biblionumber,
202 borrowernumber => $borrowernumber,
204 )->delete;
206 if ( $self->allow_delete_other ) {
207 $number_removed += $self->get_contents->search(
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
220 )->delete;
223 return $number_removed;
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(
233 borrowernumber => $borrowernumber,
235 )->count;
238 sub can_be_deleted {
239 my ( $self, $borrowernumber ) = @_;
241 return 0 unless $borrowernumber;
242 return 1 if $self->owner == $borrowernumber;
244 my $patron = Koha::Patrons->find( $borrowernumber );
246 return 1 if $self->category == $PUBLIC and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
248 return 0;
251 sub can_be_managed {
252 my ( $self, $borrowernumber ) = @_;
253 return 1
254 if $borrowernumber and $self->owner == $borrowernumber;
255 return 0;
258 sub can_biblios_be_added {
259 my ( $self, $borrowernumber ) = @_;
261 return 1
262 if $borrowernumber
263 and ( $self->owner == $borrowernumber
264 or $self->allow_add );
265 return 0;
268 sub can_biblios_be_removed {
269 my ( $self, $borrowernumber ) = @_;
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;
279 sub _type {
280 return 'Virtualshelve';