File Coverage

File:C4/Creators/Layout.pm
Coverage:15.7%

linestmtbrancondsubtimecode
1package C4::Creators::Layout;
2
3
4
4
4
779
31
156
use strict;
4
4
4
4
59
28
171
use warnings;
5
6
4
4
4
205
797
48
use autouse 'Data::Dumper' => qw(Dumper);
7
8
4
4
4
677
17
82
use C4::Context;
9
4
4
4
39
22
455
use C4::Debug;
10
4
4
4
746
13
157
use C4::Creators::PDF;
11
12BEGIN {
13
4
4
4
4
25
5
50
355
    use version; our $VERSION = qv('1.0.0_1');
14}
15
16# FIXME: Consider this style parameter verification instead...
17# my %param = @_;
18# for (keys %param)
19# { my $lc = lc($_);
20# if (exists $default{$lc})
21# { $default{$lc} = $param{$_};
22# }
23# else
24# { print STDERR "Unknown parameter $_ , not used \n";
25# }
26# }
27
28sub _check_params {
29
0
    my $exit_code = 0;
30
0
    my @valtmpl_id_params = (
31        'layout_id',
32        'barcode_type',
33        'printing_type',
34        'layout_name',
35        'guidebox',
36        'font',
37        'font_size',
38        'callnum_split',
39        'text_justify',
40        'format_string',
41        'layout_xml', # FIXME: all layouts should be stored in xml format to greatly simplify handling -chris_n
42        'creator',
43        'units',
44        'start_label',
45    );
46
0
    if (scalar(@_) >1) {
47
0
        my %given_params = @_;
48
0
        foreach my $key (keys %given_params) {
49
0
            if (!(grep m/$key/, @valtmpl_id_params)) {
50
0
                warn sprintf('(Multiple parameters) Unrecognized parameter type of "%s".', $key);
51
0
                $exit_code = 1;
52            }
53        }
54    }
55    else {
56
0
        if (!(grep m/$_/, @valtmpl_id_params)) {
57
0
            warn sprintf('(Single parameter) Unrecognized parameter type of "%s".', $_);
58
0
            $exit_code = 1;
59        }
60    }
61
0
    return $exit_code;
62}
63
64
4
4
4
901
6
72
use constant PRESET_FIELDS => [qw(title author isbn issn itemtype barcode itemcallnumber)];
65sub new {
66
0
    my $invocant = shift;
67
0
    my $self = '';
68
0
    if (_check_params(@_) eq 1) {
69
0
        return -1;
70    }
71
0
    my $type = ref($invocant) || $invocant;
72
0
0
0
    if (grep {$_ eq 'Labels'} @_) {
73
0
       $self = {
74            layout_xml => '',
75            units => 'POINT',
76            start_label => 1,
77            barcode_type => 'CODE39',
78            printing_type => 'BAR',
79            layout_name => 'DEFAULT',
80            guidebox => 0,
81            font => 'TR',
82            font_size => 3,
83            callnum_split => 0,
84            text_justify => 'L',
85
0
            format_string => join(', ', @{ PRESET_FIELDS() }),
86            @_,
87        };
88    }
89    elsif (grep {$_ eq 'Patroncards'} @_) {
90
0
        $self = {
91            layout_xml => '<opt>Default Layout</opt>',
92            @_,
93        }
94    }
95
0
    bless ($self, $type);
96
0
    return $self;
97}
98
99sub retrieve {
100
0
    my $invocant = shift;
101
0
    my %opts = @_;
102
0
    my $type = ref($invocant) || $invocant;
103
0
    my $query = "SELECT * FROM creator_layouts WHERE layout_id = ? AND creator = ?";
104
0
    my $sth = C4::Context->dbh->prepare($query);
105
0
    $sth->execute($opts{'layout_id'}, $opts{'creator'});
106
0
    if ($sth->err) {
107
0
        warn sprintf('Database returned the following error: %s', $sth->errstr);
108
0
        return -1;
109    }
110
0
    my $self = $sth->fetchrow_hashref;
111
0
    bless ($self, $type);
112
0
    return $self;
113}
114
115sub delete {
116
0
    my $self = {};
117
0
    my %opts = ();
118
0
    my $call_type = '';
119
0
    my @params = ();
120
0
    if (ref($_[0])) {
121
0
        $self = shift; # check to see if this is a method call
122
0
        $call_type = 'C4::Labels::Layout->delete';
123
0
        push @params, $self->{'layout_id'}, $self->{'creator'};
124    }
125    else {
126
0
        my $class = shift;
127
0
        %opts = @_;
128
0
        $call_type = $class . '::delete';
129
0
        push @params, $opts{'layout_id'}, $opts{'creator'};
130    }
131
0
    if (scalar(@params) < 2) { # If there is no layout id or creator type then we cannot delete it
132
0
        warn sprintf('%s : Cannot delete layout as the profile id is invalid or non-existant.', $call_type) if !$params[0];
133
0
        warn sprintf('%s : Cannot delete layout as the creator type is invalid or non-existant.', $call_type) if !$params[1];
134
0
        return -1;
135    }
136
0
    my $query = "DELETE FROM creator_layouts WHERE layout_id = ? AND creator = ?";
137
0
    my $sth = C4::Context->dbh->prepare($query);
138
0
    $sth->execute(@params);
139
0
    if ($sth->err) {
140
0
        warn sprintf('Database returned the following error on attempted DELETE: %s', $sth->errstr);
141
0
        return -1;
142    }
143}
144
145sub save {
146
0
    my $self = shift;
147
0
    if ($self->{'layout_id'}) { # if we have an id, the record exists and needs UPDATE
148
0
        my @params;
149
0
        my $query = "UPDATE creator_layouts SET ";
150
0
0
        foreach my $key (keys %{$self}) {
151
0
            next if ($key eq 'layout_id') || ($key eq 'creator');
152
0
            push (@params, $self->{$key});
153
0
            $query .= "$key=?, ";
154        }
155
0
        $query = substr($query, 0, (length($query)-2));
156
0
        $query .= " WHERE layout_id=? AND creator = ?;";
157
0
        push (@params, $self->{'layout_id'}, $self->{'creator'});
158
0
        my $sth = C4::Context->dbh->prepare($query);
159        #local $sth->{TraceLevel} = "3"; # enable DBI trace and set level; outputs to STDERR
160
0
        $sth->execute(@params);
161
0
        if ($sth->err) {
162
0
            warn sprintf('Database returned the following error: %s', $sth->errstr);
163
0
            return -1;
164        }
165
0
        return $self->{'layout_id'};
166    }
167    else { # otherwise create a new record
168
0
        my @params;
169
0
        my $query = "INSERT INTO creator_layouts (";
170
0
0
        foreach my $key (keys %{$self}) {
171
0
            push (@params, $self->{$key});
172
0
            $query .= "$key, ";
173        }
174
0
        $query = substr($query, 0, (length($query)-2));
175
0
        $query .= ") VALUES (";
176        for (my $i=1; $i<=(scalar keys %$self); $i++) {
177
0
            $query .= "?,";
178
0
        }
179
0
        $query = substr($query, 0, (length($query)-1));
180
0
        $query .= ");";
181
0
        my $sth = C4::Context->dbh->prepare($query);
182
0
        $sth->execute(@params);
183
0
        if ($sth->err) {
184
0
            warn sprintf('Database returned the following error: %s', $sth->errstr);
185
0
            return -1;
186        }
187
0
        my $sth1 = C4::Context->dbh->prepare("SELECT MAX(layout_id) FROM creator_layouts;");
188
0
        $sth1->execute();
189
0
        my $id = $sth1->fetchrow_array;
190
0
        $self->{'layout_id'} = $id;
191
0
        return $id;
192    }
193}
194
195sub get_attr {
196
0
    my $self = shift;
197
0
    if (_check_params(@_) eq 1) {
198
0
        return -1;
199    }
200
0
    my ($attr) = @_;
201
0
    if (exists($self->{$attr})) {
202
0
        return $self->{$attr};
203    }
204    else {
205
0
        return -1;
206    }
207
0
    return;
208}
209
210sub set_attr {
211
0
    my $self = shift;
212
0
    if (_check_params(@_) eq 1) {
213
0
        return -1;
214    }
215
0
    my %attrs = @_;
216
0
    foreach my $attrib (keys(%attrs)) {
217
0
        $self->{$attrib} = $attrs{$attrib};
218    };
219
0
    return 0;
220}
221
222sub get_text_wrap_cols {
223
0
    my $self = shift;
224
0
    my %params = @_;
225
0
    my $string = '';
226
0
    my $strwidth = 0;
227
0
    my $col_count = 0;
228
0
    my $textlimit = $params{'label_width'} - (( 3 * $params{'left_text_margin'} ) || 13.5 );
229
230
0
    while ($strwidth < $textlimit) {
231
0
        $string .= '0';
232
0
        $col_count++;
233
0
        $strwidth = C4::Creators::PDF->StrWidth( $string, $self->{'font'}, $self->{'font_size'} );
234    }
235
0
    return $col_count;
236}
237
2381;