-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_barcodes.cgi
More file actions
executable file
·340 lines (287 loc) · 8.65 KB
/
query_barcodes.cgi
File metadata and controls
executable file
·340 lines (287 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/perl
#query_barcodes.perl - created Fri Apr 1 11:25:43 2005
use strict; use warnings;
# CVS $Revision: 1.9 $ committed on $Date: 2006/09/22 13:21:18 $ by $Author: tbooth $
use barcodeUtil;
#use Data::Dumper;
our $q = bcgetqueryobj();
our $bc = $q->param('bc');
our %CONFIG = %{bcgetconfig()};
our $PAGE_TITLE = $CONFIG{PAGE_TITLE};
our $PAGE_DESC = $CONFIG{PAGE_DESC};
our $MASTER_SEARCH = $CONFIG{MASTER_SEARCH};
our $ENABLE_REPORTS = $barcodeUtil::ENABLE_REPORTS;
our $highestbc;
our $lowestbc;
our $database_label;
#Remove any hyphens etc from the thing.
our $querycode = bcdequote($bc);
my $main = sub
{
print bcheader();
# $q->delete('bc');
print bcstarthtml("$PAGE_TITLE - Quick Query"),
bcnavbanner(),
bch1(($PAGE_DESC ? "$PAGE_DESC - " : "") . "Query the barcode database"),
$q->h2("Enter a barcode to see details"),
$q->start_form(-name=>"queryform", -method=>"GET"),
$q->hidden(-name=>"username"), #See note below
$q->table( {-class=>"formtable"},
$q->Tr($q->td(
[$q->textfield("bc"), $q->submit( -value=>"Query" )]
))
),
$q->end_form();
#Note - the user name field is just here so that if the user does some admin, then some queries, then
#goes back to the admin tab, the username field has been preserved. It has no relevance to the query.
#Now the input field should be focussed ready for the user to scan a code
print "<script type='text/javascript'>
var bcbox = document.queryform.bc;
bcbox.focus();
bcbox.select();
</script>";
if(defined $bc)
{
print $q->hr(),
$q->h4($q->escapeHTML("Results of your search on $bc :"));
if(! $querycode)
{
print $q->p("You need to enter a valid numeric barcode to search on.");
}
#I don't support MASTER_SEARCH here any more - see public_query.cgi for the new version.
# elsif($MASTER_SEARCH)
# {
# runquery_master($bc);
# }
else
{
#Connect with DB params found in config
barcodeUtil::connectnow();
bcgetdbobj()->autocommit(1);
#Find out highest and lowest barcodes.
$highestbc = bcgethighestbc();
$lowestbc = bcdequote($CONFIG{MIN_BAR_CODE}) || 1;
runquery($bc);
bcdisconnect();
}
print $q->end_div(), bcfooter();
}
else
{
if($ENABLE_REPORTS)
{
print $q->p('This is the quick query page. Use the <a href="report_barcodes.cgi">report maker</a>
to see the full selection of available reports.');
}
print $q->end_html();
}
};
sub codetolink
{
my $code = bcquote(shift());
$q->a({-href=>$q->url(-relative=>1)."?bc=$code"}, $code);
}
sub usertolink
{
return @_ if !$ENABLE_REPORTS;
my $user = shift;
#If the reportmaker is enabled, link to the 'Show Users' query
my $link = 'report_barcodes.cgi?queryname=Show+Users;qp_USERNAME='
. CGI::escape($user)
. ';rm=results#results';
$q->a({-href => $link}, $user);
}
sub typetolink
{
my $type = shift;
#Use the type summariser, as linked in the Show Item Types report
my $link = 'request_barcodes.cgi?typespopup=1#'
. CGI::escape($type);
$q->a({-href => $link}, bczapunderscores($type));
}
sub blocktolink
{
my ($fromcode, $tocode) = @_;
my $label = bcquote($fromcode) ." : ". bcquote($tocode);
if(!$ENABLE_REPORTS)
{
return $label;
}
my $link = "report_barcodes.cgi?queryname=Data+Summary;qp_FROMCODE="
. "$fromcode;qp_TOCODE=$tocode;rm=results#results";
$q->a({-href => $link}, $label);
}
sub runquery
{
eval{
bcgetinfofornumber($querycode);
};
if( $@ )
{
#Not found - print an error and maybe say what the highest barcode is.
print $q->p("This barcode has not been allocated.");
if( $querycode > $highestbc )
{
print $q->p("The highest allocated barcode in the database is " .
($highestbc ? bcquote($highestbc) : "[nothing allocated]") . "." );
}
elsif( $querycode < $lowestbc )
{
print $q->p("The lowest possible barcode in this database is " .
bcquote($lowestbc) );
}
}
else
{
reportoncode($querycode);
}
}
=old_cruft
sub runquery_master
{
#Hmm, complexities.
#Run through all databases each time so we can carp about a bad config.
#But the target db will be the first one to potentially contain the
#code. Overlaps in the ranges will not be spotted here.
my $nn;
my $targetdb;
my %configclone = %CONFIG;
for($nn = 1; $nn <= $MASTER_SEARCH; $nn++)
{
my $lowcode = bcdequote($CONFIG{"LOW_CODE_$nn"});
my $highcode = bcdequote($CONFIG{"HIGH_CODE_$nn"});
defined($lowcode) && defined($highcode) && $highcode > $lowcode or
die "Error in the config file for database $nn in the master search.";
if($querycode >= $lowcode && $querycode <= $highcode)
{
$targetdb ||= $nn;
}
}
if(!$targetdb)
{
print $q->p("This barcode is out of range. There are $MASTER_SEARCH databases registered
in the configuration file but none should contain this code.");
}
else
{
#Fix up the connection params
for(qw{DATABASE_HOST DATABASE_USER DATABASE_NAME DATABASE_PASS DATA_SCHEMA PREFIX_LENGTH
POSTFIX_LENGTH SPACER_CHAR})
{
my $override = $CONFIG{"${_}_$targetdb"};
if($override)
{
$configclone{$_} = $override;
}
}
barcodeUtil::connectnow(\%configclone);
bcgetdbobj()->autocommit(1);
$database_label = $CONFIG{"LABEL_$targetdb"};
$highestbc = bcgethighestbc();
$lowestbc = bcdequote($CONFIG{"LOW_CODE_$targetdb"}) || 1;
runquery();
bcdisconnect();
}
}
=cut
sub reportoncode
{
my $bc = shift();
my( $username, $typename, $datestamp, $comments, $fromcode, $tocode)
= bcgetinfofornumber($bc);
#When this is called we know we have a valid allocated bc, but it may or may not have
#info associated with it.
my $table = bctypetotable($typename);
my $sth = bcprepare("
SELECT * FROM $table WHERE
barcode = ?
");
$sth->execute($bc);
my $bcinfo = $sth->fetchrow_hashref();
my $bcfields = $sth->{NAME_lc};
#Find anything in the links table
my @childcodes;
eval{
$sth = bcprepare("
SELECT childtype, childcode from barcode_link_index WHERE
parentcode = ?
ORDER BY childtype, childcode
");
$sth->execute($bc);
@childcodes = @{$sth->fetchall_arrayref()};
};
#See if this code was disposed, and if so print a warning.
my ($dispdate, $dispcomments) = bcdisposedateandcomments($bc);
if($dispdate)
{
print $q->p({-class=>'alertbox'},
"This barcode has been marked <b>disposed</b> as of $dispdate" .
($dispcomments ? " with the comments:\n<br />$dispcomments<br />" : ". ") .
"The item no longer exists.");
}
print $q->start_table({-class=>"neat1"}),
$q->Tr( $q->td( [ '<b>Barcode</b>' => codetolink($bc) ]) ),
$database_label ? $q->Tr( $q->td( [ '<b>In database</b>' => $database_label ]) ) : "",
$q->Tr( $q->td( [ '<b>Owned by</b>' => usertolink($username) ]) ),
$q->Tr( $q->td( [ '<b>Type</b>' => typetolink($typename) ]) ),
$q->Tr( $q->td( [ '<b>Part of block</b>' => blocktolink($fromcode, $tocode) ]) ),
$q->Tr( $q->td( [ '<b>Allocated on</b>' => $datestamp ]) );
print $q->Tr( $q->td( [ '<b>Range comment</b>' => $comments ]) ) if $comments;
if( !$bcinfo )
{
print $q->Tr( $q->td({ -colspan=>2 },
$q->i("No data about this item has been submitted yet") ) );
}
else
{
my $printrow = sub {
my $name = shift();
my $flags = shift();
my $val;
if(!defined($bcinfo->{$name}))
{
$val = $q->i('null');
}
elsif($flags->{bc})
{
#Try to discover what type of thing the code links to
my ( undef, $linked_code_type ) = eval{ bcgetinfofornumber($bcinfo->{$name}) };
$val = codetolink($bcinfo->{$name});
$val .= ' (' . bczapunderscores($linked_code_type) . ')' if $linked_code_type;
}
else
{
$val = $q->escapeHTML($bcinfo->{$name});
}
print $q->Tr( $q->td( [ bczapunderscores($name) => $val] ) );
};
my @demoted;
for(@$bcfields)
{
next if $_ eq 'barcode';
#Now deal with barcode links. I was going to have some cunning way to guess when
#a number was a code, but instead just use the descriptions table to make it explicit
my $flags = bcgetflagsforfield($typename, $_);
if($flags->{demote})
{
unshift @demoted, [$_, $flags];
}
else
{
&$printrow($_, $flags);
}
}
&$printrow(@$_) for @demoted;
}
#Finally list any links to child codes
if(@childcodes)
{
print $q->Tr( $q->td( [ "<b>Derived items</b>" =>
join( $q->br, map { codetolink($_->[1]) . ' (' .
bczapunderscores($_->[0]) . ')'
} @childcodes )
] ) );
}
print $q->end_table();
}
&$main();