1 package Koha
::Misc
::Files
;
3 # This file is part of Koha.
5 # Copyright 2012 Kyle M Hall
6 # Copyright 2014 Jacek Ablewicz
7 # Based on Koha/Borrower/Files.pm by Kyle M Hall
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
29 Koha::Misc::Files - module for managing miscellaneous files associated
30 with records from arbitrary tables
34 use Koha::Misc::Files;
36 my $mf = Koha::Misc::Files->new( tabletag => $tablename,
37 recordid => $recordnumber );
45 my $mf = Koha::Misc::Files->new( tabletag => $tablename,
46 recordid => $recordnumber );
48 Creates new Koha::Misc::Files object. Such object is essentially
49 a pair: in typical usage scenario, 'tabletag' parameter will be
50 a database table name, and 'recordid' an unique record ID number
51 from this table. However, this method does accept an arbitrary
52 string as 'tabletag', and an arbitrary integer as 'recordid'.
54 Particular Koha::Misc::Files object can have one or more file records
55 (actuall file contents + various file metadata) associated with it.
57 In case of an error (wrong parameter format) it returns undef.
62 my ( $class, %args ) = @_;
64 my $recid = $args{'recordid'};
65 my $tag = $args{'tabletag'};
66 ( defined($tag) && $tag ne '' && defined($recid) && $recid =~ /^\d+$/ )
69 my $self = bless( {}, $class );
71 $self->{'table_tag'} = $tag;
72 $self->{'record_id'} = '' . ( 0 + $recid );
79 my $files_descriptions = $mf->GetFilesInfo();
81 This method returns a reference to an array of hashes
82 containing files metadata (file_id, file_name, file_type,
83 file_description, file_size, date_uploaded) for all file records
84 associated with given $mf object, or an empty arrayref if there are
87 In case of an error it returns undef.
94 my $dbh = C4
::Context
->dbh;
102 LENGTH(file_content) AS file_size
104 WHERE table_tag = ? AND record_id = ?
105 ORDER BY file_name, date_uploaded
107 my $sth = $dbh->prepare($query);
108 $sth->execute( $self->{'table_tag'}, $self->{'record_id'} );
109 return $sth->fetchall_arrayref( {} );
114 $mf->AddFile( name => $filename, type => $mimetype,
115 description => $description, content => $content );
117 Adds a new file (we want to store for / associate with a given
118 object) to the database. Parameters 'name' and 'content' are mandatory.
119 Note: this method would (silently) fail if there is no 'name' given
120 or if the 'content' provided is empty.
125 my ( $self, %args ) = @_;
127 my $name = $args{'name'};
128 my $type = $args{'type'} // '';
129 my $description = $args{'description'};
130 my $content = $args{'content'};
132 return unless ( defined($name) && $name ne '' && defined($content) && $content ne '' );
134 my $dbh = C4
::Context
->dbh;
136 INSERT INTO misc_files ( table_tag, record_id, file_name, file_type, file_description, file_content )
137 VALUES ( ?,?,?,?,?,? )
139 my $sth = $dbh->prepare($query);
140 $sth->execute( $self->{'table_tag'}, $self->{'record_id'}, $name, $type,
141 $description, $content );
146 my $file = $mf->GetFile( id => $file_id );
148 For an individual, specific file ID this method returns a hashref
149 containing all metadata (file_id, table_tag, record_id, file_name,
150 file_type, file_description, file_content, date_uploaded), plus
151 an actuall contents of a file (in 'file_content'). In typical usage
152 scenarios, for a given $mf object, specific file IDs have to be
153 obtained first by GetFilesInfo() call.
155 Returns undef in case when file ID specified as 'id' parameter was not
156 found in the database.
161 my ( $self, %args ) = @_;
163 my $file_id = $args{'id'};
165 my $dbh = C4
::Context
->dbh;
167 SELECT * FROM misc_files WHERE file_id = ? AND table_tag = ? AND record_id = ?
169 my $sth = $dbh->prepare($query);
170 $sth->execute( $file_id, $self->{'table_tag'}, $self->{'record_id'} );
171 return $sth->fetchrow_hashref();
176 $mf->DelFile( id => $file_id );
178 Deletes specific, individual file record (file contents and metadata)
184 my ( $self, %args ) = @_;
186 my $file_id = $args{'id'};
188 my $dbh = C4
::Context
->dbh;
190 DELETE FROM misc_files WHERE file_id = ? AND table_tag = ? AND record_id = ?
192 my $sth = $dbh->prepare($query);
193 $sth->execute( $file_id, $self->{'table_tag'}, $self->{'record_id'} );
200 Deletes all file records associated with (stored for) a given $mf object.
207 my $dbh = C4
::Context
->dbh;
209 DELETE FROM misc_files WHERE table_tag = ? AND record_id = ?
211 my $sth = $dbh->prepare($query);
212 $sth->execute( $self->{'table_tag'}, $self->{'record_id'} );
215 =item MergeFileRecIds()
217 $mf->MergeFileRecIds(@ids_to_be_merged);
219 This method re-associates all individuall file records associated with
220 some "parent" records IDs (provided in @ids_to_be_merged) with the given
221 single $mf object (which would be treated as a "parent" destination).
223 This a helper method; typically it needs to be called only in cases when
224 some "parent" records are being merged in the (external) 'tablename'
229 sub MergeFileRecIds
{
230 my ( $self, @ids_to_merge ) = @_;
232 my $dst_recid = $self->{'record_id'};
233 @ids_to_merge = map { ( $dst_recid == $_ ) ?
() : ($_); } @ids_to_merge;
234 @ids_to_merge > 0 || return ();
236 my $dbh = C4
::Context
->dbh;
238 UPDATE misc_files SET record_id = ?
239 WHERE table_tag = ? AND record_id = ?
241 my $sth = $dbh->prepare($query);
243 for my $src_recid (@ids_to_merge) {
244 $sth->execute( $dst_recid, $self->{'table_tag'}, $src_recid );
260 Kyle M Hall E<lt>kyle.m.hall@gmail.comE<gt>,
261 Jacek Ablewicz E<lt>ablewicz@gmail.comE<gt>