Update release notes for 23.05.12 release
[koha.git] / Koha / Virtualshelves.pm
1 package Koha::Virtualshelves;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20
21 use Koha::Database;
22
23 use Koha::Patrons;
24 use Koha::Virtualshelf;
25
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::Virtualshelf - Koha Virtualshelf Object class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 disown_or_delete
38
39     $lists->disown_or_delete;
40
41 This method will transfer public/shared lists to the appropriate patron or
42 just delete them if not possible.
43
44 =cut
45
46 sub disown_or_delete {
47     my ($self) = @_;
48
49     $self->_resultset->result_source->schema->txn_do(
50         sub {
51             if ( C4::Context->preference('ListOwnershipUponPatronDeletion') eq 'transfer' ) {
52                 my $new_owner;
53
54                 $new_owner = C4::Context->preference('ListOwnerDesignated')
55                   if C4::Context->preference('ListOwnerDesignated')
56                   and Koha::Patrons->find( C4::Context->preference('ListOwnerDesignated') );
57
58                 if( !$new_owner && C4::Context->userenv ) {
59                     $new_owner = C4::Context->userenv->{number};
60                 }
61
62                 while ( my $list = $self->next ) {
63                     if ( $new_owner && ( $list->is_public or $list->is_shared ) ) {
64                         $list->transfer_ownership($new_owner);
65                     } else {
66                         $list->delete;
67                     }
68                 }
69             } else {    # 'delete'
70                 $_->delete for $self->as_list;
71             }
72         }
73     );
74
75     return $self;
76 }
77
78 =head3 get_private_shelves
79
80 =cut
81
82 sub get_private_shelves {
83     my ( $self, $params ) = @_;
84     my $page = $params->{page};
85     my $rows = $params->{rows};
86     my $borrowernumber = $params->{borrowernumber} || 0;
87
88     $self->search(
89         {
90             public => 0,
91             -or => {
92                 'virtualshelfshares.borrowernumber' => $borrowernumber,
93                 'me.owner' => $borrowernumber,
94             }
95         },
96         {
97             join => [ 'virtualshelfshares' ],
98             distinct => 'shelfnumber',
99             order_by => 'shelfname',
100             ( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ),
101         }
102     );
103 }
104
105 =head3 get_public_shelves
106
107 =cut
108
109 sub get_public_shelves {
110     my ( $self, $params ) = @_;
111     my $page = $params->{page};
112     my $rows = $params->{rows};
113
114     $self->search(
115         {
116             public => 1,
117         },
118         {
119             distinct => 'shelfnumber',
120             order_by => 'shelfname',
121             ( ( $page and $rows ) ? ( page => $page, rows => $rows ) : () ),
122         }
123     );
124 }
125
126 =head3 get_some_shelves
127
128 =cut
129
130 sub get_some_shelves {
131     my ( $self, $params ) = @_;
132     my $borrowernumber = $params->{borrowernumber} || 0;
133     my $public = $params->{public} || 0;
134     my $add_allowed = $params->{add_allowed};
135
136     my @conditions;
137     my $patron;
138     my $staffuser = 0;
139     my $permitteduser = 0;
140     if ( $borrowernumber != 0 ) {
141         $patron = Koha::Patrons->find( $borrowernumber );
142         $staffuser = $patron->can_patron_change_staff_only_lists;
143         $permitteduser = $patron->can_patron_change_permitted_staff_lists;
144     }
145     if ( $add_allowed ) {
146         if ( $permitteduser ) {
147             push @conditions, {
148                 -or =>
149                 [
150                     {
151                         "me.owner" => $borrowernumber,
152                         "me.allow_change_from_owner" => 1,
153                     },
154                     "me.allow_change_from_others"          => 1,
155                     "me.allow_change_from_staff"           => 1,
156                     "me.allow_change_from_permitted_staff" => 1
157                 ]
158             };
159         } elsif ( $staffuser ) {
160             push @conditions, {
161                 -or =>
162                 [
163                     {
164                         "me.owner" => $borrowernumber,
165                         "me.allow_change_from_owner" => 1,
166                     },
167                     "me.allow_change_from_others"          => 1,
168                     "me.allow_change_from_staff"           => 1
169                 ]
170             };
171         } else {
172             push @conditions, {
173                 -or =>
174                 [
175                     {
176                         "me.owner" => $borrowernumber,
177                         "me.allow_change_from_owner" => 1,
178                     },
179                     "me.allow_change_from_others" => 1,
180                 ]
181             };
182         }
183     }
184     if ( !$public ) {
185         push @conditions, {
186             -or =>
187             {
188                 "virtualshelfshares.borrowernumber" => $borrowernumber,
189                 "me.owner" => $borrowernumber,
190             }
191         };
192     }
193
194     $self->search(
195         {
196             public => $public,
197             ( @conditions ? ( -and => \@conditions ) : () ),
198         },
199         {
200             join => [ 'virtualshelfshares' ],
201             distinct => 'shelfnumber',
202             order_by => { -desc => 'lastmodified' },
203         }
204     );
205 }
206
207 =head3 get_shelves_containing_record
208
209 =cut
210
211 sub get_shelves_containing_record {
212     my ( $self, $params ) = @_;
213     my $borrowernumber = $params->{borrowernumber};
214     my $biblionumber   = $params->{biblionumber};
215
216     my @conditions = ( 'virtualshelfcontents.biblionumber' => $biblionumber );
217     if ($borrowernumber) {
218         push @conditions,
219           {
220               -or => [
221                 {
222                     public => 0,
223                     -or      => {
224                         'me.owner' => $borrowernumber,
225                         -or        => {
226                             'virtualshelfshares.borrowernumber' => $borrowernumber,
227                         },
228                     }
229                 },
230                 { public => 1 },
231             ]
232           };
233     } else {
234         push @conditions, { public => 1 };
235     }
236
237     return Koha::Virtualshelves->search(
238         {
239             -and => \@conditions
240         },
241         {
242             join     => [ 'virtualshelfcontents', 'virtualshelfshares' ],
243             distinct => 'shelfnumber',
244             order_by => { -asc => 'shelfname' },
245         }
246     );
247 }
248
249 =head3 _type
250
251 =cut
252
253 sub _type {
254     return 'Virtualshelve';
255 }
256
257 =head3 object_class
258
259 =cut
260
261 sub object_class {
262     return 'Koha::Virtualshelf';
263 }
264
265 1;