File: | C4/Creators/Template.pm |
Coverage: | 13.5% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package C4::Creators::Template; | |||||
2 | ||||||
3 | 3 3 3 | 625 15 112 | use strict; | |||
4 | 3 3 3 | 25 93 173 | use warnings; | |||
5 | 3 3 3 | 193 3972 49 | use POSIX qw(ceil); | |||
6 | 3 3 3 | 7515 785 67 | use autouse 'Data::Dumper' => qw(Dumper); | |||
7 | ||||||
8 | 3 3 3 | 401 31 56 | use C4::Context; | |||
9 | 3 3 3 | 42 27 369 | use C4::Debug; | |||
10 | 3 3 3 | 194 105 175 | use C4::Creators::Profile 1.000000; | |||
11 | 3 3 3 | 21 52 160 | use C4::Creators::Lib 1.000000 qw(get_unit_values); | |||
12 | ||||||
13 | BEGIN { | |||||
14 | 3 3 3 3 | 602 11 28 219 | use version; our $VERSION = qv('1.0.0_1'); | |||
15 | } | |||||
16 | ||||||
17 | sub _check_params { | |||||
18 | 0 | shift if $_[0] =~ m/::/; # this seems a bit hackish | ||||
19 | 0 | my $given_params = {}; | ||||
20 | 0 | my $exit_code = 0; | ||||
21 | 0 | my @valid_template_params = ( | ||||
22 | 'profile_id', | |||||
23 | 'template_code', | |||||
24 | 'template_desc', | |||||
25 | 'page_width', | |||||
26 | 'page_height', | |||||
27 | 'label_width', | |||||
28 | 'label_height', | |||||
29 | 'card_width', | |||||
30 | 'card_height', | |||||
31 | 'top_text_margin', | |||||
32 | 'left_text_margin', | |||||
33 | 'top_margin', | |||||
34 | 'left_margin', | |||||
35 | 'cols', | |||||
36 | 'rows', | |||||
37 | 'col_gap', | |||||
38 | 'row_gap', | |||||
39 | 'units', | |||||
40 | 'creator', | |||||
41 | 'current_label', | |||||
42 | ); | |||||
43 | 0 | if (scalar(@_) >1) { | ||||
44 | 0 | $given_params = {@_}; | ||||
45 | 0 0 | foreach my $key (keys %{$given_params}) { | ||||
46 | 0 | if (!(grep m/$key/, @valid_template_params)) { | ||||
47 | 0 | warn sprintf('Unrecognized parameter type of "%s".', $key); | ||||
48 | 0 | $exit_code = 1; | ||||
49 | } | |||||
50 | } | |||||
51 | } | |||||
52 | else { | |||||
53 | 0 | if (!(grep m/$_/, @valid_template_params)) { | ||||
54 | 0 | warn sprintf('Unrecognized parameter type of "%s".', $_); | ||||
55 | 0 | $exit_code = 1; | ||||
56 | } | |||||
57 | } | |||||
58 | 0 | return $exit_code; | ||||
59 | } | |||||
60 | ||||||
61 | sub _conv_points { | |||||
62 | 0 | my $self = shift; | ||||
63 | 0 0 0 | my @unit_value = grep {$_->{'type'} eq $self->{'units'}} @{get_unit_values()}; | ||||
64 | 0 | $self->{'page_width'} = $self->{'page_width'} * $unit_value[0]->{'value'}; | ||||
65 | 0 | $self->{'page_height'} = $self->{'page_height'} * $unit_value[0]->{'value'}; | ||||
66 | 0 | $self->{'label_width'} = $self->{'label_width'} * $unit_value[0]->{'value'}; | ||||
67 | 0 | $self->{'label_height'} = $self->{'label_height'} * $unit_value[0]->{'value'}; | ||||
68 | 0 | $self->{'top_text_margin'} = $self->{'top_text_margin'} * $unit_value[0]->{'value'}; | ||||
69 | 0 | $self->{'left_text_margin'} = $self->{'left_text_margin'} * $unit_value[0]->{'value'}; | ||||
70 | 0 | $self->{'top_margin'} = $self->{'top_margin'} * $unit_value[0]->{'value'}; | ||||
71 | 0 | $self->{'left_margin'} = $self->{'left_margin'} * $unit_value[0]->{'value'}; | ||||
72 | 0 | $self->{'col_gap'} = $self->{'col_gap'} * $unit_value[0]->{'value'}; | ||||
73 | 0 | $self->{'row_gap'} = $self->{'row_gap'} * $unit_value[0]->{'value'}; | ||||
74 | 0 | return $self; | ||||
75 | } | |||||
76 | ||||||
77 | sub _apply_profile { | |||||
78 | 0 | my $self = shift; | ||||
79 | 0 | my $creator = shift; | ||||
80 | 0 | my $profile = C4::Creators::Profile->retrieve(profile_id => $self->{'profile_id'}, creator => $creator, convert => 1); | ||||
81 | 0 | $self->{'top_margin'} = $self->{'top_margin'} + $profile->get_attr('offset_vert'); # controls vertical offset | ||||
82 | 0 | $self->{'left_margin'} = $self->{'left_margin'} + $profile->get_attr('offset_horz'); # controls horizontal offset | ||||
83 | 0 | $self->{'label_height'} = $self->{'label_height'} + $profile->get_attr('creep_vert'); # controls vertical creep | ||||
84 | 0 | $self->{'label_width'} = $self->{'label_width'} + $profile->get_attr('creep_horz'); # controls horizontal creep | ||||
85 | 0 | return $self; | ||||
86 | } | |||||
87 | ||||||
88 | sub new { | |||||
89 | 0 | my $invocant = shift; | ||||
90 | 0 | my $type = ref($invocant) || $invocant; | ||||
91 | 0 | if (_check_params(@_) eq 1) { | ||||
92 | 0 | return -1; | ||||
93 | } | |||||
94 | 0 | my $self = { | ||||
95 | profile_id => 0, | |||||
96 | template_code => 'DEFAULT TEMPLATE', | |||||
97 | template_desc => 'Default description', | |||||
98 | page_width => 0, | |||||
99 | page_height => 0, | |||||
100 | label_width => 0, | |||||
101 | label_height => 0, | |||||
102 | top_text_margin => 0, | |||||
103 | left_text_margin => 0, | |||||
104 | top_margin => 0, | |||||
105 | left_margin => 0, | |||||
106 | cols => 0, | |||||
107 | rows => 0, | |||||
108 | col_gap => 0, | |||||
109 | row_gap => 0, | |||||
110 | units => 'POINT', | |||||
111 | template_stat => 0, # false if any data has changed and the db has not been updated | |||||
112 | @_, | |||||
113 | }; | |||||
114 | 0 | bless ($self, $type); | ||||
115 | 0 | return $self; | ||||
116 | } | |||||
117 | ||||||
118 | sub retrieve { | |||||
119 | 0 | my $invocant = shift; | ||||
120 | 0 | my %opts = @_; | ||||
121 | 0 | my $type = ref($invocant) || $invocant; | ||||
122 | 0 | my $query = "SELECT * FROM " . $opts{'table_name'} . " WHERE template_id = ? AND creator = ?"; | ||||
123 | 0 | my $sth = C4::Context->dbh->prepare($query); | ||||
124 | 0 | $sth->execute($opts{'template_id'}, $opts{'creator'}); | ||||
125 | 0 | if ($sth->err) { | ||||
126 | 0 | warn sprintf('Database returned the following error: %s', $sth->errstr); | ||||
127 | 0 | return -1; | ||||
128 | } | |||||
129 | 0 | my $self = $sth->fetchrow_hashref; | ||||
130 | 0 | $self = _conv_points($self) if (($opts{convert} && $opts{convert} == 1) || $opts{profile_id}); | ||||
131 | 0 | $self = _apply_profile($self, $opts{'creator'}) if $opts{profile_id} && $self->{'profile_id'}; # don't bother if there is no profile_id | ||||
132 | 0 | $self->{'template_stat'} = 1; | ||||
133 | 0 | bless ($self, $type); | ||||
134 | 0 | return $self; | ||||
135 | } | |||||
136 | ||||||
137 | sub delete { | |||||
138 | 0 | my $self = {}; | ||||
139 | 0 | my %opts = (); | ||||
140 | 0 | my $call_type = ''; | ||||
141 | 0 | my @query_params = (); | ||||
142 | 0 | if (ref($_[0])) { | ||||
143 | 0 | $self = shift; # check to see if this is a method call | ||||
144 | 0 | $call_type = 'C4::Labels::Template->delete'; | ||||
145 | 0 | push @query_params, $self->{'template_id'}, $self->{'creator'}; | ||||
146 | } | |||||
147 | else { | |||||
148 | 0 | %opts = @_; | ||||
149 | 0 | $call_type = 'C4::Labels::Template::delete'; | ||||
150 | 0 | push @query_params, $opts{'template_id'}, $opts{'creator'}; | ||||
151 | } | |||||
152 | 0 | if (scalar(@query_params) < 2) { # If there is no template id or creator type then we cannot delete it | ||||
153 | 0 | warn sprintf('%s : Cannot delete template as the template id is invalid or non-existant.', $call_type) if !$query_params[0]; | ||||
154 | 0 | warn sprintf('%s : Cannot delete template as the creator type is invalid or non-existant.', $call_type) if !$query_params[1]; | ||||
155 | 0 | return -1; | ||||
156 | } | |||||
157 | 0 | my $query = "DELETE FROM creator_templates WHERE template_id = ? AND creator = ?"; | ||||
158 | 0 | my $sth = C4::Context->dbh->prepare($query); | ||||
159 | 0 | $sth->execute(@query_params); | ||||
160 | 0 | $self->{'template_stat'} = 0; | ||||
161 | } | |||||
162 | ||||||
163 | sub save { | |||||
164 | 0 | my $self = shift; | ||||
165 | 0 | my %opts = @_; | ||||
166 | 0 | if ($self->{'template_id'}) { # if we have an template_id, the record exists and needs UPDATE | ||||
167 | 0 | my @params; | ||||
168 | 0 | my $query = "UPDATE " . $opts{'table_name'} . " SET "; | ||||
169 | 0 0 | foreach my $key (keys %{$self}) { | ||||
170 | 0 | next if ($key eq 'template_id') || ($key eq 'template_stat') || ($key eq 'creator'); | ||||
171 | 0 | push (@params, $self->{$key}); | ||||
172 | 0 | $query .= "$key=?, "; | ||||
173 | } | |||||
174 | 0 | $query = substr($query, 0, (length($query)-2)); | ||||
175 | 0 | push (@params, $self->{'template_id'}, $self->{'creator'}); | ||||
176 | 0 | $query .= " WHERE template_id=? AND creator=?;"; | ||||
177 | 0 | my $sth = C4::Context->dbh->prepare($query); | ||||
178 | 0 | $sth->execute(@params); | ||||
179 | 0 | if ($sth->err) { | ||||
180 | 0 | warn sprintf('Database returned the following error: %s', $sth->errstr); | ||||
181 | 0 | return -1; | ||||
182 | } | |||||
183 | 0 | $self->{'template_stat'} = 1; | ||||
184 | 0 | return $self->{'template_id'}; | ||||
185 | } | |||||
186 | else { # otherwise create a new record | |||||
187 | 0 | my @params; | ||||
188 | 0 | my $query = "INSERT INTO " . $opts{'table_name'} ." ("; | ||||
189 | 0 0 | foreach my $key (keys %{$self}) { | ||||
190 | 0 | next if $key eq 'template_stat'; | ||||
191 | 0 | push (@params, $self->{$key}); | ||||
192 | 0 | $query .= "$key, "; | ||||
193 | } | |||||
194 | 0 | $query = substr($query, 0, (length($query)-2)); | ||||
195 | 0 | $query .= ") VALUES ("; | ||||
196 | for (my $i=1; $i<=((scalar keys %$self) - 1); $i++) { # key count less keys not db related... | |||||
197 | 0 | $query .= "?,"; | ||||
198 | 0 | } | ||||
199 | 0 | $query = substr($query, 0, (length($query)-1)); | ||||
200 | 0 | $query .= ");"; | ||||
201 | 0 | my $sth = C4::Context->dbh->prepare($query); | ||||
202 | 0 | $sth->execute(@params); | ||||
203 | 0 | if ($sth->err) { | ||||
204 | 0 | warn sprintf('Database returned the following error: %s', $sth->errstr); | ||||
205 | 0 | return -1; | ||||
206 | } | |||||
207 | 0 | my $sth1 = C4::Context->dbh->prepare("SELECT MAX(template_id) FROM " . $opts{'table_name'} . ";"); | ||||
208 | 0 | $sth1->execute(); | ||||
209 | 0 | my $template_id = $sth1->fetchrow_array; | ||||
210 | 0 | $self->{'template_id'} = $template_id; | ||||
211 | 0 | $self->{'template_stat'} = 1; | ||||
212 | 0 | return $template_id; | ||||
213 | } | |||||
214 | } | |||||
215 | ||||||
216 | sub get_attr { | |||||
217 | 0 | my $self = shift; | ||||
218 | 0 | if (_check_params(@_) eq 1) { | ||||
219 | 0 | return -1; | ||||
220 | } | |||||
221 | 0 | my ($attr) = @_; | ||||
222 | 0 | if (exists($self->{$attr})) { | ||||
223 | 0 | return $self->{$attr}; | ||||
224 | } | |||||
225 | else { | |||||
226 | 0 | return -1; | ||||
227 | } | |||||
228 | } | |||||
229 | ||||||
230 | sub set_attr { | |||||
231 | 0 | my $self = shift; | ||||
232 | 0 | if (_check_params(@_) eq 1) { | ||||
233 | 0 | return -1; | ||||
234 | } | |||||
235 | 0 | my %attrs = @_; | ||||
236 | 0 | foreach my $attrib (keys(%attrs)) { | ||||
237 | 0 | $self->{$attrib} = $attrs{$attrib}; | ||||
238 | }; | |||||
239 | } | |||||
240 | ||||||
241 | sub get_label_position { | |||||
242 | 0 | my ($self, $start_label) = @_; | ||||
243 | 0 | my $current_label = $self->{'current_label'}; | ||||
244 | 0 | if ($start_label eq 1) { | ||||
245 | 0 | $current_label->{'row_count'} = 1; | ||||
246 | 0 | $current_label->{'col_count'} = 1; | ||||
247 | 0 | $current_label->{'llx'} = $self->{'left_margin'}; | ||||
248 | 0 | $current_label->{'lly'} = ($self->{'page_height'} - $self->{'top_margin'} - $self->{'label_height'}); | ||||
249 | 0 | $self->{'current_label'} = $current_label; | ||||
250 | 0 | return ($current_label->{'row_count'}, $current_label->{'col_count'}, $current_label->{'llx'}, $current_label->{'lly'}); | ||||
251 | } | |||||
252 | else { | |||||
253 | 0 | $current_label->{'row_count'} = ceil($start_label / $self->{'cols'}); | ||||
254 | 0 | $current_label->{'col_count'} = ($start_label - (($current_label->{'row_count'} - 1) * $self->{'cols'})); | ||||
255 | 0 | $current_label->{'llx'} = $self->{'left_margin'} + ($self->{'label_width'} * ($current_label->{'col_count'} - 1)) + ($self->{'col_gap'} * ($current_label->{'col_count'} - 1)); | ||||
256 | 0 | $current_label->{'lly'} = $self->{'page_height'} - $self->{'top_margin'} - ($self->{'label_height'} * $current_label->{'row_count'}) - ($self->{'row_gap'} * ($current_label->{'row_count'} - 1)); | ||||
257 | 0 | $self->{'current_label'} = $current_label; | ||||
258 | 0 | return ($current_label->{'row_count'}, $current_label->{'col_count'}, $current_label->{'llx'}, $current_label->{'lly'}); | ||||
259 | } | |||||
260 | } | |||||
261 | ||||||
262 | sub get_next_label_pos { | |||||
263 | 0 | my $self = shift; | ||||
264 | 0 | my $current_label = $self->{'current_label'}; | ||||
265 | 0 | my $new_page = 0; | ||||
266 | 0 | if ($current_label->{'col_count'} lt $self->get_attr('cols')) { | ||||
267 | 0 | $current_label->{'llx'} = ($current_label->{'llx'} + $self->get_attr('label_width') + $self->get_attr('col_gap')); | ||||
268 | 0 | $current_label->{'col_count'}++; | ||||
269 | } | |||||
270 | else { | |||||
271 | 0 | $current_label->{'llx'} = $self->get_attr('left_margin'); | ||||
272 | 0 | if ($current_label->{'row_count'} eq $self->get_attr('rows')) { | ||||
273 | 0 | $new_page = 1; | ||||
274 | 0 | $current_label->{'lly'} = ($self->get_attr('page_height') - $self->get_attr('top_margin') - $self->get_attr('label_height')); | ||||
275 | 0 | $current_label->{'row_count'} = 1; | ||||
276 | } | |||||
277 | else { | |||||
278 | 0 | $current_label->{'lly'} = ($current_label->{'lly'} - $self->get_attr('row_gap') - $self->get_attr('label_height')); | ||||
279 | 0 | $current_label->{'row_count'}++; | ||||
280 | } | |||||
281 | 0 | $current_label->{'col_count'} = 1; | ||||
282 | } | |||||
283 | 0 | return ($current_label->{'llx'}, $current_label->{'lly'}, $new_page); | ||||
284 | } | |||||
285 | ||||||
286 | 1; |