Bug 17669: [QA Follow-up] Rename preference by removing underscores
[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 = $params->{override_pref} ||
90         C4::Context->preference('UploadPurgeTemporaryFilesDays');
91     return 1 if !$days;
92     my $dt = dt_from_string();
93     $dt->subtract( days => $days );
94     my $parser = Koha::Database->new->schema->storage->datetime_parser;
95     return $self->search({
96         permanent => [ undef, 0 ],
97         dtcreated => { '<' => $parser->format_datetime($dt) },
98     })->delete;
99 }
100
101 =head3 search_term
102
103 Search_term allows you to pass a term to search in filename and hashvalue.
104 If you do not pass include_private, only public records are returned.
105
106 Is only a wrapper around Koha::Objects search. Has similar return value.
107
108 =cut
109
110 sub search_term {
111     my ( $self, $params ) = @_;
112     my $term = $params->{term} // '';
113     my %public = ();
114     if( !$params->{include_private} ) {
115         %public = ( public => 1 );
116     }
117     return $self->search(
118         [ { filename => { like => '%'.$term.'%' }, %public },
119           { hashvalue => { like => '%'.$params->{term}.'%' }, %public } ],
120         { order_by => { -asc => 'id' }},
121     );
122 }
123
124 =head2 CLASS METHODS
125
126 =head3 getCategories
127
128 getCategories returns a list of upload category codes and names
129
130 =cut
131
132 sub getCategories {
133     my ( $class ) = @_;
134     my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
135     [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
136 }
137
138 =head3 _type
139
140 Returns name of corresponding DBIC resultset
141
142 =cut
143
144 sub _type {
145     return 'UploadedFile';
146 }
147
148 =head3 object_class
149
150 Returns name of corresponding Koha object class
151
152 =cut
153
154 sub object_class {
155     return 'Koha::UploadedFile';
156 }
157
158 =head1 AUTHOR
159
160 Marcel de Rooy (Rijksmuseum)
161
162 Koha Development Team
163
164 =cut
165
166 1;