Bug 17501: [Follow-up] QA Requests
[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::UploadedFile;
24
25 use parent qw(Koha::Objects);
26
27 =head1 NAME
28
29 Koha::UploadedFiles - Koha::Objects class for uploaded files
30
31 =head1 SYNOPSIS
32
33     use Koha::UploadedFiles;
34
35     # get one upload
36     my $upload01 = Koha::UploadedFiles->find( $id );
37
38     # get some uploads
39     my @uploads = Koha::UploadedFiles->search_term({ term => '.mrc' });
40
41     # delete all uploads
42     Koha::UploadedFiles->delete;
43
44 =head1 DESCRIPTION
45
46 Allows regular CRUD operations on uploaded_files via Koha::Objects / DBIx.
47
48 The delete method also takes care of deleting files. The search_term method
49 provides a wrapper around search to look for a term in multiple columns.
50
51 =head1 METHODS
52
53 =head2 INSTANCE METHODS
54
55 =head3 delete
56
57 Delete uploaded files.
58 Returns true if no errors occur. (So, false may mean partial success.)
59
60 Parameter keep_file may be used to delete records, but keep files.
61
62 =cut
63
64 sub delete {
65     my ( $self, $params ) = @_;
66     # We use the individual delete on each resultset record
67     my $err = 0;
68     while( my $row = $self->_resultset->next ) {
69         my $kohaobj = Koha::UploadedFile->_new_from_dbic( $row );
70         $err++ if !$kohaobj->delete( $params );
71     }
72     return $err==0;
73 }
74
75 =head3 search_term
76
77 Search_term allows you to pass a term to search in filename and hashvalue.
78 If you do not pass include_private, only public records are returned.
79
80 Is only a wrapper around Koha::Objects search. Has similar return value.
81
82 =cut
83
84 sub search_term {
85     my ( $self, $params ) = @_;
86     my $term = $params->{term} // '';
87     my %public = ();
88     if( !$params->{include_private} ) {
89         %public = ( public => 1 );
90     }
91     return $self->search(
92         [ { filename => { like => '%'.$term.'%' }, %public },
93           { hashvalue => { like => '%'.$params->{term}.'%' }, %public } ],
94         { order_by => { -asc => 'id' }},
95     );
96 }
97
98 =head2 CLASS METHODS
99
100 =head3 getCategories
101
102 getCategories returns a list of upload category codes and names
103
104 =cut
105
106 sub getCategories {
107     my ( $class ) = @_;
108     my $cats = C4::Koha::GetAuthorisedValues('UPLOAD');
109     [ map {{ code => $_->{authorised_value}, name => $_->{lib} }} @$cats ];
110 }
111
112 =head3 _type
113
114 Returns name of corresponding DBIC resultset
115
116 =cut
117
118 sub _type {
119     return 'UploadedFile';
120 }
121
122 =head3 object_class
123
124 Returns name of corresponding Koha object class
125
126 =cut
127
128 sub object_class {
129     return 'Koha::UploadedFile';
130 }
131
132 =head1 AUTHOR
133
134 Marcel de Rooy (Rijksmuseum)
135
136 Koha Development Team
137
138 =cut
139
140 1;