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