Bug 14544: Move share routines to Koha::Virtualshelfshare and Koha::Virtualshelfshares
[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
28 use base qw(Koha::Object);
29
30 =head1 NAME
31
32 Koha::Virtualshelf - Koha Virtualshelf Object class
33
34 =head1 API
35
36 =head2 Class Methods
37
38 =cut
39
40 =head3 type
41
42 =cut
43
44 our $PRIVATE = 1;
45 our $PUBLIC = 2;
46
47 sub store {
48     my ( $self ) = @_;
49
50     unless ( $self->is_shelfname_valid ) {
51         Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
52     }
53
54     $self->allow_add( 0 )
55         unless defined $self->allow_add;
56     $self->allow_delete_own( 1 )
57         unless defined $self->allow_delete_own;
58     $self->allow_delete_other( 0 )
59         unless defined $self->allow_delete_other;
60
61     $self->created_on( dt_from_string );
62
63     return $self->SUPER::store( $self );
64 }
65
66 sub is_shelfname_valid {
67     my ( $self ) = @_;
68
69     my $conditions = {
70         shelfname => $self->shelfname,
71         ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
72     };
73
74     if ( $self->category == $PRIVATE and defined $self->owner ) {
75         $conditions->{-or} = {
76             "virtualshelfshares.borrowernumber" => $self->owner,
77             "me.owner" => $self->owner,
78         };
79         $conditions->{category} = $PRIVATE;
80     }
81     elsif ( $self->category == $PRIVATE and not defined $self->owner ) {
82         $conditions->{owner} = undef;
83         $conditions->{category} = $PRIVATE;
84     }
85     else {
86         $conditions->{category} = $PUBLIC;
87     }
88
89     my $count = Koha::Virtualshelves->search(
90         $conditions,
91         {
92             join => 'virtualshelfshares',
93         }
94     )->count;
95     return $count ? 0 : 1;
96 }
97
98 sub get_shares {
99     my ( $self ) = @_;
100     return $self->{_result}->virtualshelfshares;
101 }
102
103 sub share {
104     my ( $self, $key ) = @_;
105     unless ( $key ) {
106         Koha::Exceptions::Virtualshelves::InvalidKeyOnSharing->throw;
107     }
108     Koha::Virtualshelfshare->new(
109         {
110             shelfnumber => $self->shelfnumber,
111             invitekey => $key,
112             sharedate => dt_from_string,
113         }
114     )->store;
115 }
116
117 sub is_shared {
118     my ( $self ) = @_;
119     return  $self->get_shares->search(
120         {
121             borrowernumber => { '!=' => undef },
122         }
123     )->count;
124 }
125
126 sub remove_share {
127     my ( $self, $borrowernumber ) = @_;
128     my $shelves = Koha::Virtualshelfshares->search(
129         {
130             shelfnumber => $self->shelfnumber,
131             borrowernumber => $borrowernumber,
132         }
133     );
134     return 0 unless $shelves->count;
135
136     # Only 1 share with 1 patron can exist
137     return $shelves->next->delete;
138 }
139
140 sub type {
141     return 'Virtualshelve';
142 }
143
144 1;