Bug 10402: Use an object for contacts
[koha.git] / C4 / Bookseller / Contact.pm
1 package C4::Bookseller::Contact;
2
3 # Copyright 2013 C & P Bibliography Services
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 3 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 =head1 NAME
21
22 C4::Bookseller::Contact - object class for contacts associated with vendors
23
24 =head1 SYNPOSIS
25
26 This class provides an object-oriented interface for handling vendor contacts.
27 It uses Class::Accessor to provide access to the following fields:
28
29 =head1 FIELDS
30
31 =over 8
32
33 =item id
34
35 ID of the contact. This is not used initially, since contacts are actually
36 stored in the aqbooksellers table.
37
38 =item name
39
40 Contact name.
41
42 =item position
43
44 Contact's position.
45
46 =item phone
47
48 Contact's primary phone number.
49
50 =item altphone
51
52 Contact's alternate phone number.
53
54 =item fax
55
56 Contact's fax number.
57
58 =item email
59
60 Contact's e-mail address.
61
62 =item notes
63
64 Notes about contact.
65
66 =item primary
67
68 Flag to indicate whether a contact is "primary" or not. Initially unused since
69 each bookseller can have only one contact.
70
71 =item bookseller
72
73 ID of the bookseller the contact is associated with.
74
75 =back
76
77 =cut
78
79 use Modern::Perl;
80 use C4::Context;
81
82 use base qw(Class::Accessor);
83
84 __PACKAGE__->mk_accessors(qw(id name position phone altphone fax email notes primary bookseller));
85
86 =head1 METHODS
87
88 =head2 get_from_bookseller
89
90     my @contacts = @{C4::Bookseller::Contact->get_from_bookseller($booksellerid)};
91
92 Returns a reference to an array of C4::Bookseller::Contact objects for the
93 specified bookseller.
94
95 =cut
96
97 sub get_from_bookseller {
98     my ($class, $bookseller) = @_;
99
100     return unless $bookseller;
101
102     my @contacts;
103     my $query = "SELECT contact AS name, contpos AS position, contphone AS phone, contaltphone AS altphone, contfax AS fax, contemail AS email, contnotes AS notes, id AS bookseller FROM aqbooksellers WHERE id = ?";
104     # When we have our own table, we can use: my $query = "SELECT * FROM aqcontacts WHERE bookseller = ?";
105     my $dbh = C4::Context->dbh;
106     my $sth = $dbh->prepare($query);
107     $sth->execute($bookseller);
108     while (my $rec = $sth->fetchrow_hashref()) {
109         $rec->{'primary'} = 1;
110         push @contacts, $class->new($rec);
111     }
112
113     return \@contacts;
114 }
115
116
117 =head2 fetch
118
119     my $contact = C4::Bookseller::Contact->fetch($contactid);
120
121 Retrieves the specified contact from the database. Currently commented out
122 because there is no separate table from which contacts can be fetched.
123
124 =cut
125
126 #sub fetch {
127 #    my ($class, $id) = @_;
128 #
129 #    my $rec = { };
130 #    if ($id) {
131 #        my $query = "SELECT * FROM aqcontacts WHERE id = ?";
132 #        my $dbh = C4::Context->dbh;
133 #        my $sth = $dbh->prepare($query);
134 #        $sth->execute($id);
135 #        $rec = $sth->fetchrow_hashref();
136 #    }
137 #    my $self = $class->SUPER::new($rec);
138 #    bless $self, $class;
139 #    return $self;
140 #}
141
142 =head2 save
143
144     $contact->save();
145
146 Save a contact to the database.
147
148 =cut
149
150 sub save {
151     my ($self) = @_;
152
153     my $query;
154 #    if ($self->id) {
155 #        $query = 'UPDATE aqcontacts SET name = ?, position = ?, phone = ?, altphone = ?, fax = ?, email = ?, notes = ?, primary = ? WHERE id = ?;';
156 #    } else {
157 #        $query = 'INSERT INTO aqcontacts (name, position, phone, altphone, fax, email, notes, primary) VALUES (?, ?, ?, ?, ? ,? ,?, ?);';
158 #    }
159     if ($self->bookseller) {
160         $query = 'UPDATE aqbooksellers SET contact = ?, contpos = ?, contphone = ?, contaltphone = ?, contfax = ?, contemail = ?, contnotes = ? WHERE id = ?;';
161     } else {
162         return;
163     }
164     my $dbh = C4::Context->dbh;
165     my $sth = $dbh->prepare($query);
166     $sth->execute($self->name, $self->position, $self->phone, $self->altphone, $self->fax, $self->email, $self->notes, $self->bookseller);
167     #$self->id = $dbh->last_insert_id(undef, undef, 'aqcontacts', undef);
168     return $self->bookseller;
169 }
170
171 =head1 AUTHOR
172
173 Jared Camins-Esakov <jcamins@cpbibliography.com>
174
175 =cut
176
177 1;