Line 0
Link Here
|
|
|
1 |
package Koha::Misc::Files; |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Copyright 2012 Kyle M Hall |
6 |
# Copyright 2014 Jacek Ablewicz |
7 |
# Based on Koha/Borrower/Files.pm by Kyle M Hall |
8 |
# |
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. |
13 |
# |
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. |
18 |
# |
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>. |
21 |
|
22 |
use Modern::Perl; |
23 |
use strict; ## not needed if Modern::Perl, but perlcritic complains.. |
24 |
use vars qw($VERSION); |
25 |
$VERSION = '0.20'; |
26 |
|
27 |
use C4::Context; |
28 |
use C4::Output; |
29 |
use C4::Dates; |
30 |
use C4::Debug; |
31 |
|
32 |
=head1 NAME |
33 |
|
34 |
Koha::Misc::Files - module for managing miscellaneous files |
35 |
associated with records from arbitrary tables |
36 |
|
37 |
=cut |
38 |
|
39 |
sub new { |
40 |
my ( $class, %args ) = @_; |
41 |
|
42 |
( defined( $args{'tabletag'} ) && defined( $args{'recordid'} ) ) |
43 |
|| return (); |
44 |
my $self = bless( {}, $class ); |
45 |
|
46 |
$self->{'table_tag'} = $args{'tabletag'}; |
47 |
$self->{'record_id'} = $args{'recordid'}; |
48 |
|
49 |
return $self; |
50 |
} |
51 |
|
52 |
=item GetFilesInfo() |
53 |
|
54 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
55 |
recordid => $recordnumber); |
56 |
my $files_hashref = $mf->GetFilesInfo |
57 |
|
58 |
=cut |
59 |
|
60 |
sub GetFilesInfo { |
61 |
my $self = shift; |
62 |
|
63 |
my $dbh = C4::Context->dbh; |
64 |
my $query = " |
65 |
SELECT |
66 |
file_id, |
67 |
file_name, |
68 |
file_type, |
69 |
file_description, |
70 |
date_uploaded |
71 |
FROM misc_files |
72 |
WHERE table_tag = ? AND record_id = ? |
73 |
ORDER BY file_name, date_uploaded |
74 |
"; |
75 |
my $sth = $dbh->prepare($query); |
76 |
$sth->execute( $self->{'table_tag'}, $self->{'record_id'} ); |
77 |
return $sth->fetchall_arrayref( {} ); |
78 |
} |
79 |
|
80 |
=item AddFile() |
81 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
82 |
recordid => $recordnumber); |
83 |
$mf->AddFile( name => $filename, type => $mimetype, description => $description, content => $content ); |
84 |
=cut |
85 |
|
86 |
sub AddFile { |
87 |
my ( $self, %args ) = @_; |
88 |
|
89 |
my $name = $args{'name'}; |
90 |
my $type = $args{'type'}; |
91 |
my $description = $args{'description'}; |
92 |
my $content = $args{'content'}; |
93 |
|
94 |
return unless ( $name && $content ); |
95 |
|
96 |
my $dbh = C4::Context->dbh; |
97 |
my $query = " |
98 |
INSERT INTO misc_files ( table_tag, record_id, file_name, file_type, file_description, file_content ) |
99 |
VALUES ( ?,?,?,?,?,? ) |
100 |
"; |
101 |
my $sth = $dbh->prepare($query); |
102 |
$sth->execute( $self->{'table_tag'}, $self->{'record_id'}, $name, $type, |
103 |
$description, $content ); |
104 |
} |
105 |
|
106 |
=item GetFile() |
107 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
108 |
recordid => $recordnumber); |
109 |
... |
110 |
my $file = $mf->GetFile( id => $file_id ); |
111 |
=cut |
112 |
|
113 |
sub GetFile { |
114 |
my ( $self, %args ) = @_; |
115 |
|
116 |
my $file_id = $args{'id'}; |
117 |
|
118 |
my $dbh = C4::Context->dbh; |
119 |
my $query = " |
120 |
SELECT * FROM misc_files WHERE file_id = ? AND table_tag = ? AND record_id = ? |
121 |
"; |
122 |
my $sth = $dbh->prepare($query); |
123 |
$sth->execute( $file_id, $self->{'table_tag'}, $self->{'record_id'} ); |
124 |
return $sth->fetchrow_hashref(); |
125 |
} |
126 |
|
127 |
=item DelFile() |
128 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
129 |
recordid => $recordnumber); |
130 |
... |
131 |
$mf->DelFile( id => $file_id ); |
132 |
=cut |
133 |
|
134 |
sub DelFile { |
135 |
my ( $self, %args ) = @_; |
136 |
|
137 |
my $file_id = $args{'id'}; |
138 |
|
139 |
my $dbh = C4::Context->dbh; |
140 |
my $query = " |
141 |
DELETE FROM misc_files WHERE file_id = ? AND table_tag = ? AND record_id = ? |
142 |
"; |
143 |
my $sth = $dbh->prepare($query); |
144 |
$sth->execute( $file_id, $self->{'table_tag'}, $self->{'record_id'} ); |
145 |
} |
146 |
|
147 |
=item DelAllFiles() |
148 |
my $mf = Koha::Misc::Files->new( tabletag => $tablename, |
149 |
recordid => $recordnumber); |
150 |
$mf->DelAllFiles; |
151 |
=cut |
152 |
|
153 |
sub DelAllFiles { |
154 |
my ($self) = @_; |
155 |
|
156 |
my $dbh = C4::Context->dbh; |
157 |
my $query = " |
158 |
DELETE FROM misc_files WHERE table_tag = ? AND record_id = ? |
159 |
"; |
160 |
my $sth = $dbh->prepare($query); |
161 |
$sth->execute( $self->{'table_tag'}, $self->{'record_id'} ); |
162 |
} |
163 |
|
164 |
1; |
165 |
__END__ |
166 |
|
167 |
=back |
168 |
|
169 |
=head1 AUTHOR |
170 |
|
171 |
Kyle M Hall <kyle.m.hall@gmail.com> |
172 |
Jacek Ablewicz <ablewicz@gmail.com> |
173 |
|
174 |
=cut |