Lines 20-59
package Koha::Misc::Files;
Link Here
|
20 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
20 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
21 |
|
21 |
|
22 |
use Modern::Perl; |
22 |
use Modern::Perl; |
23 |
use strict; ## not needed if Modern::Perl, but perlcritic complains.. |
|
|
24 |
use vars qw($VERSION); |
23 |
use vars qw($VERSION); |
25 |
$VERSION = '0.20'; |
24 |
$VERSION = '0.23'; |
26 |
|
25 |
|
27 |
use C4::Context; |
26 |
use C4::Context; |
28 |
use C4::Output; |
27 |
use C4::Output; |
29 |
use C4::Dates; |
28 |
use C4::Dates; |
30 |
use C4::Debug; |
|
|
31 |
|
29 |
|
32 |
=head1 NAME |
30 |
=head1 NAME |
33 |
|
31 |
|
34 |
Koha::Misc::Files - module for managing miscellaneous files |
32 |
Koha::Misc::Files - module for managing miscellaneous files associated |
35 |
associated with records from arbitrary tables |
33 |
with records from arbitrary tables |
|
|
34 |
|
35 |
=head1 SYNOPSIS |
36 |
|
37 |
use Koha::Misc::Files; |
38 |
|
39 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
40 |
recordid => $recordnumber ); |
41 |
|
42 |
=head1 FUNCTIONS |
43 |
|
44 |
=over |
45 |
|
46 |
=item new() |
47 |
|
48 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
49 |
recordid => $recordnumber ); |
50 |
|
51 |
Creates new Koha::Misc::Files object. Such object is essentially |
52 |
a pair: in typical usage scenario, 'tabletag' parameter will be |
53 |
a database table name, and 'recordid' an unique record ID number |
54 |
from this table. However, this method does accept an arbitrary |
55 |
string as 'tabletag', and an arbitrary integer as 'recordid'. |
56 |
|
57 |
Particular Koha::Misc::Files object can have one or more file records |
58 |
(actuall file contents + various file metadata) associated with it. |
59 |
|
60 |
In case of an error (wrong parameter format) it returns undef. |
36 |
|
61 |
|
37 |
=cut |
62 |
=cut |
38 |
|
63 |
|
39 |
sub new { |
64 |
sub new { |
40 |
my ( $class, %args ) = @_; |
65 |
my ( $class, %args ) = @_; |
41 |
|
66 |
|
42 |
( defined( $args{'tabletag'} ) && defined( $args{'recordid'} ) ) |
67 |
my $recid = $args{'recordid'}; |
|
|
68 |
my $tag = $args{'tabletag'}; |
69 |
( defined($tag) && $tag ne '' && defined($recid) && $recid =~ /^\d+$/ ) |
43 |
|| return (); |
70 |
|| return (); |
|
|
71 |
|
44 |
my $self = bless( {}, $class ); |
72 |
my $self = bless( {}, $class ); |
45 |
|
73 |
|
46 |
$self->{'table_tag'} = $args{'tabletag'}; |
74 |
$self->{'table_tag'} = $tag; |
47 |
$self->{'record_id'} = $args{'recordid'}; |
75 |
$self->{'record_id'} = '' . ( 0 + $recid ); |
48 |
|
76 |
|
49 |
return $self; |
77 |
return $self; |
50 |
} |
78 |
} |
51 |
|
79 |
|
52 |
=item GetFilesInfo() |
80 |
=item GetFilesInfo() |
53 |
|
81 |
|
54 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
82 |
my $files_descriptions = $mf->GetFilesInfo(); |
55 |
recordid => $recordnumber); |
83 |
|
56 |
my $files_hashref = $mf->GetFilesInfo |
84 |
This method returns a reference to an array of hashes |
|
|
85 |
containing files metadata (file_id, file_name, file_type, |
86 |
file_description, file_size, date_uploaded) for all file records |
87 |
associated with given $mf object, or an empty arrayref if there are |
88 |
no such records yet. |
89 |
|
90 |
In case of an error it returns undef. |
57 |
|
91 |
|
58 |
=cut |
92 |
=cut |
59 |
|
93 |
|
Lines 61-113
sub GetFilesInfo {
Link Here
|
61 |
my $self = shift; |
95 |
my $self = shift; |
62 |
|
96 |
|
63 |
my $dbh = C4::Context->dbh; |
97 |
my $dbh = C4::Context->dbh; |
64 |
my $query = " |
98 |
my $query = ' |
65 |
SELECT |
99 |
SELECT |
66 |
file_id, |
100 |
file_id, |
67 |
file_name, |
101 |
file_name, |
68 |
file_type, |
102 |
file_type, |
69 |
file_description, |
103 |
file_description, |
70 |
date_uploaded |
104 |
date_uploaded, |
|
|
105 |
LENGTH(file_content) AS file_size |
71 |
FROM misc_files |
106 |
FROM misc_files |
72 |
WHERE table_tag = ? AND record_id = ? |
107 |
WHERE table_tag = ? AND record_id = ? |
73 |
ORDER BY file_name, date_uploaded |
108 |
ORDER BY file_name, date_uploaded |
74 |
"; |
109 |
'; |
75 |
my $sth = $dbh->prepare($query); |
110 |
my $sth = $dbh->prepare($query); |
76 |
$sth->execute( $self->{'table_tag'}, $self->{'record_id'} ); |
111 |
$sth->execute( $self->{'table_tag'}, $self->{'record_id'} ); |
77 |
return $sth->fetchall_arrayref( {} ); |
112 |
return $sth->fetchall_arrayref( {} ); |
78 |
} |
113 |
} |
79 |
|
114 |
|
80 |
=item AddFile() |
115 |
=item AddFile() |
81 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
116 |
|
82 |
recordid => $recordnumber); |
117 |
$mf->AddFile( name => $filename, type => $mimetype, |
83 |
$mf->AddFile( name => $filename, type => $mimetype, description => $description, content => $content ); |
118 |
description => $description, content => $content ); |
|
|
119 |
|
120 |
Adds a new file (we want to store for / associate with a given |
121 |
object) to the database. Parameters 'name' and 'content' are mandatory. |
122 |
Note: this method would (silently) fail if there is no 'name' given |
123 |
or if the 'content' provided is empty. |
124 |
|
84 |
=cut |
125 |
=cut |
85 |
|
126 |
|
86 |
sub AddFile { |
127 |
sub AddFile { |
87 |
my ( $self, %args ) = @_; |
128 |
my ( $self, %args ) = @_; |
88 |
|
129 |
|
89 |
my $name = $args{'name'}; |
130 |
my $name = $args{'name'}; |
90 |
my $type = $args{'type'}; |
131 |
my $type = $args{'type'} // ''; |
91 |
my $description = $args{'description'}; |
132 |
my $description = $args{'description'}; |
92 |
my $content = $args{'content'}; |
133 |
my $content = $args{'content'}; |
93 |
|
134 |
|
94 |
return unless ( $name && $content ); |
135 |
return unless ( defined($name) && $name ne '' && defined($content) && $content ne '' ); |
95 |
|
136 |
|
96 |
my $dbh = C4::Context->dbh; |
137 |
my $dbh = C4::Context->dbh; |
97 |
my $query = " |
138 |
my $query = ' |
98 |
INSERT INTO misc_files ( table_tag, record_id, file_name, file_type, file_description, file_content ) |
139 |
INSERT INTO misc_files ( table_tag, record_id, file_name, file_type, file_description, file_content ) |
99 |
VALUES ( ?,?,?,?,?,? ) |
140 |
VALUES ( ?,?,?,?,?,? ) |
100 |
"; |
141 |
'; |
101 |
my $sth = $dbh->prepare($query); |
142 |
my $sth = $dbh->prepare($query); |
102 |
$sth->execute( $self->{'table_tag'}, $self->{'record_id'}, $name, $type, |
143 |
$sth->execute( $self->{'table_tag'}, $self->{'record_id'}, $name, $type, |
103 |
$description, $content ); |
144 |
$description, $content ); |
104 |
} |
145 |
} |
105 |
|
146 |
|
106 |
=item GetFile() |
147 |
=item GetFile() |
107 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
148 |
|
108 |
recordid => $recordnumber); |
149 |
my $file = $mf->GetFile( id => $file_id ); |
109 |
... |
150 |
|
110 |
my $file = $mf->GetFile( id => $file_id ); |
151 |
For an individual, specific file ID this method returns a hashref |
|
|
152 |
containing all metadata (file_id, table_tag, record_id, file_name, |
153 |
file_type, file_description, file_content, date_uploaded), plus |
154 |
an actuall contents of a file (in 'file_content'). In typical usage |
155 |
scenarios, for a given $mf object, specific file IDs have to be |
156 |
obtained first by GetFilesInfo() call. |
157 |
|
158 |
Returns undef in case when file ID specified as 'id' parameter was not |
159 |
found in the database. |
160 |
|
111 |
=cut |
161 |
=cut |
112 |
|
162 |
|
113 |
sub GetFile { |
163 |
sub GetFile { |
Lines 116-134
sub GetFile {
Link Here
|
116 |
my $file_id = $args{'id'}; |
166 |
my $file_id = $args{'id'}; |
117 |
|
167 |
|
118 |
my $dbh = C4::Context->dbh; |
168 |
my $dbh = C4::Context->dbh; |
119 |
my $query = " |
169 |
my $query = ' |
120 |
SELECT * FROM misc_files WHERE file_id = ? AND table_tag = ? AND record_id = ? |
170 |
SELECT * FROM misc_files WHERE file_id = ? AND table_tag = ? AND record_id = ? |
121 |
"; |
171 |
'; |
122 |
my $sth = $dbh->prepare($query); |
172 |
my $sth = $dbh->prepare($query); |
123 |
$sth->execute( $file_id, $self->{'table_tag'}, $self->{'record_id'} ); |
173 |
$sth->execute( $file_id, $self->{'table_tag'}, $self->{'record_id'} ); |
124 |
return $sth->fetchrow_hashref(); |
174 |
return $sth->fetchrow_hashref(); |
125 |
} |
175 |
} |
126 |
|
176 |
|
127 |
=item DelFile() |
177 |
=item DelFile() |
128 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
178 |
|
129 |
recordid => $recordnumber); |
179 |
$mf->DelFile( id => $file_id ); |
130 |
... |
180 |
|
131 |
$mf->DelFile( id => $file_id ); |
181 |
Deletes specific, individual file record (file contents and metadata) |
|
|
182 |
from the database. |
183 |
|
132 |
=cut |
184 |
=cut |
133 |
|
185 |
|
134 |
sub DelFile { |
186 |
sub DelFile { |
Lines 137-174
sub DelFile {
Link Here
|
137 |
my $file_id = $args{'id'}; |
189 |
my $file_id = $args{'id'}; |
138 |
|
190 |
|
139 |
my $dbh = C4::Context->dbh; |
191 |
my $dbh = C4::Context->dbh; |
140 |
my $query = " |
192 |
my $query = ' |
141 |
DELETE FROM misc_files WHERE file_id = ? AND table_tag = ? AND record_id = ? |
193 |
DELETE FROM misc_files WHERE file_id = ? AND table_tag = ? AND record_id = ? |
142 |
"; |
194 |
'; |
143 |
my $sth = $dbh->prepare($query); |
195 |
my $sth = $dbh->prepare($query); |
144 |
$sth->execute( $file_id, $self->{'table_tag'}, $self->{'record_id'} ); |
196 |
$sth->execute( $file_id, $self->{'table_tag'}, $self->{'record_id'} ); |
145 |
} |
197 |
} |
146 |
|
198 |
|
147 |
=item DelAllFiles() |
199 |
=item DelAllFiles() |
148 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
200 |
|
149 |
recordid => $recordnumber); |
201 |
$mf->DelAllFiles(); |
150 |
$mf->DelAllFiles; |
202 |
|
|
|
203 |
Deletes all file records associated with (stored for) a given $mf object. |
204 |
|
151 |
=cut |
205 |
=cut |
152 |
|
206 |
|
153 |
sub DelAllFiles { |
207 |
sub DelAllFiles { |
154 |
my ($self) = @_; |
208 |
my ($self) = @_; |
155 |
|
209 |
|
156 |
my $dbh = C4::Context->dbh; |
210 |
my $dbh = C4::Context->dbh; |
157 |
my $query = " |
211 |
my $query = ' |
158 |
DELETE FROM misc_files WHERE table_tag = ? AND record_id = ? |
212 |
DELETE FROM misc_files WHERE table_tag = ? AND record_id = ? |
159 |
"; |
213 |
'; |
160 |
my $sth = $dbh->prepare($query); |
214 |
my $sth = $dbh->prepare($query); |
161 |
$sth->execute( $self->{'table_tag'}, $self->{'record_id'} ); |
215 |
$sth->execute( $self->{'table_tag'}, $self->{'record_id'} ); |
162 |
} |
216 |
} |
163 |
|
217 |
|
|
|
218 |
=item MergeFileRecIds() |
219 |
|
220 |
$mf->MergeFileRecIds(@ids_to_be_merged); |
221 |
|
222 |
This method re-associates all individuall file records associated with |
223 |
some "parent" records IDs (provided in @ids_to_be_merged) with the given |
224 |
single $mf object (which would be treated as a "parent" destination). |
225 |
|
226 |
This a helper method; typically it needs to be called only in cases when |
227 |
some "parent" records are being merged in the (external) 'tablename' |
228 |
table. |
229 |
|
230 |
=cut |
231 |
|
232 |
sub MergeFileRecIds { |
233 |
my ( $self, @ids_to_merge ) = @_; |
234 |
|
235 |
my $dst_recid = $self->{'record_id'}; |
236 |
@ids_to_merge = map { ( $dst_recid == $_ ) ? () : ($_); } @ids_to_merge; |
237 |
@ids_to_merge > 0 || return (); |
238 |
|
239 |
my $dbh = C4::Context->dbh; |
240 |
my $query = ' |
241 |
UPDATE misc_files SET record_id = ? |
242 |
WHERE table_tag = ? AND record_id = ? |
243 |
'; |
244 |
my $sth = $dbh->prepare($query); |
245 |
|
246 |
for my $src_recid (@ids_to_merge) { |
247 |
$sth->execute( $dst_recid, $self->{'table_tag'}, $src_recid ); |
248 |
} |
249 |
} |
250 |
|
164 |
1; |
251 |
1; |
|
|
252 |
|
165 |
__END__ |
253 |
__END__ |
166 |
|
254 |
|
167 |
=back |
255 |
=back |
168 |
|
256 |
|
|
|
257 |
=head1 SEE ALSO |
258 |
|
259 |
Koha::Borrower::Files |
260 |
|
169 |
=head1 AUTHOR |
261 |
=head1 AUTHOR |
170 |
|
262 |
|
171 |
Kyle M Hall <kyle.m.hall@gmail.com> |
263 |
Kyle M Hall E<lt>kyle.m.hall@gmail.comE<gt>, |
172 |
Jacek Ablewicz <ablewicz@gmail.com> |
264 |
Jacek Ablewicz E<lt>ablewicz@gmail.comE<gt> |
173 |
|
265 |
|
174 |
=cut |
266 |
=cut |