[8/40] Adding new layout management page
[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 @templates = ();
110     my $query = "SELECT * FROM labels_templates;";
111     my $sth = C4::Context->dbh->prepare($query);
112     $sth->execute();
113     if ($sth->err) {
114         syslog("LOG_ERR", "C4::Labels::Lib::get_all_templates : Database returned the following error: %s", $sth->errstr);
115         return 1;
116     }
117     ADD_TEMPLATES:
118     while (my $template = $sth->fetchrow_hashref) {
119         push(@templates, $template);
120     }
121     return \@templates;
122 }
123
124 =head2 C4::Labels::Lib::get_all_layouts()
125
126     This function returns a reference to a hash containing all layouts upon success and 1 upon failure. Errors are logged to the syslog.
127
128     examples:
129
130         my $layouts = get_all_layouts();
131
132 =cut
133
134 sub get_all_layouts {
135     my @layouts = ();
136     my $query = "SELECT * FROM labels_layouts;";
137     my $sth = C4::Context->dbh->prepare($query);
138     $sth->execute();
139     if ($sth->err) {
140         syslog("LOG_ERR", "C4::Labels::Lib::get_all_layouts : Database returned the following error: %s", $sth->errstr);
141         return 1;
142     }
143     ADD_LAYOUTS:
144     while (my $layout = $sth->fetchrow_hashref) {
145         push(@layouts, $layout);
146     }
147     return \@layouts;
148 }
149
150 =head2 C4::Labels::Lib::get_all_profiles()
151
152     This function returns an arrayref whose elements are hashes containing all profiles upon success and 1 upon failure. Errors are logged
153     to the syslog. Two parameters are accepted. The first limits the field(s) returned. This parameter should be string of comma separted
154     fields. ie. "field_1, field_2, ...field_n" The second limits the records returned based on a string containing a valud SQL 'WHERE' filter.
155     NOTE: Do not pass in the keyword 'WHERE.'
156
157     examples:
158
159         my $profiles = get_all_profiles();
160         my $profiles = get_all_profiles(field_list => field_list, filter => filter_string);
161
162 =cut
163
164 sub get_all_profiles {
165     my %params = @_;
166     my @profiles = ();
167     my $query = "SELECT " . ($params{'field_list'} ? $params{'field_list'} : '*') . " FROM printers_profile";
168     $query .= ($params{'filter'} ? " WHERE $params{'filter'};" : ';');
169     my $sth = C4::Context->dbh->prepare($query);
170 #    $sth->{'TraceLevel'} = 3 if $debug;
171     $sth->execute();
172     if ($sth->err) {
173         syslog("LOG_ERR", "C4::Labels::Lib::get_all_profiles : Database returned the following error: %s", $sth->errstr);
174         return 1;
175     }
176     ADD_LAYOUTS:
177     while (my $profile = $sth->fetchrow_hashref) {
178         push(@profiles, $profile);
179     }
180     return \@profiles;
181 }
182
183 =head2 C4::Labels::Lib::get_barcode_types()
184
185     This function returns a reference to an array of hashes containing all barcode types along with their name and description.
186
187     examples:
188
189         my $barcode_types = get_barcode_types();
190
191 =cut
192
193 sub get_barcode_types {
194     return $barcode_types;
195 }
196
197 =head2 C4::Labels::Lib::get_label_types()
198
199     This function returns a reference to an array of hashes containing all label types along with their name and description.
200
201     examples:
202
203         my $label_types = get_label_types();
204
205 =cut
206
207 sub get_label_types {
208     return $label_types;
209 }
210
211 =head2 C4::Labels::Lib::get_font_types()
212
213     This function returns a reference to an array of hashes containing all font types along with their name and description.
214
215     examples:
216
217         my $font_types = get_font_types();
218
219 =cut
220
221 sub get_font_types {
222     return $font_types;
223 }
224
225 =head2 C4::Labels::Lib::get_text_justification_types()
226
227     This function returns a reference to an array of hashes containing all text justification types along with their name and description.
228
229     examples:
230
231         my $text_justification_types = get_text_justification_types();
232
233 =cut
234
235 sub get_text_justification_types {
236     return $text_justification_types;
237 }
238
239 =head2 C4::Labels::Lib::get_unit_values()
240
241     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.
242     There are 72 PS points to the inch.
243
244     examples:
245
246         my $unit_values = get_unit_values();
247
248 =cut
249
250 sub get_unit_values {
251     return $unit_values;
252 }
253
254 =head2 C4::Labels::Lib::get_column_names($table_name)
255
256 Return an arrayref of an array containing the column names of the supplied table.
257
258 =cut
259
260 sub get_column_names {
261     my $table = shift;
262     my $dbh = C4::Context->dbh();
263     my $column_names = [];
264     my $sth = $dbh->column_info(undef,undef,$table,'%');
265     while (my $info = $sth->fetchrow_hashref()){
266         $$column_names[$info->{'ORDINAL_POSITION'}] = $info->{'COLUMN_NAME'};
267     }
268     return $column_names;
269 }
270
271 =head2 C4::Labels::Lib::get_table_names($search_term)
272
273 Return an arrayref of an array containing the table names which contain the supplied search term.
274
275 =cut
276
277 sub get_table_names {
278     my $search_term = shift;
279     my $dbh = C4::Context->dbh();
280     my $table_names = [];
281     my $sth = $dbh->table_info(undef,undef,"%$search_term%");
282     while (my $info = $sth->fetchrow_hashref()){
283         push (@$table_names, $info->{'TABLE_NAME'});
284     }
285     return $table_names;
286 }
287
288 =head2 C4::Labels::Lib::SELECT()
289
290     This function returns a recordset upon success and 1 upon failure. Errors are logged to the syslog.
291
292     examples:
293
294         my $field_value = SELECT(field_name, table_name, condition);
295
296 =cut
297
298 sub SELECT {
299     my @params = @_;
300     my $query = "SELECT $params[0] FROM $params[1]";
301     $params[2] ? $query .= " WHERE $params[2];" : $query .= ';';
302     my $sth = C4::Context->dbh->prepare($query);
303 #    $sth->{'TraceLevel'} = 3;
304     $sth->execute();
305     if ($sth->err) {
306         syslog("LOG_ERR", "C4::Labels::Lib::get_single_field_value : Database returned the following error: %s", $sth->errstr);
307         return 1;
308     }
309     my $record_set = [];
310     while (my $row = $sth->fetchrow_hashref()) {
311         push(@$record_set, $row);
312     }
313     return $record_set;
314 }
315
316 1;
317 __END__
318
319 =head1 AUTHOR
320
321 Chris Nighswonger <cnighswonger AT foundations DOT edu>
322
323 =cut