[13/40] Work on profile editor interface.
[koha.git] / C4 / Labels / Lib.pm
1 package C4::Labels::Lib;
2
3 # Copyright 2009 Foundations Bible College.
4 #
5 # This file is part of Koha.
6 #       
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
10 # version.
11 #
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.
15 #
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
19
20 use strict;
21 use warnings;
22 use Sys::Syslog qw(syslog);
23 use Data::Dumper;
24
25 use C4::Context;
26 use C4::Debug;
27
28 BEGIN {
29     use version; our $VERSION = qv('1.0.0_1');
30     use base qw(Exporter);
31     our @EXPORT_OK = qw(get_all_templates
32                         get_all_layouts
33                         get_all_profiles
34                         get_barcode_types
35                         get_label_types
36                         get_font_types
37                         get_text_justification_types
38                         get_column_names
39                         get_table_names
40                         get_unit_values
41                         SELECT
42     );
43 }
44
45 my $barcode_types = [
46     {type => 'CODE39',          name => 'Code 39',              desc => 'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern.',                                  selected => 0},
47     {type => 'CODE39MOD',       name => 'Code 39 + Modulo43',   desc => 'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern. Encodes Mod 43 checksum.',         selected => 0},
48     {type => 'CODE39MOD10',     name => 'Code 39 + Modulo10',   desc => 'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern. Encodes Mod 10 checksum.',         selected => 0},
49     {type => 'COOP2OF5',        name => 'COOP2of5',             desc => 'Creates COOP2of5 barcodes from a string consisting of the numeric characters 0-9',                                                                     selected => 0},
50 #    {type => 'EAN13',           name => 'EAN13',                desc => 'Creates EAN13 barcodes from a string of 12 or 13 digits. The check number (the 13:th digit) is calculated if not supplied.',                           selected => 0},
51 #    {type => 'EAN8',            name => 'EAN8',                 desc => 'Translates a string of 7 or 8 digits to EAN8 barcodes. The check number (the 8:th digit) is calculated if not supplied.',                              selected => 0},
52 #    {type => 'IATA2of5',        name => 'IATA2of5',             desc => 'Creates IATA2of5 barcodes from a string consisting of the numeric characters 0-9',                                                                     selected => 0},
53     {type => 'INDUSTRIAL2OF5',  name => 'Industrial2of5',       desc => 'Creates Industrial2of5 barcodes from a string consisting of the numeric characters 0-9',                                                               selected => 0},
54 #    {type => 'ITF',             name => 'Interleaved2of5',      desc => 'Translates the characters 0-9 to a barcodes. These barcodes could also be called 'Interleaved2of5'.',                                                  selected => 0},
55 #    {type => 'MATRIX2OF5',      name => 'Matrix2of5',           desc => 'Creates Matrix2of5 barcodes from a string consisting of the numeric characters 0-9',                                                                   selected => 0},
56 #    {type => 'NW7',             name => 'NW7',                  desc => 'Creates a NW7 barcodes from a string consisting of the numeric characters 0-9',                                                                        selected => 0},
57 #    {type => 'UPCA',            name => 'UPCA',                 desc => 'Translates a string of 11 or 12 digits to UPCA barcodes. The check number (the 12:th digit) is calculated if not supplied.',                           selected => 0},
58 #    {type => 'UPCE',            name => 'UPCE',                 desc => 'Translates a string of 6, 7 or 8 digits to UPCE barcodes. If the string is 6 digits long, '0' is added first in the string. The check number (the 8:th digit) is calculated if not supplied.',                                 selected => 0},
59 ];
60
61 my $label_types = [
62     {type => 'BIB',     name => 'Biblio',               desc => 'Only the bibliographic data is printed.',                              selected => 0},
63     {type => 'BARBIB',  name => 'Barcode/Biblio',       desc => 'Barcode proceeds bibliographic data.',                                 selected => 0},
64     {type => 'BIBBAR',  name => 'Biblio/Barcode',       desc => 'Bibliographic data proceeds barcode.',                                 selected => 0},
65     {type => 'ALT',     name => 'Alternating',          desc => 'Barcode and bibliographic data are printed on alternating labels.',    selected => 0},
66     {type => 'BAR',     name => 'Barcode',              desc => 'Only the barcode is printed.',                                         selected => 0},
67 ];
68
69 my $font_types = [
70     {type => 'TR',      name => 'Times-Roman',                  selected => 0},
71     {type => 'TB',      name => 'Times-Bold',                   selected => 0},
72     {type => 'TI',      name => 'Times-Italic',                 selected => 0},
73     {type => 'TBI',     name => 'Times-Bold-Italic',            selected => 0},
74     {type => 'C',       name => 'Courier',                      selected => 0},
75     {type => 'CB',      name => 'Courier-Bold',                 selected => 0},
76     {type => 'CO',      name => 'Courier-Oblique',              selected => 0},
77     {type => 'CBO',     name => 'Courier-Bold-Oblique',         selected => 0},
78     {type => 'H',       name => 'Helvetica',                    selected => 0},
79     {type => 'HB',      name => 'Helvetica-Bold',               selected => 0},
80     {type => 'HBO',     name => 'Helvetica-Bold-Oblique',       selected => 0},
81 ];
82
83 my $text_justification_types = [
84     {type => 'L',       name => 'Left',                         selected => 0},
85     {type => 'C',       name => 'Center',                       selected => 0},
86     {type => 'R',       name => 'Right',                        selected => 0},
87 #    {type => 'F',       name => 'Full',                         selected => 0},    
88 ];
89
90 my $unit_values = [
91     {type       => 'POINT',      desc    => 'PostScript Points',  value   => 1,                 selected => 0},
92     {type       => 'AGATE',      desc    => 'Adobe Agates',       value   => 5.1428571,         selected => 0},
93     {type       => 'INCH',       desc    => 'US Inches',          value   => 72,                selected => 0},
94     {type       => 'MM',         desc    => 'SI Millimeters',     value   => 2.83464567,        selected => 0},
95     {type       => 'CM',         desc    => 'SI Centimeters',     value   => 28.3464567,        selected => 0},
96 ];
97
98 =head2 C4::Labels::Lib::get_all_templates()
99
100     This function returns a reference to a hash containing all templates upon success and 1 upon failure. Errors are logged to the syslog.
101
102     examples:
103
104         my $templates = get_all_templates();
105
106 =cut
107
108 sub get_all_templates {
109     my %params = @_;
110     my @templates = ();
111     my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM labels_templates";
112     $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';');
113     my $sth = C4::Context->dbh->prepare($query);
114     $sth->execute();
115     if ($sth->err) {
116         syslog("LOG_ERR", "C4::Labels::Lib::get_all_templates : Database returned the following error: %s", $sth->errstr);
117         return -1;
118     }
119     ADD_TEMPLATES:
120     while (my $template = $sth->fetchrow_hashref) {
121         push(@templates, $template);
122     }
123     return \@templates;
124 }
125
126 =head2 C4::Labels::Lib::get_all_layouts()
127
128     This function returns a reference to a hash containing all layouts upon success and 1 upon failure. Errors are logged to the syslog.
129
130     examples:
131
132         my $layouts = get_all_layouts();
133
134 =cut
135
136 sub get_all_layouts {
137     my @layouts = ();
138     my $query = "SELECT * FROM labels_layouts;";
139     my $sth = C4::Context->dbh->prepare($query);
140     $sth->execute();
141     if ($sth->err) {
142         syslog("LOG_ERR", "C4::Labels::Lib::get_all_layouts : Database returned the following error: %s", $sth->errstr);
143         return -1;
144     }
145     ADD_LAYOUTS:
146     while (my $layout = $sth->fetchrow_hashref) {
147         push(@layouts, $layout);
148     }
149     return \@layouts;
150 }
151
152 =head2 C4::Labels::Lib::get_all_profiles()
153
154     This function returns an arrayref whose elements are hashes containing all profiles upon success and 1 upon failure. Errors are logged
155     to the syslog. Two parameters are accepted. The first limits the field(s) returned. This parameter should be string of comma separted
156     fields. ie. "field_1, field_2, ...field_n" The second limits the records returned based on a string containing a valud SQL 'WHERE' filter.
157     NOTE: Do not pass in the keyword 'WHERE.'
158
159     examples:
160
161         my $profiles = get_all_profiles();
162         my $profiles = get_all_profiles(field_list => field_list, filter => filter_string);
163
164 =cut
165
166 sub get_all_profiles {
167     my %params = @_;
168     my @profiles = ();
169     my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM printers_profile";
170     $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';');
171     my $sth = C4::Context->dbh->prepare($query);
172 #    $sth->{'TraceLevel'} = 3 if $debug;
173     $sth->execute();
174     if ($sth->err) {
175         syslog("LOG_ERR", "C4::Labels::Lib::get_all_profiles : Database returned the following error: %s", $sth->errstr);
176         return -1;
177     }
178     ADD_LAYOUTS:
179     while (my $profile = $sth->fetchrow_hashref) {
180         push(@profiles, $profile);
181     }
182     return \@profiles;
183 }
184
185 =head2 C4::Labels::Lib::get_barcode_types()
186
187     This function returns a reference to an array of hashes containing all barcode types along with their name and description.
188
189     examples:
190
191         my $barcode_types = get_barcode_types();
192
193 =cut
194
195 sub get_barcode_types {
196     return $barcode_types;
197 }
198
199 =head2 C4::Labels::Lib::get_label_types()
200
201     This function returns a reference to an array of hashes containing all label types along with their name and description.
202
203     examples:
204
205         my $label_types = get_label_types();
206
207 =cut
208
209 sub get_label_types {
210     return $label_types;
211 }
212
213 =head2 C4::Labels::Lib::get_font_types()
214
215     This function returns a reference to an array of hashes containing all font types along with their name and description.
216
217     examples:
218
219         my $font_types = get_font_types();
220
221 =cut
222
223 sub get_font_types {
224     return $font_types;
225 }
226
227 =head2 C4::Labels::Lib::get_text_justification_types()
228
229     This function returns a reference to an array of hashes containing all text justification types along with their name and description.
230
231     examples:
232
233         my $text_justification_types = get_text_justification_types();
234
235 =cut
236
237 sub get_text_justification_types {
238     return $text_justification_types;
239 }
240
241 =head2 C4::Labels::Lib::get_unit_values()
242
243     This function returns a reference to an array of  hashes containing all unit types along with their description and multiplier. NOTE: All units are relative to a PostScript Point.
244     There are 72 PS points to the inch.
245
246     examples:
247
248         my $unit_values = get_unit_values();
249
250 =cut
251
252 sub get_unit_values {
253     return $unit_values;
254 }
255
256 =head2 C4::Labels::Lib::get_column_names($table_name)
257
258 Return an arrayref of an array containing the column names of the supplied table.
259
260 =cut
261
262 sub get_column_names {
263     my $table = shift;
264     my $dbh = C4::Context->dbh();
265     my $column_names = [];
266     my $sth = $dbh->column_info(undef,undef,$table,'%');
267     while (my $info = $sth->fetchrow_hashref()){
268         $$column_names[$info->{'ORDINAL_POSITION'}] = $info->{'COLUMN_NAME'};
269     }
270     return $column_names;
271 }
272
273 =head2 C4::Labels::Lib::get_table_names($search_term)
274
275 Return an arrayref of an array containing the table names which contain the supplied search term.
276
277 =cut
278
279 sub get_table_names {
280     my $search_term = shift;
281     my $dbh = C4::Context->dbh();
282     my $table_names = [];
283     my $sth = $dbh->table_info(undef,undef,"%$search_term%");
284     while (my $info = $sth->fetchrow_hashref()){
285         push (@$table_names, $info->{'TABLE_NAME'});
286     }
287     return $table_names;
288 }
289
290 =head2 C4::Labels::Lib::SELECT()
291
292     This function returns a recordset upon success and 1 upon failure. Errors are logged to the syslog.
293
294     examples:
295
296         my $field_value = SELECT(field_name, table_name, condition);
297
298 =cut
299
300 sub SELECT {
301     my @params = @_;
302     my $query = "SELECT $params[0] FROM $params[1]";
303     $params[2] ? $query .= " WHERE $params[2];" : $query .= ';';
304     my $sth = C4::Context->dbh->prepare($query);
305 #    $sth->{'TraceLevel'} = 3;
306     $sth->execute();
307     if ($sth->err) {
308         syslog("LOG_ERR", "C4::Labels::Lib::get_single_field_value : Database returned the following error: %s", $sth->errstr);
309         return 1;
310     }
311     my $record_set = [];
312     while (my $row = $sth->fetchrow_hashref()) {
313         push(@$record_set, $row);
314     }
315     return $record_set;
316 }
317
318 1;
319 __END__
320
321 =head1 AUTHOR
322
323 Chris Nighswonger <cnighswonger AT foundations DOT edu>
324
325 =cut