Removing $dbh from GetMarcFromKohaField (dbh is not used in this function.)
[koha.git] / barcodes / barcodes.pl
1 #!/usr/bin/perl
2
3 # script to generate items barcodes
4 # written 07/04
5 # by Veleda Matias - matias_veleda@hotmail.com - Physics Library UNLP Argentina and
6 #    Casta�eda Sebastian - seba3c@yahoo.com.ar - Physics Library UNLP Argentina and
7
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 use CGI;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Context;
28 use C4::Barcodes::PrinterConfig;
29
30 # This function returns the path to deal with the correct files, considering
31 # templates set and language.
32 sub getPath {
33     my $type         = shift @_;
34     my $templatesSet = C4::Context->preference('template');
35     my $lang         = C4::Context->preference('opaclanguages');
36     if ( $type eq "intranet" ) {
37         return "$ENV{'DOCUMENT_ROOT'}/intranet-tmpl/$templatesSet/$lang";
38     }
39     else {
40         return "$ENV{'DOCUMENT_ROOT'}/opac-tmpl/$templatesSet/$lang";
41     }
42 }
43
44 # Load a configuration file. Before use this function, check if that file exists.
45 sub loadConfFromFile {
46     my $fileName = shift @_;
47     my %keyValues;
48     open FILE, "<$fileName";
49     while (<FILE>) {
50         chomp;
51         if (/\s*([\w_]*)\s*=\s*([\[\]\<\>\w_\s:@,\.-]*)\s*/) {
52             $keyValues{$1} = $2;
53         }
54     }
55     close FILE;
56     return %keyValues;
57 }
58
59 # Save settings to a configuration file. It delete previous configuration settings.
60 sub saveConfToFile {
61     my $fileName  = shift @_;
62     my %keyValues = %{ shift @_ };
63     my $i;
64     open FILE, ">$fileName";
65     foreach $i ( keys(%keyValues) ) {
66         print FILE $i . " = " . $keyValues{$i} . "\n";
67     }
68     close FILE;
69 }
70
71 # Load the config file.
72 my $filenameConf =
73   &getPath("intranet") . "/includes/labelConfig/itemsLabelConfig.conf";
74 my %labelConfig = &loadConfFromFile($filenameConf);
75
76 my $input = new CGI;
77
78 # Defines type of page to use in the printer process
79 my @labelTable =
80   C4::Barcodes::PrinterConfig::labelsPage( $labelConfig{'rows'},
81     $labelConfig{'columns'} );
82
83 # It creates a list of posible intervals to choose codes to generate
84 my %list = (
85     'continuous'  => 'Continuous Range of items',
86     'individuals' => 'Individual Codes'
87 );
88 my @listValues = keys(%list);
89 my $rangeType  = CGI::scrolling_list(
90     -name     => 'rangeType',
91     -values   => \@listValues,
92     -labels   => \%list,
93     -size     => 1,
94     -default  => ['continuous'],
95     -multiple => 0,
96     -id       => "rangeType",
97     -onChange => "changeRange(this)"
98 );
99
100 # It creates a list of posible standard codifications. First checks if the user has just added a new code.
101 if ( $input->param('addCode') ) {
102     my $newCountryName = $input->param('countryName');
103     my $newCountryCode = $input->param('countryCode');
104
105     my $countryCodesFilename =
106       &getPath("intranet") . "/includes/countryCodes/countryCodes.dat";
107     open COUNTRY_CODES, ">>$countryCodesFilename";
108     print COUNTRY_CODES $newCountryCode . " = " . $newCountryName . "\n";
109     close COUNTRY_CODES;
110 }
111
112 # Takes the country codes from a file and use them to set the country list.
113 my $countryCodes =
114   &getPath("intranet") . "/includes/countryCodes/countryCodes.dat";
115 %list = &loadConfFromFile($countryCodes);
116 @listValues = keys(%list);
117 my $number_system = CGI::scrolling_list(
118     -name     => 'numbersystem',
119     -values   => \@listValues,
120     -labels   => \%list,
121     -size     => 1,
122     -multiple => 0
123 );
124
125 # Set the script name
126 my $script_name = "/cgi-bin/koha/barcodes/barcodesGenerator.pl";
127
128 # Get the template to use
129 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
130     {
131         template_name   => "barcodes/barcodes.tmpl",
132         type            => "intranet",
133         query           => $input,
134         authnotrequired => 0,
135         flagsrequired   => { tools => 1 },
136         debug           => 1,
137     }
138 );
139
140 # Replace the template values with the real ones
141 $template->param( SCRIPT_NAME   => $script_name );
142 $template->param( NUMBER_SYSTEM => $number_system );
143 $template->param( PAGES         => $labelConfig{'pageType'} );
144 $template->param( RANGE_TYPE    => $rangeType );
145 $template->param( LABEL_TABLE   => \@labelTable );
146 $template->param( COL_SPAN      => $labelConfig{'columns'} );
147 if ( $input->param('error') ) {
148     $template->param( ERROR => 1 );
149 }
150 else {
151     $template->param( ERROR => 0 );
152 }
153 $template->param(
154     intranetcolorstylesheet =>
155       C4::Context->preference("intranetcolorstylesheet"),
156     intranetstylesheet => C4::Context->preference("intranetstylesheet"),
157     IntranetNav        => C4::Context->preference("IntranetNav"),
158 );
159
160 # Shows the template with the real values replaced
161 output_html_with_http_headers $input, $cookie, $template->output;