Bug 16011: $VERSION - Remove use vars qw();
[koha.git] / Koha / Patron / Files.pm
1 package Koha::Patron::Files;
2
3 # Copyright 2012 Kyle M Hall
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22
23 use C4::Context;
24 use C4::Output;
25 use C4::Debug;
26
27 BEGIN {
28
29 }
30
31 =head1 NAME
32
33 Koha::Patron::Files - Module for managing patron files
34
35 =head1 METHODS
36
37 =over
38
39 =cut
40
41 sub new {
42     my ( $class, %args ) = @_;
43     my $self = bless( {}, $class );
44
45     $self->{'borrowernumber'} = $args{'borrowernumber'};
46
47     return $self;
48 }
49
50 =item GetFilesInfo()
51
52     my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber );
53     my $files_hashref = $bf->GetFilesInfo
54
55 =cut
56
57 sub GetFilesInfo {
58     my $self = shift;
59
60     my $dbh   = C4::Context->dbh;
61     my $query = "
62         SELECT
63             file_id,
64             file_name,
65             file_type,
66             file_description,
67             date_uploaded
68         FROM borrower_files
69         WHERE borrowernumber = ?
70         ORDER BY file_name, date_uploaded
71     ";
72     my $sth = $dbh->prepare($query);
73     $sth->execute( $self->{'borrowernumber'} );
74     return $sth->fetchall_arrayref( {} );
75 }
76
77 =item AddFile()
78
79     my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber );
80     $bh->AddFile( name => $filename, type => $mimetype,
81                   description => $description, content => $content );
82
83 =cut
84
85 sub AddFile {
86     my ( $self, %args ) = @_;
87
88     my $name        = $args{'name'};
89     my $type        = $args{'type'};
90     my $description = $args{'description'};
91     my $content     = $args{'content'};
92
93     return unless ( $name && $content );
94
95     my $dbh   = C4::Context->dbh;
96     my $query = "
97         INSERT INTO borrower_files ( borrowernumber, file_name, file_type, file_description, file_content )
98         VALUES ( ?,?,?,?,? )
99     ";
100     my $sth = $dbh->prepare($query);
101     $sth->execute( $self->{'borrowernumber'},
102         $name, $type, $description, $content );
103 }
104
105 =item GetFile()
106
107     my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber );
108     my $file = $bh->GetFile( file_id => $file_id );
109
110 =cut
111
112 sub GetFile {
113     my ( $self, %args ) = @_;
114
115     my $file_id = $args{'id'};
116
117     my $dbh   = C4::Context->dbh;
118     my $query = "
119         SELECT * FROM borrower_files WHERE file_id = ? AND borrowernumber = ?
120     ";
121     my $sth = $dbh->prepare($query);
122     $sth->execute( $file_id, $self->{'borrowernumber'} );
123     return $sth->fetchrow_hashref();
124 }
125
126 =item DelFile()
127
128     my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber );
129     $bh->DelFile( file_id => $file_id );
130
131 =cut
132
133 sub DelFile {
134     my ( $self, %args ) = @_;
135
136     my $file_id = $args{'id'};
137
138     my $dbh   = C4::Context->dbh;
139     my $query = "
140         DELETE FROM borrower_files WHERE file_id = ? AND borrowernumber = ?
141     ";
142     my $sth = $dbh->prepare($query);
143     $sth->execute( $file_id, $self->{'borrowernumber'} );
144 }
145
146 1;
147 __END__
148
149 =back
150
151 =head1 AUTHOR
152
153 Kyle M Hall <kyle.m.hall@gmail.com>
154
155 =cut