Bug 3928: Modified date should follow syspref
[koha.git] / C4 / Barcodes / PrinterConfig.pm
1 package C4::Barcodes::PrinterConfig;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 #use warnings; FIXME - Bug 2505
20 use vars qw($VERSION @EXPORT);
21
22 use PDF::API2;
23 use PDF::API2::Page;
24
25 BEGIN {
26         # set the version for version checking
27         $VERSION = 0.02;
28         require Exporter;
29         @EXPORT = qw(&labelsPage &getLabelPosition setPositionsForX setPositionsForY);
30 }
31
32 =head1 NAME
33
34 C4::Barcodes::PrinterConfig - Koha module dealing with labels in a PDF.
35
36 =head1 SYNOPSIS
37
38         use C4::Barcodes::PrinterConfig;
39
40 =head1 DESCRIPTION
41
42 This package is used to deal with labels in a pdf file. Giving some parameters,
43 this package contains several functions to handle every label considering the 
44 environment of the pdf file.
45
46 =head1 FUNCTIONS
47
48 =over 2
49
50 =cut
51
52 my @positionsForX; # Takes all the X positions of the pdf file.
53 my @positionsForY; # Takes all the Y positions of the pdf file.
54 my $firstLabel = 1; # Test if the label passed as a parameter is the first label to be printed into the pdf file.
55
56 =item setPositionsForX
57
58         C4::Barcodes::PrinterConfig::setPositionsForX($marginLeft, $labelWidth, $columns, $pageType);
59
60 Calculate and stores all the X positions across the pdf page.
61
62 C<$marginLeft> Indicates how much left margin do you want in your page type.
63
64 C<$labelWidth> Indicates the width of the label that you are going to use.
65
66 C<$columns> Indicates how many columns do you want in your page type.
67
68 C<$pageType> Page type to print (eg: a4, legal, etc).
69
70 =cut
71 #'
72 sub setPositionsForX {
73         my ($marginLeft, $labelWidth, $columns, $pageType) = @_;
74         my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch
75         my $whereToStart = ($marginLeft + ($labelWidth/2));
76         my $firstLabel = $whereToStart*$defaultDpi;
77         my $spaceBetweenLabels = $labelWidth*$defaultDpi;
78         my @positions;
79         for (my $i = 0; $i < $columns ; $i++) {
80                 push @positions, ($firstLabel+($spaceBetweenLabels*$i));
81         }
82         @positionsForX = @positions;
83 }
84
85 =item setPositionsForY
86
87         C4::Barcodes::PrinterConfig::setPositionsForY($marginBottom, $labelHeigth, $rows, $pageType);
88
89 Calculate and stores all tha Y positions across the pdf page.
90
91 C<$marginBottom> Indicates how much bottom margin do you want in your page type.
92
93 C<$labelHeigth> Indicates the height of the label that you are going to use.
94
95 C<$rows> Indicates how many rows do you want in your page type.
96
97 C<$pageType> Page type to print (eg: a4, legal, etc).
98
99 =cut
100 #'
101 sub setPositionsForY {
102         my ($marginBottom, $labelHeigth, $rows, $pageType) = @_;
103         my $defaultDpi = 72/25.4; # By default we know 25.4 mm -> 1 inch -> 72 dots per inch
104         my $whereToStart = ($marginBottom + ($labelHeigth/2));
105         my $firstLabel = $whereToStart*$defaultDpi;
106         my $spaceBetweenLabels = $labelHeigth*$defaultDpi;
107         my @positions;
108         for (my $i = 0; $i < $rows; $i++) {
109                 unshift @positions, ($firstLabel+($spaceBetweenLabels*$i));
110         }
111         @positionsForY = @positions;
112 }
113
114 =item getLabelPosition
115
116         (my $x, my $y, $pdfObject, $pageObject, $gfxObject, $textObject, $coreObject, $labelPosition) = 
117                                         C4::Barcodes::PrinterConfig::getLabelPosition($labelPosition, 
118                                                                                                                                   $pdfObject, 
119                                                                                                                                   $page,
120                                                                                                                                   $gfx,
121                                                                                                                                   $text,
122                                                                                                                                   $fontObject,
123                                                                                                                                   $pageType);   
124
125 Return the (x,y) position of the label that you are going to print considering the environment.
126
127 C<$labelPosition> Indicates which label positions do you want to place by x and y coordinates.
128
129 C<$pdfObject> The PDF object in use.
130
131 C<$page> The page in use.
132
133 C<$gfx> The gfx resource to handle with barcodes objects.
134
135 C<$text> The text resource to handle with text.
136
137 C<$fontObject> The font object
138
139 C<$pageType> Page type to print (eg: a4, legal, etc).
140
141 =cut
142 #'
143 sub getLabelPosition {
144         my ($labelNum, $pdf, $page, $gfxObject, $textObject, $fontObject, $pageType) = @_;
145         my $indexX = $labelNum % @positionsForX;
146         my $indexY = int($labelNum / @positionsForX);
147         # Calculates the next label position and return that label number
148         my $nextIndexX = $labelNum % @positionsForX;
149         my $nextIndexY = $labelNum % @positionsForY;
150         if ($firstLabel) {
151           $page = $pdf->page;
152           $page->mediabox($pageType);
153           $gfxObject = $page->gfx;
154           $textObject = $page->text;
155           $textObject->font($fontObject, 7);
156                   $firstLabel = 0;
157         } elsif (($nextIndexX == 0) && ($nextIndexY == 0)) {
158           $page = $pdf->page;
159           $page->mediabox($pageType);
160           $gfxObject = $page->gfx;
161           $textObject = $page->text;
162           $textObject->font($fontObject, 7);
163         }
164         $labelNum = $labelNum + 1;      
165         if ($labelNum == (@positionsForX*@positionsForY)) {
166                 $labelNum = 0;
167         }
168         return ($positionsForX[$indexX], $positionsForY[$indexY], $pdf, $page, $gfxObject, $textObject, $fontObject, $labelNum);
169 }
170
171 =item labelsPage
172
173         my @labelTable = C4::Barcodes::PrinterConfig::labelsPage($rows, $columns);
174
175 This function will help you to build the labels panel, where you can choose
176 wich label position do you want to start the printer process.
177
178 C<$rows> Indicates how many rows do you want in your page type.
179
180 C<$columns> Indicates how many rows do you want in your page type.
181
182 =cut
183 #'
184 sub labelsPage{
185         my ($rows, $columns) = @_;
186         my @pageType;
187         my $tagname = 0;
188         my $labelname = 1;
189         my $check;
190         for (my $i = 1; $i <= $rows; $i++) {
191                 my @column;
192                 for (my $j = 1; $j <= $columns; $j++) {
193                         my %cell;
194                         if ($tagname == 0) {
195                                 $check = 'checked';
196                         } else {
197                                 $check = '';
198                         }               
199                         %cell = (check => $check,
200                                          tagname => $tagname,
201                                  labelname => $labelname);
202                         $tagname = $tagname + 1;        
203                         $labelname = $labelname + 1;    
204                         push @column, \%cell;
205                 }
206                 my %columns = (columns => \@column);
207                 push @pageType, \%columns;
208         }
209         return @pageType;
210 }
211
212 1;
213
214 __END__
215
216 =back
217
218 =head1 AUTHOR
219
220 Koha Physics Library UNLP <matias_veleda@hotmail.com>
221
222 =cut