File: | C4/Creators/Profile.pm |
Coverage: | 14.6% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package C4::Creators::Profile; | |||||
2 | ||||||
3 | 8 8 8 | 1331 35 206 | use strict; | |||
4 | 8 8 8 | 78 31 347 | use warnings; | |||
5 | ||||||
6 | 8 8 8 | 723 1601 127 | use autouse 'Data::Dumper' => qw(Dumper); | |||
7 | ||||||
8 | 8 8 8 | 1147 51 203 | use C4::Context; | |||
9 | 8 8 8 | 48 19 935 | use C4::Debug; | |||
10 | 8 8 8 | 1213 504 745 | use C4::Creators::Lib 1.000000 qw(get_unit_values); | |||
11 | ||||||
12 | BEGIN { | |||||
13 | 8 8 8 8 | 102 54 117 647 | use version; our $VERSION = qv('1.0.0_1'); | |||
14 | } | |||||
15 | ||||||
16 | sub _check_params { | |||||
17 | 0 | my $given_params = {}; | ||||
18 | 0 | my $exit_code = 0; | ||||
19 | 0 | my @valid_profile_params = ( | ||||
20 | 'printer_name', | |||||
21 | 'template_id', | |||||
22 | 'paper_bin', | |||||
23 | 'offset_horz', | |||||
24 | 'offset_vert', | |||||
25 | 'creep_horz', | |||||
26 | 'creep_vert', | |||||
27 | 'units', | |||||
28 | 'creator', | |||||
29 | ); | |||||
30 | 0 | if (scalar(@_) >1) { | ||||
31 | 0 | $given_params = {@_}; | ||||
32 | 0 0 | foreach my $key (keys %{$given_params}) { | ||||
33 | 0 | if (!(grep m/$key/, @valid_profile_params)) { | ||||
34 | 0 | warn sprintf('Unrecognized parameter type of "%s".', $key); | ||||
35 | 0 | $exit_code = 1; | ||||
36 | } | |||||
37 | } | |||||
38 | } | |||||
39 | else { | |||||
40 | 0 | if (!(grep m/$_/, @valid_profile_params)) { | ||||
41 | 0 | warn sprintf('Unrecognized parameter type of "%s".', $_); | ||||
42 | 0 | $exit_code = 1; | ||||
43 | } | |||||
44 | } | |||||
45 | 0 | return $exit_code; | ||||
46 | } | |||||
47 | ||||||
48 | sub _conv_points { | |||||
49 | 0 | my $self = shift; | ||||
50 | 0 0 0 | my @unit_value = grep {$_->{'type'} eq $self->{units}} @{get_unit_values()}; | ||||
51 | 0 | $self->{offset_horz} = $self->{offset_horz} * $unit_value[0]->{'value'}; | ||||
52 | 0 | $self->{offset_vert} = $self->{offset_vert} * $unit_value[0]->{'value'}; | ||||
53 | 0 | $self->{creep_horz} = $self->{creep_horz} * $unit_value[0]->{'value'}; | ||||
54 | 0 | $self->{creep_vert} = $self->{creep_vert} * $unit_value[0]->{'value'}; | ||||
55 | 0 | return $self; | ||||
56 | } | |||||
57 | ||||||
58 | sub new { | |||||
59 | 0 | my $invocant = shift; | ||||
60 | 0 | if (_check_params(@_) eq 1) { | ||||
61 | 0 | return -1; | ||||
62 | } | |||||
63 | 0 | my $type = ref($invocant) || $invocant; | ||||
64 | 0 | my $self = { | ||||
65 | printer_name => 'Default Printer', | |||||
66 | template_id => '', | |||||
67 | paper_bin => 'Tray 1', | |||||
68 | offset_horz => 0, | |||||
69 | offset_vert => 0, | |||||
70 | creep_horz => 0, | |||||
71 | creep_vert => 0, | |||||
72 | units => 'POINT', | |||||
73 | @_, | |||||
74 | }; | |||||
75 | 0 | bless ($self, $type); | ||||
76 | 0 | return $self; | ||||
77 | } | |||||
78 | ||||||
79 | sub retrieve { | |||||
80 | 0 | my $invocant = shift; | ||||
81 | 0 | my %opts = @_; | ||||
82 | 0 | my $type = ref($invocant) || $invocant; | ||||
83 | 0 | my $query = "SELECT * FROM printers_profile WHERE profile_id = ? AND creator = ?"; | ||||
84 | 0 | my $sth = C4::Context->dbh->prepare($query); | ||||
85 | 0 | $sth->execute($opts{'profile_id'}, $opts{'creator'}); | ||||
86 | 0 | if ($sth->err) { | ||||
87 | 0 | warn sprintf('Database returned the following error: %s', $sth->errstr); | ||||
88 | 0 | return -1; | ||||
89 | } | |||||
90 | 0 | my $self = $sth->fetchrow_hashref; | ||||
91 | 0 | $self = _conv_points($self) if ($opts{convert} && $opts{convert} == 1); | ||||
92 | 0 | bless ($self, $type); | ||||
93 | 0 | return $self; | ||||
94 | } | |||||
95 | ||||||
96 | sub delete { | |||||
97 | 0 | my $self = {}; | ||||
98 | 0 | my %opts = (); | ||||
99 | 0 | my $call_type = ''; | ||||
100 | 0 | my @params = (); | ||||
101 | 0 | if (ref($_[0])) { | ||||
102 | 0 | $self = shift; # check to see if this is a method call | ||||
103 | 0 | $call_type = 'C4::'. $self->{'creator'} .'::Profile->delete'; | ||||
104 | 0 | push @params, $self->{'profile_id'}, $self->{'creator'}; | ||||
105 | } | |||||
106 | else { | |||||
107 | 0 | my $class = shift; #XXX: is this too hackish? | ||||
108 | 0 | %opts = @_; | ||||
109 | 0 | $call_type = $class . "::delete"; | ||||
110 | 0 | push @params, $opts{'profile_id'}, $opts{'creator'}; | ||||
111 | } | |||||
112 | 0 | if (scalar(@params) < 2) { # If there is no profile id or creator type then we cannot delete it | ||||
113 | 0 | warn sprintf('%s : Cannot delete profile as the profile id is invalid or non-existant.', $call_type) if !$params[0]; | ||||
114 | 0 | warn sprintf('%s : Cannot delete profile as the creator type is invalid or non-existant.', $call_type) if !$params[1]; | ||||
115 | 0 | return -1; | ||||
116 | } | |||||
117 | 0 | my $query = "DELETE FROM printers_profile WHERE profile_id = ? AND creator = ?"; | ||||
118 | 0 | my $sth = C4::Context->dbh->prepare($query); | ||||
119 | # $sth->{'TraceLevel'} = 3; | |||||
120 | 0 | $sth->execute(@params); | ||||
121 | 0 | if ($sth->err) { | ||||
122 | 0 | warn sprintf('Database returned the following error on attempted DELETE: %s', $sth->errstr); | ||||
123 | 0 | return -1; | ||||
124 | } | |||||
125 | 0 | return 0; | ||||
126 | } | |||||
127 | ||||||
128 | sub save { | |||||
129 | 0 | my $self = shift; | ||||
130 | 0 | if ($self->{'profile_id'}) { # if we have an profile_id, the record exists and needs UPDATE | ||||
131 | 0 | my @params; | ||||
132 | 0 | my $query = "UPDATE printers_profile SET "; | ||||
133 | 0 0 | foreach my $key (keys %{$self}) { | ||||
134 | 0 | next if ($key eq 'profile_id') || ($key eq 'creator'); | ||||
135 | 0 | push (@params, $self->{$key}); | ||||
136 | 0 | $query .= "$key=?, "; | ||||
137 | } | |||||
138 | 0 | $query = substr($query, 0, (length($query)-2)); | ||||
139 | 0 | push (@params, $self->{'profile_id'}, $self->{'creator'}); | ||||
140 | 0 | $query .= " WHERE profile_id=? AND creator=?;"; | ||||
141 | 0 | my $sth = C4::Context->dbh->prepare($query); | ||||
142 | # $sth->{'TraceLevel'} = 3; | |||||
143 | 0 | $sth->execute(@params); | ||||
144 | 0 | if ($sth->err) { | ||||
145 | 0 | warn sprintf('Database returned the following error on attempted UPDATE: %s', $sth->errstr); | ||||
146 | 0 | return -1; | ||||
147 | } | |||||
148 | 0 | return $self->{'profile_id'}; | ||||
149 | } | |||||
150 | else { # otherwise create a new record | |||||
151 | 0 | my @params; | ||||
152 | 0 | my $query = "INSERT INTO printers_profile ("; | ||||
153 | 0 0 | foreach my $key (keys %{$self}) { | ||||
154 | 0 | push (@params, $self->{$key}); | ||||
155 | 0 | $query .= "$key, "; | ||||
156 | } | |||||
157 | 0 | $query = substr($query, 0, (length($query)-2)); | ||||
158 | 0 | $query .= ") VALUES ("; | ||||
159 | for (my $i=1; $i<=(scalar keys %$self); $i++) { | |||||
160 | 0 | $query .= "?,"; | ||||
161 | 0 | } | ||||
162 | 0 | $query = substr($query, 0, (length($query)-1)); | ||||
163 | 0 | $query .= ");"; | ||||
164 | 0 | my $sth = C4::Context->dbh->prepare($query); | ||||
165 | 0 | $sth->execute(@params); | ||||
166 | 0 | if ($sth->err) { | ||||
167 | 0 | warn sprintf('Database returned the following error on attempted INSERT: %s', $sth->errstr); | ||||
168 | 0 | return -1; | ||||
169 | } | |||||
170 | 0 | my $sth1 = C4::Context->dbh->prepare("SELECT MAX(profile_id) FROM printers_profile;"); | ||||
171 | 0 | $sth1->execute(); | ||||
172 | 0 | my $tmpl_id = $sth1->fetchrow_array; | ||||
173 | 0 | return $tmpl_id; | ||||
174 | } | |||||
175 | } | |||||
176 | ||||||
177 | sub get_attr { | |||||
178 | 0 | my $self = shift; | ||||
179 | 0 | if (_check_params(@_) eq 1) { | ||||
180 | 0 | return -1; | ||||
181 | } | |||||
182 | 0 | my ($attr) = @_; | ||||
183 | 0 | if (exists($self->{$attr})) { | ||||
184 | 0 | return $self->{$attr}; | ||||
185 | } | |||||
186 | else { | |||||
187 | 0 | warn sprintf('%s is currently undefined.', $attr); | ||||
188 | 0 | return -1; | ||||
189 | } | |||||
190 | } | |||||
191 | ||||||
192 | sub set_attr { | |||||
193 | 0 | my $self = shift; | ||||
194 | 0 | if (_check_params(@_) eq 1) { | ||||
195 | 0 | return -1; | ||||
196 | } | |||||
197 | 0 | my %attrs = @_; | ||||
198 | 0 | foreach my $attrib (keys(%attrs)) { | ||||
199 | 0 | $self->{$attrib} = $attrs{$attrib}; | ||||
200 | }; | |||||
201 | 0 | return 0; | ||||
202 | } | |||||
203 | ||||||
204 | 1; |