1 package Koha
::UploadedFiles
;
3 # Copyright Rijksmuseum 2016
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 use Koha
::UploadedFile
;
27 use parent
qw(Koha::Objects);
31 Koha::UploadedFiles - Koha::Objects class for uploaded files
35 use Koha::UploadedFiles;
38 my $upload01 = Koha::UploadedFiles->find( $id );
41 my @uploads = Koha::UploadedFiles->search_term({ term => '.mrc' });
44 Koha::UploadedFiles->delete;
48 Allows regular CRUD operations on uploaded_files via Koha::Objects / DBIx.
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.
55 =head2 INSTANCE METHODS
59 Delete uploaded files.
61 Parameter keep_file may be used to delete records, but keep files.
63 Returns the number of deleted records, 0E0 or -1 (Unknown).
64 Please note that the number of deleted records is not automatically the same
65 as the number of files.
70 my ( $self, $params ) = @_;
71 $self = Koha
::UploadedFiles
->new if !ref($self); # handle class call
72 # We use the individual delete on each resultset record
74 while( my $row = $self->next ) {
75 my $deleted = eval { $row->delete( $params ) };
76 $rv++ if $deleted && !$@
;
78 return $rv==0 ?
"0E0" : $rv;
81 =head3 delete_temporary
83 Delete_temporary is called by cleanup_database and only removes temporary
84 uploads older than [pref UploadPurgeTemporaryFilesDays] days.
85 It is possible to override the pref with the override_pref parameter.
87 Return value: see delete.
91 sub delete_temporary
{
92 my ( $self, $params ) = @_;
93 my $days = C4
::Context
->preference('UploadPurgeTemporaryFilesDays');
94 if( exists $params->{override_pref
} ) {
95 $days = $params->{override_pref
};
96 } elsif( !defined($days) || $days eq '' ) { # allow 0, not NULL or ""
99 my $dt = dt_from_string
();
100 $dt->subtract( days
=> $days );
101 my $parser = Koha
::Database
->new->schema->storage->datetime_parser;
102 return $self->search({
103 permanent
=> [ undef, 0 ],
104 dtcreated
=> { '<' => $parser->format_datetime($dt) },
108 =head3 delete_missing
110 $cnt = Koha::UploadedFiles->delete_missing();
112 $cnt = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
114 Deletes all records where the actual file is not found.
116 Supports a keep_record hash parameter to do a check only.
118 Return value: If you pass keep_record, it returns the number of records where
119 the file is not found, or 0E0. Otherwise it returns a number, 0E0 or -1 just
125 my ( $self, $params ) = @_;
126 $self = Koha
::UploadedFiles
->new if !ref($self); # handle class call
128 while( my $row = $self->next ) {
129 my $file = $row->full_path;
131 if( $params->{keep_record
} ) {
135 # We are passing keep_file since we already know that the file
136 # is missing and we do not want to see the warning
137 # Apply the same logic as in delete for the return value
138 my $deleted = eval { $row->delete({ keep_file
=> 1 }) };
139 $rv++ if $deleted && !$@
;
141 return $rv==0 ?
"0E0" : $rv;
146 Search_term allows you to pass a term to search in filename and hashvalue.
147 If you do not pass include_private, only public records are returned.
149 Is only a wrapper around Koha::Objects search. Has similar return value.
154 my ( $self, $params ) = @_;
155 my $term = $params->{term
} // '';
157 if( !$params->{include_private
} ) {
158 %public = ( public
=> 1 );
160 return $self->search(
161 [ { filename
=> { like
=> '%'.$term.'%' }, %public },
162 { hashvalue
=> { like
=> '%'.$params->{term
}.'%' }, %public } ],
163 { order_by
=> { -asc
=> 'id' }},
171 getCategories returns a list of upload category codes and names
177 my $cats = C4
::Koha
::GetAuthorisedValues
('UPLOAD');
178 [ map {{ code
=> $_->{authorised_value
}, name
=> $_->{lib
} }} @
$cats ];
183 Returns name of corresponding DBIC resultset
188 return 'UploadedFile';
193 Returns name of corresponding Koha object class
198 return 'Koha::UploadedFile';
203 Marcel de Rooy (Rijksmuseum)
205 Koha Development Team