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 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
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.
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.
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.
60 Returns true if no errors occur. (So, false may mean partial success.)
62 Parameter keep_file may be used to delete records, but keep files.
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
71 while( my $row = $self->next ) {
72 $err++ if !$row->delete( $params );
77 =head3 delete_temporary
79 Delete_temporary is called by cleanup_database and only removes temporary
80 uploads older than [pref Upload_PurgeTemporaryFiles_Days] days.
81 It is possible to override the pref with the override_pref parameter.
83 Returns true if no errors occur. (Even when no files had to be deleted.)
87 sub delete_temporary {
88 my ( $self, $params ) = @_;
89 my $days = $params->{override_pref} ||
90 C4::Context->preference('Upload_PurgeTemporaryFiles_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) },
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.
106 Is only a wrapper around Koha::Objects search. Has similar return value.
111 my ( $self, $params ) = @_;
112 my $term = $params->{term} // '';
114 if( !$params->{include_private} ) {
115 %public = ( public => 1 );
117 return $self->search(
118 [ { filename => { like => '%'.$term.'%' }, %public },
119 { hashvalue => { like => '%'.$params->{term}.'%' }, %public } ],
120 { order_by => { -asc => 'id' }},
128 getCategories returns a list of upload category codes and names
134 my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
135 [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
140 Returns name of corresponding DBIC resultset
145 return 'UploadedFile';
150 Returns name of corresponding Koha object class
155 return 'Koha::UploadedFile';
160 Marcel de Rooy (Rijksmuseum)
162 Koha Development Team