Bug 17669: [QA Follow-up] Allow zero in temp-uploads-days
[koha.git] / Koha / UploadedFiles.pm
1 package Koha::UploadedFiles;
2
3 # Copyright Rijksmuseum 2016
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use C4::Koha;
23 use Koha::Database;
24 use Koha::DateUtils;
25 use Koha::UploadedFile;
26
27 use parent qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::UploadedFiles - Koha::Objects class for uploaded files
32
33 =head1 SYNOPSIS
34
35     use Koha::UploadedFiles;
36
37     # get one upload
38     my $upload01 = Koha::UploadedFiles->find( $id );
39
40     # get some uploads
41     my @uploads = Koha::UploadedFiles->search_term({ term => '.mrc' });
42
43     # delete all uploads
44     Koha::UploadedFiles->delete;
45
46 =head1 DESCRIPTION
47
48 Allows regular CRUD operations on uploaded_files via Koha::Objects / DBIx.
49
50 The delete method also takes care of deleting files. The search_term method
51 provides a wrapper around search to look for a term in multiple columns.
52
53 =head1 METHODS
54
55 =head2 INSTANCE METHODS
56
57 =head3 delete
58
59 Delete uploaded files.
60 Returns true if no errors occur. (So, false may mean partial success.)
61
62 Parameter keep_file may be used to delete records, but keep files.
63
64 =cut
65
66 sub delete {
67     my ( $self, $params ) = @_;
68     $self = Koha::UploadedFiles->new if !ref($self); # handle class call
69     # We use the individual delete on each resultset record
70     my $err = 0;
71     while( my $row = $self->next ) {
72         $err++ if !$row->delete( $params );
73     }
74     return $err==0;
75 }
76
77 =head3 delete_temporary
78
79 Delete_temporary is called by cleanup_database and only removes temporary
80 uploads older than [pref UploadPurgeTemporaryFilesDays] days.
81 It is possible to override the pref with the override_pref parameter.
82
83 Returns true if no errors occur. (Even when no files had to be deleted.)
84
85 =cut
86
87 sub delete_temporary {
88     my ( $self, $params ) = @_;
89     my $days = C4::Context->preference('UploadPurgeTemporaryFilesDays');
90     if( exists $params->{override_pref} ) {
91         $days = $params->{override_pref};
92     } elsif( !defined($days) || $days eq '' ) { # allow 0, not NULL or ""
93         return 1;
94     }
95     my $dt = dt_from_string();
96     $dt->subtract( days => $days );
97     my $parser = Koha::Database->new->schema->storage->datetime_parser;
98     return $self->search({
99         permanent => [ undef, 0 ],
100         dtcreated => { '<' => $parser->format_datetime($dt) },
101     })->delete;
102 }
103
104 =head3 search_term
105
106 Search_term allows you to pass a term to search in filename and hashvalue.
107 If you do not pass include_private, only public records are returned.
108
109 Is only a wrapper around Koha::Objects search. Has similar return value.
110
111 =cut
112
113 sub search_term {
114     my ( $self, $params ) = @_;
115     my $term = $params->{term} // '';
116     my %public = ();
117     if( !$params->{include_private} ) {
118         %public = ( public => 1 );
119     }
120     return $self->search(
121         [ { filename => { like => '%'.$term.'%' }, %public },
122           { hashvalue => { like => '%'.$params->{term}.'%' }, %public } ],
123         { order_by => { -asc => 'id' }},
124     );
125 }
126
127 =head2 CLASS METHODS
128
129 =head3 getCategories
130
131 getCategories returns a list of upload category codes and names
132
133 =cut
134
135 sub getCategories {
136     my ( $class ) = @_;
137     my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
138     [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
139 }
140
141 =head3 _type
142
143 Returns name of corresponding DBIC resultset
144
145 =cut
146
147 sub _type {
148     return 'UploadedFile';
149 }
150
151 =head3 object_class
152
153 Returns name of corresponding Koha object class
154
155 =cut
156
157 sub object_class {
158     return 'Koha::UploadedFile';
159 }
160
161 =head1 AUTHOR
162
163 Marcel de Rooy (Rijksmuseum)
164
165 Koha Development Team
166
167 =cut
168
169 1;