| 1 | package QWizard; | ||
| 2 | |||
| 3 | our $VERSION = '3.15'; | ||
| 4 | require Exporter; | ||
| 5 | |||
| 6 | our @ISA = qw(Exporter); | ||
| 7 | our @EXPORT = qw(qwdebug qwisdebugon qwparam qwparams qwpref | ||
| 8 | qw_upload_fh qw_upload_file | ||
| 9 | qw_required_field qw_integer qw_optional_integer | ||
| 10 | qw_check_int_ranges qw_check_length_ranges | ||
| 11 | qw_hex qw_optional_hex qw_check_hex_and_length); | ||
| 12 | |||
| 13 | use Data::Dumper; | ||
| 14 | our $qwdebug = 0; | ||
| 15 | our $qwdebug_indent = 0; | ||
| 16 | our $qwvar; | ||
| 17 | our $qwcurrent; | ||
| 18 | |||
| 19 | our %states = ( ASKING => 1, | ||
| 20 | CONFIRMING => 2, | ||
| 21 | ACTING => 3, | ||
| 22 | FINISHED => 4, | ||
| 23 | CANCELED => 5, | ||
| 24 | ); | ||
| 25 | |||
| 26 | our $PRIM_NOTDONE = 0; | ||
| 27 | our $PRIM_DONE = 1; | ||
| 28 | our $PRIM_ANSWERED = 2; | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | my $type = shift; | ||
| 34 | $qwdebug_indent = 0; | ||
| 35 | my ($class) = ref($type) || $type; | ||
| 36 | my $self; | ||
| 37 | %$self = @_; | ||
| 38 | map { $self->{'primaries'}{$_}{'module_name'} = $_ } | ||
| 39 | keys(%{$self->{'primaries'}}); | ||
| 40 | if (!$self->{'generator'}) { | ||
| 41 | eval { require QWizard::Generator::Best; }; | ||
| 42 | $self->{'generator'} = | ||
| 43 | new QWizard::Generator::Best(@{$self->{'generator_args'}}); | ||
| 44 | die "Can't create a suitable QWizard generator" | ||
| 45 | if (!defined($self->{'generator'})); | ||
| 46 | } | ||
| 47 | |||
| 48 | bless($self, $class); | ||
| 49 | |||
| 50 | $self->qwsetdebug(); | ||
| 51 | |||
| 52 | # | ||
| 53 | # Get the URI preference option and set our preferences. | ||
| 54 | # | ||
| 55 | my $npprefs = $self->{'npprefs'} || ""; | ||
| 56 | $npprefs =~ s/&np-prefs=//; | ||
| 57 | parseprefs($self,$npprefs); | ||
| 58 | |||
| 59 | # remember ourselves for later usage | ||
| 60 | $qwcurrent = $self; | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | # | ||
| 66 | # $wiz->run_hooks(NAME, HOOK_ARGS) | ||
| 67 | # runs all hooks bound to NAME with a passed reference to QWIZARD, | ||
| 68 | # earlier passed MAGIC_ARGS (see below) and a copied ARGS in an array | ||
| 69 | # reference, which is passed in from the calling args. The result is: | ||
| 70 | # | ||
| 71 | # CODE->($wiz, MAGIC_ARGS, [HOOK_ARGS]) | ||
| 72 | # | ||
| 73 | sub run_hooks { | ||
| 74 | my $self = shift; | ||
| 75 | my $hookname = shift; | ||
| 76 | my @args = @_; | ||
| 77 | qwdebug("checking for hooks on $hookname"); | ||
| 78 | if (exists($self->{'hooks'}{$hookname})) { | ||
| 79 | foreach my $hook (@{$self->{'hooks'}{$hookname}}) { | ||
| 80 | qwdebug("running a hook for $hookname"); | ||
| 81 | $hook->{'code'}($self, @{$hook->{'args'}}, \@args); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | # | ||
| 87 | # $wiz->add_hooks(NAME, SUB_REF, MAGIC_ARGS) | ||
| 88 | # Adds a SUB_REF code block to the NAME set of hooks. Optionally, a | ||
| 89 | # set of MAGIC_ARGS may be passed as well. For portability ease, it is | ||
| 90 | # suggested that MAGIC_ARGS be a single argument of an array reference | ||
| 91 | # of any arguments that need passing. | ||
| 92 | # | ||
| 93 | sub add_hook { | ||
| 94 | my $self = shift; | ||
| 95 | my $hookname = shift; | ||
| 96 | my $coderef = shift; | ||
| 97 | my @magic_args = @_; | ||
| 98 | my $hook_definition = {'code' => $coderef}; | ||
| 99 | push @{$self->{'hooks'}{$hookname}}, $hook_definition; | ||
| 100 | if ($#magic_args > -1) { | ||
| 101 | $hook_definition->{'args'} = \@magic_args; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | ######################################################################## | ||
| 106 | # parseprefs() | ||
| 107 | # | ||
| 108 | # Valid preferences and their values: | ||
| 109 | # pref_debug 0, 1 | ||
| 110 | # pref_history dont, sidebar, frame | ||
| 111 | # pref_intro 0, 1 | ||
| 112 | # | ||
| 113 | sub parseprefs { | ||
| 114 | my $self = shift; # Me. | ||
| 115 | my $prefs = shift; # Preference argument from URI. | ||
| 116 | my @preflist; # List of preferences. | ||
| 117 | my $pcnt; # Number of preferences. | ||
| 118 | my $prefstr; # Preference string. | ||
| 119 | |||
| 120 | # | ||
| 121 | # Break the URI preference argument up into individual preferences. | ||
| 122 | # | ||
| 123 | @preflist = split(',',$prefs); | ||
| 124 | $pcnt = @preflist; | ||
| 125 | |||
| 126 | # | ||
| 127 | # Examine each preference individually, ensuring the name and value | ||
| 128 | # are both valid. | ||
| 129 | # | ||
| 130 | for(my $ind=0;$ind<$pcnt;$ind++) | ||
| 131 | { | ||
| 132 | my @pieces; # Preference pieces. | ||
| 133 | my $prefname; # Name of preference. | ||
| 134 | my $prefval; # Value of preference. | ||
| 135 | |||
| 136 | @pieces = split('=',$preflist[$ind]); | ||
| 137 | $prefname = "pref_" . $pieces[0]; | ||
| 138 | $prefval = $pieces[1]; | ||
| 139 | |||
| 140 | # | ||
| 141 | # Check the validity of the preference name and value. | ||
| 142 | # | ||
| 143 | if($prefname eq "pref_debug") | ||
| 144 | { | ||
| 145 | if(($prefval != 0) && ($prefval != 1)) | ||
| 146 | { | ||
| 147 | warn "QW:parseprefs: invalid value \"$prefval\" for preference \"$prefname\"\n"; | ||
| 148 | next; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | elsif($prefname eq "pref_history") | ||
| 152 | { | ||
| 153 | if(($prefval ne "dont") && ($prefval ne "frame") && | ||
| 154 | ($prefval ne "sidebar")) | ||
| 155 | { | ||
| 156 | warn "QW:parseprefs: invalid value \"$prefval\" for preference \"$prefname\"\n"; | ||
| 157 | next; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | elsif($prefname eq "pref_intro") | ||
| 161 | { | ||
| 162 | if(($prefval != 0) && ($prefval != 1)) | ||
| 163 | { | ||
| 164 | warn "QW:parseprefs: invalid value \"$prefval\" for preference \"$prefname\"\n"; | ||
| 165 | next; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | else | ||
| 169 | { | ||
| 170 | warn "QW:parseprefs: unknown preference \"$prefname\"\n"; | ||
| 171 | next; | ||
| 172 | } | ||
| 173 | |||
| 174 | # | ||
| 175 | # Set the preference value. | ||
| 176 | # | ||
| 177 | # warn "QW:parseprefs: setting \"$prefname\" to $prefval\n\n"; | ||
| 178 | $self->qwpref($prefname,$prefval); | ||
| 179 | $self->qwparam($prefname,$prefval); | ||
| 180 | } | ||
| 181 | |||
| 182 | } | ||
| 183 | |||
| 184 | # | ||
| 185 | # Primary manipulation routines | ||
| 186 | # | ||
| 187 | sub get_primary { | ||
| 188 | my ($self, $namefull) = @_; | ||
| 189 | |||
| 190 | my $name = drop_remap_prefix($namefull); | ||
| 191 | if ($self->{'primaries'}{$name}) { | ||
| 192 | return $self->{'primaries'}{$name} | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | sub add_primary { | ||
| 197 | my $self = shift; | ||
| 198 | my $name = shift; | ||
| 199 | my %primary = @_; | ||
| 200 | |||
| 201 | $self->{'primaries'}{$name} = \%primary; | ||
| 202 | $self->{'primaries'}{$name}{'name'} = $name | ||
| 203 | if (!$self->{'primaries'}{$name}{'name'}); | ||
| 204 | return \%primary; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub merge_primaries { | ||
| 208 | my $self = shift; | ||
| 209 | my $primaries = shift; | ||
| 210 | foreach my $i (keys(%$primaries)) { | ||
| 211 | $self->{'primaries'}{$i} = $primaries->{$i}; | ||
| 212 | $self->{'primaries'}{$i}{'name'} = $i | ||
| 213 | if (!$self->{'primaries'}{$i}{'name'}); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | # | ||
| 219 | # Functions that actually do the work | ||
| 220 | # | ||
| 221 | sub magic { | ||
| 222 | my $self = shift; | ||
| 223 | |||
| 224 | $self->{'last_screen'} = 0; | ||
| 225 | |||
| 226 | $self->run_hooks('start_magic'); | ||
| 227 | do { | ||
| 228 | qwdebug("------------------------------------------------------------------"); | ||
| 229 | qwdebug("incoming: " . ref($self->{'generator'}) . " :" | ||
| 230 | . ($self->qwparam('pass_vars') || "")); | ||
| 231 | qwdebug("incoming variables: " . ($self->qwparam('pass_vars') || "")); | ||
| 232 | qwdebug("incoming stack: " . ($self->qwparam('qwizard_tree') || "")); | ||
| 233 | if ($self->qwparam('disp_help_p')) { | ||
| 234 | $self->display_help(); | ||
| 235 | } elsif (!$self->qwparam('qw_cancel') && $self->qwparam('pass_vars')) { | ||
| 236 | qwdebug("called with an existing todo list, continuing to work."); | ||
| 237 | $self->keep_working(@_); | ||
| 238 | qwdebug("done calling existing"); | ||
| 239 | } else { | ||
| 240 | qwdebug("starting with the default list of primaries.\n"); | ||
| 241 | $self->do_named_primaries(@_); | ||
| 242 | qwdebug("done with the default list of primaries.\n"); | ||
| 243 | } | ||
| 244 | } while ((!$self->{'last_screen'} && !$self->{'one_pass'} && | ||
| 245 | !$self->{'generator'}{'one_pass'})); | ||
| 246 | $self->run_hooks('end_magic'); | ||
| 247 | } | ||
| 248 | |||
| 249 | sub do_named_primaries { | ||
| 250 | my $self = shift; | ||
| 251 | my $top = {}; | ||
| 252 | $top->{'name'} = 'topcontainer'; | ||
| 253 | $top->{'done'} = $PRIM_DONE; | ||
| 254 | foreach my $i (@_) { | ||
| 255 | push @{$top->{'children'}}, { name => $i, parent => $top }; | ||
| 256 | } | ||
| 257 | $self->do_primaries($top, @_); | ||
| 258 | } | ||
| 259 | |||
| 260 | sub do_primaries { | ||
| 261 | my $self = shift; | ||
| 262 | $self->{'active'} = shift; | ||
| 263 | my ($name, $namefull, @pris, @errors); | ||
| 264 | |||
| 265 | my %error_primaries= | ||
| 266 | ('noprimary' => | ||
| 267 | { | ||
| 268 | 'questions' => | ||
| 269 | [ { type => 'table', | ||
| 270 | error => 'Server Error.', | ||
| 271 | text => 'The following QWizard components were missing:', | ||
| 272 | values => [ sub { | ||
| 273 | return [\@errors]; | ||
| 274 | } ], | ||
| 275 | }, | ||
| 276 | { type => 'hidden', | ||
| 277 | name => 'wiz_canceled', | ||
| 278 | values => 1 }, | ||
| 279 | ], | ||
| 280 | } | ||
| 281 | ); | ||
| 282 | |||
| 283 | $self->start_page(); | ||
| 284 | $self->ask_questions(); | ||
| 285 | } | ||
| 286 | |||
| 287 | sub finished { | ||
| 288 | my $self = shift; | ||
| 289 | $self->{'generator'}->finished(); | ||
| 290 | } | ||
| 291 | |||
| 292 | sub set_progress { | ||
| 293 | my $self = shift; | ||
| 294 | $self->{'generator'}->set_progress(@_); | ||
| 295 | } | ||
| 296 | |||
| 297 | sub has_actions { | ||
| 298 | my $self = shift; | ||
| 299 | my $has_actions = 0; | ||
| 300 | $self->foreach_primary($self->{'active'}, | ||
| 301 | sub { my $pdesc = $_[0]; | ||
| 302 | my $p = $_[1]->get_primary($pdesc->{'name'}); | ||
| 303 | if (exists($p->{'actions'})) { | ||
| 304 | ${$_[2]} = 1; | ||
| 305 | } | ||
| 306 | }, $self, \$has_actions); | ||
| 307 | return $has_actions; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub has_actions_or_post_answers { | ||
| 311 | my $self = shift; | ||
| 312 | my $has_stuff = 0; | ||
| 313 | $self->foreach_primary($self->{'active'}, | ||
| 314 | sub { my $pdesc = $_[0]; | ||
| 315 | my $p = $_[1]->get_primary($pdesc->{'name'}); | ||
| 316 | if (exists($p->{'actions'}) || | ||
| 317 | exists($p->{'post_answers'})) { | ||
| 318 | ${$_[2]} = 1; | ||
| 319 | } | ||
| 320 | }, $self, \$has_stuff); | ||
| 321 | return $has_stuff; | ||
| 322 | } | ||
| 323 | |||
| 324 | sub keep_working { | ||
| 325 | my ($self, @otherwise) = @_; | ||
| 326 | $self->run_hooks('keep_working_begin', $self, $self->{'generator'}); | ||
| 327 | if ($self->{'generator'}{'keep_working_hook'}) { | ||
| 328 | $self->{'generator'}{'keep_working_hook'}->($self->{'generator'}); | ||
| 329 | } | ||
| 330 | my $str = $self->qwparam('qwizard_tree'); | ||
| 331 | $self->{'active'} = $self->decode_todo_list(\$str); | ||
| 332 | |||
| 333 | # do stuff from the last run that needs doing, like messing with | ||
| 334 | # variables that HTML doesn't deal with properly (checkbox off | ||
| 335 | # values, etc). | ||
| 336 | my $num = 0; | ||
| 337 | qwdebug("munging input data as needed and counting last run"); | ||
| 338 | $self->foreach_primary($self->{'active'}, | ||
| 339 | sub { | ||
| 340 | my $num = $_[1]; | ||
| 341 | if ($_[0]{'done'} == $PRIM_ANSWERED) { | ||
| 342 | $$num++; | ||
| 343 | qwdebug("munging form data for primary $_[0]->{name}"); | ||
| 344 | $_[2]->munge_form_data($_[2]->get_primary($_[0]{'name'})); | ||
| 345 | } | ||
| 346 | }, \$num, $self); | ||
| 347 | qwdebug("$num primaries ran last time\n"); | ||
| 348 | |||
| 349 | $self->start_page(); | ||
| 350 | my $redisplay; | ||
| 351 | if (qwparam('redo_screen') || !($self->check_answers($num))) { | ||
| 352 | $redisplay++; | ||
| 353 | } else { | ||
| 354 | if ($num > 0) { | ||
| 355 | # run post_answers clauses and mark as done | ||
| 356 | $self->run_hooks('post_answers_begin'); | ||
| 357 | qwdebug("Running post answers clauses"); | ||
| 358 | $self->foreach_primary($self->{'active'}, | ||
| 359 | sub { | ||
| 360 | if ($_[0]{'done'} == $PRIM_ANSWERED) { | ||
| 361 | my ($pdesc, $self, $redisplay) = @_; | ||
| 362 | my $redo; | ||
| 363 | my $p = $self->get_primary($pdesc->{'name'}); | ||
| 364 | my $post_answers = | ||
| 365 | $p->{'post_answers'}; | ||
| 366 | $post_answers = [$post_answers] | ||
| 367 | if (ref($post_answers) ne 'ARRAY'); | ||
| 368 | # stash remap tag in prim to establish context | ||
| 369 | if (exists $pdesc->{'remap'}) { | ||
| 370 | $p->{'remap'} = $pdesc->{'remap'}; | ||
| 371 | } | ||
| 372 | # run any post_answers clauses | ||
| 373 | if ($#$post_answers > -1) { | ||
| 374 | # remember context for add_todos calls | ||
| 375 | $self->{'context'} = $pdesc; | ||
| 376 | my $results = | ||
| 377 | $self->do_list(@$post_answers); | ||
| 378 | $redo += scalar(grep(/REDISPLAY/,@$results)); | ||
| 379 | qwdebug("results: ",join(",",@$results)); | ||
| 380 | } | ||
| 381 | |||
| 382 | # remap if needed | ||
| 383 | if (exists $pdesc->{'remap'}) { | ||
| 384 | my $newvars = | ||
| 385 | $self->map_primary($p, $pdesc->{'remap'}, ''); | ||
| 386 | push @{$self->{'pass_next'}}, @$newvars; | ||
| 387 | # remove remap context from prim | ||
| 388 | delete $p->{'remap'}; | ||
| 389 | } | ||
| 390 | # mark as finished | ||
| 391 | $_[0]{'done'} = $PRIM_DONE unless $redo; | ||
| 392 | $$redisplay += $redo; | ||
| 393 | }}, $self, \$redisplay); | ||
| 394 | $self->run_hooks('post_answers_end'); | ||
| 395 | } | ||
| 396 | } | ||
| 397 | |||
| 398 | if ($redisplay) { | ||
| 399 | qwdebug("redo_screen set, redoing last screen"); | ||
| 400 | |||
| 401 | # mark as undone again | ||
| 402 | $self->foreach_primary($self->{'active'}, | ||
| 403 | sub { | ||
| 404 | $_[0]{'done'} = $PRIM_NOTDONE | ||
| 405 | if ($_[0]{'done'} == $PRIM_ANSWERED); | ||
| 406 | }); | ||
| 407 | # remember the current value for anything that needs to process it. | ||
| 408 | $self->qwparam('redoing_now', $self->qwparam('redo_screen') || 1); | ||
| 409 | # nuke for next runs unless something resets it again. | ||
| 410 | $self->qwparam('redo_screen',0); | ||
| 411 | } | ||
| 412 | |||
| 413 | qwdebug("processing next primaries on the todo list"); | ||
| 414 | |||
| 415 | if ($self->do_primaries($self->{'active'})) { | ||
| 416 | # There were no primaries left to process, so display the commit | ||
| 417 | # screen or run the actions. | ||
| 418 | qwdebug("no screens to display, entering confirmation mode"); | ||
| 419 | if ($self->qwparam('no_actions') || !$self->has_actions() || | ||
| 420 | !$self->confirm_or_run_actions()) { | ||
| 421 | |||
| 422 | # cancel button hit, restart at top | ||
| 423 | $self->reset_qwizard(); | ||
| 424 | $self->{'state'} = $states{'CANCELED'}; | ||
| 425 | } | ||
| 426 | } else { | ||
| 427 | $self->{'state'} = $states{'ASKING'}; | ||
| 428 | } | ||
| 429 | $self->qwparam('redoing_now', 0); | ||
| 430 | |||
| 431 | # | ||
| 432 | # Handle any auto-updating that's required. | ||
| 433 | # | ||
| 434 | my $aupd = qwpref('updateflag') || 0; | ||
| 435 | if($aupd > 0) | ||
| 436 | { | ||
| 437 | # warn "keep_working: aupd: $aupd > 0\n"; | ||
| 438 | my $tempus = qwparam('upd_time'); | ||
| 439 | |||
| 440 | # | ||
| 441 | # If the time was set to zero, we're assuming it's a manual pause | ||
| 442 | # and we won't do anything. | ||
| 443 | # | ||
| 444 | if($tempus > 0) | ||
| 445 | { | ||
| 446 | # warn "keep_working: calling do_autoupd($tempus)\n"; | ||
| 447 | $self->{'generator'}->do_autoupd($tempus); | ||
| 448 | $self->add_todos('basic_monitor_table'); | ||
| 449 | } | ||
| 450 | } | ||
| 451 | |||
| 452 | } | ||
| 453 | |||
| 454 | # Modifies values to change them when not supported via a generator. | ||
| 455 | # I.e., when a HTML checkbox is off, we set the value to the "off" value | ||
| 456 | # even though HTML doesn't return one. | ||
| 457 | sub munge_form_data { | ||
| 458 | my ($self, $p) = @_; | ||
| 459 | foreach my $q (@{$p->{'questions'}}) { | ||
| 460 | if (ref($q) eq "HASH") { | ||
| 461 | |||
| 462 | # XXX: These are somewhat HTML specific and should be | ||
| 463 | # moved to the generator... | ||
| 464 | if ($q->{'type'} eq "checkbox" && | ||
| 465 | $q->{values} && $#{$q->{values}} > 0) { | ||
| 466 | if (!$self->qwparam($q->{'name'})) { | ||
| 467 | my $vals = $self->get_values($q->{values}); | ||
| 468 | $vals = [1,0] if ($#$vals == -1); | ||
| 469 | $self->qwparam($q->{'name'}, $vals->[1]); | ||
| 470 | } | ||
| 471 | } elsif ($q->{'type'} eq 'button') { | ||
| 472 | # remap from the values clause which was the text to | ||
| 473 | # the default clause which is the expected value. | ||
| 474 | my $vals = $self->get_value($q->{values}); | ||
| 475 | if (defined ($self->qwparam($q->{'name'})) && | ||
| 476 | $self->qwparam($q->{'name'}) eq $vals) { | ||
| 477 | $self->qwparam($q->{'name'}, | ||
| 478 | $self->get_value($q->{'default'}, undef, [$p, $q])); | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | # each question can have it's own callback. | ||
| 483 | if (exists($q->{'handle_results'})) { | ||
| 484 | $q->{'handle_results'}($self, $p, $q); | ||
| 485 | } | ||
| 486 | } | ||
| 487 | } | ||
| 488 | } | ||
| 489 | |||
| 490 | sub check_answers { | ||
| 491 | my ($self, $num) = @_; | ||
| 492 | my $ret = 1; | ||
| 493 | |||
| 494 | qwdebug("checking previous results for errors"); | ||
| 495 | if ($num > 0) { | ||
| 496 | $self->foreach_primary($self->{'active'}, | ||
| 497 | sub { | ||
| 498 | my $pdesc = $_[0]; | ||
| 499 | return if ($pdesc->{'done'} != $PRIM_ANSWERED); | ||
| 500 | my $p = $_[1]->get_primary($pdesc->{'name'}); | ||
| 501 | my $ret = $_[2]; | ||
| 502 | qwdebug("checking results in " . ($p->{'name'}||$p->{'title'})); | ||
| 503 | if ($p->{'check_value'}) { | ||
| 504 | qwdebug("checking primary's check_value func"); | ||
| 505 | my @args; | ||
| 506 | my $code; | ||
| 507 | if (ref($p->{check_value}) eq "CODE") { | ||
| 508 | $code = $p->{check_value}; | ||
| 509 | } elsif (ref($p->{check_value}) eq "ARRAY") { | ||
| 510 | $code = $p->{check_value}[0]; | ||
| 511 | @args = @{$p->{check_value}}[1..$#{$p->{check_value}}]; | ||
| 512 | } | ||
| 513 | my $err = $code->($self, $p, @args); | ||
| 514 | if ($err and $err ne 'OK') { | ||
| 515 | $$ret = 0; | ||
| 516 | unless ($err eq 'REDISPLAY') { | ||
| 517 | qwdebug("Error found in primary check_value:$err"); | ||
| 518 | $pdesc->{'errcount'}++; | ||
| 519 | $pdesc->{'error'} = $err; | ||
| 520 | } | ||
| 521 | } | ||
| 522 | } | ||
| 523 | foreach my $q (@{$p->{'questions'}}) { | ||
| 524 | if (ref($q) eq 'HASH' && $q->{check_value}) { | ||
| 525 | my @args; | ||
| 526 | my $code; | ||
| 527 | if ($q->{'doif'} && ! $q->{'doif'}->($q, $self, $p)) { | ||
| 528 | next; | ||
| 529 | } | ||
| 530 | if (ref($q->{check_value}) eq "CODE") { | ||
| 531 | $code = $q->{check_value}; | ||
| 532 | } elsif (ref($q->{check_value}) eq "ARRAY") { | ||
| 533 | $code = $q->{check_value}[0]; | ||
| 534 | @args = @{$q->{check_value}}[1..$#{$q->{check_value}}]; | ||
| 535 | } | ||
| 536 | my $err = $code->($self, $q, $p, @args); | ||
| 537 | if ($err && $err ne 'OK') { | ||
| 538 | $$ret = 0; | ||
| 539 | unless ($err eq 'REDISPLAY') { | ||
| 540 | my $name = $q->{'name'}; | ||
| 541 | qwdebug("Error found in question $name: $err"); | ||
| 542 | $pdesc->{'errcount'}++; | ||
| 543 | $pdesc->{'qerrs'}{$q->{'name'}} = $err; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | } | ||
| 547 | } | ||
| 548 | }, | ||
| 549 | $self, \$ret); | ||
| 550 | } | ||
| 551 | return $ret; | ||
| 552 | } | ||
| 553 | |||
| 554 | sub remove_todo { | ||
| 555 | my ($self, $todo) = @_; | ||
| 556 | $self->foreach_primary($self->{'active'}, | ||
| 557 | sub { | ||
| 558 | if (!$todo || | ||
| 559 | $_[0]{'name'} == $todo) { | ||
| 560 | qwdebug("removing $_[0]{name} from the list of things to do"); | ||
| 561 | $_[0]{'noactions'} = 1; | ||
| 562 | $_[0]{'done'} = $PRIM_DONE; | ||
| 563 | } | ||
| 564 | }, $self); | ||
| 565 | } | ||
| 566 | |||
| 567 | sub add_todos { | ||
| 568 | my $self = shift; | ||
| 569 | my $order = '-early'; | ||
| 570 | my $merge = 0; | ||
| 571 | my $remap = $self->{'remap'}; | ||
| 572 | my $addto = $self->{'context'}; | ||
| 573 | my $actionslast = 0; | ||
| 574 | my $count = 0; | ||
| 575 | |||
| 576 | $remap .= $addto->{'remap'} if ($addto->{'remap'}); | ||
| 577 | |||
| 578 | while ($_[0] =~ /^-/) { | ||
| 579 | my $arg = shift; | ||
| 580 | $order = $arg if ($arg eq '-early'); | ||
| 581 | $order = $arg if ($arg eq '-late'); | ||
| 582 | $merge = 1 if ($arg eq '-merge'); | ||
| 583 | $count = $merge = shift if ($arg eq '-mergen'); | ||
| 584 | $actionslast = 1 if ($arg eq '-actionslast'); | ||
| 585 | $remap .= shift if ($arg eq '-remap'); | ||
| 586 | } | ||
| 587 | |||
| 588 | foreach my $it (reverse @_) { | ||
| 589 | $count++; | ||
| 590 | my $newchild = {}; | ||
| 591 | $newchild->{'name'} = $it; | ||
| 592 | $newchild->{'parent'} = $addto; | ||
| 593 | $newchild->{'remap'} = $remap if ($remap); | ||
| 594 | $newchild->{'actionslast'} = 1 if ($actionslast); | ||
| 595 | if ($order eq '-early') { | ||
| 596 | # XXX: note that this may mess up action ordering. This | ||
| 597 | # should go just before the last *done* clause? | ||
| 598 | qwdebug("Adding (early) $it to todos"); | ||
| 599 | unshift @{$addto->{'children'}}, $newchild; | ||
| 600 | } else { | ||
| 601 | qwdebug("Adding (late) $it to todos"); | ||
| 602 | push @{$addto->{'children'}}, $newchild; | ||
| 603 | } | ||
| 604 | } | ||
| 605 | if (($#_ > -1 || $merge > 1) && $merge) { | ||
| 606 | if ($order eq '-early') { | ||
| 607 | $addto->{'children'}[0]{'merge'} = $count; | ||
| 608 | } else { | ||
| 609 | $addto->{'children'}[$#{$addto->{'children'}}]{'merge'} = $count; | ||
| 610 | } | ||
| 611 | } | ||
| 612 | } | ||
| 613 | |||
| 614 | # Takes a list of "stuff" and executes it. Code snippets return | ||
| 615 | # errors, and strings may be included directly in the list which will | ||
| 616 | # be returned as comments/output to the user. | ||
| 617 | sub do_list { | ||
| 618 | my $self = shift; | ||
| 619 | my @ret; | ||
| 620 | foreach my $action (@_) { | ||
| 621 | my $result; | ||
| 622 | if (ref($action) eq 'CODE') { | ||
| 623 | $result = $action->($self); | ||
| 624 | } elsif (ref($action) eq 'ARRAY') { | ||
| 625 | my @a = @{$action}; | ||
| 626 | my $it = shift @a; | ||
| 627 | die "fatal spec error. not an action..." if (ref($it) ne 'CODE'); | ||
| 628 | $result = $it->($self, @a); | ||
| 629 | } elsif (defined($action)) { | ||
| 630 | $result = "msg:" . $action; | ||
| 631 | $result =~ s/\@([^\@]+)\@/$self->qwparam($1)/eg; | ||
| 632 | } | ||
| 633 | if (defined($result)) { | ||
| 634 | $result = [$result] if (ref($result) ne 'ARRAY'); | ||
| 635 | foreach my $r (@$result) { | ||
| 636 | if ($r =~ s/^msg://i) { | ||
| 637 | push @ret, $r; | ||
| 638 | } elsif ($r ne "OK") { | ||
| 639 | push @ret, "Error: " . $r; | ||
| 640 | } | ||
| 641 | } | ||
| 642 | } | ||
| 643 | } | ||
| 644 | return \@ret; | ||
| 645 | } | ||
| 646 | |||
| 647 | sub do_actions { | ||
| 648 | my ($self) = @_; | ||
| 649 | $self->run_hooks('actions_begin'); | ||
| 650 | $self->{'generator'}->start_actions($self); | ||
| 651 | qwdebug("actions: ", Dumper($self->{'active'})); | ||
| 652 | $self->do_primary_actions($self->{'active'}, 0); | ||
| 653 | $self->run_hooks('before_actions_end'); | ||
| 654 | $self->{'generator'}->end_actions($self); | ||
| 655 | $self->run_hooks('actions_end'); | ||
| 656 | } | ||
| 657 | |||
| 658 | sub do_action_conform { | ||
| 659 | my ($self) = @_; | ||
| 660 | $self->{'generator'}->start_confirm($self); | ||
| 661 | $self->do_primary_actions($self->{'active'}, 1); | ||
| 662 | $self->pass_vars(); | ||
| 663 | $self->{generator}->do_hidden($self, 'qwizard_tree', | ||
| 664 | $self->qwparam('qwizard_tree')); | ||
| 665 | $self->{'generator'}->end_confirm($self); | ||
| 666 | } | ||
| 667 | |||
| 668 | sub do_primary_action_list { | ||
| 669 | my ($self, $pdesc, $confirmonly) = @_; | ||
| 670 | my $results; | ||
| 671 | |||
| 672 | return if ($pdesc->{'noactions'}); | ||
| 673 | |||
| 674 | my $p = $self->get_primary($pdesc->{'name'}); | ||
| 675 | return if (!$p); | ||
| 676 | |||
| 677 | qwdebug("running actions for ", $p->{name}, " for confirm=", | ||
| 678 | $confirmonly, "\n"); | ||
| 679 | $self->maybe_start_remap($pdesc->{'remap'}, $p); | ||
| 680 | if ($confirmonly) { | ||
| 681 | if (exists($p->{'actions_descr'})) { | ||
| 682 | foreach my $desc (@{$p->{'actions_descr'}}) { | ||
| 683 | if (ref($desc) eq 'CODE') { | ||
| 684 | $desc = $desc->($self); | ||
| 685 | } | ||
| 686 | if ($desc && $desc ne 'OK') { | ||
| 687 | # shouldn't be there but a common mistake. | ||
| 688 | $desc =~ s/^msg:\s*//; | ||
| 689 | $self->{'generator'}->do_confirm_message($self, | ||
| 690 | $self->unparamstr($desc)); | ||
| 691 | } | ||
| 692 | } | ||
| 693 | } | ||
| 694 | } else { | ||
| 695 | if (exists($p->{'actions'})) { | ||
| 696 | my $actions = $p->{'actions'}; | ||
| 697 | $actions = [$actions] | ||
| 698 | if (ref($actions) ne 'ARRAY'); | ||
| 699 | $results = $self->do_list(@$actions); | ||
| 700 | foreach my $r (@$results) { | ||
| 701 | next if ($r eq 'OK'); | ||
| 702 | if ($r =~ /Error: (.*)/) { | ||
| 703 | # XXX: revert entire DB transaction. | ||
| 704 | qwdebug(" ERROR: $1"); | ||
| 705 | $self->{'generator'}->do_action_error($self, $1); | ||
| 706 | } else { | ||
| 707 | $self->{'generator'}->do_action_output($self, $r); | ||
| 708 | } | ||
| 709 | } | ||
| 710 | } | ||
| 711 | } | ||
| 712 | $self->maybe_end_remap($pdesc->{'remap'}, $p); | ||
| 713 | } | ||
| 714 | |||
| 715 | sub do_primary_actions { | ||
| 716 | my ($self, $top, $confirmonly) = @_; | ||
| 717 | my ($i, $c); | ||
| 718 | |||
| 719 | # first run all the runfirst marked children, back to front | ||
| 720 | for($i = $#{$top->{'children'}}; $i > -1; $i--) { | ||
| 721 | $c = $top->{'children'}[$i]; | ||
| 722 | if (!$c->{'actionslast'}) { | ||
| 723 | $self->do_primary_actions($c, $confirmonly); | ||
| 724 | } | ||
| 725 | } | ||
| 726 | |||
| 727 | $self->do_primary_action_list($top, $confirmonly); | ||
| 728 | |||
| 729 | # then run the child actions which are to be run last. | ||
| 730 | for($i = $#{$top->{'children'}}; $i > -1; $i--) { | ||
| 731 | $c = $top->{'children'}[$i]; | ||
| 732 | if ($c->{'actionslast'}) { | ||
| 733 | $self->do_primary_actions($c, $confirmonly); | ||
| 734 | } | ||
| 735 | } | ||
| 736 | } | ||
| 737 | |||
| 738 | # returns 1 if a primary with a given name was ever run. | ||
| 739 | # useful in action sequences to determine if something was done | ||
| 740 | sub primary_was_run { | ||
| 741 | my ($self, $name) = @_; | ||
| 742 | my $run = 0; | ||
| 743 | $self->foreach_primary($self->{'active'}, | ||
| 744 | sub { | ||
| 745 | my $run = $_[2]; | ||
| 746 | $$run++ if ($_[0]->{name} eq $_[1]); | ||
| 747 | }); | ||
| 748 | return 1 if ($run); | ||
| 749 | return 0; | ||
| 750 | } | ||
| 751 | |||
| 752 | sub confirm_or_run_actions { | ||
| 753 | my ($self) = @_; | ||
| 754 | $self->start_page(); | ||
| 755 | if ($self->qwparam('wiz_confirmed') || $self->{'no_confirm'} || | ||
| 756 | $self->qwparam('no_confirm')) { | ||
| 757 | $self->{'state'} = $states{'ACTING'}; | ||
| 758 | |||
| 759 | if (ref($self->{'begin_section_hook'}) eq "CODE") { | ||
| 760 | qwdebug("found begin_section_hook. running."); | ||
| 761 | $self->{'begin_section_hook'}($self); | ||
| 762 | } | ||
| 763 | |||
| 764 | qwdebug("Confirmation given (or not needed). Running actions."); | ||
| 765 | $self->do_actions(); | ||
| 766 | |||
| 767 | # | ||
| 768 | # Call the end_section_hook to close off the HTML table in the | ||
| 769 | # Commit. This is needed to let Netscape properly display the | ||
| 770 | # post-Commit page. | ||
| 771 | # | ||
| 772 | if (ref($self->{'end_section_hook'}) eq "CODE") { | ||
| 773 | qwdebug("found end_section_hook. running."); | ||
| 774 | $self->{'end_section_hook'}($self); | ||
| 775 | } | ||
| 776 | |||
| 777 | # Reset and start over. | ||
| 778 | $self->reset_qwizard(); | ||
| 779 | } elsif ($self->qwparam('wiz_canceled')) { | ||
| 780 | qwdebug("user hit the cancel button"); | ||
| 781 | |||
| 782 | # Reset and start over. | ||
| 783 | $self->reset_qwizard(); | ||
| 784 | $self->{'state'} = $states{'CANCELED'}; | ||
| 785 | return 0; | ||
| 786 | } else { | ||
| 787 | |||
| 788 | $self->{'state'} = $states{'CONFIRMING'}; | ||
| 789 | qwdebug("Displaying confirmation page"); | ||
| 790 | if (ref($self->{'begin_section_hook'}) eq "CODE") { | ||
| 791 | qwdebug("found begin_section_hook. running."); | ||
| 792 | $self->{'begin_section_hook'}($self); | ||
| 793 | } | ||
| 794 | |||
| 795 | $self->do_action_conform(); | ||
| 796 | |||
| 797 | if (ref($self->{'end_section_hook'}) eq "CODE") { | ||
| 798 | qwdebug("found end_section_hook. running."); | ||
| 799 | $self->{'end_section_hook'}($self); | ||
| 800 | } | ||
| 801 | } | ||
| 802 | |||
| 803 | return 1; | ||
| 804 | } | ||
| 805 | |||
| 806 | sub is_done { | ||
| 807 | my $self = shift; | ||
| 808 | return 0 if (($self->{'state'} != $QWizard::states{'ACTING'} && | ||
| 809 | $self->{'state'} != $QWizard::states{'FINISHED'}) || | ||
| 810 | $self->{'state'} == $QWizard::states{'CANCELED'}); | ||
| 811 | return 1; | ||
| 812 | } | ||
| 813 | |||
| 814 | sub reset_qwizard { | ||
| 815 | my $self = shift; | ||
| 816 | $self->{'last_screen'} = 1; | ||
| 817 | $self->{'generator'}->clear_params(); | ||
| 818 | $self->{'state'} = $states{'FINISHED'}; | ||
| 819 | } | ||
| 820 | |||
| 821 | sub save_queue { | ||
| 822 | my ($self, $queue, $name) = @_; | ||
| 823 | $name = "wizard_queue_list" if (!$name); | ||
| 824 | my $str = join(",",@$queue); | ||
| 825 | $self->{generator}->do_hidden($self, $name, $str); | ||
| 826 | qwdebug("saving queue: $name (value=$str)"); | ||
| 827 | return 1; | ||
| 828 | } | ||
| 829 | |||
| 830 | sub pass_queue { | ||
| 831 | my ($self, $name) = @_; | ||
| 832 | $self->{generator}->do_hidden($self, ($name || 'wizard_queue_list'), | ||
| 833 | qwparam($name)); | ||
| 834 | } | ||
| 835 | |||
| 836 | sub get_queue { | ||
| 837 | my ($self, $name) = @_; | ||
| 838 | my @queue_list = split(/,/,$self->qwparam($name || "wizard_queue_list")); | ||
| 839 | return \@queue_list; | ||
| 840 | } | ||
| 841 | |||
| 842 | # adds a list of hidden tags to be passed along next time. | ||
| 843 | sub add_and_pass_vars { | ||
| 844 | my $self = shift; | ||
| 845 | |||
| 846 | my $newvars = $self->add_vars(@_); | ||
| 847 | $self->pass_vars($newvars); | ||
| 848 | } | ||
| 849 | |||
| 850 | sub add_vars { | ||
| 851 | my $self = shift; | ||
| 852 | my $newvars = shift; | ||
| 853 | |||
| 854 | # already in a form entry, just add them to the list for next time; | ||
| 855 | foreach my $i (@_) { | ||
| 856 | $newvars->{$i} = 1; | ||
| 857 | } | ||
| 858 | return $newvars; | ||
| 859 | } | ||
| 860 | |||
| 861 | # passes known list of hidden tags. | ||
| 862 | sub pass_vars { | ||
| 863 | my $self = shift; | ||
| 864 | my $vars = $self->qwparam('pass_vars') || ""; | ||
| 865 | my $newvars = shift; | ||
| 866 | |||
| 867 | qwdebug("vars to process: $vars"); | ||
| 868 | if (defined($vars)) { | ||
| 869 | foreach my $i (split(/,/,$vars), @{$self->{pass_next}}) { | ||
| 870 | if ($self->{'generator'}->skip_storage($i)) { | ||
| 871 | # we were asked to skip this particular variable for | ||
| 872 | # future use. | ||
| 873 | delete $newvars->{$i}; | ||
| 874 | next; | ||
| 875 | } | ||
| 876 | next if ($newvars->{$i}); # already handled | ||
| 877 | $newvars->{$i} = 1; | ||
| 878 | qwdebug("passing on: $i -> ", qwparam($i)); | ||
| 879 | $self->{'generator'}->do_pass($self, $i); | ||
| 880 | } | ||
| 881 | } | ||
| 882 | |||
| 883 | qwdebug("passing on pass_vars: ", join(",",keys(%$newvars))); | ||
| 884 | my @pass_next = ($self && ref($self->{pass_next}) eq "ARRAY") ? | ||
| 885 | @{$self->{pass_next}} : (); | ||
| 886 | |||
| 887 | $self->{'generator'}->do_hidden($self, | ||
| 888 | 'pass_vars', join(",",keys(%$newvars), | ||
| 889 | @pass_next)); | ||
| 890 | } | ||
| 891 | |||
| 892 | # returns a list of values based on an array of values, code, and | ||
| 893 | # arrays containing code and parameters. | ||
| 894 | sub get_values { | ||
| 895 | my ($self, $vals, $norecurs, $extra_args) = @_; | ||
| 896 | my @values; | ||
| 897 | return [] if (!defined($vals)); | ||
| 898 | if (ref($vals) eq "ARRAY") { | ||
| 899 | if (ref($vals->[0]) eq "CODE") { | ||
| 900 | my @args = @$vals; | ||
| 901 | my $cd = shift @args; | ||
| 902 | push(@args, @$extra_args) if ref($extra_args) eq "ARRAY"; | ||
| 903 | if ($norecurs) { | ||
| 904 | push @values, @{$cd->($self, @args)}; | ||
| 905 | } else { | ||
| 906 | push @values, @{$self->get_values($cd->($self, @args))}; | ||
| 907 | } | ||
| 908 | } else { | ||
| 909 | if ($norecurs) { | ||
| 910 | push @values, @$vals; | ||
| 911 | } else { | ||
| 912 | map { push @values, @{$self->get_values($_)} } @$vals; | ||
| 913 | } | ||
| 914 | } | ||
| 915 | } elsif (ref($vals) eq "CODE") { | ||
| 916 | if ($norecurs) { | ||
| 917 | push @values, @{$vals->($self, $extra_args)}; | ||
| 918 | } else { | ||
| 919 | push @values, @{$self->get_values($vals->($self, $extra_args))}; | ||
| 920 | } | ||
| 921 | } elsif (ref($vals) eq 'HASH') { | ||
| 922 | @values = ($vals); | ||
| 923 | } elsif (ref($vals) eq "") { | ||
| 924 | $vals =~ s/\@([^\@]+)\@/$self->qwparam($1)/eg; | ||
| 925 | @values = ($vals); | ||
| 926 | } | ||
| 927 | return \@values; | ||
| 928 | } | ||
| 929 | |||
| 930 | # same as get_values, but truncates return list to a single value. | ||
| 931 | sub get_value { | ||
| 932 | my ($self, $vals, $norecurse, $extra_args) = @_; | ||
| 933 | return ${$self->get_values($vals, $norecurse, $extra_args)}[0]; | ||
| 934 | } | ||
| 935 | |||
| 936 | sub get_values_from_labels { | ||
| 937 | my ($self, $labels) = @_; | ||
| 938 | my ($vals, $i); | ||
| 939 | if (ref($labels) eq 'HASH') { | ||
| 940 | @$vals = keys(%$labels); | ||
| 941 | } elsif (ref($labels) eq 'ARRAY') { | ||
| 942 | for($i = 0; $i <= $#$labels; $i += 2) { | ||
| 943 | push @$vals, $labels->[$i]; | ||
| 944 | } | ||
| 945 | } | ||
| 946 | return $vals; | ||
| 947 | } | ||
| 948 | |||
| 949 | sub get_labels { | ||
| 950 | my ($self, $q) = @_; | ||
| 951 | my $labels; | ||
| 952 | if (ref($q->{labels}) eq "HASH") { | ||
| 953 | %$labels = %{$q->{labels}}; | ||
| 954 | } else { | ||
| 955 | %$labels = @{$self->get_values($q->{'labels'})} if ($q->{'labels'}); | ||
| 956 | } | ||
| 957 | |||
| 958 | return $labels; | ||
| 959 | } | ||
| 960 | |||
| 961 | sub get_values_and_labels { | ||
| 962 | my ($self, $q, $pad) = @_; | ||
| 963 | $pad = "" if (!$pad); | ||
| 964 | my ($vals, $labels); | ||
| 965 | if (exists($q->{values})) { | ||
| 966 | $vals = $self->get_values($q->{'values'}); | ||
| 967 | if (ref($q->{labels}) eq "HASH") { | ||
| 968 | %$labels = %{$q->{labels}}; | ||
| 969 | } else { | ||
| 970 | %$labels = @{$self->get_values($q->{'labels'})} if ($q->{'labels'}); | ||
| 971 | } | ||
| 972 | # create a label if one doesn't exist | ||
| 973 | foreach my $v (@$vals) { | ||
| 974 | $labels->{$v} = $v if (!exists($labels->{$v})); | ||
| 975 | } | ||
| 976 | } else { | ||
| 977 | # no values, generate them from the labels | ||
| 978 | if (ref($q->{labels}) eq "HASH") { | ||
| 979 | $vals = [keys(%{$q->{labels}})]; | ||
| 980 | %{$labels} = %{$q->{labels}}; | ||
| 981 | } else { | ||
| 982 | my $labs = $self->get_values($q->{'labels'}); | ||
| 983 | %$labels = @$labs; | ||
| 984 | $vals = $self->get_values_from_labels($labs); | ||
| 985 | } | ||
| 986 | } | ||
| 987 | if ($pad ne "") { | ||
| 988 | # prepend any specified padding for spacing reasons | ||
| 989 | map { $labels->{$_} = $pad . $labels->{$_} } keys(%$labels); | ||
| 990 | } | ||
| 991 | return ($vals, $labels); | ||
| 992 | } | ||
| 993 | |||
| 994 | sub make_help_link { | ||
| 995 | my ($self, $p, $qcount, $text) = @_; | ||
| 996 | my $top = $self->{top_location}; | ||
| 997 | $top .= (($top =~ /\?/) ? "&" : "?"); | ||
| 998 | $top .= | ||
| 999 | "disp_help_p=$p->{module_name}&disp_q_num=$qcount"; | ||
| 1000 | return "<a target=\"helpwin\" onClick=\"window.open('$top','helpwin','width=500,height=300,toolbar=0,status=1')\" href=\"$top\">"; | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | sub build_dynamic_questions { | ||
| 1004 | my ($self, $p, $newqs, @questions) = @_; | ||
| 1005 | |||
| 1006 | # generate dynamic questions | ||
| 1007 | foreach my $q (@questions) { | ||
| 1008 | # XXX: use arrays to mean something else special in the future? | ||
| 1009 | if (ref($q) ne "HASH") { | ||
| 1010 | # question is not a normal hash, push it on anyway | ||
| 1011 | # (labels and separators are strings) | ||
| 1012 | push @$newqs, $q; | ||
| 1013 | } elsif ($q->{type} eq 'dynamic') { | ||
| 1014 | # question is a dynamic question | ||
| 1015 | qwdebug("Expanding a dynamic"); | ||
| 1016 | if ($q->{'doif'} && ! $q->{'doif'}->($q, $self, $p)) { | ||
| 1017 | qwdebug("Dynamic question skipped"); | ||
| 1018 | next; | ||
| 1019 | } | ||
| 1020 | # my $v = $self->get_values($q->{values}); | ||
| 1021 | my @dynamicqs = @{$self->get_values($q->{values})}; | ||
| 1022 | |||
| 1023 | # allow for sub-expansion of these questions as well: recursion | ||
| 1024 | $self->build_dynamic_questions($p, $newqs, @dynamicqs); | ||
| 1025 | } else { | ||
| 1026 | # question is a normal hash, and not dynamic. push it on. | ||
| 1027 | push @$newqs, $q; | ||
| 1028 | } | ||
| 1029 | } | ||
| 1030 | } | ||
| 1031 | |||
| 1032 | # collects data | ||
| 1033 | sub ask_questions { | ||
| 1034 | my ($self) = @_; | ||
| 1035 | my (@passvars, %results, $submods, $count); | ||
| 1036 | my $repeat = 1; | ||
| 1037 | $self->{'passvars'} = \@passvars; # ugly hack for multi-checkboxes | ||
| 1038 | my $newvars = {}; | ||
| 1039 | my $donefirstpass = 0; | ||
| 1040 | |||
| 1041 | $self->{'allqcount'} = 0; | ||
| 1042 | |||
| 1043 | # count outstanding errors | ||
| 1044 | $self->{'errs'} = 0; | ||
| 1045 | $self->foreach_primary($self->{'active'}, | ||
| 1046 | sub { | ||
| 1047 | if ($_[0]->{'errcount'}) { | ||
| 1048 | $_[1]->{'errs'} += $_[0]->{'errcount'}; | ||
| 1049 | qwdebug("primary $_[0]{name} has ", | ||
| 1050 | $_[0]->{'errcount'}, " errors"); | ||
| 1051 | delete $_[0]->{'errcount'}; | ||
| 1052 | } | ||
| 1053 | }, $self); | ||
| 1054 | |||
| 1055 | $self->{'generator'}->start_primaries(); | ||
| 1056 | # process each primary we should be processing | ||
| 1057 | while (my $pdesc = $self->get_next_primary()) { | ||
| 1058 | my $p = $self->get_primary($pdesc->{'name'}); | ||
| 1059 | $repeat += $pdesc->{'merge'} - 1 if ($pdesc->{'merge'}); | ||
| 1060 | |||
| 1061 | if (ref($p) ne "HASH") { | ||
| 1062 | qwdebug("ERROR: primary $pdesc->{'name'} does not exist\n"); | ||
| 1063 | # XXX: not portable error code | ||
| 1064 | print "QWizard Error. Called on primary \"$pdesc->{name}\", which doesn't exist\n"; | ||
| 1065 | $pdesc->{'done'} = $PRIM_ANSWERED; | ||
| 1066 | die if (!$self->{'skip_bad_primary_requests'}); | ||
| 1067 | next; | ||
| 1068 | } | ||
| 1069 | |||
| 1070 | if (exists($p->{'doif'}) && ! $p->{'doif'}->(undef, $self, $p)) { | ||
| 1071 | qwdebug("Primary skipped by doif result"); | ||
| 1072 | $pdesc->{'done'} = $PRIM_ANSWERED; | ||
| 1073 | next; | ||
| 1074 | } | ||
| 1075 | # stash remap tag in prim to establish context | ||
| 1076 | if (exists $pdesc->{'remap'}) { | ||
| 1077 | $p->{'remap'} = $pdesc->{'remap'}; | ||
| 1078 | } | ||
| 1079 | qwdebug("Processing Primary (more=$repeat): " . | ||
| 1080 | ($p->{name} || $p->{title}) . | ||
| 1081 | (($p->{'remap'}) ? " remap prefix: $p->{remap}" : "")); | ||
| 1082 | $self->{'state'} = $states{'ASKING'}; | ||
| 1083 | if ($p->{'take_over'}) { | ||
| 1084 | qwdebug("Passing control to primary, take_over hook found"); | ||
| 1085 | $p->{'take_over'}($self, $p); | ||
| 1086 | return 0; | ||
| 1087 | } else { | ||
| 1088 | if (!$count) { | ||
| 1089 | if (ref($self->{'begin_section_hook'}) eq "CODE") { | ||
| 1090 | qwdebug("found begin_section_hook. running."); | ||
| 1091 | $self->{'begin_section_hook'}($self, $p); | ||
| 1092 | } | ||
| 1093 | } | ||
| 1094 | } | ||
| 1095 | if (exists($p->{'setup'})) { | ||
| 1096 | qwdebug("primary setup function found. Running."); | ||
| 1097 | delete $p->{'runstate'} if (exists($p->{'runstate'})); | ||
| 1098 | my $res = $p->{'setup'}($self, $p); | ||
| 1099 | if ($res ne "OK" && $res ne "") { | ||
| 1100 | qwdebug("primary's setup returned an error: $res"); | ||
| 1101 | $self->{'generator'}->do_error('', $self, $p, $res); | ||
| 1102 | return 0; | ||
| 1103 | } | ||
| 1104 | } | ||
| 1105 | $count++; | ||
| 1106 | |||
| 1107 | # the top most bar, if it exists | ||
| 1108 | if (($p->{'topbar'} || $self->{'topbar'})) { | ||
| 1109 | my @barobjects; | ||
| 1110 | @barobjects = ($self->{'topbar'}) | ||
| 1111 | if (ref($self->{'topbar'}) ne 'ARRAY'); | ||
| 1112 | @barobjects = @{$self->{'topbar'}} | ||
| 1113 | if (ref($self->{'topbar'}) eq 'ARRAY'); | ||
| 1114 | push @barobjects, ($p->{'topbar'}) | ||
| 1115 | if (ref($p->{'topbar'}) ne 'ARRAY'); | ||
| 1116 | push @barobjects, @{$p->{'topbar'}} | ||
| 1117 | if (ref($p->{'topbar'}) eq 'ARRAY'); | ||
| 1118 | $self->{'generator'}->do_top_bar(undef, $self, $p, \@barobjects); | ||
| 1119 | } | ||
| 1120 | |||
| 1121 | $self->{'generator'}->start_main_section($self, $p); | ||
| 1122 | |||
| 1123 | if (!$donefirstpass) { | ||
| 1124 | $donefirstpass = 1; | ||
| 1125 | |||
| 1126 | # left side frame if it exists | ||
| 1127 | if (($p->{'leftside'} || $self->{'leftside'})) { | ||
| 1128 | my @sideobjects; | ||
| 1129 | if (exists($self->{'leftside'})) { | ||
| 1130 | if (ref($self->{'leftside'}) eq 'ARRAY') { | ||
| 1131 | @sideobjects = @{$self->{'leftside'}} | ||
| 1132 | } else { | ||
| 1133 | @sideobjects = ($self->{'leftside'}) | ||
| 1134 | } | ||
| 1135 | } | ||
| 1136 | if (exists($p->{'leftside'})) { | ||
| 1137 | if (ref($p->{'leftside'}) eq 'ARRAY') { | ||
| 1138 | push @sideobjects, @{$p->{'leftside'}} | ||
| 1139 | } else { | ||
| 1140 | push @sideobjects, ($p->{'leftside'}) | ||
| 1141 | } | ||
| 1142 | } | ||
| 1143 | $self->{'generator'}->do_left_side(undef, $self, $p, | ||
| 1144 | \@sideobjects); | ||
| 1145 | } | ||
| 1146 | |||
| 1147 | $self->{'generator'}->start_center_section($self, $p); | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | if ($p->{'questions'}) { | ||
| 1151 | $self->{'qcount'} = -1; | ||
| 1152 | qwdebug("starting queston processing/display"); | ||
| 1153 | $self->run_hooks('ask_questions_begin'); | ||
| 1154 | $self->{'generator'}->start_questions($self, $p, | ||
| 1155 | $self->unparamstr($self->get_value($p->{title},undef,[$p])), | ||
| 1156 | ((defined(qwpref('pref_intro')) && qwpref('pref_intro') ne '0') ? $self->unparamstr($self->get_value($p->{introduction},undef,[$p])) : "")); | ||
| 1157 | $self->run_hooks('ask_questions_started'); | ||
| 1158 | my @questions = @{$p->{'questions'}}; | ||
| 1159 | my ($q, @newqs); | ||
| 1160 | |||
| 1161 | $self->build_dynamic_questions($p, \@newqs, @questions); | ||
| 1162 | |||
| 1163 | if ($pdesc->{'error'}) { | ||
| 1164 | $self->{'generator'}->do_error('', $self, $p, $pdesc->{'error'}); | ||
| 1165 | delete $pdesc->{'error'}; | ||
| 1166 | } | ||
| 1167 | foreach $q (@newqs) { | ||
| 1168 | $self->{'currentq'} = $q; # for generators mostly | ||
| 1169 | $self->{'currentp'} = $p; # for generators mostly | ||
| 1170 | push @passvars, $self->ask_question($p, $q); | ||
| 1171 | } | ||
| 1172 | $self->{'generator'}->end_questions($self, $p); | ||
| 1173 | $self->run_hooks('ask_questions_end'); | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | $self->{'generator'}->end_main_section($self, $p); | ||
| 1177 | |||
| 1178 | if ($p->{'sub_modules'}) { | ||
| 1179 | qwdebug("Adding submodules to todo list: $p->{sub_modules}"); | ||
| 1180 | my $vals = $self->get_values($p->{'sub_modules'}); | ||
| 1181 | if ($#$vals > -1) { | ||
| 1182 | map { | ||
| 1183 | if (!exists($self->{primaries}{$_})) { | ||
| 1184 | print STDERR "ERROR: Reference to no such primary: $_\n"; | ||
| 1185 | # XXX: error to console! | ||
| 1186 | } else { | ||
| 1187 | $self->add_todos($_); | ||
| 1188 | qwdebug(" adding: $_"); | ||
| 1189 | } | ||
| 1190 | } @$vals; | ||
| 1191 | } | ||
| 1192 | } | ||
| 1193 | if (exists($p->{'add_vars'})) { | ||
| 1194 | # XXX: this doesn't do remapping of these names properly. | ||
| 1195 | $newvars = $self->add_vars($newvars, @{$p->{'add_vars'}}); | ||
| 1196 | } | ||
| 1197 | $repeat--; | ||
| 1198 | $pdesc->{'done'} = $PRIM_ANSWERED; | ||
| 1199 | # remove remap context from prim | ||
| 1200 | delete $p->{'remap'} if exists $p->{'remap'}; | ||
| 1201 | |||
| 1202 | if ($repeat <= 0) { | ||
| 1203 | qwdebug("Done processing screen's primaries "); | ||
| 1204 | if ($self->{'allqcount'}) { | ||
| 1205 | qwdebug("Done with questions... Getting user input\n"); | ||
| 1206 | |||
| 1207 | # figure out the value of the next button so we can pass it down | ||
| 1208 | my $next = ((defined(qwparam('no_actions')) | ||
| 1209 | && qwparam('no_actions') ne '') || | ||
| 1210 | !$self->has_actions_or_post_answers()) ? | ||
| 1211 | '_Finished' : '_Next'; | ||
| 1212 | if ((defined(qwparam('no_actions')) && | ||
| 1213 | qwparam('no_actions') ne '') || | ||
| 1214 | !$self->has_actions_or_post_answers()) { | ||
| 1215 | $self->{'last_screen'} = 1; | ||
| 1216 | } | ||
| 1217 | $next = qwparam('QWizard_next') if (qwparam('QWizard_next')); | ||
| 1218 | |||
| 1219 | # first close the middle section if needed | ||
| 1220 | $self->{'generator'}->end_center_section($self, $p, $next); | ||
| 1221 | |||
| 1222 | # append the right side modules | ||
| 1223 | # right side frame if it exists | ||
| 1224 | if (exists($p->{'rightside'}) || exists($self->{'rightside'})) { | ||
| 1225 | my @sideobjects; | ||
| 1226 | |||
| 1227 | # apply the wizards right side objects | ||
| 1228 | if (exists($self->{'rightside'})) { | ||
| 1229 | if (ref($self->{'rightside'}) eq 'ARRAY') { | ||
| 1230 | @sideobjects = @{$self->{'rightside'}} | ||
| 1231 | } else { | ||
| 1232 | @sideobjects = ($self->{'rightside'}) | ||
| 1233 | } | ||
| 1234 | } | ||
| 1235 | |||
| 1236 | # apply the (XXX: last only) primaries objects | ||
| 1237 | if (exists($p->{'rightside'})) { | ||
| 1238 | if (ref($p->{'rightside'}) eq 'ARRAY') { | ||
| 1239 | push @sideobjects, @{$p->{'rightside'}} | ||
| 1240 | } else { | ||
| 1241 | push @sideobjects, ($p->{'rightside'}) | ||
| 1242 | } | ||
| 1243 | } | ||
| 1244 | $self->{'generator'}->do_right_side(undef, $self, $p, | ||
| 1245 | \@sideobjects); | ||
| 1246 | } | ||
| 1247 | |||
| 1248 | $self->{'generator'}->end_primaries(); | ||
| 1249 | |||
| 1250 | # save current vars list. | ||
| 1251 | $newvars = $self->add_and_pass_vars($newvars, @passvars); | ||
| 1252 | |||
| 1253 | # save the call tree | ||
| 1254 | my $treestr = $self->encode_todo_list(); | ||
| 1255 | $self->{generator}->do_hidden($self, 'qwizard_tree', $treestr); | ||
| 1256 | qwdebug("saving qwizard tree: $treestr"); | ||
| 1257 | |||
| 1258 | # call the generator's wait_for routine to wrap up. | ||
| 1259 | if ($self->{'generator'}->wait_for($self, $next, $p)) { | ||
| 1260 | $self->run_hooks('end_section', $p); | ||
| 1261 | if ($self->qwparam('redo_screen')) { | ||
| 1262 | delete $self->{'last_screen'} | ||
| 1263 | if (exists($self->{'last_screen'})); | ||
| 1264 | } | ||
| 1265 | $self->reset_qwizard() if ($self->{'last_screen'}); | ||
| 1266 | return 0; | ||
| 1267 | } | ||
| 1268 | $self->reset_qwizard() if ($self->{'last_screen'}); | ||
| 1269 | } else { | ||
| 1270 | # no questions generated by current screen, try one more. | ||
| 1271 | $repeat++; | ||
| 1272 | } | ||
| 1273 | } | ||
| 1274 | } | ||
| 1275 | return 1; | ||
| 1276 | } | ||
| 1277 | |||
| 1278 | sub do_widget { | ||
| 1279 | my ($self, $q, $p, $def) = @_; | ||
| 1280 | # have the generator display the question widget based on the type | ||
| 1281 | my $typemap = | ||
| 1282 | $self->{'generator'}->get_handler($q->{'type'} || 'text',$q); | ||
| 1283 | if ($typemap) { | ||
| 1284 | # The generator supplied its own function and what it | ||
| 1285 | # wants to receive argument-wise. | ||
| 1286 | my $arguments = | ||
| 1287 | $self->{'generator'}->get_arguments($self, $q, | ||
| 1288 | $typemap->{'argdef'}, $def); | ||
| 1289 | $typemap->{'function'}->($self->{'generator'}, $q, $self, $p, | ||
| 1290 | @$arguments); | ||
| 1291 | } elsif ($q->{type} eq "hidden") { | ||
| 1292 | $self->{'generator'}->do_hidden($self, $q->{'name'}, | ||
| 1293 | $def || $self->get_value($q->{values})); | ||
| 1294 | if ($q->{text}) { | ||
| 1295 | $self->{'generator'}->do_label($q, $self, $p, | ||
| 1296 | [$q->{text}], | ||
| 1297 | $def); | ||
| 1298 | } | ||
| 1299 | } elsif ($q->{type} eq "raw") { | ||
| 1300 | print $self->get_value($q->{values}, $def); | ||
| 1301 | } elsif ($q->{type} eq "tree") { | ||
| 1302 | $self->{'generator'}->do_tree($q, $self, $p, | ||
| 1303 | $self->get_labels($q), | ||
| 1304 | $self->get_value($q->{'expand_all'}), | ||
| 1305 | $def); | ||
| 1306 | } else { | ||
| 1307 | $typemap = | ||
| 1308 | $self->{'generator'}->get_handler('unknown',$q); | ||
| 1309 | if ($typemap) { | ||
| 1310 | # The generator supplied its own error handling function | ||
| 1311 | my $arguments = | ||
| 1312 | $self->{'generator'}->get_arguments($self, $q, | ||
| 1313 | $typemap->{'argdef'}, | ||
| 1314 | $def); | ||
| 1315 | $typemap->{'function'}->($self->{'generator'}, $q, $self, $p, | ||
| 1316 | @$arguments); | ||
| 1317 | } else { | ||
| 1318 | print STDERR "Unsupported question type ($q->{type})\n"; | ||
| 1319 | } | ||
| 1320 | qwdebug("Unsupported question type ($q->{type})"); | ||
| 1321 | qwdebug(Dumper($q)); | ||
| 1322 | return; | ||
| 1323 | } | ||
| 1324 | } | ||
| 1325 | |||
| 1326 | sub ask_question { | ||
| 1327 | my ($self, $p, $q) = @_; | ||
| 1328 | |||
| 1329 | my $pdesc = $self->{'context'}; | ||
| 1330 | |||
| 1331 | # increment the question counts | ||
| 1332 | $self->{'qcount'}++; | ||
| 1333 | $self->{'allqcount'}++; | ||
| 1334 | |||
| 1335 | if (ref($q) ne "HASH") { | ||
| 1336 | $self->{'generator'}->do_separator($q, $self, $p, $q); | ||
| 1337 | return; | ||
| 1338 | } else { | ||
| 1339 | qwdebug("Processing Question: " . | ||
| 1340 | sprintf("%-10s %s", ($q->{type} || "text"), | ||
| 1341 | "$q->{name}")); | ||
| 1342 | # determine the default value. this is either pulled from the | ||
| 1343 | # question default clause or from the last value entered from | ||
| 1344 | # the user if an error occurred and we're redisplaying the | ||
| 1345 | # question (or repeating it for a tree). | ||
| 1346 | my $def; | ||
| 1347 | if ($self->qwparam('redoing_now')) { | ||
| 1348 | $def = $self->qwparam($q->{'name'}); | ||
| 1349 | qwdebug(" pulling original: $q->{name}: $def\n"); | ||
| 1350 | } else { | ||
| 1351 | if (qwparam($q->{'name'}) && | ||
| 1352 | !$q->{'override'} && !$self->{'override'}) { | ||
| 1353 | print STDERR "*** QWizard: Warning redefining value from question named $q->{name} (value was: " . qwparam($q->{'name'}) . "). This may or may not have been intentional. Add the 'override' flag to the second question, or rename the parent's question, to turn off this warning in the future\n"; | ||
| 1354 | } | ||
| 1355 | $def = $self->get_value($q->{default}, undef, [$p, $q]); | ||
| 1356 | } | ||
| 1357 | |||
| 1358 | if ($q->{'doif'} && ! $q->{'doif'}->($q, $self, $p)) { | ||
| 1359 | qwdebug("Question skipped"); | ||
| 1360 | $self->{'qcount'}--; | ||
| 1361 | $self->{'allqcount'}++; | ||
| 1362 | if ($self->qwparam($q->{'name'}) eq '' && | ||
| 1363 | $def ne '') { | ||
| 1364 | $self->qwparam($q->{'name'}, $def); | ||
| 1365 | } | ||
| 1366 | return; | ||
| 1367 | } | ||
| 1368 | |||
| 1369 | # display the error | ||
| 1370 | if ($pdesc && exists($pdesc->{'qerrs'}{$q->{'name'}})) { | ||
| 1371 | $self->{'generator'}->do_error($q, $self, $p, | ||
| 1372 | $pdesc->{'qerrs'}{$q->{'name'}}); | ||
| 1373 | delete $pdesc->{'qerrs'}{$q->{'name'}}; | ||
| 1374 | } | ||
| 1375 | |||
| 1376 | # have the generator display the question text | ||
| 1377 | $self->{'generator'}->do_question($q, $self, $p, | ||
| 1378 | $self->unparamstr($self->get_value($q->{text})), | ||
| 1379 | $self->{'qcount'}); | ||
| 1380 | |||
| 1381 | $self->do_widget($q, $p, $def); | ||
| 1382 | # return the name | ||
| 1383 | $self->{'generator'}->do_question_end($q, $self, $p, $self->{'qcount'}); | ||
| 1384 | return $q->{'name'}; | ||
| 1385 | } | ||
| 1386 | } | ||
| 1387 | |||
| 1388 | sub display_help { | ||
| 1389 | my $self = shift; | ||
| 1390 | $self->start_page(); | ||
| 1391 | my $p = $self->{'primaries'}{$self->qwparam('disp_help_p')}; | ||
| 1392 | if (!$p) { | ||
| 1393 | print "<h1>No help available</h1>","\n"; | ||
| 1394 | print STDERR "illegal help requested for non-existent primary: " . | ||
| 1395 | $self->qwparam('disp_help_p') . "\n"; | ||
| 1396 | return; | ||
| 1397 | } | ||
| 1398 | my $q = $p->{'questions'}[$self->qwparam('disp_q_num')]; | ||
| 1399 | if (!$q) { | ||
| 1400 | print "<h1>No help available</h1>","\n"; | ||
| 1401 | print STDERR "illegal help requested for non-existent question #" . | ||
| 1402 | $self->qwparam('disp_q_num') . " in primary: " . $self->qwparam('disp_help_p') . "\n"; | ||
| 1403 | return; | ||
| 1404 | } | ||
| 1405 | |||
| 1406 | if ($q->{text}) { | ||
| 1407 | print "<h1>Help information for $q->{text}</h1>"; | ||
| 1408 | } else { | ||
| 1409 | print "<h1>Help text</h1>"; | ||
| 1410 | } | ||
| 1411 | print "<p>\n"; | ||
| 1412 | if ($q->{helptext}) { | ||
| 1413 | print "<table width=450><tr><td>\n"; | ||
| 1414 | print "$q->{helptext}\n"; | ||
| 1415 | print "</td></tr></table>\n"; | ||
| 1416 | } else { | ||
| 1417 | print "none available!\n"; | ||
| 1418 | print STDERR "no help available for question #" . | ||
| 1419 | $self->qwparam('disp_q_num') . " in primary: " . $self->qwparam('disp_help_p') . "\n"; | ||
| 1420 | } | ||
| 1421 | } | ||
| 1422 | |||
| 1423 | sub unparamstr { | ||
| 1424 | my ($self, $str) = @_; | ||
| 1425 | $str =~ s/\@([^\@]+)\@/qwparam($1)/eg; | ||
| 1426 | return $str; | ||
| 1427 | } | ||
| 1428 | |||
| 1429 | sub start_page { | ||
| 1430 | my $self = shift; | ||
| 1431 | $qwcurrent = $self; | ||
| 1432 | qwdebug("starting page"); | ||
| 1433 | $self->run_hooks('start_page_begin'); | ||
| 1434 | $self->{'generator'}->init_screen($self, | ||
| 1435 | $self->{'generator'}->{'title'} || | ||
| 1436 | $self->{'title'} || | ||
| 1437 | 'the wizard'); | ||
| 1438 | $self->run_hooks('start_page_end'); | ||
| 1439 | } | ||
| 1440 | |||
| 1441 | sub print_stack_trace { | ||
| 1442 | return if(42 > 0); | ||
| 1443 | print STDERR "stack trace:\n"; | ||
| 1444 | for (my $i = 1; $i <= 10; $i++) { | ||
| 1445 | my @callinfo = caller($i); | ||
| 1446 | print STDERR " $callinfo[0] $callinfo[1]:$callinfo[2] sub:$callinfo[3]\n"; | ||
| 1447 | } | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | sub forget_param { | ||
| 1451 | my $self = shift; | ||
| 1452 | $self->{'generator'}->forget_param(@_); | ||
| 1453 | } | ||
| 1454 | |||
| 1455 | sub qwparam { | ||
| 1456 | my $it = shift; | ||
| 1457 | my $self; | ||
| 1458 | if (ref($it) eq "QWizard") { | ||
| 1459 | $self = $it; | ||
| 1460 | $it = shift; | ||
| 1461 | } else { | ||
| 1462 | $self = $qwcurrent; | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | my $generator = $self->{'generator'}; | ||
| 1466 | |||
| 1467 | if (!$generator) { | ||
| 1468 | print STDERR "QWizard::qwparam: no generator\n"; | ||
| 1469 | print_stack_trace(); | ||
| 1470 | return(""); | ||
| 1471 | } | ||
| 1472 | |||
| 1473 | return $generator->qwparam($it, @_); | ||
| 1474 | } | ||
| 1475 | |||
| 1476 | sub qwparams { | ||
| 1477 | my $self; | ||
| 1478 | if (ref($_[0]) eq "QWizard") { | ||
| 1479 | $self = shift; | ||
| 1480 | } else { | ||
| 1481 | $self = $qwcurrent; | ||
| 1482 | } | ||
| 1483 | |||
| 1484 | my $generator = $self->{'generator'}; | ||
| 1485 | |||
| 1486 | if (!$generator) { | ||
| 1487 | print STDERR "QWizard::qwparams: no generator\n"; | ||
| 1488 | print_stack_trace(); | ||
| 1489 | return(""); | ||
| 1490 | } | ||
| 1491 | |||
| 1492 | my @results; | ||
| 1493 | foreach my $name (@_) { | ||
| 1494 | push @results, $generator->qwparam($name) || ''; | ||
| 1495 | } | ||
| 1496 | return @results; | ||
| 1497 | } | ||
| 1498 | |||
| 1499 | sub qw_upload_fh { | ||
| 1500 | my $it = shift; | ||
| 1501 | my $self; | ||
| 1502 | if (ref($it) eq "QWizard") { | ||
| 1503 | $self = $it; | ||
| 1504 | $it = shift; | ||
| 1505 | } else { | ||
| 1506 | $self = $qwcurrent; | ||
| 1507 | } | ||
| 1508 | |||
| 1509 | my $generator = $self->{'generator'}; | ||
| 1510 | |||
| 1511 | if (!$generator) { | ||
| 1512 | print STDERR "QWizard::qw_upload_fh: no generator\n"; | ||
| 1513 | print_stack_trace(); | ||
| 1514 | |||
| 1515 | return(""); | ||
| 1516 | } | ||
| 1517 | |||
| 1518 | return $generator->qw_upload_fh($it, @_); | ||
| 1519 | } | ||
| 1520 | |||
| 1521 | sub qw_upload_file { | ||
| 1522 | my $it = shift; | ||
| 1523 | my $self; | ||
| 1524 | if (ref($it) eq "QWizard") { | ||
| 1525 | $self = $it; | ||
| 1526 | $it = shift; | ||
| 1527 | } else { | ||
| 1528 | $self = $qwcurrent; | ||
| 1529 | } | ||
| 1530 | |||
| 1531 | my $generator = $self->{'generator'}; | ||
| 1532 | |||
| 1533 | if (!$generator) { | ||
| 1534 | print STDERR "QWizard::qw_upload_file: no generator\n"; | ||
| 1535 | print_stack_trace(); | ||
| 1536 | |||
| 1537 | return(""); | ||
| 1538 | } | ||
| 1539 | |||
| 1540 | return $generator->qw_upload_file($it, @_); | ||
| 1541 | } | ||
| 1542 | |||
| 1543 | sub qwpref { | ||
| 1544 | my $it = shift; | ||
| 1545 | my $self; | ||
| 1546 | if (ref($it) eq "QWizard") { | ||
| 1547 | $self = $it; | ||
| 1548 | $it = shift; | ||
| 1549 | } else { | ||
| 1550 | $self = $qwcurrent; | ||
| 1551 | } | ||
| 1552 | my $generator = $self->{'generator'}; | ||
| 1553 | |||
| 1554 | if (!$generator) { | ||
| 1555 | print STDERR "QWizard::qwpref: no generator\n"; | ||
| 1556 | print_stack_trace(); | ||
| 1557 | return(""); | ||
| 1558 | } | ||
| 1559 | return $generator->qwpref($it, @_); | ||
| 1560 | } | ||
| 1561 | |||
| 1562 | # | ||
| 1563 | # De/Encoding of primary decision tree based into ASCII storage strings | ||
| 1564 | # and other primary list manipulation routines. | ||
| 1565 | # | ||
| 1566 | sub encode_todo_list { | ||
| 1567 | my ($self, $top) = @_; | ||
| 1568 | $top = $self->{'active'} if (!$top); | ||
| 1569 | |||
| 1570 | my $name = $top->{'name'}; | ||
| 1571 | # XXX: sub-mark [ , ] , , , and : | ||
| 1572 | |||
| 1573 | # encode keywords | ||
| 1574 | foreach my $k (keys(%$top)) { | ||
| 1575 | next if ($k =~ /^(children|parent|name|qerrs)/); | ||
| 1576 | $name .= ":$k:$top->{$k}"; | ||
| 1577 | } | ||
| 1578 | |||
| 1579 | # encode children | ||
| 1580 | if ($top->{'children'} && $#{$top->{'children'}} > -1) { | ||
| 1581 | my @cnames; | ||
| 1582 | foreach my $c (@{$top->{'children'}}) { | ||
| 1583 | push @cnames, $self->encode_todo_list($c); | ||
| 1584 | } | ||
| 1585 | $name .= "[" . join(",",@cnames) . "]"; | ||
| 1586 | } | ||
| 1587 | return $name; | ||
| 1588 | } | ||
| 1589 | |||
| 1590 | sub decode_todo_list { | ||
| 1591 | my ($self, $str) = @_; | ||
| 1592 | my $top = {}; | ||
| 1593 | |||
| 1594 | $$str =~ s/^([^\]\[:,]+)//; | ||
| 1595 | $top->{'name'} = $1; | ||
| 1596 | |||
| 1597 | # decode keywords | ||
| 1598 | while ($$str && $$str =~ s/^:([^\]\[,:]*):([^\]\[,:]*)//) { | ||
| 1599 | $top->{$1} = $2; | ||
| 1600 | } | ||
| 1601 | |||
| 1602 | if ($$str && $$str !~ /^[\[\],]/) { | ||
| 1603 | print STDERR "decoding error1: $$str is an illegal string"; | ||
| 1604 | return; | ||
| 1605 | } | ||
| 1606 | |||
| 1607 | # decode children | ||
| 1608 | $top->{'children'} = []; | ||
| 1609 | if ($$str && $$str =~ s/^\[//) { | ||
| 1610 | while ($$str && $$str !~ /^\]/) { | ||
| 1611 | my $child = $self->decode_todo_list($str); | ||
| 1612 | if (!$child) { | ||
| 1613 | print STDERR "decoding error2: $$str is an illegal string"; | ||
| 1614 | return; | ||
| 1615 | } | ||
| 1616 | $child->{'parent'} = $top; | ||
| 1617 | push @{$top->{'children'}}, $child; | ||
| 1618 | } | ||
| 1619 | $$str =~ s/^\]//; | ||
| 1620 | } | ||
| 1621 | |||
| 1622 | # delete comma before the next in the list | ||
| 1623 | $$str =~ s/^,//; | ||
| 1624 | |||
| 1625 | return $top; | ||
| 1626 | } | ||
| 1627 | |||
| 1628 | sub get_next_primary { | ||
| 1629 | my ($self, $top) = @_; | ||
| 1630 | $top = $self->{'active'} if (!$top); | ||
| 1631 | |||
| 1632 | return $top if (!exists($top->{'done'}) || $top->{'done'} == $PRIM_NOTDONE); | ||
| 1633 | if ($top->{'children'}) { | ||
| 1634 | foreach my $c (@{$top->{'children'}}) { | ||
| 1635 | my $it = $self->get_next_primary($c); | ||
| 1636 | $self->{'context'} = $it; | ||
| 1637 | return $it if ($it); | ||
| 1638 | } | ||
| 1639 | } | ||
| 1640 | } | ||
| 1641 | |||
| 1642 | sub foreach_primary { | ||
| 1643 | my $self = shift; | ||
| 1644 | my $top = shift; | ||
| 1645 | my $sub = shift; | ||
| 1646 | my $c; | ||
| 1647 | |||
| 1648 | my $response = $sub->($top, @_); | ||
| 1649 | return if (defined($response) && $response eq 'STOP'); | ||
| 1650 | if ($top->{'children'}) { | ||
| 1651 | for ($c = 0; $c <= $#{$top->{'children'}}; $c++) { | ||
| 1652 | if ($c == $#{$top->{'children'}}) { | ||
| 1653 | $top->{'children'}[$c]{'qw_last_foreach'} = 1; | ||
| 1654 | } | ||
| 1655 | $self->foreach_primary($top->{'children'}[$c], $sub, @_); | ||
| 1656 | if ($c == $#{$top->{'children'}}) { | ||
| 1657 | delete $top->{'children'}[$c]{'qw_last_foreach'}; | ||
| 1658 | } | ||
| 1659 | } | ||
| 1660 | } | ||
| 1661 | } | ||
| 1662 | |||
| 1663 | # | ||
| 1664 | # remaps variables before processing. | ||
| 1665 | # | ||
| 1666 | sub map_primary { | ||
| 1667 | my ($self, $p, $to, $from) = @_; | ||
| 1668 | my @retnames; | ||
| 1669 | foreach my $q (@{$p->{questions}}) { | ||
| 1670 | next if (ref($q) ne 'HASH'); | ||
| 1671 | # note: qwparam(from, to) so read this backwards. | ||
| 1672 | qwparam($to . $q->{name}, qwparam($from . $q->{name})); | ||
| 1673 | push @retnames, $to . $q->{name}; | ||
| 1674 | } | ||
| 1675 | foreach my $v (@{$self->get_values($p->{parent_variables}, 1)}) { | ||
| 1676 | # note: qwparam(from, to) so read this backwards. | ||
| 1677 | qwparam($to . $v, qwparam($from . $v)); | ||
| 1678 | push @retnames, $to . $v; | ||
| 1679 | } | ||
| 1680 | |||
| 1681 | return \@retnames; | ||
| 1682 | } | ||
| 1683 | |||
| 1684 | sub drop_remap_prefix { | ||
| 1685 | my $x = shift; | ||
| 1686 | if (ref($x) eq 'ARRAY') { | ||
| 1687 | my @z; | ||
| 1688 | foreach my $y (@$x) { | ||
| 1689 | push @z, drop_remap_prefix($y); | ||
| 1690 | } | ||
| 1691 | return \@z; | ||
| 1692 | } else { | ||
| 1693 | $x =~ s/^remap:.*://; | ||
| 1694 | } | ||
| 1695 | return $x; | ||
| 1696 | } | ||
| 1697 | |||
| 1698 | sub maybe_start_remap { | ||
| 1699 | my ($self, $remapname, $prim) = @_; | ||
| 1700 | if ($remapname) { | ||
| 1701 | # save state | ||
| 1702 | $self->map_primary($prim, "NETPOLICYSave" . $remapname, ''); | ||
| 1703 | # restore remapped data | ||
| 1704 | $self->map_primary($prim, '', $remapname); | ||
| 1705 | # stash remap tag in primary to establish context for actions | ||
| 1706 | $prim->{'remap'} = $remapname; | ||
| 1707 | } | ||
| 1708 | } | ||
| 1709 | |||
| 1710 | sub maybe_end_remap { | ||
| 1711 | my ($self, $remapname, $prim) = @_; | ||
| 1712 | if ($remapname) { | ||
| 1713 | # resave to mapped data prefix | ||
| 1714 | $self->map_primary($prim, $remapname, ''); | ||
| 1715 | # restore saved state | ||
| 1716 | $self->map_primary($prim, '', "NETPOLICYSave" . $remapname); | ||
| 1717 | # remove remap context from prim | ||
| 1718 | delete $prim->{'remap'}; | ||
| 1719 | } | ||
| 1720 | } | ||
| 1721 | |||
| 1722 | |||
| 1723 | ############################################################################### | ||
| 1724 | # | ||
| 1725 | # Debugging routines | ||
| 1726 | # | ||
| 1727 | |||
| 1728 | #----------------------------------------------------------------------- | ||
| 1729 | # Routine: dump_primaries() | ||
| 1730 | # | ||
| 1731 | # Purpose: This routine dumps all the primary values. | ||
| 1732 | # Or something. | ||
| 1733 | # | ||
| 1734 | sub dump_primaries { | ||
| 1735 | my $self = shift; | ||
| 1736 | return Dumper($self->{'primaries'}); | ||
| 1737 | } | ||
| 1738 | |||
| 1739 | #----------------------------------------------------------------------- | ||
| 1740 | # Routine: qwsetdebug() | ||
| 1741 | # | ||
| 1742 | # Purpose: This routine sets the QWizard debugging flag based on the | ||
| 1743 | # value of the debugging preference. | ||
| 1744 | # | ||
| 1745 | sub qwsetdebug { | ||
| 1746 | my ($self) = @_; | ||
| 1747 | |||
| 1748 | my $prefval = $self->qwpref('pref_debug'); | ||
| 1749 | |||
| 1750 | # | ||
| 1751 | # If the QWizard debugging preference doesn't exist yet (which means the | ||
| 1752 | # generator has yet to be assigned) then we'll assume debugging should | ||
| 1753 | # be turned off. If the debugging preference has been set, we'll get it | ||
| 1754 | # and set the QWizard global debugging flag to that value. | ||
| 1755 | # | ||
| 1756 | if(!defined($prefval) || $prefval eq "") | ||
| 1757 | { | ||
| 1758 | $qwdebug = 0; | ||
| 1759 | $self->qwpref('pref_debug','No'); | ||
| 1760 | } | ||
| 1761 | else | ||
| 1762 | { | ||
| 1763 | $qwdebug = $prefval; | ||
| 1764 | if($prefval != 1) | ||
| 1765 | { | ||
| 1766 | $qwdebug = 0; | ||
| 1767 | } | ||
| 1768 | } | ||
| 1769 | } | ||
| 1770 | |||
| 1771 | #----------------------------------------------------------------------- | ||
| 1772 | # Routine: qwisdebugon() | ||
| 1773 | # | ||
| 1774 | # Purpose: Return the debugging flag. | ||
| 1775 | # | ||
| 1776 | sub qwisdebugon { | ||
| 1777 | return $qwdebug; | ||
| 1778 | } | ||
| 1779 | |||
| 1780 | #----------------------------------------------------------------------- | ||
| 1781 | # Routine: qwdebug_set_output() | ||
| 1782 | # | ||
| 1783 | # Purpose: This routine writes a line of QWizard debugging output to | ||
| 1784 | # Currently only a scalar variable supported. | ||
| 1785 | # | ||
| 1786 | sub qwdebug_set_output { | ||
| 1787 | shift if (ref($_[0]) eq 'QWizard'); | ||
| 1788 | $qwvar = $_[0]; | ||
| 1789 | } | ||
| 1790 | |||
| 1791 | #----------------------------------------------------------------------- | ||
| 1792 | # Routine: qwdebug() | ||
| 1793 | # | ||
| 1794 | # Purpose: This routine writes a line of QWizard debugging output to | ||
| 1795 | # the browser if the QWizard debugging flag is turned on. | ||
| 1796 | # | ||
| 1797 | sub qwdebug { | ||
| 1798 | if ($qwdebug) { | ||
| 1799 | my $count = 0; | ||
| 1800 | while(1) { | ||
| 1801 | $count++; | ||
| 1802 | last if ($count > 10); | ||
| 1803 | my @caller = caller($count); | ||
| 1804 | last if ($caller[3] =~ /magic/); | ||
| 1805 | } | ||
| 1806 | my $str = "QWizard:" . " " x $count; | ||
| 1807 | $str .= join('',@_); | ||
| 1808 | $str .= "\n" if ($_[$#_] !~ /\n$/); | ||
| 1809 | if (ref($qwvar) eq 'SCALAR') { | ||
| 1810 | $$qwvar .= $str; | ||
| 1811 | } else { | ||
| 1812 | print STDERR $str; | ||
| 1813 | } | ||
| 1814 | } | ||
| 1815 | } | ||
| 1816 | |||
| 1817 | |||
| 1818 | ############################################################################### | ||
| 1819 | # | ||
| 1820 | # Useful check_value functions. | ||
| 1821 | # | ||
| 1822 | |||
| 1823 | sub qw_required_field { | ||
| 1824 | if (!length(qwparam($_[1]->{'name'}))) { | ||
| 1825 | return "This is a required field:"; | ||
| 1826 | } | ||
| 1827 | } | ||
| 1828 | |||
| 1829 | sub qw_integer { | ||
| 1830 | return qw_optional_integer(@_) if (qwparam($_[1]->{'name'}) ne ''); | ||
| 1831 | return qw_required_field(@_); | ||
| 1832 | } | ||
| 1833 | |||
| 1834 | sub qw_optional_integer { | ||
| 1835 | if (length(qwparam($_[1]->{'name'})) > 0 && | ||
| 1836 | qwparam($_[1]->{'name'}) !~ /^\d+$/) { | ||
| 1837 | return "This must be an integer value [I.E., a number]"; | ||
| 1838 | } | ||
| 1839 | } | ||
| 1840 | |||
| 1841 | sub qw_hex { | ||
| 1842 | return qw_optional_hex(@_) if (qwparam($_[1]->{'name'}) ne ''); | ||
| 1843 | return qw_required_field(@_); | ||
| 1844 | } | ||
| 1845 | |||
| 1846 | sub qw_optional_hex { | ||
| 1847 | if (qwparam($_[1]->{'name'}) !~ /^[a-fA-F0-9]*$/) { | ||
| 1848 | return "This must be an hex string value [a-f,0-9]"; | ||
| 1849 | } | ||
| 1850 | if (length(qwparam($_[1]->{'name'})) % 2) { | ||
| 1851 | return "Hex string values must have an even length ('ab' is legal, 'abc' is not)"; | ||
| 1852 | } | ||
| 1853 | } | ||
| 1854 | |||
| 1855 | sub qw_check_hex_and_length { | ||
| 1856 | my ($self, $q, $p, $length) = @_; | ||
| 1857 | my $err; | ||
| 1858 | |||
| 1859 | # must be hex | ||
| 1860 | $err = qw_hex($self, $q, $p); | ||
| 1861 | if($err) { | ||
| 1862 | return $err; | ||
| 1863 | } | ||
| 1864 | |||
| 1865 | # length | ||
| 1866 | if (length(qwparam($q->{'name'})) != (2 * $length)) { | ||
| 1867 | return "Value must be " . $length . " bytes, " . | ||
| 1868 | "which is " . (2 * $length) . " hex characters."; | ||
| 1869 | } | ||
| 1870 | } | ||
| 1871 | |||
| 1872 | sub check_range { | ||
| 1873 | my $val = shift; | ||
| 1874 | my $acceptmsg = ""; | ||
| 1875 | for(my $i=0; $i <= $#_; $i+=2) { | ||
| 1876 | qwdebug("checking " . $val . " against rage of " . $_[$i] . " to " . | ||
| 1877 | $_[$i+1]); | ||
| 1878 | if ($val >= $_[$i] && $val <= $_[$i+1]) { | ||
| 1879 | return; | ||
| 1880 | } | ||
| 1881 | $acceptmsg .= "(>= $_[$i] and <= $_[$i+1]) or "; | ||
| 1882 | } | ||
| 1883 | $acceptmsg =~ s/ or $//; | ||
| 1884 | return $acceptmsg; | ||
| 1885 | } | ||
| 1886 | |||
| 1887 | sub qw_check_length_ranges { | ||
| 1888 | my $wiz = shift; | ||
| 1889 | my $q = shift; | ||
| 1890 | my $p = shift; | ||
| 1891 | my $val = length(qwparam($q->{'name'})); | ||
| 1892 | my $ret = 0; | ||
| 1893 | my $acceptmsg = ""; | ||
| 1894 | qwdebug("checking length rages of '" . qwparam($q->{'name'}) . "' len = " . | ||
| 1895 | $val ); | ||
| 1896 | $acceptmsg = check_range($val, @_); | ||
| 1897 | if ($acceptmsg) { | ||
| 1898 | return "Answer is not a valid length. Acceptable length of answers: $acceptmsg"; | ||
| 1899 | } | ||
| 1900 | return; | ||
| 1901 | } | ||
| 1902 | |||
| 1903 | sub qw_check_int_ranges { | ||
| 1904 | my $ret = qw_integer(@_); | ||
| 1905 | return $ret if ($ret); | ||
| 1906 | my $wiz = shift; | ||
| 1907 | my $q = shift; | ||
| 1908 | my $p = shift; | ||
| 1909 | my $val = qwparam($q->{'name'}); | ||
| 1910 | $ret = 0; | ||
| 1911 | my $acceptmsg = ""; | ||
| 1912 | for(my $i=0; $i <= $#_; $i+=2) { | ||
| 1913 | if ($val >= $_[$i] && $val <= $_[$i+1]) { | ||
| 1914 | return; | ||
| 1915 | } | ||
| 1916 | $acceptmsg .= "(>= $_[$i] and <= $_[$i+1]) or "; | ||
| 1917 | } | ||
| 1918 | $acceptmsg =~ s/ or $//; | ||
| 1919 | if ($acceptmsg) { | ||
| 1920 | return "Answer out of range. Acceptable value ranges: $acceptmsg"; | ||
| 1921 | } | ||
| 1922 | return; | ||
| 1923 | } | ||
| 1924 | |||
| 1925 | 1; | ||
| 1926 | |||
| 1927 | ############################################################################# | ||
| 1928 | |||
| 1929 | =pod | ||
| 1930 | |||
| 1931 | =head1 NAME | ||
| 1932 | |||
| 1933 | QWizard - Display a series of questions, get the answers, and act on the | ||
| 1934 | answers. | ||
| 1935 | |||
| 1936 | =head1 SYNOPSIS | ||
| 1937 | |||
| 1938 | # | ||
| 1939 | # The following code works as a application *or* as a CGI script both: | ||
| 1940 | # | ||
| 1941 | |||
| 1942 | use QWizard; | ||
| 1943 | |||
| 1944 | my %primaries = | ||
| 1945 | ( | ||
| 1946 | starting_node => | ||
| 1947 | { title => "starting here", | ||
| 1948 | introduction => "foo bar", | ||
| 1949 | questions => | ||
| 1950 | [{ type => 'text', | ||
| 1951 | name => 'mytext', | ||
| 1952 | text => 'enter something:', | ||
| 1953 | default => "hello world" }, | ||
| 1954 | { type => 'checkbox', | ||
| 1955 | text => 'yes or no:', | ||
| 1956 | values => ['yes','no'], | ||
| 1957 | name => 'mycheck'} ], | ||
| 1958 | actions => | ||
| 1959 | [sub { return [ | ||
| 1960 | "msg: text = " . qwparam('mytext'), | ||
| 1961 | "msg: checkbox = " . qwparam('mycheck') | ||
| 1962 | ];}] | ||
| 1963 | } | ||
| 1964 | ); | ||
| 1965 | |||
| 1966 | my $qw = new QWizard(primaries => \%primaries, | ||
| 1967 | title => "window title"); | ||
| 1968 | |||
| 1969 | $qw->magic('starting_node'); | ||
| 1970 | |||
| 1971 | |||
| 1972 | # | ||
| 1973 | # PLEASE see the examples in the examples directory. | ||
| 1974 | # | ||
| 1975 | |||
| 1976 | =head1 DESCRIPTION | ||
| 1977 | |||
| 1978 | QWizard displays a list of grouped questions, and retrieves and processes | ||
| 1979 | user-specified answers to the questions. Multiple question/answer sets may | ||
| 1980 | be displayed before the answers are dealt with. Once a "commit" action is | ||
| 1981 | taken (instigated by the user), a series of actions is performed to handle | ||
| 1982 | the answers. The actions are executed in the order required by the QWizard | ||
| 1983 | programmer. | ||
| 1984 | |||
| 1985 | QWizard's real power lies in its inherent ability to keep track of all state | ||
| 1986 | information between one wizard screen and the next, even in normally stateless | ||
| 1987 | transaction environments like HTTP and HTML. This allows a QWizard programmer | ||
| 1988 | to collect a large body of data with a number of simple displays. After all | ||
| 1989 | the data has been gathered and verified, then it can be handled as appropriate | ||
| 1990 | (e.g., written to a database, used for system configuration, or used to | ||
| 1991 | generate a graph.) | ||
| 1992 | |||
| 1993 | Current user interfaces that exist are HTML, Gtk2, Tk, and (minimally) | ||
| 1994 | ReadLine. A single QWizard script implementation can make use of any | ||
| 1995 | of the output formats without code modification. Thus it is extremely | ||
| 1996 | easy to write portable I<wizard> scripts that can be used without | ||
| 1997 | modification by both graphical window environments (Gtk2 and Tk) and | ||
| 1998 | HTML-based web environments (e.g., CGI scripts.), as well with | ||
| 1999 | intercative command line enviornments (ReadLine). | ||
| 2000 | |||
| 2001 | Back-end interfaces (child classes of the I<QWizard::Generator> | ||
| 2002 | module) are responsible for displaying the information to the user. | ||
| 2003 | Currently HTML, Gtk2, Tk and ReadLine, are the output mechanisms that | ||
| 2004 | work the best (in that order). Some others are planned (namely a | ||
| 2005 | curses version), but are not far along in development. Developing new | ||
| 2006 | generator back-ends is fairly simple and doesn't take a lot of code | ||
| 2007 | (assuming the graphic interface is fairly powerful and contains a | ||
| 2008 | widget library.) | ||
| 2009 | |||
| 2010 | QWizard operates by displaying a series of "screens" to the user. Each screen | ||
| 2011 | is defined in a QWizard construct called a I<primary> that describes the | ||
| 2012 | attributes of a given screen, including the list of I<questions> to be | ||
| 2013 | presented to the user. Primaries can contain questions, things to do | ||
| 2014 | immediately after the questions are answered (I<post_answers>), and things | ||
| 2015 | to do once the entire series of screens have been answered (I<actions>). | ||
| 2016 | Other information, such as a title and an introduction, can also be attached | ||
| 2017 | to a primary. | ||
| 2018 | |||
| 2019 | An example very minimal primary definition containing one question: | ||
| 2020 | |||
| 2021 | my %primaries = ( | ||
| 2022 | myprimary => | ||
| 2023 | { | ||
| 2024 | title => "my screen title", | ||
| 2025 | introduction => "optional introduction to the screen", | ||
| 2026 | questions => | ||
| 2027 | [ | ||
| 2028 | { | ||
| 2029 | type => 'checkbox', | ||
| 2030 | text => 'Should the chicken cross the road?', | ||
| 2031 | } | ||
| 2032 | ], | ||
| 2033 | } | ||
| 2034 | |||
| 2035 | After defining a set of primaries, a new QWizard object must be created. The | ||
| 2036 | QWizard I<new>() constructor is given a set of options, such as window title | ||
| 2037 | and a reference to a hash table containing the primaries. (The complete set | ||
| 2038 | of options may be found in the "QWizard new() Options" section.) The question | ||
| 2039 | display and data collection is started by calling the I<magic>() routine of | ||
| 2040 | the new QWizard object. | ||
| 2041 | |||
| 2042 | my $qw = new QWizard(primaries => \%primaries, | ||
| 2043 | title => 'my title'); | ||
| 2044 | $qw->magic('myprimary'); | ||
| 2045 | |||
| 2046 | There are examples distributed with the QWizard module sources that | ||
| 2047 | may help to understand the whole system and what it is capable of. | ||
| 2048 | See the B<examples> directory of the QWizard source code tree for | ||
| 2049 | details. Also, QWizard was written mostly due to requirements of the | ||
| 2050 | Net-Policy project. Net-Policy makes very extensive use of QWizard | ||
| 2051 | and is another good place to look for examples. In fact, the QWizard | ||
| 2052 | CVS code is located inside the Net-Policy CVS tree. See | ||
| 2053 | http://net-policy.sourceforge.net/ for details on the Net-Policy | ||
| 2054 | project. There are a number of screen shots showing all the | ||
| 2055 | interfaces as well on the main net-policy web site. | ||
| 2056 | |||
| 2057 | =head2 MAGIC() PSEUDO-CODE | ||
| 2058 | |||
| 2059 | A pseudo-code walk-through of the essential results of the I<magic>() routine | ||
| 2060 | above is below. In a CGI script, for example, the I<magic>() routine will be | ||
| 2061 | called multiple times (once per screen) but the results will be the same in | ||
| 2062 | the end -- it's all taken care of magically ;-). | ||
| 2063 | |||
| 2064 | ################ | ||
| 2065 | ## WARNING: pseudo-code describing a process! Not real code! | ||
| 2066 | ################ | ||
| 2067 | |||
| 2068 | # Loop through each primary and display the primary's questions. | ||
| 2069 | while(primaries to process) { | ||
| 2070 | display_primary_questions(); | ||
| 2071 | get_user_input(); | ||
| 2072 | check_results(); | ||
| 2073 | run_primary_post_answers(); | ||
| 2074 | } | ||
| 2075 | |||
| 2076 | # Displays a "will be doing these things" screen, | ||
| 2077 | # and has a commit button. | ||
| 2078 | display_commit_screen(); | ||
| 2079 | |||
| 2080 | # Loop through each primary and run its actions. | ||
| 2081 | # Note: see action documentation about execution order! | ||
| 2082 | foreach (primary that was displayed) { | ||
| 2083 | results = run_primary_actions(); | ||
| 2084 | display(results); | ||
| 2085 | } | ||
| 2086 | |||
| 2087 | # If magic() is called again, it restarts from | ||
| 2088 | # the top primary again. | ||
| 2089 | |||
| 2090 | =head2 QWIZARD NEW() OPTIONS | ||
| 2091 | |||
| 2092 | Options passed to the QWizard new() operator define how the QWizard instance | ||
| 2093 | will behave. Options are passed in the following manner: | ||
| 2094 | |||
| 2095 | new QWizard (option => value, ...) | ||
| 2096 | |||
| 2097 | Valid options are: | ||
| 2098 | |||
| 2099 | =over 4 | ||
| 2100 | |||
| 2101 | =item title => "document title" | ||
| 2102 | |||
| 2103 | The document title to be printed in the title bar of the window. | ||
| 2104 | |||
| 2105 | =item generator => GENERATOR | ||
| 2106 | |||
| 2107 | GENERATOR is a reference to a valid QWizard generator. | ||
| 2108 | Current generator classes are: | ||
| 2109 | |||
| 2110 | - QWizard::Generator::Best (default: picks the best available) | ||
| 2111 | - QWizard::Generator::HTML | ||
| 2112 | - QWizard::Generator::Gtk2 | ||
| 2113 | - QWizard::Generator::Tk | ||
| 2114 | - QWizard::Generator::ReadLine (limited in functionality) | ||
| 2115 | |||
| 2116 | The I<QWizard::Generator::Best> generator is used if no specific generator | ||
| 2117 | is specified. The I<Best> generator will create an HTML generator if used | ||
| 2118 | in a web context (i.e., a CGI script), or else pick the best of the available | ||
| 2119 | other generators (Gtk2, then Tk, then ReadLine). | ||
| 2120 | |||
| 2121 | This example forces a Gtk2 generator to be used: | ||
| 2122 | |||
| 2123 | my $wiz = new QWizard(generator => new QWizard::QWizard::Gtk2(), | ||
| 2124 | # ... | ||
| 2125 | ); | ||
| 2126 | |||
| 2127 | =item generator_args => [ARGS], | ||
| 2128 | |||
| 2129 | If you want the default generator that QWizard will provide you, but | ||
| 2130 | would still like to provide that generator with some arguments you can | ||
| 2131 | use this token to pass an array reference of arguments. These | ||
| 2132 | arguments will be passed to the new() method of the Generator that is | ||
| 2133 | created. | ||
| 2134 | |||
| 2135 | =item top_location => "webaddress" | ||
| 2136 | |||
| 2137 | This should be the top location of a web page where the questions will be | ||
| 2138 | displayed. This is needed for "go to top" buttons and the like to work. This | ||
| 2139 | is not needed if the QWizard-based script is not going to be used in a CGI | ||
| 2140 | or other web-based environment (eg, if it's going to be used in mod_perl). | ||
| 2141 | |||
| 2142 | =item primaries => \%my_primaries | ||
| 2143 | |||
| 2144 | I<my_primaries> will define the list of questions to be given to the user. | ||
| 2145 | I<my_primaries> just defines the questions, but does not mean the user will | ||
| 2146 | be prompted with each question. The questions in this series that will be | ||
| 2147 | displayed for the user to answer is determined by the I<magic>() function's | ||
| 2148 | starting arguments, described below. The format of the I<primaries> hash is | ||
| 2149 | described in the B<Primaries Definition> section below. The recognized | ||
| 2150 | values in the I<primaries> hash is described in the B<Primaries Options> | ||
| 2151 | section. | ||
| 2152 | |||
| 2153 | =item no_confirm => 1 | ||
| 2154 | |||
| 2155 | If set, the final confirmation screen will not be displayed, but instead the | ||
| 2156 | resulting actions will be automatically run. This can also be achieved inside | ||
| 2157 | the wizard tokens primaries by setting a question name to I<no_confirm> with | ||
| 2158 | a value of 1 (using a hidden question type.) | ||
| 2159 | |||
| 2160 | =item begin_section_hook => \&subroutine | ||
| 2161 | |||
| 2162 | This function will be called just before each screen is displayed. It | ||
| 2163 | can be used to perform such functions as printing preliminary | ||
| 2164 | information and initializing data. | ||
| 2165 | |||
| 2166 | =item end_section_hook => \&subroutine | ||
| 2167 | |||
| 2168 | This function will be called just after a set of questions is displayed. | ||
| 2169 | |||
| 2170 | =item topbar | ||
| 2171 | |||
| 2172 | A place to add extra widgets to a primary at the very top. | ||
| 2173 | |||
| 2174 | See the I<bar> documentation in the I<QUESTION DEFINITIONS> section | ||
| 2175 | below for details on this field. | ||
| 2176 | |||
| 2177 | =item leftside => [WIDGETS] | ||
| 2178 | |||
| 2179 | =item rightside => [WIDGETS] | ||
| 2180 | |||
| 2181 | Adds a left or right side frame to the main screen where the WIDGETS | ||
| 2182 | are always shown for all primaries. Basically, these should be | ||
| 2183 | "axillary" widgets that augment the widgets is the main set of | ||
| 2184 | questions. They can be used for just about anything, but the look and | ||
| 2185 | feel will likely be better if they're suplimental. | ||
| 2186 | |||
| 2187 | The WIDGETS are normal question widgets, just as can appear in the | ||
| 2188 | questions section of the primaries definition as described below. | ||
| 2189 | |||
| 2190 | In addition, however, there can be subgroupings with a title as well. | ||
| 2191 | These are then in a sub-array and are displayed with a title above | ||
| 2192 | them. EG: | ||
| 2193 | |||
| 2194 | leftside => [ | ||
| 2195 | { type => 'button', | ||
| 2196 | # ... normal button widget definition; see below | ||
| 2197 | }, | ||
| 2198 | [ | ||
| 2199 | "Special Grouped-together Buttons", | ||
| 2200 | { type => 'button', | ||
| 2201 | # ... | ||
| 2202 | }, | ||
| 2203 | { type => 'button', | ||
| 2204 | # ... | ||
| 2205 | }, | ||
| 2206 | ], | ||
| 2207 | ], | ||
| 2208 | |||
| 2209 | The above grouped set of buttons will appear slightly differently and | ||
| 2210 | grouped together under the title "Special Grouped-together Buttons". | ||
| 2211 | |||
| 2212 | The widget-test-screen.pl in the examples directory shows examples of | ||
| 2213 | using this. | ||
| 2214 | |||
| 2215 | Important note: Not all backends support this yet. HTML and Gtk2 do, though. | ||
| 2216 | |||
| 2217 | =back | ||
| 2218 | |||
| 2219 | =head2 PRIMARIES DEFINITION | ||
| 2220 | |||
| 2221 | The I<primaries> argument of the I<new>() function defines the list of | ||
| 2222 | questions that may be posed to a user. Each primary in the hash will contain | ||
| 2223 | a list of questions, answers, etc., and are grouped together by a name (the | ||
| 2224 | key in the hash). Thus, a typical primary set definition would look something | ||
| 2225 | like: | ||
| 2226 | |||
| 2227 | %my_primaries = | ||
| 2228 | ( | ||
| 2229 | # The name of the primary. | ||
| 2230 | 'question_set_1' => | ||
| 2231 | # its definition | ||
| 2232 | { | ||
| 2233 | title => 'My question set', | ||
| 2234 | questions => | ||
| 2235 | # questions are defined in an array of hashes. | ||
| 2236 | [{type => 'checkbox', | ||
| 2237 | text => 'Is this fun?', | ||
| 2238 | name => is_fun, | ||
| 2239 | default => 1, | ||
| 2240 | values => [1, 0] }, | ||
| 2241 | {type => 'text', | ||
| 2242 | text => 'Enter your name:', | ||
| 2243 | name => 'their_name'}], | ||
| 2244 | post_answers => | ||
| 2245 | # post_answers is a list of things to do immediately after | ||
| 2246 | # this set of questions has been asked. | ||
| 2247 | [ sub { print "my question set answered" } ], | ||
| 2248 | actions => | ||
| 2249 | # actions is a list of actions run when all is said and done. | ||
| 2250 | [ sub { | ||
| 2251 | return "msg: %s thinks this %s fun.\n", | ||
| 2252 | qwparam('their_name'), | ||
| 2253 | (qwparam('is_fun')) ? "is" : "isn't" | ||
| 2254 | }], | ||
| 2255 | actions_descr => | ||
| 2256 | # An array of strings displayed to the user before they agree | ||
| 2257 | # to commit to their answers. | ||
| 2258 | [ 'I\'m going to process stuff from @their_name@)' ] | ||
| 2259 | }); | ||
| 2260 | |||
| 2261 | See the I<QWizard::API> module for an alternative, less verbose, form of API | ||
| 2262 | for creating primaries which can produce more compact-looking code. | ||
| 2263 | |||
| 2264 | =head3 VALUE conventions | ||
| 2265 | |||
| 2266 | In the documentation to follow, any time the keyword VALUE appears, the | ||
| 2267 | following types of "values" can be used in its place: | ||
| 2268 | |||
| 2269 | - "a string" | ||
| 2270 | - 10 | ||
| 2271 | - \&sub_to_call | ||
| 2272 | - sub { return "a calculated string or value" } | ||
| 2273 | - [\&sub_to_call, arguments, to, sub, ...] | ||
| 2274 | |||
| 2275 | Subroutines are called and expected to return a single value or an array | ||
| 2276 | reference of multiple values. | ||
| 2277 | |||
| 2278 | Much of the time the VALUE keyword appears in array brackets: []. Thus you | ||
| 2279 | may often specify multiple values in various ways. E.g., a values clause in | ||
| 2280 | a question may be given in this manner: | ||
| 2281 | |||
| 2282 | sub my_examp1 { return 3; } | ||
| 2283 | sub my_examp2 { return [$_[0]..$_[1]]; } | ||
| 2284 | |||
| 2285 | values => [1, 2, \&my_examp1, [\&my_examp2, 4, 10]], | ||
| 2286 | |||
| 2287 | After everything is evaluated, the end result of this (complex) example will | ||
| 2288 | be an array passed of digits from 1 to 10 passed to the values clause. | ||
| 2289 | |||
| 2290 | In any function at any point in time during processing, the I<qwparam>() | ||
| 2291 | function can be called to return the results of a particular question as it | ||
| 2292 | was answered by the user. I.e., if a question named I<their_name> was | ||
| 2293 | answered with "John Doe" at any point in the past series of wizard screens, | ||
| 2294 | then I<qwparam('their_name')> would return "John Doe". As most VALUE | ||
| 2295 | functions will be designed to process previous user input, understanding this | ||
| 2296 | is the key to using the QWizard Perl module. More information and examples | ||
| 2297 | follow in the sections below. | ||
| 2298 | |||
| 2299 | =head2 PRIMARY OPTIONS | ||
| 2300 | |||
| 2301 | These are the tokens that can be specified in a primary: | ||
| 2302 | |||
| 2303 | =over 4 | ||
| 2304 | |||
| 2305 | =item title => VALUE | ||
| 2306 | |||
| 2307 | The title name for the set of questions. This will be displayed at | ||
| 2308 | the top of the screen. | ||
| 2309 | |||
| 2310 | =item introduction => VALUE | ||
| 2311 | |||
| 2312 | Introductory text to be printed above the list of questions for a given | ||
| 2313 | primary. This is useful as a starting piece of text to help the user with | ||
| 2314 | this particular wizard screen. Display of the introductory text is controlled | ||
| 2315 | by the Net-Policy I<pref_intro> user preference. The default is to display | ||
| 2316 | introductory text, but this setting can be turned off and on by the user. | ||
| 2317 | |||
| 2318 | =item questions => [{ QUESTION_DEFINITION }, { QUESTION_DEFINITION }, ...] | ||
| 2319 | |||
| 2320 | This is a list of questions to pose to the user for this screen. | ||
| 2321 | |||
| 2322 | The B<Question Definitions> section describes valid question formatting. | ||
| 2323 | |||
| 2324 | =item post_answers => [ VALUES ] | ||
| 2325 | |||
| 2326 | This is a list of actions to run after the questions on the screen have been | ||
| 2327 | answered. Although this is a VALUES clause, as described above, these should | ||
| 2328 | normally be subroutines and not hard-coded values. The first argument to the | ||
| 2329 | VALUE functions will be a reference to the wizard. This is particularly | ||
| 2330 | useful to conditionally add future screens/primaries that need to be shown | ||
| 2331 | to the user. This can be done by using the following I<add_todos>() function | ||
| 2332 | call in the I<action> section: | ||
| 2333 | |||
| 2334 | if (some_condition()) { | ||
| 2335 | $_[0]->add_todos('primary1', ...); | ||
| 2336 | } | ||
| 2337 | |||
| 2338 | See the B<QWizard Object Functions> section | ||
| 2339 | for more information on the I<add_todos>() function, but the above | ||
| 2340 | will add the 'primary1' screen to the list of screens to display for the user | ||
| 2341 | before the wizard is finished. | ||
| 2342 | |||
| 2343 | A post_answers subroutine should return the word "OK" for it to be | ||
| 2344 | successful (right now, this isn't checked, but it may be (again) in | ||
| 2345 | the future). It may also return "REDISPLAY" which will cause the | ||
| 2346 | screen to displayed again. | ||
| 2347 | |||
| 2348 | For HTML output, these will be run just before the next screen is | ||
| 2349 | printed after the user has submitted the answers back to the web | ||
| 2350 | server. For window-based output (Gtk2, Tk, etc.) the results are | ||
| 2351 | similar and these subroutines are evaluated before the next window is | ||
| 2352 | drawn. | ||
| 2353 | |||
| 2354 | =item check_value => sub { ... } | ||
| 2355 | |||
| 2356 | =item check_value => [sub { ... }, arg1, arg2, ...] | ||
| 2357 | |||
| 2358 | The primary may have a check_value clause assigned to it to do high | ||
| 2359 | level consistency checks. Returning anything but "OK" will have the | ||
| 2360 | returned text displayed as an error to the user that they should fix | ||
| 2361 | before they're allowed to continue past the screen. | ||
| 2362 | |||
| 2363 | =item actions => [ VALUES ] | ||
| 2364 | |||
| 2365 | The action functions will be run after the entire wizard series of questions | ||
| 2366 | has been displayed and answered and after the user has hit the "commit" | ||
| 2367 | button. It is assumed that the actions of the earlier screens are dependent | ||
| 2368 | on the actions of the later screens and so the action functions will be | ||
| 2369 | executed in reverse order from the way the screens were displayed. See the | ||
| 2370 | I<add_todos>() function description in the B<QWizard Object Functions> section | ||
| 2371 | for more information on to change the order of execution away from the default. | ||
| 2372 | |||
| 2373 | The collected values returned from the VALUES evaluation will be displayed | ||
| 2374 | to the user. Any message beginning with a 'msg:' prefix will be displayed | ||
| 2375 | as a normal output line. Any value not prefixed with 'msg:' will be displayed | ||
| 2376 | as an error (typically displayed in bold and red by most generators.) | ||
| 2377 | |||
| 2378 | =item actions_descr => [ VALUES ] | ||
| 2379 | |||
| 2380 | Just before the actions are run, a change-summary screen is shown to the user. | ||
| 2381 | A "commit" button will also be given on this screen. VALUE strings, function | ||
| 2382 | results, etc., will be displayed as a list on this commit screen. Strings | ||
| 2383 | may have embedded special @TAG@ keywords which will be replaced by the value | ||
| 2384 | for the question with a name of TAG. These strings should indicate to the | ||
| 2385 | user what the commit button will do for any actions to be run by this set of | ||
| 2386 | questions. If any question was defined whose name was I<no_confirm> and whose | ||
| 2387 | value was 1, this screen will be skipped and the actions will be run directly. | ||
| 2388 | |||
| 2389 | =item sub_modules => [ 'subname1', ... ] | ||
| 2390 | |||
| 2391 | This hash value adds the specified sub-modules to the list of screens to | ||
| 2392 | display after this one. This is equivalent to having a I<post_answers> clause | ||
| 2393 | that includes the function: | ||
| 2394 | |||
| 2395 | sub { $_[0]->add_todos('subname1', ...); } | ||
| 2396 | |||
| 2397 | =item doif => sub { LOGIC } | ||
| 2398 | |||
| 2399 | Allows primaries to be I<optional> and only displayed under certain | ||
| 2400 | conditions. | ||
| 2401 | |||
| 2402 | If specified, it should be a CODE reference which when executed should | ||
| 2403 | return a 1 if the primary is to be displayed or a 0 if not. The | ||
| 2404 | primary will be entirely skipped if the CODE reference returns a 0. | ||
| 2405 | |||
| 2406 | =item topbar | ||
| 2407 | |||
| 2408 | A place to add extra widgets to a primary at the very top. | ||
| 2409 | |||
| 2410 | See the I<bar> documentation in the I<QUESTION DEFINITIONS> section | ||
| 2411 | below for details on this field. | ||
| 2412 | |||
| 2413 | =item leftside => [WIDGETS] | ||
| 2414 | |||
| 2415 | =item rightside => [WIDGETS] | ||
| 2416 | |||
| 2417 | Adds a left or right side frame to the main screen where the WIDGETS | ||
| 2418 | are shown for this primary. | ||
| 2419 | |||
| 2420 | Important note: See the leftside/rightside documentation for QWizard | ||
| 2421 | for more details and support important notes there. | ||
| 2422 | |||
| 2423 | =item take_over => \&subroutine | ||
| 2424 | |||
| 2425 | This hash value lets a subroutine completely take control of processing | ||
| 2426 | beyond this point. The wizard methodology functionally stops here and control | ||
| 2427 | for anything in the future is entirely passed to this subroutine. This should | ||
| 2428 | be rarely (if ever) used and it is really a way of breaking out of the wizard | ||
| 2429 | completely. | ||
| 2430 | |||
| 2431 | =back | ||
| 2432 | |||
| 2433 | =head2 QUESTION DEFINITIONS | ||
| 2434 | |||
| 2435 | Questions are implemented as a collection of hash references. A question | ||
| 2436 | generally has the following format: | ||
| 2437 | |||
| 2438 | { | ||
| 2439 | type => QUESTION_TYPE | ||
| 2440 | text => QUESTION_TEXT, | ||
| 2441 | name => NAME_FOR_ANSWER, | ||
| 2442 | default => VALUE, | ||
| 2443 | # for menus, checkboxes, multichecks, ... : | ||
| 2444 | values => [ VALUE1, VALUE2, ... ], # i.e., [VALUES] | ||
| 2445 | # for menus, checkboxes, multichecks, ... : | ||
| 2446 | labels => { value1 => label1, value2 => label2 } # i.e., [VALUES] | ||
| 2447 | } | ||
| 2448 | |||
| 2449 | Other than this sort of hash reference, the only other type of question | ||
| 2450 | allowed in the question array is a single "" empty string. The empty string | ||
| 2451 | acts as a vertical spatial separator, indicating that a space should occur | ||
| 2452 | between the previous question and the next question. | ||
| 2453 | |||
| 2454 | The fields available to question types are given below. Unless otherwise | ||
| 2455 | stated, the fields are available to all question types. | ||
| 2456 | |||
| 2457 | =over | ||
| 2458 | |||
| 2459 | =item name => 'NAME' | ||
| 2460 | |||
| 2461 | Names the answer to the question. This name can then be used later in other | ||
| 2462 | sections (I<action>, I<post_answers>, etc.) to retrieve the value of the | ||
| 2463 | answer using the I<qwparam>() function. For example, I<qwparam>('NAME') at | ||
| 2464 | any point in future executed code should return the value provided by the | ||
| 2465 | user for the question named 'NAME'. | ||
| 2466 | |||
| 2467 | The namespace for these names is shared among all primaries (except 'remapped' | ||
| 2468 | primaries, which are described later). A warning will be issued if different | ||
| 2469 | questions from two different primaries use the same name. This warning will | ||
| 2470 | not be given if the question contains an I<override> flag set to 1. | ||
| 2471 | |||
| 2472 | =item text => 'QUESTION TEXT' | ||
| 2473 | |||
| 2474 | Text displayed for the user for the given question. The text will generally | ||
| 2475 | be on the left of the screen, and the widget the user is supposed to interact | ||
| 2476 | with will be to the question text's right. (This is subject to the | ||
| 2477 | implementation of the back-end question Generator. The standard QWizard | ||
| 2478 | generators use this layout scheme.) | ||
| 2479 | |||
| 2480 | =item type => 'TYPE' | ||
| 2481 | |||
| 2482 | Defines the type of question. TYPE can be one of: | ||
| 2483 | |||
| 2484 | =over 8 | ||
| 2485 | |||
| 2486 | =item label | ||
| 2487 | |||
| 2488 | Displays information on the screen without requesting any input. The text | ||
| 2489 | of the question is printed on the left followed by the values portion on the | ||
| 2490 | right. If the values portion is omitted, the text portion is printed across | ||
| 2491 | the entire width of the screen. | ||
| 2492 | |||
| 2493 | =item paragraph | ||
| 2494 | |||
| 2495 | Paragraphs are similar to labels but are designed for spaces where | ||
| 2496 | text needs to be wrapped and is likely to be quite long. | ||
| 2497 | |||
| 2498 | =item text | ||
| 2499 | |||
| 2500 | Text input. Displays an entry box where a standard single line of text can | ||
| 2501 | be entered. | ||
| 2502 | |||
| 2503 | =item textbox | ||
| 2504 | |||
| 2505 | Text input, but in a large box allowing for multi-line entries. | ||
| 2506 | |||
| 2507 | =item hidetext | ||
| 2508 | |||
| 2509 | Obscured text input. Displays a text entry box, but with the typed text | ||
| 2510 | echoed as asterisks. This is suitable for prompting users for entering | ||
| 2511 | passwords, as it is not shown on the screen. | ||
| 2512 | |||
| 2513 | =item checkbox | ||
| 2514 | |||
| 2515 | A checkbox. The I<values> clause should have only 2 values in it: one for | ||
| 2516 | its "on" value, and one for its "off" value (which defaults to 1 and 0, | ||
| 2517 | respectively). | ||
| 2518 | |||
| 2519 | The I<button_label> clause can specify text to put right next to the | ||
| 2520 | checkbox itself. | ||
| 2521 | |||
| 2522 | If a backend supports key accelerators (GTk2): Checkbox labels can be | ||
| 2523 | bound to Alt-key accelerators. See QUESTION KEY-ACCELERATORS below | ||
| 2524 | for more information. | ||
| 2525 | |||
| 2526 | =item multi_checkbox | ||
| 2527 | |||
| 2528 | Multiple checkboxes, one for each label/value pair. The I<name> question | ||
| 2529 | field is a prefix, and all values and/or label keywords will be the second | ||
| 2530 | half of the name. | ||
| 2531 | |||
| 2532 | For example, the following clauses: | ||
| 2533 | |||
| 2534 | { | ||
| 2535 | type => 'multi_checkbox', | ||
| 2536 | name => 'something', | ||
| 2537 | values => ['end1','end2'], | ||
| 2538 | ... | ||
| 2539 | } | ||
| 2540 | |||
| 2541 | will give parameters of 'somethingend1' and 'somethingend2'. | ||
| 2542 | |||
| 2543 | If a backend supports key accelerators (GTk2): Checkbox labels can be | ||
| 2544 | bound to Alt-key accelerators. See QUESTION KEY-ACCELERATORS below | ||
| 2545 | for more information. | ||
| 2546 | |||
| 2547 | =item radio | ||
| 2548 | |||
| 2549 | Radio buttons, only one of which can be selected at a time. If two questions | ||
| 2550 | have the same I<name> and are of type 'radio', they will be "linked" together | ||
| 2551 | such that clicking on a radio button for one question will affect the other. | ||
| 2552 | |||
| 2553 | If a backend supports key accelerators (GTk2): Radio button labels can | ||
| 2554 | be bound to Alt-key accelerators. See QUESTION KEY-ACCELERATORS below | ||
| 2555 | for more information. | ||
| 2556 | |||
| 2557 | =item menu | ||
| 2558 | |||
| 2559 | Pull-down menu, where each label is displayed as a menu item. If just the | ||
| 2560 | I<values> clause (see below) is used, the labels on the screen will match the | ||
| 2561 | values. If the I<default> clause is set, then that menu entry will be the | ||
| 2562 | menu's initial selection. If the I<labels> clause is used, the values shown | ||
| 2563 | to the user will be converted to the screen representations that will differ | ||
| 2564 | from the I<qwparam>() values available later. This is useful for displaying | ||
| 2565 | human representations of programmatic values. E.g.: | ||
| 2566 | |||
| 2567 | { | ||
| 2568 | type => 'menu', | ||
| 2569 | name => 'mymenu', | ||
| 2570 | labels => [ 1 => 'my label1', | ||
| 2571 | 2 => 'my label2'] | ||
| 2572 | } | ||
| 2573 | |||
| 2574 | In this example, the user will see a menu containing 2 entries "my label1" | ||
| 2575 | and "my label2", but I<qwparam>() will return 1 or 2 for I<qwparam('mymenu')>. | ||
| 2576 | |||
| 2577 | =item table | ||
| 2578 | |||
| 2579 | Table to display. The I<values> section should return a reference to an | ||
| 2580 | array, where each element of the array is a row containing the columns to | ||
| 2581 | display for that row. The top-most table must actually be returned in an | ||
| 2582 | array itself. (This is due to an oddity of internal QWizard processing). | ||
| 2583 | E.g.: | ||
| 2584 | |||
| 2585 | { | ||
| 2586 | type => 'table', | ||
| 2587 | text => 'The table:', | ||
| 2588 | values => sub { | ||
| 2589 | my $table = [['row1:col1', 'row1:col2'], | ||
| 2590 | ['row2:col1', 'row2:col2']]; | ||
| 2591 | return [$table]; | ||
| 2592 | } | ||
| 2593 | } | ||
| 2594 | |||
| 2595 | This would be displayed graphically on the screen in this manner: | ||
| 2596 | |||
| 2597 | row1:col1 row1:col2 | ||
| 2598 | |||
| 2599 | row2:col1 row2:col2 | ||
| 2600 | |||
| 2601 | Additionally, a column value within the table may itself be a sub-table | ||
| 2602 | (another double-array reference set) or a hash reference, which will be a | ||
| 2603 | sub-widget to display any of the other types listed in this section. | ||
| 2604 | |||
| 2605 | Finally, a I<headers> clause may be added to the question definition | ||
| 2606 | which will add column headers to the table. E.g.: | ||
| 2607 | |||
| 2608 | headers => [['col1 header','col2 header']] | ||
| 2609 | |||
| 2610 | =item fileupload | ||
| 2611 | |||
| 2612 | A dialog box for a user to upload a file into the application. When a | ||
| 2613 | user submits a file the question I<name> can be used later to retrieve | ||
| 2614 | a read file handle on the file using the function | ||
| 2615 | I<qw_upload_fh('NAME')>. I<qwparam('NAME')> will return the original | ||
| 2616 | name of the file submitted, but because of the variability in how | ||
| 2617 | web-browsers submit file names along with the data, this field should | ||
| 2618 | generally not be used. Instead, get access to the data through the | ||
| 2619 | I<qw_upload_fh>() function instead. The second best alternative is to | ||
| 2620 | use the I<qw_upload_file('NAME')> function which returns a safe path | ||
| 2621 | to access under web-environments (which will be something like | ||
| 2622 | /tmp/qwHTMLXXXXXX...) | ||
| 2623 | |||
| 2624 | =item filedownload | ||
| 2625 | |||
| 2626 | A button that allows the user to download something generated by the | ||
| 2627 | application. The data that will be stored in this file should be | ||
| 2628 | defined in the 'data' field B<or> the 'datafn' field. The name | ||
| 2629 | displayed within the button will be taken from the default question | ||
| 2630 | parameter. | ||
| 2631 | |||
| 2632 | The I<data> field is processed early during display of the screen, so | ||
| 2633 | generation of large sets of data that won't always be downloaded or | ||
| 2634 | will take a lot of memor shouldn't use the I<data> field. The I<data> | ||
| 2635 | field is processed like any other value field where raw data or a | ||
| 2636 | coderef can be passed that will be called to return the data. | ||
| 2637 | |||
| 2638 | The I<datafn> field should contain a I<CODE> reference that will be | ||
| 2639 | called with five arguments: | ||
| 2640 | |||
| 2641 | =over | ||
| 2642 | |||
| 2643 | =item a IO::File filehandle to print to | ||
| 2644 | |||
| 2645 | =item the file name (if appropriate) it's going to print to. | ||
| 2646 | |||
| 2647 | =item a QWizard object reference | ||
| 2648 | |||
| 2649 | =item a reference to the primary hash | ||
| 2650 | |||
| 2651 | =item a reference to the question hash | ||
| 2652 | |||
| 2653 | Example usage: | ||
| 2654 | |||
| 2655 | { type => 'filedownload', | ||
| 2656 | text => 'Download a file:', | ||
| 2657 | datafn => | ||
| 2658 | sub { | ||
| 2659 | my $fh = shift; | ||
| 2660 | print $fh "hello world: val=" . qwparam('someparam') . "\n"; | ||
| 2661 | } | ||
| 2662 | }, | ||
| 2663 | |||
| 2664 | Currently only Gtk2 supports this button, but others will in the | ||
| 2665 | future. | ||
| 2666 | |||
| 2667 | =item image | ||
| 2668 | |||
| 2669 | Image file. The image name is specified by the I<image> hash keyword. | ||
| 2670 | Several optional hash keys are recognized to control display of the image. | ||
| 2671 | I<imagealt> specifies a string to display if the image file is not found. | ||
| 2672 | I<height> specifies the height of the image. I<width> specifies the width | ||
| 2673 | of the image. (I<height> and I<width> are currently only implemented for | ||
| 2674 | HTML.) | ||
| 2675 | |||
| 2676 | =item graph | ||
| 2677 | |||
| 2678 | Graph of passed data. This is only available if the I<GD::Graph> module is | ||
| 2679 | installed. Data is passed in from the I<values> clause and is expected to | ||
| 2680 | be an array of arrays of data, where the first row is the x-axis data, and | ||
| 2681 | the rest are y values (one line will be drawn for each y value). | ||
| 2682 | |||
| 2683 | Additionally, the I<GD::Graph> options can be specified with a | ||
| 2684 | I<graph_options> tag to the question, allowing creation of such things as | ||
| 2685 | axis labels and legends. | ||
| 2686 | |||
| 2687 | =item tree | ||
| 2688 | |||
| 2689 | Hierarchical tree. Displays a selectable hierarchical tree set from which | ||
| 2690 | the user should pick a single item. Two references to subroutines must be | ||
| 2691 | passed in via the I<parent> and I<children> question tags. Also, a I<root> | ||
| 2692 | tag should specify the starting point. | ||
| 2693 | |||
| 2694 | Widget Options: | ||
| 2695 | |||
| 2696 | =over | ||
| 2697 | |||
| 2698 | =item parent => CODEREF | ||
| 2699 | |||
| 2700 | The I<parent> function will be called with a wizard reference and a | ||
| 2701 | node name. It is expected to return the name of the node's parent. | ||
| 2702 | |||
| 2703 | The function should return B<undef> when no parent exists above the | ||
| 2704 | current node. | ||
| 2705 | |||
| 2706 | =item children => CODEREF | ||
| 2707 | |||
| 2708 | The I<children> function will be passed a wizard reference and a node | ||
| 2709 | name. It is expected to return an array reference to all the children | ||
| 2710 | names. It may also return a hash reference for some names instead, | ||
| 2711 | which will contain an internal name tag along with a label tag for | ||
| 2712 | displaying something to the user which is different than is internally | ||
| 2713 | passed around as the resulting selected value. | ||
| 2714 | |||
| 2715 | An example return array structure could look like: | ||
| 2716 | |||
| 2717 | [ | ||
| 2718 | 'simple string 1', | ||
| 2719 | 'simple string 2', | ||
| 2720 | { | ||
| 2721 | name => 'myanswer:A', | ||
| 2722 | label => 'Answer #A' | ||
| 2723 | }, | ||
| 2724 | { | ||
| 2725 | name => 'myanswer:B', | ||
| 2726 | label => 'Answer #B' | ||
| 2727 | }, | ||
| 2728 | ] | ||
| 2729 | |||
| 2730 | The function should return B<undef> when no children exist below the | ||
| 2731 | current node. | ||
| 2732 | |||
| 2733 | =item expand_all => NUM | ||
| 2734 | |||
| 2735 | The I<expand_all> tag may be passed which will indicate that all | ||
| 2736 | initial option trees sholud be expanded up to the number indicated by | ||
| 2737 | the expand_all tag. | ||
| 2738 | |||
| 2739 | =back | ||
| 2740 | |||
| 2741 | =item button | ||
| 2742 | |||
| 2743 | Button widget. When the button is clicked, the QWizard parameter I<name> | ||
| 2744 | (available by calling I<qwparam('name')>) will be assigned the value indicated | ||
| 2745 | by the I<default> clause. The parameter value will not be set if the button | ||
| 2746 | is not clicked. The button's label text will be set to the value of the | ||
| 2747 | I<values> clause. | ||
| 2748 | |||
| 2749 | The button widget will be equivalent to pressing the next button. The next | ||
| 2750 | primary will be shown after the user presses the button. | ||
| 2751 | |||
| 2752 | If a backend supports key accelerators (GTk2): Button labels can be | ||
| 2753 | bound to Alt-key accelerators. See QUESTION KEY-ACCELERATORS below | ||
| 2754 | for more information. | ||
| 2755 | |||
| 2756 | =item bar | ||
| 2757 | |||
| 2758 | A bar widget is functionally a separator in the middle of the list of | ||
| 2759 | questions. It is useful for breaking a set of questions in two as | ||
| 2760 | well as providing button-containers or menu containers within widget | ||
| 2761 | sets and not having them tied to the normal QWizard left/right feel. | ||
| 2762 | One intentional artifact of this is they can be used to provide a | ||
| 2763 | visual difference between the flow of the questions. EG, if the | ||
| 2764 | QWizard primary showed a screen which had two questions in it, it | ||
| 2765 | would look something like the following when displayed by most of the | ||
| 2766 | generators that exist today: | ||
| 2767 | |||
| 2768 | +-------------------+-----------------+ | ||
| 2769 | | Question 1 | Answer Widget 1 | | ||
| 2770 | | Longer Question 2 | Answer Widget 2 | | ||
| 2771 | +-------------------+-----------------+ | ||
| 2772 | |||
| 2773 | Adding a bar in the middle of these questions, however, would break | ||
| 2774 | the forced columns above into separate pieces: | ||
| 2775 | |||
| 2776 | +------------+------------------------+ | ||
| 2777 | | Question 1 | Answer Widget 1 | | ||
| 2778 | +------------+------------------------+ | ||
| 2779 | | BAR | | ||
| 2780 | +-------------------+-----------------+ | ||
| 2781 | | Longer Question 2 | Answer Widget 2 | | ||
| 2782 | +-------------------+-----------------+ | ||
| 2783 | |||
| 2784 | Finally, there is an implicit I<top> bar in every primary and the | ||
| 2785 | QWizard object as a whole. You can push objects onto this bar by | ||
| 2786 | adding objects to the $qwizard->{'topbar'} array or by adding objects | ||
| 2787 | to a primary's 'topbar' tag. E.G. | ||
| 2788 | |||
| 2789 | my $qw = new QWizard(primaries => \%primaries, | ||
| 2790 | topbar => [ | ||
| 2791 | { | ||
| 2792 | type => 'menu', name => 'menuname', | ||
| 2793 | values => [qw(1 2 3 4)], | ||
| 2794 | # ... | ||
| 2795 | }]); | ||
| 2796 | |||
| 2797 | The widgets shown in the topbar will be a merge of those from the | ||
| 2798 | QWizard object and the primary currently being displayed. | ||
| 2799 | |||
| 2800 | TODO: make it work better with merged primaries | ||
| 2801 | |||
| 2802 | TODO: make a bottom bar containing the next/prev/cancel buttons | ||
| 2803 | |||
| 2804 | =item hidden | ||
| 2805 | |||
| 2806 | This clause is used to set internal parameters (name => value), but these | ||
| 2807 | values are not shown to the user. | ||
| 2808 | |||
| 2809 | B<Note:> This is not a secure way to hide information from the user. The | ||
| 2810 | data set using I<hidden> are contained, for example, in the HTML text sent | ||
| 2811 | to the user. | ||
| 2812 | |||
| 2813 | =item dynamic | ||
| 2814 | |||
| 2815 | A dynamic question is one where the I<values> field is evaluated and is | ||
| 2816 | expected to return an array of question definitions which are in turn | ||
| 2817 | each evaluated as a question. It is useful primarily when doing | ||
| 2818 | things like creating a user-defined number of input fields, or | ||
| 2819 | interacting with an external data source where the number of questions | ||
| 2820 | and their nature is directly related to the external data source. | ||
| 2821 | |||
| 2822 | =item raw | ||
| 2823 | |||
| 2824 | Raw data. The I<values> portion is displayed straight to the screen. Use | ||
| 2825 | of this is strongly discouraged. Obviously, the I<values> portion should be | ||
| 2826 | a subroutine that understands how to interact with the generator. | ||
| 2827 | |||
| 2828 | Really, don't use this. It's for emergencies only. It only works with HTML | ||
| 2829 | output. | ||
| 2830 | |||
| 2831 | =back | ||
| 2832 | |||
| 2833 | =item values => [ VALUES ] | ||
| 2834 | |||
| 2835 | An array of values that may be assigned to question types that need choices | ||
| 2836 | (eg: I<menu>, I<checkbox>, I<multi_checkbox>.) It should be a reference to | ||
| 2837 | an array containing a list of strings, functions to execute, and possibly | ||
| 2838 | sub-arrays containing a function and arguments, as described by the VALUE | ||
| 2839 | conventions section above. Any function listed in a I<values> clause should | ||
| 2840 | return a list of strings. | ||
| 2841 | |||
| 2842 | The I<values> clause is not needed if the I<labels> clause is present. | ||
| 2843 | |||
| 2844 | =item labels => [ VALUE1 => 'LABEL1', VALUE2 => 'LABEL2', ... ] | ||
| 2845 | |||
| 2846 | Assigns labels to the question's values. Labels are displayed to the user | ||
| 2847 | instead of the raw values. This is useful for converting human-readable text | ||
| 2848 | strings into real-world values for use in code. | ||
| 2849 | |||
| 2850 | If the I<values> clause is not specified and the I<labels> clause is, the | ||
| 2851 | values to display are extracted from this I<labels> clause directly. If a | ||
| 2852 | value from the I<values> clause does not have a corresponding label, the raw | ||
| 2853 | value is presented and used instead. Generally, only the I<labels> clause | ||
| 2854 | should be used with radio buttons, menus, or check boxes; but either or both | ||
| 2855 | in combination work. | ||
| 2856 | |||
| 2857 | The I<labels> clause subscribes to all the properties of the VALUES convention | ||
| 2858 | previously discussed. Thus, it may be a function, an array of functions, or | ||
| 2859 | any other type of data that a VALUE may be. The final results should be an | ||
| 2860 | array, especially if the I<values> clause is not present, as the order | ||
| 2861 | displayed to the user can be specified. It can also be a hash as well but | ||
| 2862 | the displayed order is subject to Perl I<keys>() conventions and thus an array | ||
| 2863 | is preferred when no I<values> clause has been defined. | ||
| 2864 | |||
| 2865 | =item default => VALUE | ||
| 2866 | |||
| 2867 | The default value to use for this question. It may be a subroutine | ||
| 2868 | reference which will be called to calculate and return the value. | ||
| 2869 | |||
| 2870 | =item check_value => \&subroutine | ||
| 2871 | |||
| 2872 | A script to check the answer submitted by the user for legality. The | ||
| 2873 | script should return 'OK' to indicate no error found. In the event an | ||
| 2874 | error is detected, it should return an error string. The string will | ||
| 2875 | be shown to the user as an error message that the user must fix before | ||
| 2876 | being allowed to proceed further in the wizard screens. Alternately, | ||
| 2877 | the script may return 'REDISPLAY' to indicate no error but that screen | ||
| 2878 | should be redisplyed (perhaps with new values set with qwparam() from | ||
| 2879 | within the script). In the case of error or 'REDISPLAY', the current | ||
| 2880 | primary screen will be repeated until the function returns 'OK'. | ||
| 2881 | |||
| 2882 | The arguments passed to the function are the reference to the wizard, | ||
| 2883 | a reference to the question definition (the hash), and a reference | ||
| 2884 | to the primary containing the question (also a hash.) The function | ||
| 2885 | should use the I<qwparam>() function to obtain the value to check. An | ||
| 2886 | array can be passed in which the first argument should be the | ||
| 2887 | subroutine reference, and the remaining arguments will be passed back | ||
| 2888 | to the subroutine after the already mentioned default arguments. | ||
| 2889 | |||
| 2890 | There are a set of standard functions that can be used for checking | ||
| 2891 | values. These are: | ||
| 2892 | |||
| 2893 | =over 4 | ||
| 2894 | |||
| 2895 | =item \&qw_required_field | ||
| 2896 | |||
| 2897 | Ensures that a value is supplied or else a "This is a required field" | ||
| 2898 | error message is returned. The function only checks that the value | ||
| 2899 | is non-zero in length. | ||
| 2900 | |||
| 2901 | =item \&qw_integer | ||
| 2902 | |||
| 2903 | =item \&qw_optional_integer | ||
| 2904 | |||
| 2905 | Ensures that the value is an integer value (required or not, respectively.) | ||
| 2906 | |||
| 2907 | =item \&qw_hex | ||
| 2908 | |||
| 2909 | =item \&qw_optional_hex | ||
| 2910 | |||
| 2911 | Ensures that the value is a hex string (required or not, respectively.) | ||
| 2912 | |||
| 2913 | =item [\&qw_check_hex_and_length, length] | ||
| 2914 | |||
| 2915 | Ensures that a value is supplied and is a hex string sufficiently long | ||
| 2916 | for I<length> bytes. This means that the hex string must be "I<length> * 2" | ||
| 2917 | ASCII characters (two hex characters per byte.) | ||
| 2918 | |||
| 2919 | =item [\&qw_check_int_ranges, low1, high1, low2, high2, ...] | ||
| 2920 | |||
| 2921 | Ensures that the value specified falls within one of the I<lowX> - | ||
| 2922 | I<highX> ranges. The value must be between (I<low1> and I<high1>) or | ||
| 2923 | (I<low2> and I<high2>). | ||
| 2924 | |||
| 2925 | =item [\&qw_check_length_ranges, low1, high1, low2, high2, ...] | ||
| 2926 | |||
| 2927 | I<qw_check_length_ranges> is similar to I<qw_check_int_ranges>(), but it | ||
| 2928 | checks that the length of the data string specified by the user falls within | ||
| 2929 | the given ranges. | ||
| 2930 | |||
| 2931 | =back | ||
| 2932 | |||
| 2933 | =item doif => sub { LOGIC } | ||
| 2934 | |||
| 2935 | Allows questions to be I<optional> and only displayed under certain | ||
| 2936 | conditions. | ||
| 2937 | |||
| 2938 | If specified, it should be a CODE reference which when executed should | ||
| 2939 | return a 1 if the question is to be displayed or a 0 if not. The | ||
| 2940 | question will be entirely skipped if the CODE reference returns a 0. | ||
| 2941 | |||
| 2942 | =item helptext | ||
| 2943 | =item helpdescr | ||
| 2944 | |||
| 2945 | If specified, these define the help text for a question. I<helpdescr> should | ||
| 2946 | be short descriptions printed on screen when the wizard screen is displayed, | ||
| 2947 | and I<helptext> should be a full length description of help that will be | ||
| 2948 | displayed only when the user clicks on the help button. I<helpdescr> is | ||
| 2949 | optional, and a button will be shown linking to I<helptext> regardless. | ||
| 2950 | |||
| 2951 | =item indent => 1 | ||
| 2952 | |||
| 2953 | Slightly indents the question for some generators. | ||
| 2954 | |||
| 2955 | =item submit => 1 | ||
| 2956 | |||
| 2957 | When this is specified as a question argument, if the user changes the | ||
| 2958 | value then it will also be the equivelent of pressing the 'Next' | ||
| 2959 | button at the same time. With the HTML generator, this requires | ||
| 2960 | javascript and thus you shouldn't absolutely depend on it working. | ||
| 2961 | |||
| 2962 | =item refresh_on_change => 1 | ||
| 2963 | |||
| 2964 | If the contents of a screen are generated based on data extracted from | ||
| 2965 | dynamically changing sources (e.g., a database), then setting this | ||
| 2966 | parameter to 1 will make the current question force a refresh if the | ||
| 2967 | value changes (ie, when they pull down a menu and change the value or | ||
| 2968 | click on a checkbox or ...) and the screen be redrawn (possibly | ||
| 2969 | changing its contents). | ||
| 2970 | |||
| 2971 | As an example, Net-Policy uses this functionality to allow users to | ||
| 2972 | redisplay generated data tables and changes the column that is used | ||
| 2973 | for sorting depending on a menu widget. | ||
| 2974 | |||
| 2975 | =item handle_results => sub { ... } | ||
| 2976 | |||
| 2977 | The handle_results tag can specify a CODE reference to be run when the | ||
| 2978 | questions are answered so each question can perform its own | ||
| 2979 | processing. This is sort of like a per-question post_answers hook | ||
| 2980 | equivalent. | ||
| 2981 | |||
| 2982 | =back | ||
| 2983 | |||
| 2984 | =back | ||
| 2985 | |||
| 2986 | =head3 QUESTION KEY-ACCELERATORS | ||
| 2987 | |||
| 2988 | Some generators (currently only Gtk2 actually) support key | ||
| 2989 | accelerators so that you can bind alt-keys to widgets. This is done | ||
| 2990 | by including a '_' (underscore) character where appropriate to create | ||
| 2991 | the binding. EG: | ||
| 2992 | |||
| 2993 | { | ||
| 2994 | type => 'radio', | ||
| 2995 | text => 'select one:', | ||
| 2996 | values => ['_Option1','O_ption2', 'Option3'] | ||
| 2997 | } | ||
| 2998 | |||
| 2999 | When Gtk2 gets the above construct it will make Alt-o be equivelent to | ||
| 3000 | pressing the first option and Alt-p to the second. It will also | ||
| 3001 | display the widget with a underline under the character that is bound | ||
| 3002 | to the widget. HTML and other non-accelerator supported interfaces | ||
| 3003 | will strip out the _ character before displaying the string in a widget. | ||
| 3004 | |||
| 3005 | In addition, unless a I<no_auto_accelerators => 1> option is passed to | ||
| 3006 | the generator creation arguments, widgets will automatically get | ||
| 3007 | accelerators assigned to them. In the above case the 't' in Option3 | ||
| 3008 | would automatically get assigned the Alt-t accelerator (the 't' is | ||
| 3009 | selected because it hasn't been used yet, unlike the o and p | ||
| 3010 | characters). You can also prefix something with a ! character to | ||
| 3011 | force a single widget to not receive an auto-accelerator (EG: | ||
| 3012 | "!Option4" wouldn't get one). | ||
| 3013 | |||
| 3014 | =head1 SPECIAL VARIABLES | ||
| 3015 | |||
| 3016 | A few QWizard parameters are special and help control how QWizard behaves. | ||
| 3017 | Most of these should be set in the primaries question sets using a hidden | ||
| 3018 | question type. | ||
| 3019 | |||
| 3020 | =over | ||
| 3021 | |||
| 3022 | =item no_actions | ||
| 3023 | |||
| 3024 | If set to 1, the I<actions> phase will not be run. | ||
| 3025 | |||
| 3026 | =item no_confirm | ||
| 3027 | |||
| 3028 | If set to 1, the screen which prompts the user to decide if they really want | ||
| 3029 | to commit their series of answers won't be shown. Instead, QWizard will jump | ||
| 3030 | straight to the I<actions> execution (if appropriate.) This can also be given | ||
| 3031 | as a parameter to the QWizard I<new>() function to make it always true. | ||
| 3032 | |||
| 3033 | =item allow_refresh | ||
| 3034 | |||
| 3035 | If the contents of a screen are generated based on data extracted from | ||
| 3036 | dynamically changing sources (e.g., a database), then setting this | ||
| 3037 | parameter to 1 will add a "Refresh" button beside the "Next" button | ||
| 3038 | so that the user can request the screen be redrawn (possibly changing | ||
| 3039 | its contents). | ||
| 3040 | |||
| 3041 | As an example, Net-Policy uses this functionality to allow users to redisplay | ||
| 3042 | generated graphs and maps that will change dynamically as network data are | ||
| 3043 | collected. | ||
| 3044 | |||
| 3045 | This token can also be set directly in a primary definition to affect | ||
| 3046 | just that primary screen. | ||
| 3047 | |||
| 3048 | =item QWizard_next | ||
| 3049 | |||
| 3050 | The button text to display for the "Next" button. This defaults to | ||
| 3051 | "_Next" but can be overridden using this parameter. | ||
| 3052 | |||
| 3053 | =item QWizard_commit | ||
| 3054 | |||
| 3055 | The button text to display for the "Commit" button. This defaults to | ||
| 3056 | "_Commit" but can be overridden using this parameter. The commit | ||
| 3057 | button is shown after the questions have been asked and the | ||
| 3058 | actions_descr's are being shown to ask the user if they really want to | ||
| 3059 | run the actions. | ||
| 3060 | |||
| 3061 | =item QWizard_finish | ||
| 3062 | |||
| 3063 | The button text to display for the "Finish" button. This defaults to | ||
| 3064 | "_Finish" but can be overridden using this parameter. The finish | ||
| 3065 | button is shown after the actions have been run and the results are | ||
| 3066 | being displayed. | ||
| 3067 | |||
| 3068 | =back | ||
| 3069 | |||
| 3070 | =head1 QWIZARD RESERVED VARIABLES | ||
| 3071 | |||
| 3072 | The following parameters are used internally by QWizard. They should not be | ||
| 3073 | modified. | ||
| 3074 | |||
| 3075 | =over | ||
| 3076 | |||
| 3077 | =item pass_vars | ||
| 3078 | |||
| 3079 | =item qw_cancel | ||
| 3080 | |||
| 3081 | =item no_cancel | ||
| 3082 | |||
| 3083 | =item qwizard_tree | ||
| 3084 | |||
| 3085 | =item display_help_p | ||
| 3086 | |||
| 3087 | =item disp_q_num | ||
| 3088 | |||
| 3089 | =item redo_screen | ||
| 3090 | |||
| 3091 | =item upd_time | ||
| 3092 | |||
| 3093 | =item wiz_confirmed | ||
| 3094 | |||
| 3095 | =item wiz_canceled | ||
| 3096 | |||
| 3097 | =item wizard_queue_list | ||
| 3098 | |||
| 3099 | =back | ||
| 3100 | |||
| 3101 | =head1 QWizard OBJECT FUNCTIONS | ||
| 3102 | |||
| 3103 | The following functions are defined in the QWizard class and can be called | ||
| 3104 | as needed. | ||
| 3105 | |||
| 3106 | =over | ||
| 3107 | |||
| 3108 | =item $qw->magic(primary_name, ...); | ||
| 3109 | |||
| 3110 | This tells QWizard to start its magic, beginning at the primary named | ||
| 3111 | I<primary_name>. Multiple primaries will be displayed one after the other | ||
| 3112 | until the list of primaries to display is empty. The I<actions> clauses of | ||
| 3113 | all these primaries will not be run, however, until after all the primaries | ||
| 3114 | have been processed. | ||
| 3115 | |||
| 3116 | The I<magic>() routine exits only after all the primaries have been run up | ||
| 3117 | through their actions, or unless one of the following conditions occurs: | ||
| 3118 | |||
| 3119 | - $qw->{'one_pass'} == 1 | ||
| 3120 | - $qw->{'generator'}{'one_pass'} == 1 | ||
| 3121 | |||
| 3122 | By default, some of the stateless generators (HTML) will set their I<one_pass> | ||
| 3123 | option automatically since it is expected that the client will exit the | ||
| 3124 | I<magic>() loop and return later with the next set of data to process. The | ||
| 3125 | I<magic>() routine will automatically restart where it left off if the last | ||
| 3126 | set of primaries being displayed was never finished. This is common for | ||
| 3127 | stateless generators like HTTP and HTML. | ||
| 3128 | |||
| 3129 | =item $qw->finished(); | ||
| 3130 | |||
| 3131 | Closes the open qwizard window. Useful after your magic() routine has | ||
| 3132 | ended and you don't intend to call it again. Calling finished() will | ||
| 3133 | remove the QWizard window from visibility. | ||
| 3134 | |||
| 3135 | =item $qw->set_progress(FRACTION, TEXT); | ||
| 3136 | |||
| 3137 | If available, some generators (Gtk2 can) may be able to display a | ||
| 3138 | progress meter. If they are, calling this function inside action | ||
| 3139 | clauses, for example, will start the display of this meter with | ||
| 3140 | I<FRACTION> complete (0 <= I<FRACTION> <= 1). The I<TEXT> is optional | ||
| 3141 | and if left blank will be set to I<NN%> showing the percentage complete. | ||
| 3142 | |||
| 3143 | =item $qw->add_todos([options], primary_name, ...); | ||
| 3144 | |||
| 3145 | Adds a primary to the list of screens to display to the user. This | ||
| 3146 | function should be called during the I<post_answers> section of a primary. | ||
| 3147 | Options that can be passed before the first primary name are: | ||
| 3148 | |||
| 3149 | =over | ||
| 3150 | |||
| 3151 | =item -early | ||
| 3152 | |||
| 3153 | Adds the primaries in question as early as possible in the todo list | ||
| 3154 | (next, unless trumped by future calls.) This is the default. | ||
| 3155 | |||
| 3156 | =item -late | ||
| 3157 | |||
| 3158 | Adds the primary to the B<end> of the list of primaries to call, such | ||
| 3159 | that it is called last, unless another call to I<add_todos>() appends | ||
| 3160 | something even later. | ||
| 3161 | |||
| 3162 | =item -actionslast | ||
| 3163 | |||
| 3164 | All the actions of subsequent primaries that have been added as the result | ||
| 3165 | of a current primary's I<post_answers> clauses are called B<before> the | ||
| 3166 | actions for the current primary. This means that the I<actions> of any | ||
| 3167 | childrens are executed prior to the I<actions> of their parents. This is | ||
| 3168 | done by default, as the general usage prediction is that parent primaries are | ||
| 3169 | likely to be dependent on the actions of their children in order for their | ||
| 3170 | own actions to be successful. | ||
| 3171 | |||
| 3172 | However, this flag indicates that the actions of the childrens' primaries | ||
| 3173 | listed in this call are to be called B<before> the current primary's actions. | ||
| 3174 | |||
| 3175 | =item -merge | ||
| 3176 | |||
| 3177 | Merges all the specified primaries listed into a single screen. This has the | ||
| 3178 | effect of having multiple primaries displayed in one window. | ||
| 3179 | |||
| 3180 | Important note: you can not use both -remap (see below) and -merge at | ||
| 3181 | the same time! This will break the remapping support and you will not | ||
| 3182 | get expected results! | ||
| 3183 | |||
| 3184 | =item -remap => 'NAME' | ||
| 3185 | |||
| 3186 | If a series of questions must be called repeatedly, you can use this flag to | ||
| 3187 | I<remap> the names of the child primary questions to begin with this prefix. | ||
| 3188 | The children's clauses (I<questions>, I<actions>, I<post_answers>, etc.) will | ||
| 3189 | be called in such a way that they can be oblivious to the fact this is being | ||
| 3190 | done behind their backs, allowing I<qwparam>() to work as expected. However, | ||
| 3191 | for the current primary (and any parents), the 'NAME' prefix will be added | ||
| 3192 | to the front of any question name values that the child results in defining. | ||
| 3193 | |||
| 3194 | This is rather complex and is better illustrated through an example. There | ||
| 3195 | is an example that illustrates this in the QWizard Perl module source code | ||
| 3196 | B<examples> directory, in the file B<number_adding.pl>. This code repeatedly | ||
| 3197 | asks for numbers from the user using the same primary. | ||
| 3198 | |||
| 3199 | Important note: you can not use both -remap and -merge at the same | ||
| 3200 | time! This will break the remapping support and you will not get | ||
| 3201 | expected results! | ||
| 3202 | |||
| 3203 | =back | ||
| 3204 | |||
| 3205 | =item $qw->add_primary(key => value, key => value); | ||
| 3206 | |||
| 3207 | Adds a primary definition into the existing primary data set for the | ||
| 3208 | QWizard object. One key value pair B<MUST> be a 'name' => 'NAME' | ||
| 3209 | pair, where NAME will be the installed primary name for later referral | ||
| 3210 | (e.g., in I<add_todos>() calls.) If a name collision takes place (a | ||
| 3211 | primary already exists under the given name), the original is kept | ||
| 3212 | and the new is not installed. | ||
| 3213 | |||
| 3214 | =item $qw->merge_primaries(\%new_primaries); | ||
| 3215 | |||
| 3216 | Merges a new set of primaries into the existing set. If a name | ||
| 3217 | collision takes place, the original is kept and the new is not | ||
| 3218 | installed. | ||
| 3219 | |||
| 3220 | =item $qw->get_primary('NAME'); | ||
| 3221 | |||
| 3222 | Returns a primary definition given its NAME. | ||
| 3223 | |||
| 3224 | =item $val = $qw->qwparam('NAME') | ||
| 3225 | |||
| 3226 | =item $val = qwparam('NAME') | ||
| 3227 | |||
| 3228 | =item $qw->qwparam('NAME', 'VALUE') | ||
| 3229 | |||
| 3230 | =item qwparam('NAME', 'VALUE') | ||
| 3231 | |||
| 3232 | Retrieves a value specified by NAME that was submitted by a user from a | ||
| 3233 | QWizard widget. If a VALUE is specified as a second argument, it replaces | ||
| 3234 | the previous value with the new for future calls. | ||
| 3235 | |||
| 3236 | QWizard parameters are accessible until the last screen in which all the | ||
| 3237 | actions are run and the results are displayed. Parameters are not retained | ||
| 3238 | across primary execution. | ||
| 3239 | |||
| 3240 | The I<qwparam>() function is exported by the QWizard module by default, so | ||
| 3241 | the function shouldn't need to be called directly from the QWizard object. | ||
| 3242 | Thus, just calling I<qwparam('NAME')> by itself will work. | ||
| 3243 | |||
| 3244 | =item $val = $qw->qwpref('NAME') | ||
| 3245 | |||
| 3246 | =item $val = qwpref('NAME') | ||
| 3247 | |||
| 3248 | =item $qw->qwpref('NAME', 'VALUE') | ||
| 3249 | |||
| 3250 | =item qwpref('NAME', 'VALUE') | ||
| 3251 | |||
| 3252 | I<qwpref>() acts almost identically to I<qwparam>(), except that it is | ||
| 3253 | expected to be used for "preferences" -- hence the name. The preferences are | ||
| 3254 | stored persistently across primary screens, unlike parameters. Preferences | ||
| 3255 | are not erased between multiple passes through the QWizard screens. (In the | ||
| 3256 | HTML generator, they are implemented using cookies). | ||
| 3257 | |||
| 3258 | =back | ||
| 3259 | |||
| 3260 | =head1 HOOKS | ||
| 3261 | |||
| 3262 | TBD: Document the $qw->add_hook and $qw->run_hooks methods that exist. | ||
| 3263 | |||
| 3264 | (basically $qw->add_hook('start_magic', \&coderef) will run coderef at | ||
| 3265 | the start of the magic function. Search the QWizard code for | ||
| 3266 | run_hooks for a list of hook spots available. | ||
| 3267 | |||
| 3268 | =head1 DEBUGGING | ||
| 3269 | |||
| 3270 | The variable I<$QWizard::qwdebug> controls debugging output from QWizard. If | ||
| 3271 | set to 1, it dumps processing information to STDERR. This can be very useful | ||
| 3272 | when debugging QWizard scripts as it displays the step-by-step process about | ||
| 3273 | how QWizard is processing information. | ||
| 3274 | |||
| 3275 | Additionally, a I<qwdebug_set_output>() function exists which can control the | ||
| 3276 | debugging output destination. Its argument should be a reference to a | ||
| 3277 | variable where the debugging output will be stored. Thus, debugging | ||
| 3278 | information can be stored to a previously opened error log file by doing the | ||
| 3279 | following: | ||
| 3280 | |||
| 3281 | our $dvar; | ||
| 3282 | $QWizard::qwdebug = 1; | ||
| 3283 | $qw->qwdebug_set_output(\$dvar); | ||
| 3284 | $qw->magic('stuff'); | ||
| 3285 | print LOGFILE $dvar; | ||
| 3286 | |||
| 3287 | =head1 EXAMPLES | ||
| 3288 | |||
| 3289 | There are a few usage examples in the B<examples> directory of the | ||
| 3290 | source package. These examples can be run from the command line or | ||
| 3291 | installed as a CGI script without modification. They will run as a | ||
| 3292 | CGI script if run from a web server, or will launch a Gtk2 or Tk | ||
| 3293 | window if run from the command line. | ||
| 3294 | |||
| 3295 | =head1 EXPORT | ||
| 3296 | |||
| 3297 | I<qwparam>(), I<qwpref>() | ||
| 3298 | |||
| 3299 | I<qw_required_field>(), I<qw_integer>(), I<qw_optional_integer>(), | ||
| 3300 | I<qw_check_int_ranges>(), I<qw_check_length_ranges>(), I<qw_hex>(), | ||
| 3301 | I<qw_optional_hex>(), I<qw_check_hex_and_length>() | ||
| 3302 | |||
| 3303 | =head1 AUTHOR | ||
| 3304 | |||
| 3305 | Wes Hardaker, hardaker@users.sourceforge.net | ||
| 3306 | |||
| 3307 | =head1 COPYRIGHT and LICENSE | ||
| 3308 | |||
| 3309 | Copyright (c) 2003-2007, SPARTA, Inc. All rights reserved | ||
| 3310 | |||
| 3311 | Copyright (c) 2006-2007, Wes Hardaker. All rights reserved | ||
| 3312 | |||
| 3313 | QWizard is free software; you can redistribute it and/or modify it under | ||
| 3314 | the same terms as Perl itself. | ||
| 3315 | |||
| 3316 | =head1 SEE ALSO | ||
| 3317 | |||
| 3318 | For extra information, consult the following manual pages: | ||
| 3319 | |||
| 3320 | =over | ||
| 3321 | |||
| 3322 | =item I<QWizard_Widgets> | ||
| 3323 | |||
| 3324 | Manual page for more information about the Various QWizard display | ||
| 3325 | generators and the questions and arguments that each one supports. | ||
| 3326 | This page is actually generated from what the generator actually | ||
| 3327 | advertises that it supports. | ||
| 3328 | |||
| 3329 | =item I<QWizard::API> | ||
| 3330 | |||
| 3331 | If you get tired of typing anonymous hash references, this API set | ||
| 3332 | will let you generate some widgets with less typing by using APIs | ||
| 3333 | instead. | ||
| 3334 | |||
| 3335 | Example API call: | ||
| 3336 | |||
| 3337 | perl -MQWizard::API -MData::Dumper -e 'print Dumper(qw_checkbox("my ?","it",'A','B', default => 'B'));' | ||
| 3338 | $VAR1 = { | ||
| 3339 | 'text' => 'it', | ||
| 3340 | 'name' => 'my ?', | ||
| 3341 | 'default' => 'B', | ||
| 3342 | 'values' => [ | ||
| 3343 | 'A', | ||
| 3344 | 'B' | ||
| 3345 | ], | ||
| 3346 | 'type' => 'checkbox' | ||
| 3347 | }; | ||
| 3348 | |||
| 3349 | =item I<Net-Policy: http://net-policy.sourceforge.net/> | ||
| 3350 | |||
| 3351 | The entire QWizard system was created to support a | ||
| 3352 | multiple-access-point network management system called "Net-Policy" | ||
| 3353 | and the SVN repository for Net-Policy actually contains the QWizard | ||
| 3354 | development tree. | ||
| 3355 | |||
| 3356 | =back | ||
| 3357 | |||
| 3358 | =cut |