1 |
#
|
2 |
# Query.pm
|
3 |
#
|
4 |
# Originally Written by Christopher Williams
|
5 |
#
|
6 |
# Description
|
7 |
# -----------
|
8 |
# Hold information request data
|
9 |
#
|
10 |
# Interface
|
11 |
# ---------
|
12 |
# new() : A new Query object
|
13 |
# querylist() : return ordered list of query names
|
14 |
# title() : return/set the title text
|
15 |
# intro() : return/set the intro text
|
16 |
# querytype(name) : return/set the input type of the named query
|
17 |
# querymessage(name) : return/set the message text associated with the named
|
18 |
# query
|
19 |
# queryprompt(name) : return/set the prompt string for the query
|
20 |
# queryoptions(name) : return/set the query option list for the named query
|
21 |
# currentvalue(name) : return the current value of the query list exapnded
|
22 |
# in the context of the other query values - affected
|
23 |
# by lodgevalue.
|
24 |
# getparam(name) : return the absolute value of the param (non expanded) - not
|
25 |
# affected by lodgevalue until OK called.
|
26 |
# setparam(name,value) : set the value of a given query to list
|
27 |
# expand(@string) : expand given strings
|
28 |
# lodgevalue(name,val) : set a value of a given query variable to the list
|
29 |
# current value will be expanded in terms of these
|
30 |
# OK() : set all query values to the values of the lodged
|
31 |
# variables
|
32 |
# cancel() : clear the values of any lodged variables
|
33 |
# setparam(name,value) : hard set
|
34 |
# params : return list of all parameters
|
35 |
|
36 |
package ActiveDoc::Query;
|
37 |
require 5.004;
|
38 |
|
39 |
sub new {
|
40 |
my $class=shift;
|
41 |
$self={};
|
42 |
bless $self, $class;
|
43 |
return $self;
|
44 |
}
|
45 |
|
46 |
sub title {
|
47 |
my $self=shift;
|
48 |
@_?$self->{title}=shift
|
49 |
:((defined $self->{title})?$self->{title}:"");
|
50 |
}
|
51 |
|
52 |
sub intro {
|
53 |
my $self=shift;
|
54 |
@_?$self->{intro}=shift
|
55 |
:((defined $self->{intro})?$self->{intro}:"");
|
56 |
}
|
57 |
|
58 |
sub querytype {
|
59 |
my $self=shift;
|
60 |
my $name=shift;
|
61 |
|
62 |
@_?$self->{querytype}{$name}=shift
|
63 |
:$self->{querytype}{$name};
|
64 |
}
|
65 |
|
66 |
sub querymessage {
|
67 |
my $self=shift;
|
68 |
my $name=shift;
|
69 |
|
70 |
@_?($self->{querymessage}{$name}=shift)
|
71 |
:($self->expand($self->{querymessage}{$name}));
|
72 |
}
|
73 |
|
74 |
sub queryprompt {
|
75 |
my $self=shift;
|
76 |
my $name=shift;
|
77 |
|
78 |
my $rv=undef;
|
79 |
if ( @_ ) {
|
80 |
$self->{queryprompt}{$name}=shift;
|
81 |
$rv=$self->{queryprompt}{$name};
|
82 |
} elsif ( defined $self->{queryprompt}{$name} ) {
|
83 |
$rv=$self->expand($self->{queryprompt}{$name});
|
84 |
}
|
85 |
return $rv;
|
86 |
}
|
87 |
|
88 |
sub queryoptions {
|
89 |
my $self=shift;
|
90 |
my $name=shift;
|
91 |
my @out=();
|
92 |
|
93 |
if ( @_ ) {
|
94 |
push @{$self->{queryoptions}{$name}},@_;
|
95 |
}
|
96 |
foreach $option ( @{$self->{queryoptions}{$name}} ) {
|
97 |
push @out, $self->expand($option);
|
98 |
}
|
99 |
|
100 |
return @out;
|
101 |
}
|
102 |
|
103 |
sub expand {
|
104 |
my $self=shift;
|
105 |
my $string=shift;
|
106 |
|
107 |
$string=~s{
|
108 |
\$\((\w+)\)
|
109 |
}{
|
110 |
if (defined $self->{lodgehash}{$1}) {
|
111 |
$self->expand($self->{lodgehash}{$1});
|
112 |
} else {
|
113 |
"\$$1";
|
114 |
}
|
115 |
}egx;
|
116 |
$string=~s{
|
117 |
\$(\w+)
|
118 |
}{
|
119 |
if (defined $self->{lodgehash}{$1}) {
|
120 |
$self->expand($self->{lodgehash}{$1});
|
121 |
} else {
|
122 |
"\$$1";
|
123 |
}
|
124 |
}egx;
|
125 |
return $string;
|
126 |
}
|
127 |
|
128 |
sub currentvalue {
|
129 |
my $self=shift;
|
130 |
my $name=shift;
|
131 |
|
132 |
$self->expand(
|
133 |
((defined $self->{lodgehash}{$name})?$self->{lodgehash}{$name}:""));
|
134 |
}
|
135 |
|
136 |
sub getparam {
|
137 |
my $self=shift;
|
138 |
my $name=shift;
|
139 |
return $self->{params}{$name};
|
140 |
}
|
141 |
|
142 |
sub lodgevalue {
|
143 |
my $self=shift;
|
144 |
my $name=shift;
|
145 |
|
146 |
$self->{lodgehash}{$name}=shift;
|
147 |
}
|
148 |
|
149 |
sub OK {
|
150 |
my $self=shift;
|
151 |
|
152 |
foreach $name ( $self->querylist() ) {
|
153 |
$self->setparam($name, $self->{lodgehash}{$name});
|
154 |
}
|
155 |
}
|
156 |
|
157 |
sub querylist {
|
158 |
my $self=shift;
|
159 |
return ( keys %{$self->{querytype}});
|
160 |
}
|
161 |
|
162 |
sub cancel {
|
163 |
my $self=shift;
|
164 |
|
165 |
foreach $name ( keys %{$self->{lodgehash}} ) {
|
166 |
$self->{lodgehash}{$name}=$self->{params}{$name};
|
167 |
}
|
168 |
}
|
169 |
|
170 |
sub setparam {
|
171 |
my $self=shift;
|
172 |
my $name=shift;
|
173 |
my $value=shift;
|
174 |
|
175 |
print "Setting $name = $value\n";
|
176 |
$self->{params}{$name}=$value;
|
177 |
$self->lodgevalue($name,$value);
|
178 |
}
|
179 |
|
180 |
sub params {
|
181 |
my $self=shift;
|
182 |
|
183 |
return ( keys %{$self->{params}} );
|
184 |
}
|