Bug 14751 - Allow C4::Context->interface to be set to 'sip' or 'commandline'
[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 claimacquisition
67
68 Whether the contact should receive acquisitions claims.
69
70 =item claimissues
71
72 Whether the contact should receive serials claims.
73
74 =item acqprimary
75
76 Whether the contact is the primary contact for acquisitions.
77
78 =item serialsprimary
79
80 Whether the contact is the primary contact for serials.
81
82 =item bookseller
83
84 ID of the bookseller the contact is associated with.
85
86 =back
87
88 =cut
89
90 use Modern::Perl;
91 use C4::Context;
92
93 use base qw(Class::Accessor);
94
95 __PACKAGE__->mk_accessors(qw(id name position phone altphone fax email notes claimacquisition claimissues acqprimary serialsprimary bookseller));
96
97 =head1 METHODS
98
99 =head2 get_from_bookseller
100
101     my @contacts = @{C4::Bookseller::Contact->get_from_bookseller($booksellerid)};
102
103 Returns a reference to an array of C4::Bookseller::Contact objects for the
104 specified bookseller. This will always return at least one item, though that one
105 item may be an empty contact.
106
107 =cut
108
109 sub get_from_bookseller {
110     my ($class, $bookseller) = @_;
111
112     return unless $bookseller;
113
114     my @contacts;
115     my $query = "SELECT * FROM aqcontacts WHERE booksellerid = ?";
116     my $dbh = C4::Context->dbh;
117     my $sth = $dbh->prepare($query);
118     $sth->execute($bookseller);
119     while (my $rec = $sth->fetchrow_hashref()) {
120         push @contacts, $class->new($rec);
121     }
122
123     push @contacts, $class->new() unless @contacts;
124
125     return \@contacts;
126 }
127
128
129 =head2 fetch
130
131     my $contact = C4::Bookseller::Contact->fetch($contactid);
132
133 Retrieves the specified contact from the database. Currently commented out
134 because there is no separate table from which contacts can be fetched.
135
136 =cut
137
138 sub fetch {
139     my ($class, $id) = @_;
140
141     my $rec = { };
142     if ($id) {
143         my $query = "SELECT * FROM aqcontacts WHERE id = ?";
144         my $dbh = C4::Context->dbh;
145         my $sth = $dbh->prepare($query);
146         $sth->execute($id);
147         $rec = $sth->fetchrow_hashref();
148     }
149     my $self = $class->new($rec);
150     bless $self, $class;
151     return $self;
152 }
153
154 =head2 save
155
156     $contact->save();
157
158 Save a contact to the database.
159
160 =cut
161
162 sub save {
163     my ($self) = @_;
164
165     my $query;
166     my @params = (
167         $self->name,  $self->position,
168         $self->phone, $self->altphone,
169         $self->fax,   $self->email,
170         $self->notes, $self->acqprimary ? 1 : 0,
171         $self->serialsprimary ? 1 : 0, $self->claimacquisition ? 1 : 0,
172         $self->claimissues ? 1 : 0, $self->bookseller
173     );
174     if ($self->id) {
175         $query = 'UPDATE aqcontacts SET name = ?, position = ?, phone = ?, altphone = ?, fax = ?, email = ?, notes = ?, acqprimary = ?, serialsprimary = ?, claimacquisition = ?, claimissues = ?, booksellerid = ? WHERE id = ?;';
176         push @params, $self->id;
177     } else {
178         $query = 'INSERT INTO aqcontacts (name, position, phone, altphone, fax, email, notes, acqprimary, serialsprimary, claimacquisition, claimissues, booksellerid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
179     }
180     my $dbh = C4::Context->dbh;
181     my $sth = $dbh->prepare($query);
182     $sth->execute(@params);
183     $self->id($dbh->{'mysql_insertid'}) unless $self->id;
184     return $self->id;
185 }
186
187 sub delete {
188     my ($self) = @_;
189
190     return unless $self->id;
191
192     my $dbh = C4::Context->dbh;
193     my $sth = $dbh->prepare("DELETE FROM aqcontacts WHERE id = ?;");
194     $sth->execute($self->id);
195     return;
196 }
197
198 =head1 AUTHOR
199
200 Jared Camins-Esakov <jcamins@cpbibliography.com>
201
202 =cut
203
204 1;