Bug 14544: Koha::Virtualshelfcontent[s]
[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 Koha::Database;
23 use Koha::DateUtils qw( dt_from_string );
24 use Koha::Exceptions;
25 use Koha::Virtualshelfshare;
26 use Koha::Virtualshelfshares;
27 use Koha::Virtualshelfcontent;
28
29 use base qw(Koha::Object);
30
31 =head1 NAME
32
33 Koha::Virtualshelf - Koha Virtualshelf Object class
34
35 =head1 API
36
37 =head2 Class Methods
38
39 =cut
40
41 =head3 type
42
43 =cut
44
45 our $PRIVATE = 1;
46 our $PUBLIC = 2;
47
48 sub store {
49     my ( $self ) = @_;
50
51     unless ( $self->is_shelfname_valid ) {
52         Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
53     }
54
55     $self->allow_add( 0 )
56         unless defined $self->allow_add;
57     $self->allow_delete_own( 1 )
58         unless defined $self->allow_delete_own;
59     $self->allow_delete_other( 0 )
60         unless defined $self->allow_delete_other;
61
62     $self->created_on( dt_from_string );
63
64     return $self->SUPER::store( $self );
65 }
66
67 sub is_shelfname_valid {
68     my ( $self ) = @_;
69
70     my $conditions = {
71         shelfname => $self->shelfname,
72         ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
73     };
74
75     if ( $self->category == $PRIVATE and defined $self->owner ) {
76         $conditions->{-or} = {
77             "virtualshelfshares.borrowernumber" => $self->owner,
78             "me.owner" => $self->owner,
79         };
80         $conditions->{category} = $PRIVATE;
81     }
82     elsif ( $self->category == $PRIVATE and not defined $self->owner ) {
83         $conditions->{owner} = undef;
84         $conditions->{category} = $PRIVATE;
85     }
86     else {
87         $conditions->{category} = $PUBLIC;
88     }
89
90     my $count = Koha::Virtualshelves->search(
91         $conditions,
92         {
93             join => 'virtualshelfshares',
94         }
95     )->count;
96     return $count ? 0 : 1;
97 }
98
99 sub get_shares {
100     my ( $self ) = @_;
101     return $self->{_result}->virtualshelfshares;
102 }
103
104 sub get_contents {
105     my ( $self ) = @_;
106     return $self->{_result}->virtualshelfcontents;
107 }
108
109 sub share {
110     my ( $self, $key ) = @_;
111     unless ( $key ) {
112         Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
113     }
114     Koha::Virtualshelfshare->new(
115         {
116             shelfnumber => $self->shelfnumber,
117             invitekey => $key,
118             sharedate => dt_from_string,
119         }
120     )->store;
121 }
122
123 sub is_shared {
124     my ( $self ) = @_;
125     return  $self->get_shares->search(
126         {
127             borrowernumber => { '!=' => undef },
128         }
129     )->count;
130 }
131
132 sub remove_share {
133     my ( $self, $borrowernumber ) = @_;
134     my $shelves = Koha::Virtualshelfshares->search(
135         {
136             shelfnumber => $self->shelfnumber,
137             borrowernumber => $borrowernumber,
138         }
139     );
140     return 0 unless $shelves->count;
141
142     # Only 1 share with 1 patron can exist
143     return $shelves->next->delete;
144 }
145
146 sub add_biblio {
147     my ( $self, $biblionumber, $borrowernumber ) = @_;
148     return unless $biblionumber;
149     my $already_exists = $self->get_contents->search(
150         {
151             biblionumber => $biblionumber,
152         }
153     )->count;
154     return if $already_exists;
155     my $content = Koha::Virtualshelfcontent->new(
156         {
157             shelfnumber => $self->shelfnumber,
158             biblionumber => $biblionumber,
159             borrowernumber => $borrowernumber,
160         }
161     )->store;
162     $self->lastmodified(dt_from_string);
163     $self->store;
164
165     return $content;
166 }
167
168 sub remove_biblios {
169     my ( $self, $params ) = @_;
170     my $biblionumbers = $params->{biblionumbers} || [];
171     my $borrowernumber = $params->{borrowernumber};
172     return unless @$biblionumbers;
173
174     my $number_removed = 0;
175     for my $biblionumber ( @$biblionumbers ) {
176         if ( $self->owner == $borrowernumber or $self->allow_delete_own ) {
177             $number_removed += $self->get_contents->search(
178                 {
179                     biblionumber => $biblionumber,
180                     borrowernumber => $borrowernumber,
181                 }
182             )->delete;
183         }
184         if ( $self->allow_delete_other ) {
185             $number_removed += $self->get_contents->search(
186                 {
187                     biblionumber => $biblionumber,
188                     # FIXME
189                     # This does not make sense, but it's has been backported from DelFromShelf.
190                     # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
191                     borrowernumber => {
192                         -or => {
193                             '!=' => $borrowernumber,
194                             '=' => undef
195                         }
196                     },
197                 }
198             )->delete;
199         }
200     }
201     return $number_removed;
202 }
203
204 sub type {
205     return 'Virtualshelve';
206 }
207
208 1;