1 package C4::Labels::Profile;
3 # Copyright 2009 Foundations Bible College.
5 # This file is part of Koha.
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 2 of the License, or (at your option) any later
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.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
22 use Sys::Syslog qw(syslog);
27 use C4::Labels::Lib 1.000000 qw(get_unit_values);
30 use version; our $VERSION = qv('1.0.0_1');
34 my $given_params = {};
36 my @valid_profile_params = (
48 foreach my $key (keys %{$given_params}) {
49 if (!(grep m/$key/, @valid_profile_params)) {
50 syslog("LOG_ERR", "C4::Labels::Profile : Unrecognized parameter type of \"%s\".", $key);
56 if (!(grep m/$_/, @valid_profile_params)) {
57 syslog("LOG_ERR", "C4::Labels::Profile : Unrecognized parameter type of \"%s\".", $_);
66 my @unit_value = grep {$_->{'type'} eq $self->{units}} get_unit_values();
67 $self->{offset_horz} = $self->{offset_horz} * $unit_value[0]->{'value'};
68 $self->{offset_vert} = $self->{offset_vert} * $unit_value[0]->{'value'};
69 $self->{creep_horz} = $self->{creep_horz} * $unit_value[0]->{'value'};
70 $self->{creep_vert} = $self->{creep_vert} * $unit_value[0]->{'value'};
76 C4::Labels::Profile - A class for creating and manipulating profile objects in Koha
82 =head2 C4::Labels::Profile->new()
84 Invoking the I<new> method constructs a new profile object containing the default values for a template.
87 my $profile = Profile->new(); # Creates and returns a new profile object
89 B<NOTE:> This profile is I<not> written to the database untill $profile->save() is invoked. You have been warned!
95 if (_check_params(@_) eq 1) {
98 my $type = ref($invocant) || $invocant;
110 bless ($self, $type);
114 =head2 C4::Labels::Profile->retrieve(profile_id => profile_id, convert => 1)
116 Invoking the I<retrieve> method constructs a new profile object containing the current values for profile_id. The method returns
117 a new object upon success and 1 upon failure. Errors are logged to the syslog. One further option maybe accessed. See the examples
118 below for further description.
122 my $profile = C4::Labels::Profile->retrieve(profile_id => 1); # Retrieves profile record 1 and returns an object containing the record
124 my $profile = C4::Labels::Profile->retrieve(profile_id => 1, convert => 1); # Retrieves profile record 1, converts the units to points,
125 and returns an object containing the record
130 my $invocant = shift;
132 my $type = ref($invocant) || $invocant;
133 my $query = "SELECT * FROM printers_profile WHERE profile_id = ?";
134 my $sth = C4::Context->dbh->prepare($query);
135 $sth->execute($opts{profile_id});
137 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
140 my $self = $sth->fetchrow_hashref;
141 $self = _conv_points($self) if ($opts{convert} && $opts{convert} == 1);
142 bless ($self, $type);
146 =head2 C4::Labels::Profile->delete(profile_id => profile_id) | $profile->delete()
148 Invoking the delete method attempts to delete the profile from the database. The method returns 0 upon success
149 and 1 upon failure. Errors are logged to the syslog.
152 my $exitstat = $profile->delete(); # to delete the record behind the $profile object
153 my $exitstat = C4::Labels::Profile->delete(profile_id => 1); # to delete profile record 1
159 if (!$self->{'profile_id'}) { # If there is no profile profile_id then we cannot delete it
160 syslog("LOG_ERR", "Cannot delete profile as it has not been saved.");
163 my $query = "DELETE FROM printers_profile WHERE profile_id = ?";
164 my $sth = C4::Context->dbh->prepare($query);
165 $sth->execute($self->{'profile_id'});
169 =head2 $profile->save()
171 Invoking the I<save> method attempts to insert the profile into the database if the profile is new and
172 update the existing profile record if the profile exists. The method returns the new record profile_id upon
173 success and -1 upon failure (This avoids conflicting with a record profile_id of 1). Errors are logged to the syslog.
176 my $exitstat = $profile->save(); # to save the record behind the $profile object
182 if ($self->{'profile_id'}) { # if we have an profile_id, the record exists and needs UPDATE
184 my $query = "UPDATE printers_profile SET ";
185 foreach my $key (keys %{$self}) {
186 next if $key eq 'profile_id';
187 push (@params, $self->{$key});
188 $query .= "$key=?, ";
190 $query = substr($query, 0, (length($query)-2));
191 push (@params, $self->{'profile_id'});
192 $query .= " WHERE profile_id=?;";
193 warn "DEBUG: Updating: $query\n" if $debug;
194 my $sth = C4::Context->dbh->prepare($query);
195 $sth->execute(@params);
197 syslog("LOG_ERR", "C4::Labels::Profile : Database returned the following error: %s", $sth->errstr);
200 return $self->{'profile_id'};
202 else { # otherwise create a new record
204 my $query = "INSERT INTO printers_profile (";
205 foreach my $key (keys %{$self}) {
206 push (@params, $self->{$key});
209 $query = substr($query, 0, (length($query)-2));
210 $query .= ") VALUES (";
211 for (my $i=1; $i<=(scalar keys %$self); $i++) {
214 $query = substr($query, 0, (length($query)-1));
216 warn "DEBUG: Saving: $query\n" if $debug;
217 my $sth = C4::Context->dbh->prepare($query);
218 $sth->execute(@params);
220 syslog("LOG_ERR", "C4::Labels::Profile : Database returned the following error: %s", $sth->errstr);
223 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(profile_id) FROM printers_profile;");
225 my $tmpl_id = $sth1->fetchrow_array;
230 =head2 $profile->get_attr(attr)
232 Invoking the I<get_attr> method will return the value of the requested attribute or 1 on errors.
235 my $value = $profile->get_attr(attr);
241 if (_check_params(@_) eq 1) {
245 if (exists($self->{$attr})) {
246 return $self->{$attr};
249 syslog("LOG_ERR", "C4::Labels::Profile : %s is currently undefined.", $attr);
254 =head2 $profile->set_attr(attr => value)
256 Invoking the I<set_attr> method will set the value of the supplied attribute to the supplied value.
259 $profile->set_attr(attr => value);
265 if (_check_params(@_) eq 1) {
268 my ($attr, $value) = @_;
269 $self->{$attr} = $value;
279 Chris Nighswonger <cnighswonger AT foundations DOT edu>
285 drawbox( ($left_margin), ($top_margin), ($page_width-(2*$left_margin)), ($page_height-(2*$top_margin)) ); # FIXME: Breakout code to print alignment page for printer profile setup
289 sub draw_boundaries ($llx_spine, $llx_circ1, $llx_circ2,
290 $lly, $spine_width, $label_height, $circ_width)
292 This sub draws boundary lines where the label outlines are, to aid in printer testing, and debugging.
296 # FIXME: Template use for profile adjustment...
297 #sub draw_boundaries {
300 # $llx_spine, $llx_circ1, $llx_circ2, $lly,
301 # $spine_width, $label_height, $circ_width
304 # my $lly_initial = ( ( 792 - 36 ) - 90 );
305 # $lly = $lly_initial; # FIXME - why are we ignoring the y_pos parameter by redefining it?
308 # for ( $i = 1 ; $i <= 8 ; $i++ ) {
310 # _draw_box( $llx_spine, $lly, ($spine_width), ($label_height) );
312 # #warn "OLD BOXES x=$llx_spine, y=$lly, w=$spine_width, h=$label_height";
313 # _draw_box( $llx_circ1, $lly, ($circ_width), ($label_height) );
314 # _draw_box( $llx_circ2, $lly, ($circ_width), ($label_height) );
316 # $lly = ( $lly - $label_height );