Merge branch 'bug_8945' into 3.12-master
[koha.git] / Koha / Borrower / Files.pm
1 package Koha::Borrower::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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use vars qw($VERSION);
23
24 use C4::Context;
25 use C4::Output;
26 use C4::Dates;
27 use C4::Debug;
28
29 BEGIN {
30
31     # set the version for version checking
32     $VERSION = 0.01;
33 }
34
35 =head1 NAME
36
37 Koha::Borrower::Files - Module for managing borrower files
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::Borrower::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     my $bf = Koha::Borrower::Files->new( borrowernumber => $borrowernumber );
79     $bh->AddFile( name => $filename, type => $mimetype, description => $description, content => $content );
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     my $bf = Koha::Borrower::Files->new( borrowernumber => $borrowernumber );
104     my $file = $bh->GetFile( file_id => $file_id );
105 =cut
106
107 sub GetFile {
108     my ( $self, %args ) = @_;
109
110     my $file_id = $args{'id'};
111
112     my $dbh   = C4::Context->dbh;
113     my $query = "
114         SELECT * FROM borrower_files WHERE file_id = ? AND borrowernumber = ?
115     ";
116     my $sth = $dbh->prepare($query);
117     $sth->execute( $file_id, $self->{'borrowernumber'} );
118     return $sth->fetchrow_hashref();
119 }
120
121 =item DelFile()
122     my $bf = Koha::Borrower::Files->new( borrowernumber => $borrowernumber );
123     $bh->DelFile( file_id => $file_id );
124 =cut
125
126 sub DelFile {
127     my ( $self, %args ) = @_;
128
129     my $file_id = $args{'id'};
130
131     my $dbh   = C4::Context->dbh;
132     my $query = "
133         DELETE FROM borrower_files WHERE file_id = ? AND borrowernumber = ?
134     ";
135     my $sth = $dbh->prepare($query);
136     $sth->execute( $file_id, $self->{'borrowernumber'} );
137 }
138
139 1;
140 __END__
141
142 =back
143
144 =head1 AUTHOR
145
146 Kyle M Hall <kyle.m.hall@gmail.com>
147
148 =cut