line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
############################################################################## |
2
|
|
|
|
|
|
|
# DBI web interface Version 0.20 # |
3
|
|
|
|
|
|
|
# Copyright 1999-2000 James Furness furn@base6.com # |
4
|
|
|
|
|
|
|
# Created 01/05/99 Last Modified 13/05/00 # |
5
|
|
|
|
|
|
|
############################################################################## |
6
|
|
|
|
|
|
|
# COPYRIGHT NOTICE # |
7
|
|
|
|
|
|
|
# Copyright 1999-2000 James Furness . All Rights Reserved. # |
8
|
|
|
|
|
|
|
# # |
9
|
|
|
|
|
|
|
# This module is free software; it may be used freely and redistributed # |
10
|
|
|
|
|
|
|
# for free providing this copyright header remains part of the module. You # |
11
|
|
|
|
|
|
|
# may not charge for the redistribution of this module. Selling this code # |
12
|
|
|
|
|
|
|
# without James Furness' written permission is expressly forbidden. # |
13
|
|
|
|
|
|
|
# # |
14
|
|
|
|
|
|
|
# This module may not be modified without first notifying James Furness # |
15
|
|
|
|
|
|
|
# (This is to enable me to track modifications). In all # |
16
|
|
|
|
|
|
|
# cases this copyright header should remain fully intact in all # |
17
|
|
|
|
|
|
|
# modifications. # |
18
|
|
|
|
|
|
|
# # |
19
|
|
|
|
|
|
|
# This code is provided on an "As Is" basis, without warranty, expressed or # |
20
|
|
|
|
|
|
|
# implied. The author disclaims all warranties with regard to this software, # |
21
|
|
|
|
|
|
|
# including all implied warranties of merchantability and fitness, in no # |
22
|
|
|
|
|
|
|
# event shall the author, James Furness be liable for any special, indirect # |
23
|
|
|
|
|
|
|
# or consequential damages or any damages whatsoever including but not # |
24
|
|
|
|
|
|
|
# limited to loss of use, data or profits. By using this module you agree to # |
25
|
|
|
|
|
|
|
# indemnify James Furness from any liability that might arise from it's use. # |
26
|
|
|
|
|
|
|
# Should this code prove defective, you assume the cost of any and all # |
27
|
|
|
|
|
|
|
# necessary repairs, servicing, correction and any other costs arising # |
28
|
|
|
|
|
|
|
# directly or indrectly from it's use. # |
29
|
|
|
|
|
|
|
# # |
30
|
|
|
|
|
|
|
# This copyright notice must remain fully intact at all times. # |
31
|
|
|
|
|
|
|
# Use of this program or its output constitutes acceptance of these terms. # |
32
|
|
|
|
|
|
|
# # |
33
|
|
|
|
|
|
|
# Parts of this module are based upon mysql-lib.pl by Ron Crisco. # |
34
|
|
|
|
|
|
|
############################################################################## |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
package DBIx::glueHTML; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=pod |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 NAME |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
DBIx::glueHTML - Class for creating a CGI interface to a database |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 SYNOPSIS |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
use CGI; |
47
|
|
|
|
|
|
|
use DBI; |
48
|
|
|
|
|
|
|
use DBIx::glueHTML; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$cgi = new CGI; |
51
|
|
|
|
|
|
|
$dbh = DBI->connect("DBI:mysql:[DATABASE]:[HOSTNAME]","[USERNAME]","[PASSWORD]") ); |
52
|
|
|
|
|
|
|
$DBinterface = new DBIx::glueHTML ($cgi, $dbh, "[INFOTABLE NAME]"); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Below here is only executed if a glueHTML action was not taken, so print a menu |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
print $cgi->header; |
57
|
|
|
|
|
|
|
print "Add\n "; |
58
|
|
|
|
|
|
|
print "Search\n"; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 DESCRIPTION |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
The C class allows a CGI interface to a database. |
63
|
|
|
|
|
|
|
It enables a CGI interface to a database to be created, supporting record addition, |
64
|
|
|
|
|
|
|
modification, deletion and searching. It provides a user friendly interface with |
65
|
|
|
|
|
|
|
descriptions of fields provided. The field descriptions along with information on |
66
|
|
|
|
|
|
|
whether the field is visible, hidden or excluded are extracted from a table, allowing |
67
|
|
|
|
|
|
|
easy modification and addition of fields and tables without having to edit code. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 Features |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=over 4 |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item Simple database administration |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Forms are created automatically on demand, SQL statements are generated as needed and processed. |
76
|
|
|
|
|
|
|
The module contains enough autonomy to potentially run with only wrapper perl code placed around |
77
|
|
|
|
|
|
|
it. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=item Full form configuration |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Forms can be modified to add descriptions and extra information to fields, making it easy to change |
82
|
|
|
|
|
|
|
output without having to edit code. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=item Control |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Extensive callback procedures and configuration options allow output, password protection and logging |
87
|
|
|
|
|
|
|
to be configured as desired. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=item Full HTML customisation |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
HTML output and table formats can be customised easily by the user. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=back |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
$| = 1; # Flush all buffers |
98
|
|
|
|
|
|
|
require 5.004; # Require at least perl 5.004 |
99
|
|
|
|
|
|
|
|
100
|
1
|
|
|
1
|
|
10060
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
101
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
51
|
|
102
|
1
|
|
|
1
|
|
2022
|
use CGI; |
|
1
|
|
|
|
|
16798
|
|
|
1
|
|
|
|
|
7
|
|
103
|
1
|
|
|
1
|
|
1308
|
use DBI; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
$VERSION = '0.20'; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
108
|
|
|
|
|
|
|
# Class constructors/destructors |
109
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
110
|
|
|
|
|
|
|
=pod |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 METHODS |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 Main Methods |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=over 4 |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item B<$DBinterface = new DBIx::glueHTML (>I I I I<[Suppress paramcheck]>B<);> |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Constructs a new C object. You must pass a reference |
121
|
|
|
|
|
|
|
to a CGI object which will be used to get the script's parameters and a |
122
|
|
|
|
|
|
|
database handle (Returned from a Cconnect> function) which will |
123
|
|
|
|
|
|
|
be used to communicate with the database. The third parameter defines the |
124
|
|
|
|
|
|
|
name of the I which is used to determine hidden/excluded fields, |
125
|
|
|
|
|
|
|
field names and descriptions as described below in B. After |
126
|
|
|
|
|
|
|
initialisation, the CGI object is checked for a 'glueHTML-action' parameter. |
127
|
|
|
|
|
|
|
If this is present, control is taken from the script and the specified action |
128
|
|
|
|
|
|
|
is performed on the specified table. This parameter is set when an action which |
129
|
|
|
|
|
|
|
requires further processing is in progress. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
The final parameter, suppress paramcheck, is optional and when set to 1 will |
132
|
|
|
|
|
|
|
cause the script NOT to perform the parameter check. You MUST then call the |
133
|
|
|
|
|
|
|
check_params function in your code or forms will not work. Overriding |
134
|
|
|
|
|
|
|
the script in this way is not recommended unless necessary for error handler |
135
|
|
|
|
|
|
|
or security check handler setting. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
sub new |
139
|
|
|
|
|
|
|
{ |
140
|
|
|
|
|
|
|
my $proto = shift; |
141
|
|
|
|
|
|
|
my $class = ref($proto) || $proto; |
142
|
|
|
|
|
|
|
my $cgipkg = shift; |
143
|
|
|
|
|
|
|
my $dbihdl = shift; |
144
|
|
|
|
|
|
|
my $infotbl = shift; |
145
|
|
|
|
|
|
|
my $suppresscheck = shift || 0; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
my $self = bless {}, $class; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
$self->{CGI} = $cgipkg; # CGI package |
150
|
|
|
|
|
|
|
$self->{DBH} = $dbihdl; # DBI database handle |
151
|
|
|
|
|
|
|
$self->{ITABLE} = $infotbl; # Info Table name |
152
|
|
|
|
|
|
|
$self->{ERRHDL} = undef; # Error handler |
153
|
|
|
|
|
|
|
$self->{LOGFILE} = undef; # Log file |
154
|
|
|
|
|
|
|
$self->{LOGCALLBACK} = undef; # Logging callback function |
155
|
|
|
|
|
|
|
$self->{PRINTHEADER} = undef; # HTML header |
156
|
|
|
|
|
|
|
$self->{STARTTABLE} = undef; # HTML format |
157
|
|
|
|
|
|
|
$self->{STARTTABLEROW} = undef; # HTML format |
158
|
|
|
|
|
|
|
$self->{PRINTTABLECELL} = undef; # HTML format |
159
|
|
|
|
|
|
|
$self->{PRINTTABLEHEADERCELL}= undef; # HTML format |
160
|
|
|
|
|
|
|
$self->{PRINTENDTABLEROW} = undef; # HTML format |
161
|
|
|
|
|
|
|
$self->{PRINTEDITTABLEROW} = undef; # HTML format |
162
|
|
|
|
|
|
|
$self->{ENDTABLE} = undef; # HTML format |
163
|
|
|
|
|
|
|
$self->{PRINTFOOTER} = undef; # HTML footer |
164
|
|
|
|
|
|
|
$self->{PRINTCONTENTTYPE} = 1; # Print content type or let printheader() handle things |
165
|
|
|
|
|
|
|
$self->{USEGMTTIME} = 1; # Use GMT time or local time |
166
|
|
|
|
|
|
|
$self->{TIMEMOD} = 0; # Add or subtract time |
167
|
|
|
|
|
|
|
$self->{ACCESSCALLBACK} = undef; # Security check callback |
168
|
|
|
|
|
|
|
$self->{FORMFIELDCALLBACK} = undef; # Form field change/hide callback |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
if ($suppresscheck != 1) { |
171
|
|
|
|
|
|
|
$self->check_params; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
return $self; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub DESTROY { } |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
########################################################################## |
180
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
181
|
|
|
|
|
|
|
# User-called functions |
182
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=pod |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=back |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 Optional Methods |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Optional methods which can be called to directly jump to a script function, |
191
|
|
|
|
|
|
|
for example to directly initiate a delete or modify on a record. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=over 4 |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=cut |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item B B<();> |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
# Check form parameters |
200
|
|
|
|
|
|
|
$DBinterface->check_params; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Causes the glueHTML-action parameter to be rechecked. If it contains |
203
|
|
|
|
|
|
|
the value 'add','modify','delete' or 'search', the respective function |
204
|
|
|
|
|
|
|
will be called ('exec_add','exec_modify','exec_delete' or 'exec_search'). |
205
|
|
|
|
|
|
|
this function is essential to the correct functioning of the interfaces |
206
|
|
|
|
|
|
|
with two and three part forms, and is called automatically when a |
207
|
|
|
|
|
|
|
glueHTML object is created, unless the 'suppress paramcheck' parameter |
208
|
|
|
|
|
|
|
is set to 1. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub check_params { |
213
|
|
|
|
|
|
|
my $self = shift; |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
if ($self->{CGI}->param("glueHTML-action") eq "add") { |
216
|
|
|
|
|
|
|
$self->exec_add; |
217
|
|
|
|
|
|
|
exit; |
218
|
|
|
|
|
|
|
} elsif ($self->{CGI}->param("glueHTML-action") eq "modify") { |
219
|
|
|
|
|
|
|
$self->exec_modify; |
220
|
|
|
|
|
|
|
exit; |
221
|
|
|
|
|
|
|
} elsif ($self->{CGI}->param("glueHTML-action") eq "delete") { |
222
|
|
|
|
|
|
|
$self->exec_delete; |
223
|
|
|
|
|
|
|
exit; |
224
|
|
|
|
|
|
|
} elsif ($self->{CGI}->param("glueHTML-action") eq "search") { |
225
|
|
|
|
|
|
|
$self->exec_search; |
226
|
|
|
|
|
|
|
exit; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=item B B<();> |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
# Now set the 'glueHTML-table' parameter so the script knows |
233
|
|
|
|
|
|
|
# what table to deal with |
234
|
|
|
|
|
|
|
$cgi->param(-name=>'glueHTML-table',-value=>'mytable'); |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
# Now call the function |
237
|
|
|
|
|
|
|
$DBinterface->exec_search; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Searches the table named in the CGI parameter 'glueHTML-table'. |
240
|
|
|
|
|
|
|
The user will be presented with a blank form with the fields of the table. |
241
|
|
|
|
|
|
|
They press submit to search the table (Wildcards can be used). They are then |
242
|
|
|
|
|
|
|
returned a table with a modify and delete button and the fields for each |
243
|
|
|
|
|
|
|
record found. |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=cut |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
sub exec_search { |
248
|
|
|
|
|
|
|
my $self = shift; |
249
|
|
|
|
|
|
|
my $table = $self->{CGI}->param("glueHTML-table"); |
250
|
|
|
|
|
|
|
my ($tablename, $name, $label, $lookup, $extrahash, $hidden, $exclude, |
251
|
|
|
|
|
|
|
$additionalwhere) = $self->_getTableInfoHash($table); |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
# Check access privs |
254
|
|
|
|
|
|
|
$self->_checkAccess; |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
if ($self->{CGI}->param('post')) { |
257
|
|
|
|
|
|
|
my ($i, $j, %types, %params, $pri, $cursor, $sql, @row, $val, $numcols, |
258
|
|
|
|
|
|
|
@fielddesc, @fieldtypes, @primary_keys, $content); |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
$self->_printHeader("Search Results", ""); |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
$numcols = 0; |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
# Now look up primary key fields and field types... |
265
|
|
|
|
|
|
|
my ($desc_cursor) = $self->_execSql ("describe $table"); |
266
|
|
|
|
|
|
|
while (@fielddesc = $desc_cursor->fetchrow) { |
267
|
|
|
|
|
|
|
$numcols++; |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
# Stuff the paramaters into a hash before we delete them |
270
|
|
|
|
|
|
|
$params{$fielddesc[0]} = $self->{CGI}->param($fielddesc[0]); |
271
|
|
|
|
|
|
|
$types{$fielddesc[0]} = $fielddesc[1]; |
272
|
|
|
|
|
|
|
if ($fielddesc[3] eq "PRI") { |
273
|
|
|
|
|
|
|
push @primary_keys, $fielddesc[0]; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
$desc_cursor->finish; |
277
|
|
|
|
|
|
|
$numcols += 2; # Add Modify and Delete cols |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
# now we execute the SQL, and return a list of matches |
280
|
|
|
|
|
|
|
$cursor = $self->_execSql($self->_selectSql($table, $additionalwhere)); |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
# delete the current params so they don't get incorporated in the forms |
283
|
|
|
|
|
|
|
$self->{CGI}->delete_all; |
284
|
|
|
|
|
|
|
$self->_startTable($numcols, "Search Results"); |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
# now print header row |
287
|
|
|
|
|
|
|
$self->_printStartTableRow(); |
288
|
|
|
|
|
|
|
$self->_printTableHeaderCell("Modify"); |
289
|
|
|
|
|
|
|
$self->_printTableHeaderCell("Delete"); |
290
|
|
|
|
|
|
|
for ($i=0; $i < $cursor->{NUM_OF_FIELDS}; $i++) { |
291
|
|
|
|
|
|
|
$self->_printTableHeaderCell("$cursor->{NAME}->[$i]"); |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
$self->_printEndTableRow(); |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
while (@row = $cursor->fetchrow_array) { |
296
|
|
|
|
|
|
|
$self->_printStartTableRow(); |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
# now print the Modify Form |
299
|
|
|
|
|
|
|
print $self->{CGI}->startform; |
300
|
|
|
|
|
|
|
$content = ""; |
301
|
|
|
|
|
|
|
# Print the primary keys |
302
|
|
|
|
|
|
|
for ($i=0; $i < $cursor->{NUM_OF_FIELDS}; $i++) { |
303
|
|
|
|
|
|
|
foreach $pri (@primary_keys) { |
304
|
|
|
|
|
|
|
if ($pri eq $cursor->{NAME}->[$i]) { |
305
|
|
|
|
|
|
|
print "{NAME}->[$i]\" VALUE=\"$row[$i]\">"; |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
# Print state tracking elements |
310
|
|
|
|
|
|
|
print $self->{CGI}->hidden(-name => 'glueHTML-action', value => 'modify'); |
311
|
|
|
|
|
|
|
print $self->{CGI}->hidden(-name => 'glueHTML-table', value => $table); |
312
|
|
|
|
|
|
|
$self->_printHidden; # Print any hidden elements necessary |
313
|
|
|
|
|
|
|
$self->_printTableCell ($self->{CGI}->submit('Modify')); |
314
|
|
|
|
|
|
|
print $self->{CGI}->endform; |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
# now print the Delete Form |
317
|
|
|
|
|
|
|
print $self->{CGI}->startform; |
318
|
|
|
|
|
|
|
$content = ""; |
319
|
|
|
|
|
|
|
# Print the primary keys |
320
|
|
|
|
|
|
|
for ($i=0; $i < $cursor->{NUM_OF_FIELDS}; $i++) { |
321
|
|
|
|
|
|
|
foreach $pri (@primary_keys) { |
322
|
|
|
|
|
|
|
if ($pri eq $cursor->{NAME}->[$i]) { |
323
|
|
|
|
|
|
|
print "{NAME}->[$i]\" VALUE=\"$row[$i]\">"; |
324
|
|
|
|
|
|
|
} |
325
|
|
|
|
|
|
|
} |
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
# Print state tracking elements |
328
|
|
|
|
|
|
|
print $self->{CGI}->hidden(-name => 'glueHTML-action', value => 'delete'); |
329
|
|
|
|
|
|
|
print $self->{CGI}->hidden(-name => 'glueHTML-table', value => $table); |
330
|
|
|
|
|
|
|
$self->_printHidden; # Print any hidden elements necessary |
331
|
|
|
|
|
|
|
$self->_printTableCell ($self->{CGI}->submit('Delete')); |
332
|
|
|
|
|
|
|
print $self->{CGI}->endform; |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
# now print the fields |
335
|
|
|
|
|
|
|
for ($i=0; $i < $cursor->{NUM_OF_FIELDS}; $i++) { |
336
|
|
|
|
|
|
|
my $pos = 0; |
337
|
|
|
|
|
|
|
$val = $row[$i]; |
338
|
|
|
|
|
|
|
$val =~ s/&/&/g; |
339
|
|
|
|
|
|
|
$val =~ s/</g; |
340
|
|
|
|
|
|
|
$val =~ s/>/>/g; |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
# Don't print the whole of the text fields |
343
|
|
|
|
|
|
|
if ($types{$cursor->{NAME}->[$i]} =~ "text") { |
344
|
|
|
|
|
|
|
my ($search) = ""; |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
if ($search = $params{$cursor->{NAME}->[$i]}) { |
347
|
|
|
|
|
|
|
$search =~ s/&/&/g; |
348
|
|
|
|
|
|
|
$search =~ s/</g; |
349
|
|
|
|
|
|
|
$search =~ s/>/>/g; |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
# Make wildcards work in highlight |
352
|
|
|
|
|
|
|
$search =~ s/_/(.)/g; |
353
|
|
|
|
|
|
|
$search =~ s/%/(.*)/g; |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
# This chunk borrowed from plan_search.pl by Richard Smith :p |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
# Find our search string in the field |
358
|
|
|
|
|
|
|
$pos = index(lc($val), lc($search)); |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
# Grab the string for 100 characters before it |
361
|
|
|
|
|
|
|
$pos = $pos - 100; |
362
|
|
|
|
|
|
|
if ($pos < 0) { |
363
|
|
|
|
|
|
|
$pos = 0; |
364
|
|
|
|
|
|
|
} |
365
|
|
|
|
|
|
|
} |
366
|
|
|
|
|
|
|
my ($subtext) = substr($val, $pos, 300); |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
# Change the search string to bold in the part of the string we're showing |
369
|
|
|
|
|
|
|
if ($search ne "") { $subtext =~ s/($search)/$1<\/b>/gi; } |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
if (length($val) > 300) { # Show truncation marks if too long |
372
|
|
|
|
|
|
|
if ($pos < 1) { |
373
|
|
|
|
|
|
|
$val = $subtext . "..."; |
374
|
|
|
|
|
|
|
} else { |
375
|
|
|
|
|
|
|
$val = "..." . $subtext . "..."; |
376
|
|
|
|
|
|
|
} |
377
|
|
|
|
|
|
|
} else { |
378
|
|
|
|
|
|
|
$val = $subtext; |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
$self->_printTableCell ("$val "); |
382
|
|
|
|
|
|
|
} |
383
|
|
|
|
|
|
|
$self->_printEndTableRow(); |
384
|
|
|
|
|
|
|
} |
385
|
|
|
|
|
|
|
$self->_endTable(); |
386
|
|
|
|
|
|
|
$self->_printFooter; |
387
|
|
|
|
|
|
|
exit; |
388
|
|
|
|
|
|
|
} else { |
389
|
|
|
|
|
|
|
# give them the form |
390
|
|
|
|
|
|
|
$self->_form($table,"search","Search $tablename","Search $tablename","nodefaults",""); |
391
|
|
|
|
|
|
|
exit; |
392
|
|
|
|
|
|
|
} |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
=item B B<();> |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
# Assume $cgi->param has been set to indicate the primary keys |
398
|
|
|
|
|
|
|
# for the table being modified, i.e 'Primary Key Name' = 'Primary |
399
|
|
|
|
|
|
|
# Key Value' |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
# Now set the 'glueHTML-table' parameter so the script knows |
402
|
|
|
|
|
|
|
# what table to deal with |
403
|
|
|
|
|
|
|
$cgi->param(-name=>'glueHTML-table',-value=>'mytable'); |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
# Now call the function |
406
|
|
|
|
|
|
|
$DBinterface->exec_modify; |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
Modifies a record from the table named in the CGI parameter 'glueHTML-table' |
409
|
|
|
|
|
|
|
where the CGI parameters which have the same name as a table column. For example |
410
|
|
|
|
|
|
|
for a table called 'data' with an 'ID' column containing the primary keys for |
411
|
|
|
|
|
|
|
that table, set the 'glueHTML-table' parameter to 'data' and set the 'ID' |
412
|
|
|
|
|
|
|
parameter to the ID number of the record you want to modify. The user will then |
413
|
|
|
|
|
|
|
be presented with a form containing the data in the table for them to modify. |
414
|
|
|
|
|
|
|
They then press submit to commit the data |
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
=cut |
417
|
|
|
|
|
|
|
sub exec_modify { |
418
|
|
|
|
|
|
|
my $self = shift; |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
# Check access privs |
421
|
|
|
|
|
|
|
$self->_checkAccess; |
422
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
# Execute the modify if the user already has the form else give the user the form |
424
|
|
|
|
|
|
|
if ($self->{CGI}->param('post')) { |
425
|
|
|
|
|
|
|
$self->_modifyRecord($self->{CGI}->param("glueHTML-table")); |
426
|
|
|
|
|
|
|
} else { |
427
|
|
|
|
|
|
|
$self->_form($self->{CGI}->param("glueHTML-table"),"modify","Modify Record","Modify Record","","fill_from_table"); |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
} |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
=item B B<();> |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
# Now set the 'glueHTML-table' parameter so the script knows |
434
|
|
|
|
|
|
|
# what table to deal with |
435
|
|
|
|
|
|
|
$cgi->param(-name=>'glueHTML-table',-value=>'mytable'); |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
# Now call the function |
438
|
|
|
|
|
|
|
$DBinterface->exec_add; |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
Adds a record to the table named in the CGI parameter 'glueHTML-table'. |
441
|
|
|
|
|
|
|
The user will be presented with a empty form containing just the defaults for |
442
|
|
|
|
|
|
|
the values of that table (Defined in the SQL). They then press submit to commit |
443
|
|
|
|
|
|
|
the data to the table. |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
=cut |
446
|
|
|
|
|
|
|
sub exec_add { |
447
|
|
|
|
|
|
|
my $self = shift; |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
# Check access privs |
450
|
|
|
|
|
|
|
$self->_checkAccess; |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
if ($self->{CGI}->param('post')) { |
453
|
|
|
|
|
|
|
$self->_insertRecord($self->{CGI}->param("glueHTML-table")); |
454
|
|
|
|
|
|
|
} else { |
455
|
|
|
|
|
|
|
$self->_form($self->{CGI}->param("glueHTML-table"),"add","Add Record","Add Record","",""); |
456
|
|
|
|
|
|
|
} |
457
|
|
|
|
|
|
|
} |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
=item B B<();> |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
# Assume $cgi->param has been set to indicate the primary keys |
462
|
|
|
|
|
|
|
# for the table being modified, i.e 'Primary Key Name' = 'Primary |
463
|
|
|
|
|
|
|
# Key Value' |
464
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
# Now set the 'glueHTML-table' parameter so the script knows |
466
|
|
|
|
|
|
|
# what table to deal with |
467
|
|
|
|
|
|
|
$cgi->param(-name=>'glueHTML-table',-value=>'mytable'); |
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
# Now call the function |
470
|
|
|
|
|
|
|
$DBinterface->exec_delete; |
471
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
Deletes a record from the table named in the CGI parameter 'glueHTML-table' |
473
|
|
|
|
|
|
|
where the CGI parameters which have the same name as a table column. For example |
474
|
|
|
|
|
|
|
for a table called 'data' with an 'ID' column containing the primary keys for |
475
|
|
|
|
|
|
|
that table, set the 'glueHTML-table' parameter to 'data' and set the 'ID' |
476
|
|
|
|
|
|
|
parameter to the ID number of the record you want to delete. |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
This function will output a confirmation page requiring users to confirm the delete |
479
|
|
|
|
|
|
|
or press their browser's back button to cancel. To skip confirmation, set the 'confirm' |
480
|
|
|
|
|
|
|
parameter to 'Y'. |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
=cut |
483
|
|
|
|
|
|
|
sub exec_delete { |
484
|
|
|
|
|
|
|
my $self = shift; |
485
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
# Check access privs |
487
|
|
|
|
|
|
|
$self->_checkAccess; |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
# Delete the record |
490
|
|
|
|
|
|
|
$self->_deleteRecord($self->{CGI}->param("glueHTML-table")); |
491
|
|
|
|
|
|
|
} |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
494
|
|
|
|
|
|
|
# General support functions |
495
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
496
|
|
|
|
|
|
|
=pod |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
=back |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
=head2 Optional Customisation Methods |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
Optional methods which can be called to alter the behaviour of the script |
503
|
|
|
|
|
|
|
or enable features such as logging. |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
=over 4 |
506
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
=cut |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
=item B B<(>IB<);> |
510
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
sub log_callback { |
512
|
|
|
|
|
|
|
my $description = shift; |
513
|
|
|
|
|
|
|
my $sql = shift; |
514
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
open (LOG,">>$logfile") |
516
|
|
|
|
|
|
|
print LOG "$description (Executing $sql)"; |
517
|
|
|
|
|
|
|
close(LOG); |
518
|
|
|
|
|
|
|
} |
519
|
|
|
|
|
|
|
$DBinterface = new DBIx::glueHTML ($cgi, $dbh, $table, 1); |
520
|
|
|
|
|
|
|
$DBinterface->set_logcallback(\&log_callback); |
521
|
|
|
|
|
|
|
$DBinterface->check_params(); |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
Enables logging of SQL changes to the database via the user |
524
|
|
|
|
|
|
|
defined routine. The first parameter passed is a description, |
525
|
|
|
|
|
|
|
such as 'Record added to mytable' and the second parameter is |
526
|
|
|
|
|
|
|
the SQL statement which was used. |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
NOTE: check_params() MUST be called or glueHTML will not function correctly. |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
=cut |
531
|
|
|
|
|
|
|
sub set_logcallback { |
532
|
|
|
|
|
|
|
# $self &callback; |
533
|
|
|
|
|
|
|
$_[0]->{LOGCALLBACK} = $_[1]; |
534
|
|
|
|
|
|
|
} |
535
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
=item B B<(>IB<);> |
537
|
|
|
|
|
|
|
$DBinterface = new DBIx::glueHTML ($cgi, $dbh, $table, 1); |
538
|
|
|
|
|
|
|
$DBinterface->set_logfile("/usr/local/logs/mydb-log"); |
539
|
|
|
|
|
|
|
$DBinterface->check_params(); |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
Enables logging of SQL changes to the database automatically |
542
|
|
|
|
|
|
|
without providing a callback. The script will open the file |
543
|
|
|
|
|
|
|
specified, with no locking (Althoughthis might be added in |
544
|
|
|
|
|
|
|
future). The file must be writeable to the CGI, on UNIX you |
545
|
|
|
|
|
|
|
normally need to I. However this may |
546
|
|
|
|
|
|
|
differ depending on your system and what operating system |
547
|
|
|
|
|
|
|
you have. |
548
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
NOTE: check_params() MUST be called or glueHTML will not function correctly. |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
=cut |
552
|
|
|
|
|
|
|
sub set_logfile { |
553
|
|
|
|
|
|
|
# $self $logfile; |
554
|
|
|
|
|
|
|
$_[0]->{LOGFILE} = $_[1]; |
555
|
|
|
|
|
|
|
} |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
# Internal function to log output if logging is enabled |
558
|
|
|
|
|
|
|
sub _logEvent { |
559
|
|
|
|
|
|
|
my $self = shift; |
560
|
|
|
|
|
|
|
my $cmd = shift; |
561
|
|
|
|
|
|
|
my $sql = shift; |
562
|
|
|
|
|
|
|
my $logfile = undef; |
563
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
# If we have a callback, use it |
565
|
|
|
|
|
|
|
if (defined $self->{LOGCALLBACK}) { |
566
|
|
|
|
|
|
|
&{$self->{LOGCALLBACK}} ($cmd, $sql); |
567
|
|
|
|
|
|
|
return; |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
# Else output to a logfile ourselves |
570
|
|
|
|
|
|
|
} elsif (defined $self->{LOGFILE}) { |
571
|
|
|
|
|
|
|
$logfile = $self->{LOGFILE}; |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
# Else forget logging |
574
|
|
|
|
|
|
|
} else { |
575
|
|
|
|
|
|
|
return; |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
} |
578
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
# Get and format the time |
580
|
|
|
|
|
|
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time); |
581
|
|
|
|
|
|
|
if ($sec < 10) { $sec = "0$sec"; } |
582
|
|
|
|
|
|
|
if ($min < 10) { $min = "0$min"; } |
583
|
|
|
|
|
|
|
if ($hour < 10) { $hour = "0$hour"; } |
584
|
|
|
|
|
|
|
if ($mon < 10) { $mon = "0$mon"; } |
585
|
|
|
|
|
|
|
if ($mday < 10) { $mday = "0$mday"; } |
586
|
|
|
|
|
|
|
my (@months) = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); |
587
|
|
|
|
|
|
|
my ($cur_date) = "[" . $mday . "/" . $months[$mon] . "/" . $year . ":" . $hour . ":" . $min . ":" . $sec . " +0000]"; |
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
# Open the logfile for append |
590
|
|
|
|
|
|
|
if (! open(LOG,">>$logfile")) { |
591
|
|
|
|
|
|
|
# Send warnings to the browser and STDERR on failure |
592
|
|
|
|
|
|
|
warn ("Unable to open logfile $logfile for append ($!)"); |
593
|
|
|
|
|
|
|
print "WARNING: Unable to open logfile $logfile for append ($!)"; |
594
|
|
|
|
|
|
|
return; |
595
|
|
|
|
|
|
|
} |
596
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
# Print to the logfile |
598
|
|
|
|
|
|
|
print LOG "$cur_date $cmd" . ($sql ne "" ? " SQL: '$sql'" : "") . "\n"; |
599
|
|
|
|
|
|
|
|
600
|
|
|
|
|
|
|
# Close the logfile |
601
|
|
|
|
|
|
|
close (LOG); |
602
|
|
|
|
|
|
|
} |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
=item B B<(>IB<);> |
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
sub errorhandler { |
607
|
|
|
|
|
|
|
my $errstr = shift; |
608
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
print "Fatal Error"; |
610
|
|
|
|
|
|
|
print $errstr; |
611
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
exit; |
613
|
|
|
|
|
|
|
} |
614
|
|
|
|
|
|
|
$DBinterface = new DBIx::glueHTML ($cgi, $dbh, $table, 1); |
615
|
|
|
|
|
|
|
$DBinterface->set_errorhandler(\&errorhandler); |
616
|
|
|
|
|
|
|
$DBinterface->check_params(); |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
Transfers error handling in the script from the I procedure |
619
|
|
|
|
|
|
|
to the subroutine passed as the argument. The errorhandling routine |
620
|
|
|
|
|
|
|
should not return, and should terminate the program after the error |
621
|
|
|
|
|
|
|
has been output. |
622
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
NOTE: check_params() MUST be called or glueHTML will not function correctly. |
624
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
=cut |
626
|
|
|
|
|
|
|
sub set_errhandler { |
627
|
|
|
|
|
|
|
# $self &errorhandler; |
628
|
|
|
|
|
|
|
$_[0]->{ERRHDL} = $_[1]; |
629
|
|
|
|
|
|
|
} |
630
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
=item B B<(>IB<);> |
632
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
sub checkaccess { |
634
|
|
|
|
|
|
|
if ($cgi->param("password") eq "letmein") { # Example security check |
635
|
|
|
|
|
|
|
return; # Valid password - return to allow function to continue |
636
|
|
|
|
|
|
|
} else { |
637
|
|
|
|
|
|
|
die ("Incorrect password"); # Incorrect - die to stop execution |
638
|
|
|
|
|
|
|
} |
639
|
|
|
|
|
|
|
} |
640
|
|
|
|
|
|
|
$DBinterface = new DBIx::glueHTML ($cgi, $dbh, $table, 1); |
641
|
|
|
|
|
|
|
$DBinterface->set_accesscallback(\&checkaccess); |
642
|
|
|
|
|
|
|
$DBinterface->check_params(); |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
Enables a security check function to approve or deny access. The function is |
645
|
|
|
|
|
|
|
called before changes to the database are made. The function should return to |
646
|
|
|
|
|
|
|
allow an action to complete or die to terminate the program and prevent access. |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
NOTE: check_params() MUST be called or glueHTML will not function correctly. |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
=cut |
651
|
|
|
|
|
|
|
sub set_accesscallback { |
652
|
|
|
|
|
|
|
my $self = shift; |
653
|
|
|
|
|
|
|
my $callback = shift; |
654
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
$self->{ACCESSCALLBACK} = $callback; |
656
|
|
|
|
|
|
|
} |
657
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
# Internal function to call the user defined security check |
659
|
|
|
|
|
|
|
sub _checkAccess { |
660
|
|
|
|
|
|
|
my $self = shift; |
661
|
|
|
|
|
|
|
my $callback = $self->{ACCESSCALLBACK}; |
662
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
if (ref($callback) eq 'CODE') { |
664
|
|
|
|
|
|
|
&$callback(); # nicer to perl 5.003 users |
665
|
|
|
|
|
|
|
} |
666
|
|
|
|
|
|
|
} |
667
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
=item B B<(>IB<);> |
669
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
sub setfieldvalue { |
671
|
|
|
|
|
|
|
my $table = shift; # Database table name |
672
|
|
|
|
|
|
|
my $mode = shift; # "add"/"modify"/"search" |
673
|
|
|
|
|
|
|
my $field = shift; # Field name |
674
|
|
|
|
|
|
|
my $value = shift; # Field value in database or form |
675
|
|
|
|
|
|
|
my $default= shift; # Field default value |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
# Additions to the News table |
678
|
|
|
|
|
|
|
if ($table eq "news" && $mode eq "add") { |
679
|
|
|
|
|
|
|
# If it's the UserID field |
680
|
|
|
|
|
|
|
if ($field eq "UserID") { |
681
|
|
|
|
|
|
|
# Hide the field and set it to the current user's ID |
682
|
|
|
|
|
|
|
return ($userID, 1); |
683
|
|
|
|
|
|
|
} |
684
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
# Modifications to |
686
|
|
|
|
|
|
|
} else if ($table eq "something" && $mode eq "modify") { |
687
|
|
|
|
|
|
|
# Something field |
688
|
|
|
|
|
|
|
if ($field eq "something") { |
689
|
|
|
|
|
|
|
# Do Something |
690
|
|
|
|
|
|
|
} else if () { |
691
|
|
|
|
|
|
|
# Do Something else |
692
|
|
|
|
|
|
|
} |
693
|
|
|
|
|
|
|
} |
694
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
# Etc.... |
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
($val, $forcehidden) = &{$self->{FORMFIELDCALLBACK}}($table, $mode, $field, $value, $default); |
698
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
# Default behaviour - CALLBACK MUST RETURN THIS FOR UNRECOGNISED TABLES/MODES |
700
|
|
|
|
|
|
|
return (undef, 0); |
701
|
|
|
|
|
|
|
} |
702
|
|
|
|
|
|
|
$DBinterface = new DBIx::glueHTML ($cgi, $dbh, $table, 1); |
703
|
|
|
|
|
|
|
$DBinterface->set_fieldvaluecallback(\&setfieldvalue); |
704
|
|
|
|
|
|
|
$DBinterface->check_params(); |
705
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
Enables the script to override data in any input form printed by the script. The |
707
|
|
|
|
|
|
|
callback is passed the database table, the mode (add/modify), the current value |
708
|
|
|
|
|
|
|
and the default value. The callback can then change the value of the field, and/or |
709
|
|
|
|
|
|
|
choose to force the field to be hidden. For example a user ID field can be defaulted |
710
|
|
|
|
|
|
|
to the currently logged on user's ID and hidden to prevent changing. |
711
|
|
|
|
|
|
|
|
712
|
|
|
|
|
|
|
The callback should return an array consisting of the value to be replaced or undef |
713
|
|
|
|
|
|
|
if the value is not to be changed, and 1 to hide the field or 0 to allow the field |
714
|
|
|
|
|
|
|
to remain visible. If no changes are to be made, (undef, 0) must be returned. |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
NOTE: check_params() MUST be called or glueHTML will not function correctly. |
717
|
|
|
|
|
|
|
|
718
|
|
|
|
|
|
|
=cut |
719
|
|
|
|
|
|
|
sub set_fieldvaluecallback { |
720
|
|
|
|
|
|
|
my $self = shift; |
721
|
|
|
|
|
|
|
my $callback = shift; |
722
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
$self->{FORMFIELDCALLBACK} = $callback; |
724
|
|
|
|
|
|
|
} |
725
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
# Internal function to output errors and exit the program |
727
|
|
|
|
|
|
|
sub _die { |
728
|
|
|
|
|
|
|
my $self = shift; |
729
|
|
|
|
|
|
|
my $errstr = shift; |
730
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
if (defined $self->{ERRHDL}) { |
732
|
|
|
|
|
|
|
&{$self->{ERRHDL}} ($errstr); |
733
|
|
|
|
|
|
|
} |
734
|
|
|
|
|
|
|
|
735
|
|
|
|
|
|
|
# Call die whether or not the user defined error handler has been called |
736
|
|
|
|
|
|
|
# - the error is fatal and we should not get here if the user defined |
737
|
|
|
|
|
|
|
# handler operates correctly anyway. |
738
|
|
|
|
|
|
|
die $errstr; |
739
|
|
|
|
|
|
|
} |
740
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
=item B B<(>IB<,> I |
742
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
$DBinterface->set_timezone(1, 0); # Set time to GMT +0000 |
744
|
|
|
|
|
|
|
$DBinterface->set_timezone(0, -5); # Set time to server time -0500 |
745
|
|
|
|
|
|
|
$DBinterface->set_timezone(1, -8); # Set time to GMT -0800 |
746
|
|
|
|
|
|
|
$DBinterface->set_timezone(0, 2); # Set time to server time +0200 |
747
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
Changes the time zone used for timestamps inserted into database records. The |
749
|
|
|
|
|
|
|
first parameter specifies whether to use GMT time or to use the server time, |
750
|
|
|
|
|
|
|
i.e the computer running this script's internal clock. The second parameter |
751
|
|
|
|
|
|
|
allows time to be added or subtracted in hours. |
752
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
=cut |
754
|
|
|
|
|
|
|
sub set_timezone { |
755
|
|
|
|
|
|
|
# $self $usegmttime; |
756
|
|
|
|
|
|
|
$_[0]->{USEGMTTIME} = $_[1]; |
757
|
|
|
|
|
|
|
# $self $timemod; |
758
|
|
|
|
|
|
|
$_[0]->{TIMEMOD} = $_[2]; |
759
|
|
|
|
|
|
|
} |
760
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
762
|
|
|
|
|
|
|
# HTML formatting functions |
763
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
764
|
|
|
|
|
|
|
=pod |
765
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
=back |
767
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
=head2 Optional HTML Customisation Methods |
769
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
=over 4 |
771
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
=item Future Additions |
773
|
|
|
|
|
|
|
|
774
|
|
|
|
|
|
|
In a later version, callbacks to add user defined form parameters to |
775
|
|
|
|
|
|
|
allow state keeping such as password protection etc. |
776
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
=item B B<(>IB<,> IB<);> |
778
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
sub printheader { |
780
|
|
|
|
|
|
|
my $title = shift; |
781
|
|
|
|
|
|
|
my $headtext = shift; |
782
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
print $cgi->header; |
784
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
print $cgi->start_html(-title=>"$headtext"); |
786
|
|
|
|
|
|
|
|
787
|
|
|
|
|
|
|
if ($headtext ne "") { |
788
|
|
|
|
|
|
|
print $cgi->h3($headtext); |
789
|
|
|
|
|
|
|
} |
790
|
|
|
|
|
|
|
} |
791
|
|
|
|
|
|
|
$DBinterface->set_printheader(\&printheader, 1); |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
Transfers the header HTML outputting function to a user defined function |
794
|
|
|
|
|
|
|
to allow HTML customisation. (This is printed at the top of every page |
795
|
|
|
|
|
|
|
outputed by this module) |
796
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
The first parameter is a function reference, the second parameter is 1 to |
798
|
|
|
|
|
|
|
allow this module to print the HTTP Content-Type header automatically, 0 |
799
|
|
|
|
|
|
|
to suppress this. |
800
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
=cut |
802
|
|
|
|
|
|
|
sub set_printheader { |
803
|
|
|
|
|
|
|
# $self &printheader; |
804
|
|
|
|
|
|
|
$_[0]->{PRINTHEADER} = $_[1]; |
805
|
|
|
|
|
|
|
# $self $printcontenttype |
806
|
|
|
|
|
|
|
$_[0]->{PRINTCONTENTTYPE} = $_[2] || 0; |
807
|
|
|
|
|
|
|
} |
808
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
# Internal function to start the output in the user's desired style |
810
|
|
|
|
|
|
|
sub _printHeader { |
811
|
|
|
|
|
|
|
my $self = shift; |
812
|
|
|
|
|
|
|
my $title = shift; |
813
|
|
|
|
|
|
|
my $headtext = shift; |
814
|
|
|
|
|
|
|
my ($package, $filename, $line) = caller(); |
815
|
|
|
|
|
|
|
|
816
|
|
|
|
|
|
|
if ($self->{PRINTCONTENTTYPE} == 1) { |
817
|
|
|
|
|
|
|
print $self->{CGI}->header; |
818
|
|
|
|
|
|
|
} |
819
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
if (defined $self->{PRINTHEADER}) { |
821
|
|
|
|
|
|
|
&{$self->{PRINTHEADER}} ($title, $headtext); |
822
|
|
|
|
|
|
|
} else { |
823
|
|
|
|
|
|
|
# Just incase it got missed |
824
|
|
|
|
|
|
|
if ($self->{PRINTCONTENTTYPE} != 1) { |
825
|
|
|
|
|
|
|
print $self->{CGI}->header; |
826
|
|
|
|
|
|
|
} |
827
|
|
|
|
|
|
|
print $self->{CGI}->start_html(-title=>"$title", |
828
|
|
|
|
|
|
|
-bgcolor=>"#FFFFFF", |
829
|
|
|
|
|
|
|
-text=>"#000077" |
830
|
|
|
|
|
|
|
); |
831
|
|
|
|
|
|
|
if ($headtext ne "") { |
832
|
|
|
|
|
|
|
print $self->{CGI}->h3($headtext); |
833
|
|
|
|
|
|
|
} |
834
|
|
|
|
|
|
|
} |
835
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
print "\n\n\n"; |
837
|
|
|
|
|
|
|
} |
838
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
=item B B<(>IB<);> |
840
|
|
|
|
|
|
|
|
841
|
|
|
|
|
|
|
sub printfooter { |
842
|
|
|
|
|
|
|
print $cgi->end_html; |
843
|
|
|
|
|
|
|
} |
844
|
|
|
|
|
|
|
$DBinterface->set_printfooter(\&printfooter); |
845
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
Transfers the footer HTML outputting function to a user defined function |
847
|
|
|
|
|
|
|
to allow HTML customisation. (This is printed at the bottom of every |
848
|
|
|
|
|
|
|
page outputed by this module) |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
=cut |
851
|
|
|
|
|
|
|
sub set_printfooter { |
852
|
|
|
|
|
|
|
# $self &printfooter; |
853
|
|
|
|
|
|
|
$_[0]->{PRINTFOOTER} = $_[1]; |
854
|
|
|
|
|
|
|
} |
855
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
# Internal function to end the output in the user's desired style |
857
|
|
|
|
|
|
|
sub _printFooter { |
858
|
|
|
|
|
|
|
my $self = shift; |
859
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
if (defined $self->{PRINTFOOTER}) { |
861
|
|
|
|
|
|
|
&{$self->{PRINTFOOTER}}; |
862
|
|
|
|
|
|
|
} else { |
863
|
|
|
|
|
|
|
print " |