Bug 12176: Remove HTML from additem.pl
[koha.git] / C4 / Serials / Frequency.pm
blobd71df7a8b4330f013454fe7d1d4f38bff35727a1
1 package C4::Serials::Frequency;
3 # Copyright 2011-2013 Biblibre SARL
5 # This file is part of Koha.
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.
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.
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>.
20 use strict;
21 use warnings;
23 use C4::Context;
25 use vars qw($VERSION @ISA @EXPORT);
27 BEGIN {
28 # set the version for version checking
29 $VERSION = 3.01;
30 require Exporter;
31 @ISA = qw(Exporter);
32 @EXPORT = qw(
33 &GetSubscriptionFrequencies
34 &GetSubscriptionFrequency
35 &AddSubscriptionFrequency
36 &ModSubscriptionFrequency
37 &DelSubscriptionFrequency
39 &GetSubscriptionsWithFrequency
44 =head1 NAME
46 C4::Serials::Frequency - Serials Frequency module
48 =head1 FUNCTIONS
50 =head2 GetSubscriptionFrequencies
52 =over 4
54 =item C<@frequencies> = &GetSubscriptionFrequencies();
56 gets frequencies restricted on filters
58 =back
60 =cut
62 sub GetSubscriptionFrequencies {
63 my $dbh = C4::Context->dbh;
64 my $query = qq{
65 SELECT *
66 FROM subscription_frequencies
67 ORDER BY displayorder
69 my $sth = $dbh->prepare($query);
70 $sth->execute();
72 my $results = $sth->fetchall_arrayref( {} );
73 return @$results;
76 =head2 GetSubscriptionFrequency
78 =over 4
80 =item $frequency = &GetSubscriptionFrequency($frequencyid);
82 gets frequency where $frequencyid is the identifier
84 =back
86 =cut
88 sub GetSubscriptionFrequency {
89 my ($frequencyid) = @_;
91 my $dbh = C4::Context->dbh;
92 my $query = qq{
93 SELECT *
94 FROM subscription_frequencies
95 WHERE id = ?
97 my $sth = $dbh->prepare($query);
98 $sth->execute($frequencyid);
100 return $sth->fetchrow_hashref;
103 =head2 AddSubscriptionFrequency
105 =over 4
107 =item C<$frequencyid> = &AddSubscriptionFrequency($frequency);
109 Add a new frequency
111 =item C<$frequency> is a hashref that can contains the following keys
113 =over 2
115 =item * description
117 =item * unit
119 =item * issuesperunit
121 =item * unitsperissue
123 =item * expectedissuesayear
125 =item * displayorder
127 =back
129 Only description is mandatory.
131 =back
133 =cut
135 sub AddSubscriptionFrequency {
136 my $frequency = shift;
138 unless(ref($frequency) eq 'HASH' && defined $frequency->{'description'} && $frequency->{'description'} ne '') {
139 return;
142 my @keys;
143 my @values;
144 foreach (qw/ description unit issuesperunit unitsperissue expectedissuesayear displayorder /) {
145 if(exists $frequency->{$_}) {
146 push @keys, $_;
147 push @values, $frequency->{$_};
151 my $dbh = C4::Context->dbh;
152 my $query = "INSERT INTO subscription_frequencies";
153 $query .= '(' . join(',', @keys) . ')';
154 $query .= ' VALUES (' . ('?,' x (scalar(@keys)-1)) . '?)';
155 my $sth = $dbh->prepare($query);
156 my $rv = $sth->execute(@values);
158 if(defined $rv) {
159 return $dbh->last_insert_id(undef, undef, "subscription_frequencies", undef);
162 return $rv;
165 =head2 ModSubscriptionFrequency
167 =over 4
169 =item &ModSubscriptionFrequency($frequency);
171 Modifies a frequency
173 =item C<$frequency> is a hashref that can contains the following keys
175 =over 2
177 =item * id
179 =item * description
181 =item * unit
183 =item * issuesperunit
185 =item * unitsperissue
187 =item * expectedissuesayear
189 =item * displayorder
191 =back
193 Only id is mandatory.
195 =back
197 =cut
199 sub ModSubscriptionFrequency {
200 my $frequency = shift;
202 unless(
203 ref($frequency) eq 'HASH'
204 && defined $frequency->{'id'} && $frequency->{'id'} > 0
205 && (
206 (defined $frequency->{'description'}
207 && $frequency->{'description'} ne '')
208 || !defined $frequency->{'description'}
211 return;
214 my @keys;
215 my @values;
216 foreach (qw/ description unit issuesperunit unitsperissue expectedissuesayear displayorder /) {
217 if(exists $frequency->{$_}) {
218 push @keys, $_;
219 push @values, $frequency->{$_};
223 my $dbh = C4::Context->dbh;
224 my $query = "UPDATE subscription_frequencies";
225 $query .= ' SET ' . join(' = ?,', @keys) . ' = ?';
226 $query .= ' WHERE id = ?';
227 my $sth = $dbh->prepare($query);
229 return $sth->execute(@values, $frequency->{'id'});
232 =head2 DelSubscriptionFrequency
234 =over 4
236 =item &DelSubscriptionFrequency($frequencyid);
238 Delete a frequency
240 =back
242 =cut
244 sub DelSubscriptionFrequency {
245 my $frequencyid = shift;
247 my $dbh = C4::Context->dbh;
248 my $query = qq{
249 DELETE FROM subscription_frequencies
250 WHERE id = ?
252 my $sth = $dbh->prepare($query);
253 $sth->execute($frequencyid);
256 =head2 GetSubscriptionsWithFrequency
258 my @subs = GetSubscriptionsWithFrequency($frequencyid);
260 Returns all subscriptions that are using a particular frequency
262 =cut
264 sub GetSubscriptionsWithFrequency {
265 my ($frequencyid) = @_;
267 return unless $frequencyid;
269 my $dbh = C4::Context->dbh;
270 my $query = qq{
271 SELECT *
272 FROM subscription
273 LEFT JOIN biblio ON subscription.biblionumber = biblio.biblionumber
274 WHERE periodicity = ?
276 my $sth = $dbh->prepare($query);
277 my @results;
278 if ($sth->execute($frequencyid)) {
279 @results = @{ $sth->fetchall_arrayref({}) };
281 return @results;
286 __END__
288 =head1 AUTHOR
290 Koha Developement team <info@koha.org>
292 =cut