Bug 15548: Move new patron related code to Patron*
[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 use vars qw($VERSION);
23
24 use C4::Context;
25 use C4::Output;
26 use C4::Debug;
27
28 BEGIN {
29
30     # set the version for version checking
31     $VERSION = 0.01;
32 }
33
34 =head1 NAME
35
36 Koha::Patron::Files - Module for managing patron files
37
38 =head1 METHODS
39
40 =over
41
42 =cut
43
44 sub new {
45     my ( $class, %args ) = @_;
46     my $self = bless( {}, $class );
47
48     $self->{'borrowernumber'} = $args{'borrowernumber'};
49
50     return $self;
51 }
52
53 =item GetFilesInfo()
54
55     my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber );
56     my $files_hashref = $bf->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 borrower_files
72         WHERE borrowernumber = ?
73         ORDER BY file_name, date_uploaded
74     ";
75     my $sth = $dbh->prepare($query);
76     $sth->execute( $self->{'borrowernumber'} );
77     return $sth->fetchall_arrayref( {} );
78 }
79
80 =item AddFile()
81
82     my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber );
83     $bh->AddFile( name => $filename, type => $mimetype,
84                   description => $description, content => $content );
85
86 =cut
87
88 sub AddFile {
89     my ( $self, %args ) = @_;
90
91     my $name        = $args{'name'};
92     my $type        = $args{'type'};
93     my $description = $args{'description'};
94     my $content     = $args{'content'};
95
96     return unless ( $name && $content );
97
98     my $dbh   = C4::Context->dbh;
99     my $query = "
100         INSERT INTO borrower_files ( borrowernumber, file_name, file_type, file_description, file_content )
101         VALUES ( ?,?,?,?,? )
102     ";
103     my $sth = $dbh->prepare($query);
104     $sth->execute( $self->{'borrowernumber'},
105         $name, $type, $description, $content );
106 }
107
108 =item GetFile()
109
110     my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber );
111     my $file = $bh->GetFile( file_id => $file_id );
112
113 =cut
114
115 sub GetFile {
116     my ( $self, %args ) = @_;
117
118     my $file_id = $args{'id'};
119
120     my $dbh   = C4::Context->dbh;
121     my $query = "
122         SELECT * FROM borrower_files WHERE file_id = ? AND borrowernumber = ?
123     ";
124     my $sth = $dbh->prepare($query);
125     $sth->execute( $file_id, $self->{'borrowernumber'} );
126     return $sth->fetchrow_hashref();
127 }
128
129 =item DelFile()
130
131     my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber );
132     $bh->DelFile( file_id => $file_id );
133
134 =cut
135
136 sub DelFile {
137     my ( $self, %args ) = @_;
138
139     my $file_id = $args{'id'};
140
141     my $dbh   = C4::Context->dbh;
142     my $query = "
143         DELETE FROM borrower_files WHERE file_id = ? AND borrowernumber = ?
144     ";
145     my $sth = $dbh->prepare($query);
146     $sth->execute( $file_id, $self->{'borrowernumber'} );
147 }
148
149 1;
150 __END__
151
152 =back
153
154 =head1 AUTHOR
155
156 Kyle M Hall <kyle.m.hall@gmail.com>
157
158 =cut