Bug 24545: Fix license statements
[koha-equinox.git] / Koha / Subscription / Numberpatterns.pm
1 package Koha::Subscription::Numberpatterns;
2
3 # Copyright 2016 BibLibre Morgane Alonso
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 use Koha::Database;
22 use Koha::Subscription::Numberpattern;
23 use base qw(Koha::Objects);
24
25 =head1 NAME
26
27 Koha::SubscriptionNumberpatterns - Koha SubscriptionNumberpattern object set class
28
29 =head1 API
30
31 =head2 Class Methods
32
33 =cut
34
35 =head3 uniqueLabel
36
37 =cut
38
39 sub uniqueLabel {
40     my ($self, $label) = @_;
41
42     my $samelabel = Koha::Subscription::Numberpatterns->search({label => $label})->next();
43     if ($samelabel) {
44         my $i = 2;
45         my $newlabel = $samelabel->label . " ($i)";
46         while (my $othersamelabel = $self->search({label => $newlabel})->next()) {
47             $i++;
48             $newlabel = $samelabel->label . " ($i)";
49         }
50         $label = $newlabel;
51     }
52     return $label;
53 }
54
55 =head3 new_or_existing
56
57 =cut
58
59 sub new_or_existing {
60     my ($self, $params) = @_;
61
62     my $params_np;
63     if ( $params->{'numbering_pattern'} eq 'mana' ) {
64         foreach (qw/numberingmethod label1 add1 every1 whenmorethan1 setto1
65                    numbering1 label2 add2 every2 whenmorethan2 setto2 numbering2
66                    label3 add3 every3 whenmorethan3 setto3 numbering3/) {
67             $params_np->{$_} = $params->{$_} if $params->{$_};
68         }
69
70         my $existing = Koha::Subscription::Numberpatterns->search($params_np)->next();
71
72         if ($existing) {
73             return $existing->id;
74         }
75
76         $params_np->{label} = Koha::Subscription::Numberpatterns->uniqueLabel($params->{'patternname'});
77         $params_np->{description} = $params->{'sndescription'};
78
79
80         my $subscription_np = Koha::Subscription::Numberpattern->new()->set($params_np)->store();
81         return $subscription_np->id;
82     }
83
84     return $params->{'numbering_pattern'};
85 }
86
87 =head3 type
88
89 =cut
90
91 sub _type {
92     return 'SubscriptionNumberpattern';
93 }
94
95 =head3 object_class
96
97 =cut
98
99 sub object_class {
100     return 'Koha::Subscription::Numberpattern';
101 }
102
103 =head1 AUTHOR
104
105 Morgane Alonso <morgane.alonso@biblibre.com>
106
107 =cut
108
109 1;