| 1 | package CGI::OptimalQuery::InteractiveFilter; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | no warnings qw( uninitialized ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use base 'CGI::OptimalQuery::Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | use Data::Dumper; | ||
| 8 | use DBIx::OptimalQuery(); | ||
| 9 | use CGI(); | ||
| 10 | |||
| 11 | |||
| 12 | my $DEFAULT_CSS = <<'TILEND'; | ||
| 13 | <STYLE type="text/css"> | ||
| 14 | /* ----- base functionality ----- */ | ||
| 15 | |||
| 16 | form.filterForm td.lp_col, | ||
| 17 | form.filterForm td.rp_col { | ||
| 18 | text-align: center; | ||
| 19 | } | ||
| 20 | |||
| 21 | form.filterForm td.lp_col input, | ||
| 22 | form.filterForm td.rp_col input { | ||
| 23 | color: #999999; | ||
| 24 | background-color: white; | ||
| 25 | border: 1px solid #efefef; | ||
| 26 | font-size: 1em; | ||
| 27 | width: 2em; | ||
| 28 | } | ||
| 29 | |||
| 30 | form.filterForm td.l_col input.colvalbtn { display: none; } | ||
| 31 | form.filterForm td.d_col input { color: #990000; background-color: white; border: 1px solid white;} | ||
| 32 | form.filterForm tr.footer td.d_col input { background-color: #990000; color: white; border: 1px solid #666666;} | ||
| 33 | form.filterForm .d_col { color: #990000; } | ||
| 34 | |||
| 35 | div.RO_NAMED_FILTER { | ||
| 36 | background-color: #efefef; | ||
| 37 | color: #222222; | ||
| 38 | font-size: 1.1em; | ||
| 39 | text-align: center; | ||
| 40 | border: 1px solid #666666; | ||
| 41 | } | ||
| 42 | |||
| 43 | form.filterForm input, | ||
| 44 | form.filterForm select { | ||
| 45 | border: 1px solid #666666; | ||
| 46 | background-color: #efefef; | ||
| 47 | padding: 2px; | ||
| 48 | vertical-align: middle; | ||
| 49 | } | ||
| 50 | |||
| 51 | td.f_col { text-align: center; } | ||
| 52 | |||
| 53 | |||
| 54 | form.filterForm .hide { display: none; } | ||
| 55 | |||
| 56 | |||
| 57 | /* -------------------- prefs -------------------- */ | ||
| 58 | form.filterForm .submit_off,.add_off { display: none; } | ||
| 59 | form.filterForm .c_col { text-align: center; } | ||
| 60 | form.filterForm input.submit_ok { background-color: lightgreen; } | ||
| 61 | form.filterForm input.submit_off { background-color: yellow; } | ||
| 62 | form.filterForm .paren_warn { text-align: center; background-color: yellow; } | ||
| 63 | form.filterForm .paren_match { display: none; } | ||
| 64 | form.filterForm .delbtn { color: white; background-color: #990000; } | ||
| 65 | form.filterForm .noparen { color: white; } | ||
| 66 | </STYLE> | ||
| 67 | TILEND | ||
| 68 | |||
| 69 | =comment CSS MORE POSSIBILITIES | ||
| 70 | # Simplest mode | ||
| 71 | form.filterForm .colvalbtn { display:none; } | ||
| 72 | form.filterForm .noparen { display:none; } | ||
| 73 | form.filterForm #paren_warn { display:none; } | ||
| 74 | |||
| 75 | # Disable deleting controls | ||
| 76 | form.filterForm .d_col { display:none; } | ||
| 77 | |||
| 78 | # other possibilities ... | ||
| 79 | form.filterForm label:after { content: " mark"; } | ||
| 80 | <!-- Different Style Sheet possibilites (see documentation) --> | ||
| 81 | <link title="full" rel="stylesheet" type="text/css" href="http:///css/OQfilter_full.css" /> | ||
| 82 | <link title="simplest" rel="alternate stylesheet" type="text/css" href="http:///css/OQfilter_simplest.css" /> | ||
| 83 | <link title="test" rel="alternate stylesheet" type="text/css" href="http:///css/OQfilter_test.css" /> | ||
| 84 | =cut | ||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | # ------------------------- new ------------------------- | ||
| 90 | sub new { | ||
| 91 | my $pack = shift; | ||
| 92 | my $o = $pack->SUPER::new(@_); | ||
| 93 | $$o{view} = ''; | ||
| 94 | $$o{schema}{options}{'CGI::OptimalQuery::InteractiveFilter'}{css} ||= $DEFAULT_CSS; | ||
| 95 | $o->process_actions(); | ||
| 96 | return $o; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | # ------------------------- print ------------------------- | ||
| 101 | sub output { | ||
| 102 | my $o = shift; | ||
| 103 | $$o{output_handler}->(CGI::header()); | ||
| 104 | my $view = $$o{view}; | ||
| 105 | $$o{output_handler}->($o->$view()) if $o->can($view); | ||
| 106 | return undef; | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | =comment | ||
| 113 | Grammar Translations: basically this describes how to convert rules | ||
| 114 | into elements of an expression array. Each element in this array | ||
| 115 | is a hash ref with keys: L_PAREN, R_PAREN, ANDOR, CMPOP, R_VALUE, | ||
| 116 | L_COLMN, R_COLMN, FUNCT, ARG_XYZ. Later this array can be translated | ||
| 117 | to CGI params that represent the filter for an HTML form. | ||
| 118 | Notice: The hash key is the rule name, the value is a sub ref where | ||
| 119 | the following args are defined: | ||
| 120 | $_[0] is $oq | ||
| 121 | $_[1] is rule name | ||
| 122 | $_[2] is token 1, $_[3] is token 2, etc ... | ||
| 123 | =cut | ||
| 124 | my %translations = ( | ||
| 125 | |||
| 126 | # *** RULE *** | ||
| 127 | # exp: | ||
| 128 | # '(' exp ')' logicOp exp | ||
| 129 | # | '(' exp ')' | ||
| 130 | # | comparisonExp logicOp exp | ||
| 131 | # | comparisonExp | ||
| 132 | 'exp' => sub { | ||
| 133 | # expression array is just an array of exp | ||
| 134 | # each element is a hashref containing keys | ||
| 135 | # L_PAREN, R_PAREN, ANDOR, CMPOP, R_VALUE, | ||
| 136 | # L_COLMN, R_COLMN, FUNCT, ARG_XYZ | ||
| 137 | my $expression_array; | ||
| 138 | |||
| 139 | # handle tokens: | ||
| 140 | # '(' exp ')' logicOp exp | ||
| 141 | # | '(' exp ')' | ||
| 142 | if ($_[2] eq '(') { | ||
| 143 | $expression_array = $_[3]; | ||
| 144 | $$expression_array[0]{L_PAREN}++; | ||
| 145 | $$expression_array[-1]{R_PAREN}++; | ||
| 146 | |||
| 147 | # handle tokens: logicOp exp | ||
| 148 | if (exists $_[5]{ANDOR} && ref($_[6]) eq 'ARRAY') { | ||
| 149 | $$expression_array[-1]{ANDOR} = $_[5]{ANDOR}; | ||
| 150 | push @$expression_array, @{ $_[6] }; # append exp | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | # handle tokens: | ||
| 155 | # comparisonExp logicOp exp | ||
| 156 | # | comparisonExp | ||
| 157 | else { | ||
| 158 | $expression_array = [ $_[2] ]; | ||
| 159 | |||
| 160 | # add: logicOp exp | ||
| 161 | if (exists $_[3]{ANDOR} && ref($_[4]) eq 'ARRAY') { | ||
| 162 | $$expression_array[-1]{ANDOR} = $_[3]{ANDOR}; | ||
| 163 | push @$expression_array, @{ $_[4] }; # append exp | ||
| 164 | } | ||
| 165 | } | ||
| 166 | return $expression_array; | ||
| 167 | }, | ||
| 168 | |||
| 169 | # *** RULE *** | ||
| 170 | # namedFilter | ||
| 171 | # | colAlias compOp colAlias | ||
| 172 | # | colAlias compOp bindVal | ||
| 173 | 'comparisonExp' => sub { | ||
| 174 | |||
| 175 | # if not a named filter | ||
| 176 | # combine CMPOP and R_VALUE | R_COLMN in | ||
| 177 | if (exists $_[2]{COLMN}) { | ||
| 178 | $_[2]{L_COLMN} = delete $_[2]{COLMN}; | ||
| 179 | $_[2]{CMPOP} = $_[3]{CMPOP}; | ||
| 180 | if (! ref $_[4]) { $_[2]{R_VALUE} = $_[4]; } | ||
| 181 | else { $_[2]{R_COLMN} = $_[4]{COLMN}; } | ||
| 182 | } | ||
| 183 | return $_[2]; | ||
| 184 | }, | ||
| 185 | |||
| 186 | # remove quotes from string | ||
| 187 | 'quotedString' => sub { $_ = $_[2]; s/^\'// || s/^\"//; s/\'$// || s/\"$//; $_; }, | ||
| 188 | |||
| 189 | # *** RULE *** | ||
| 190 | # colAlias: '[' /\w+/ ']' | ||
| 191 | 'colAlias' => sub { | ||
| 192 | die "could not find colAlias '$_[3]'" unless exists $_[0]{select}{$_[3]}; | ||
| 193 | return { 'COLMN' => $_[3] }; | ||
| 194 | }, | ||
| 195 | |||
| 196 | # *** RULE *** logicOp: /and/i | /or/i | ||
| 197 | 'logicOp' => sub { { ANDOR => uc($_[2]) } }, | ||
| 198 | |||
| 199 | # *** RULE *** compOp: '=' | '!=' | '<' |, .... | ||
| 200 | 'compOp' => sub { { CMPOP => lc($_[2]) } }, | ||
| 201 | |||
| 202 | # *** RULE *** nullComp: /is\ null/i | /is\ not\ null/i | ||
| 203 | 'namedFilter' => sub { | ||
| 204 | die "could not find named filter '$_[2]'" unless exists $_[0]{named_filters}{$_[2]}; | ||
| 205 | my $rv = { 'FUNCT' => $_[2].'()' }; | ||
| 206 | my %args; | ||
| 207 | %args = @{ $_[4] } if ref($_[4]) eq 'ARRAY'; | ||
| 208 | foreach my $key (keys %args) { $$rv{'ARG_'.$key} = $args{$key}; } | ||
| 209 | return $rv; | ||
| 210 | }, | ||
| 211 | |||
| 212 | # just return the first token for all other rules not specified | ||
| 213 | '*default*' => sub { $_[2] } | ||
| 214 | ); | ||
| 215 | |||
| 216 | |||
| 217 | |||
| 218 | # ------------------------- process actions ------------------------- | ||
| 219 | sub process_actions { | ||
| 220 | my $o = shift; | ||
| 221 | my $q = $$o{q}; | ||
| 222 | $$o{view} = 'html_filter_form'; | ||
| 223 | |||
| 224 | # should we load a fresh filter into the appropriate params | ||
| 225 | # representing the filter? | ||
| 226 | if ($q->param('filter') ne '') { | ||
| 227 | my $expression_array = $$o{oq}->parse($DBIx::OptimalQuery::filterGrammar, | ||
| 228 | $q->param('filter'), \%translations); | ||
| 229 | die "bad filter!\nfilter= ".$q->param('filter'). | ||
| 230 | "\nexp=".Dumper( $expression_array )."\n" unless ref($expression_array) eq 'ARRAY'; | ||
| 231 | |||
| 232 | # fill in the params representing the filter state | ||
| 233 | my $i = 0; | ||
| 234 | foreach my $exp (@$expression_array) { | ||
| 235 | $i++; | ||
| 236 | while (my ($k,$v) = each %$exp) { $q->param('F'.$i.'_'.$k,$v); } | ||
| 237 | } | ||
| 238 | $q->param('FINDX', $i); | ||
| 239 | $q->param('hideParen', ($i < 3)); | ||
| 240 | $$o{view} = 'html_filter_form'; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | # did the user request a new expression? | ||
| 245 | if ( defined $q->param('NEXT_EXPR') | ||
| 246 | && scalar $q->param('NEXT_EXPR') ne '-- add new filter element --') { | ||
| 247 | my $val = scalar $q->param('NEXT_EXPR'); | ||
| 248 | my $findx = $q->param('FINDX'); | ||
| 249 | $findx = 0 unless $findx > 0; | ||
| 250 | $findx++; | ||
| 251 | my $pn = 'F' . $findx . '_'; | ||
| 252 | if( $val =~ /\(\)\Z/ ) { # ends with a () | ||
| 253 | $q->param($pn.'FUNCT', $val); | ||
| 254 | } else { | ||
| 255 | $q->param($pn.'L_COLMN', $val); | ||
| 256 | $q->param($pn.'L_VALUE', ''); | ||
| 257 | if ($o->typ4clm($val) eq 'char' || | ||
| 258 | $o->typ4clm($val) eq 'clob') { | ||
| 259 | $q->param($pn.'CMPOP', 'contains'); | ||
| 260 | } else { | ||
| 261 | $q->param($pn.'CMPOP', '='); | ||
| 262 | } | ||
| 263 | } | ||
| 264 | $q->param('FINDX', $findx); | ||
| 265 | $q->param('hideParen', ( $findx < 3 ) ); | ||
| 266 | $q->param('NEXT_EXPR', '--- Choose Next Filter ---'); | ||
| 267 | $$o{view} = 'html_filter_form'; | ||
| 268 | |||
| 269 | } | ||
| 270 | |||
| 271 | # did user submit the filter? | ||
| 272 | elsif ($q->param('act') eq 'submit_filter') { | ||
| 273 | my $ftxt = $o->recreateFilterString(); | ||
| 274 | $q->param('filter', $ftxt); | ||
| 275 | $$o{view} = 'html_parent_update'; | ||
| 276 | if ($$o{error}) { | ||
| 277 | $$o{view} = 'html_filter_form'; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | # did user request to delete filter | ||
| 282 | elsif ($q->param('act') eq 'submit_del') { | ||
| 283 | delselForm( $q ); | ||
| 284 | $$o{view} = 'html_filter_form'; | ||
| 285 | } | ||
| 286 | |||
| 287 | return undef; | ||
| 288 | } | ||
| 289 | |||
| 290 | |||
| 291 | # ------------------------- cmp_val ------------------------- | ||
| 292 | sub cmp_val ( $$$$ ) { | ||
| 293 | my( $q, $pnam, $vals, $lbls ) = @_; | ||
| 294 | |||
| 295 | |||
| 296 | my $isUserVal = ( $q->param($pnam.'COLMN') eq '' | ||
| 297 | || $q->param($pnam.'ISVAL') ); | ||
| 298 | |||
| 299 | return | ||
| 300 | $q->button( -name=>$pnam.'BTN', | ||
| 301 | -label=>$isUserVal?'value:':'column:', | ||
| 302 | -onClick=>"toggle_value('$pnam');", | ||
| 303 | -class=>'colvalbtn') | ||
| 304 | . $q->hidden( -name=>$pnam.'ISVAL', -default=>$isUserVal ) | ||
| 305 | . $q->textfield( -name=>$pnam.'VALUE', | ||
| 306 | -class=> ( $isUserVal ? 'val' : 'hide' ) ) | ||
| 307 | . $q->popup_menu( -name=>$pnam.'COLMN', | ||
| 308 | -values=> $vals, -labels=> $lbls, | ||
| 309 | -onChange=>"submit_act('refresh');", | ||
| 310 | -class=> $isUserVal ? 'hide' : 'col'); | ||
| 311 | |||
| 312 | } | ||
| 313 | |||
| 314 | # ------------------------- recreateFilterString ------------------------- | ||
| 315 | sub recreateFilterString { | ||
| 316 | my $o = shift; | ||
| 317 | my $q = $$o{q}; | ||
| 318 | |||
| 319 | # pull out the fuctions arguments from the form | ||
| 320 | my %funct_args = (); | ||
| 321 | foreach my $fak ( $q->param() ){ | ||
| 322 | my @ary = split 'ARG_', $fak; | ||
| 323 | $funct_args{$ary[0]}{$ary[1]} = $q->param($fak) | ||
| 324 | if defined $ary[1]; | ||
| 325 | } | ||
| 326 | |||
| 327 | my $ftext = ''; | ||
| 328 | my $ei = scalar $q->param('FINDX'); | ||
| 329 | for( my $i = 1; $i <= $ei; $i++ ) { | ||
| 330 | my $p = 'F' . $i . '_'; | ||
| 331 | |||
| 332 | my $parcnt = $q->param($p.'L_PAREN'); | ||
| 333 | $ftext .= ($parcnt < 1 ? '' : '('x$parcnt . ' ' ); | ||
| 334 | |||
| 335 | if( defined $q->param($p.'FUNCT') | ||
| 336 | && $q->param($p.'FUNCT') ne '' ) { | ||
| 337 | |||
| 338 | # TODO: Grab the $p.'ARG_' and Dump it. | ||
| 339 | my $f = $q->param($p.'FUNCT'); | ||
| 340 | $f =~ s/\(\)\Z//; | ||
| 341 | my $args = ''; | ||
| 342 | while (my ($k,$v) = each %{ $funct_args{$p} }) { | ||
| 343 | $args .= ',' if $args; | ||
| 344 | $v = "'".$v."'" if $v =~ /\W/; | ||
| 345 | $args .= "$k,$v"; | ||
| 346 | } | ||
| 347 | $ftext .= " $f($args) "; | ||
| 348 | } else { | ||
| 349 | if( $q->param($p.'L_ISVAL') ) { | ||
| 350 | $ftext .= '\'' . $q->param($p.'L_VALUE') . '\''; | ||
| 351 | } else { | ||
| 352 | $ftext .= '[' . $q->param($p.'L_COLMN') . ']'; | ||
| 353 | |||
| 354 | # force operator to be "like/not like" if a numeric operator | ||
| 355 | if ($o->typ4clm(uc($q->param($p.'L_COLMN'))) eq 'clob' && | ||
| 356 | $q->param($p.'CMPOP') !~ /\w/) { | ||
| 357 | if ($q->param($p.'CMPOP') =~ /\!/) { | ||
| 358 | $q->param($p.'CMPOP', "not like"); | ||
| 359 | } else { | ||
| 360 | $q->param($p.'CMPOP', "like"); | ||
| 361 | } | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | $ftext .= ' ' . $q->param($p.'CMPOP') . ' '; | ||
| 366 | |||
| 367 | if( $q->param($p.'R_ISVAL') ) { | ||
| 368 | my $val = $q->param($p.'R_VALUE'); | ||
| 369 | if ($val =~ /\'/ || $val =~ /\"/) { | ||
| 370 | if ($val !~ /\"/) { $val = '"'.$val.'"'; } | ||
| 371 | elsif ($val !~ /\'/) { $val = "'".$val."'"; } | ||
| 372 | else { $val =~ s/\'|\"//g; } | ||
| 373 | } else { | ||
| 374 | $val = "'$val'"; | ||
| 375 | } | ||
| 376 | $ftext .= $val; | ||
| 377 | |||
| 378 | # if date comparison and right side is value and numeric operator | ||
| 379 | # ensure the right side valud fits date_format string | ||
| 380 | if ($q->param($p.'L_COLMN') && | ||
| 381 | $q->param($p.'CMPOP') !~ /\w/ && | ||
| 382 | exists $$o{schema}{select}{$q->param($p.'L_COLMN')} && | ||
| 383 | exists $$o{schema}{select}{$q->param($p.'L_COLMN')}[3]{date_format}) { | ||
| 384 | my $date_format = $$o{schema}{select}{$q->param($p.'L_COLMN')}[3]{date_format}; | ||
| 385 | local $$o{dbh}->{RaiseError} = 0; | ||
| 386 | local $$o{dbh}->{PrintError} = 0; | ||
| 387 | if ($$o{dbh}{Driver}{Name} eq 'Oracle') { | ||
| 388 | my $dt = $q->param($p.'R_VALUE'); | ||
| 389 | if ($dt ne '') { | ||
| 390 | my ($rv) = $$o{dbh}->selectrow_array("SELECT 1 FROM dual WHERE to_date(?,'$date_format') IS NOT NULL", undef, $dt); | ||
| 391 | if (! $rv) { | ||
| 392 | $$o{error} = "invalid date: \"$dt\", must be in format: \"$date_format\""; | ||
| 393 | return undef; | ||
| 394 | } | ||
| 395 | } | ||
| 396 | } | ||
| 397 | } | ||
| 398 | } else { | ||
| 399 | $ftext .= '[' . $q->param($p.'R_COLMN') . ']'; | ||
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | $parcnt = $q->param($p.'R_PAREN'); | ||
| 404 | $ftext .= ( $parcnt<1 ? '' : ')'x$parcnt .' ' ) . "\n"; | ||
| 405 | |||
| 406 | $ftext .= $q->param($p.'ANDOR') . "\n" unless( $i == $ei ); | ||
| 407 | |||
| 408 | } | ||
| 409 | |||
| 410 | $ftext =~ s/\n//g; | ||
| 411 | return $ftext; | ||
| 412 | } | ||
| 413 | |||
| 414 | # ------------------------- delselForm ------------------------- | ||
| 415 | sub delselForm( $ ) { | ||
| 416 | my( $q ) = @_; | ||
| 417 | |||
| 418 | my $oei = scalar $q->param('FINDX'); | ||
| 419 | my $ni=1; | ||
| 420 | for( my $oi = 1; $oi <= $oei; $oi++ ) { | ||
| 421 | my $op = 'F' . $oi . '_'; | ||
| 422 | unless( $q->param($op.'DELME') ) { | ||
| 423 | if( $oi != $ni ){ | ||
| 424 | my $np = 'F' . $ni . '_'; | ||
| 425 | $q->delete($np.'FUNCT'); # clear so NOT assumed a func | ||
| 426 | |||
| 427 | |||
| 428 | foreach my $par ( $q->param() ) { | ||
| 429 | if( $par =~ s/\A$op// ){ | ||
| 430 | $q->param( $np.$par, $q->param($op.$par) ); | ||
| 431 | } | ||
| 432 | } | ||
| 433 | } | ||
| 434 | $ni++; | ||
| 435 | } | ||
| 436 | } | ||
| 437 | $ni--; | ||
| 438 | $q->param('FINDX', $ni); | ||
| 439 | return $oei - $ni; | ||
| 440 | } | ||
| 441 | |||
| 442 | |||
| 443 | # ------------------------- typ4clm ------------------------- | ||
| 444 | sub typ4clm ($$) { | ||
| 445 | my( $o, $clm ) = @_; | ||
| 446 | $clm =~ s/\A\[//; | ||
| 447 | $clm =~ s/\]\Z//; | ||
| 448 | return $o->{oq}->get_col_types('filter')->{uc($clm)}; | ||
| 449 | } | ||
| 450 | |||
| 451 | # ------------------------- cmpopLOV ------------------------- | ||
| 452 | sub cmpopLOV { ['=','!=','<','<=','>','>=','like','not like','contains','not contains'] } | ||
| 453 | |||
| 454 | |||
| 455 | # ------------------------- html_parent_update ------------------------- | ||
| 456 | sub html_parent_update( $ ) { | ||
| 457 | |||
| 458 | my ($o) = @_; | ||
| 459 | |||
| 460 | my $q = $o->{q}; | ||
| 461 | |||
| 462 | my $filter = $q->param('filter'); | ||
| 463 | $filter =~ s/\n/\ /g; | ||
| 464 | |||
| 465 | my $js; | ||
| 466 | $js = " | ||
| 467 | if( window.opener | ||
| 468 | && !window.opener.closed | ||
| 469 | && window.opener.OQval ) { | ||
| 470 | var w = window.opener; | ||
| 471 | w.OQval('filter', '".$o->escape_js($filter)."'); | ||
| 472 | if (w.OQval('rows_page') == 'All') w.OQval('rows_page', 10); | ||
| 473 | w.OQrefresh(); | ||
| 474 | window.close(); | ||
| 475 | } | ||
| 476 | |||
| 477 | function show_defaultOQ() { | ||
| 478 | window.document.failedFilterForm.submit(); | ||
| 479 | return true; | ||
| 480 | } | ||
| 481 | "; | ||
| 482 | |||
| 483 | my $doc = $q->start_html( -title=>'OQFilter', -script=> $js ) | ||
| 484 | . '<H3>Unable to contact this filters parent.</H3>' | ||
| 485 | . $q->start_form( -name=>'failedFilterForm', -class=>'filterForm', -action => $$o{schema}{URI_standalone}); | ||
| 486 | |||
| 487 | |||
| 488 | if (ref($$o{schema}{state_params}) eq 'ARRAY') { | ||
| 489 | foreach my $p (@{ $$o{schema}{state_params} }) { | ||
| 490 | $doc .= "<input type='hidden' name='$p' value='".$o->escape_html($q->param($p))."'>"; | ||
| 491 | } | ||
| 492 | } | ||
| 493 | |||
| 494 | $doc .= $q->hidden( -name=>'filter', -value=>'') | ||
| 495 | . '<A HREF="" onclick="show_defaultOQ();return false;">' | ||
| 496 | . 'Click here for a default view</A> of the following RAW filter.' | ||
| 497 | . '<HR /><PRE>' | ||
| 498 | . $o->escape_html( $q->param('filter') ) | ||
| 499 | . '</PRE><HR />' | ||
| 500 | . $q->end_html() ; | ||
| 501 | |||
| 502 | return $doc; | ||
| 503 | } | ||
| 504 | |||
| 505 | # ------------------------- getFunctionNames ------------------------- | ||
| 506 | sub getFunctionNames( $ ) { | ||
| 507 | my( $o ) = @_; | ||
| 508 | my %functs = (); # ( t1=>'Test One', t2=>"Test Two" ); | ||
| 509 | foreach my $k ( keys %{$o->{schema}->{'named_filters'}} ) { | ||
| 510 | my $fref = $o->{schema}->{'named_filters'}{$k}; | ||
| 511 | if (ref $fref eq 'ARRAY') { $functs{"$k".'()'} = $fref->[2]; } | ||
| 512 | elsif (ref $fref eq 'HASH' && $fref->{'title'} ne '') { | ||
| 513 | $functs{"$k".'()'} = $fref->{'title'}; | ||
| 514 | } | ||
| 515 | } | ||
| 516 | return %functs; | ||
| 517 | } | ||
| 518 | |||
| 519 | # ------------------------- getColumnNames ------------------------- | ||
| 520 | sub getColumnNames( $ ) { | ||
| 521 | my( $o ) = @_; | ||
| 522 | my %cols = (); # ( t1=>'Test One', t2=>"Test Two" ); | ||
| 523 | foreach my $k ( keys %{$o->{schema}->{'select'}} ) { | ||
| 524 | next if $$o{schema}{select}{$k}[3]{is_hidden}; | ||
| 525 | my $cref = $o->{schema}->{'select'}{$k}; | ||
| 526 | $cols{"$k"} = | ||
| 527 | ( ref $cref eq 'ARRAY' ) ? $cref->[2] : 'bad:'.(ref $cref) ; | ||
| 528 | } | ||
| 529 | return %cols; | ||
| 530 | } | ||
| 531 | |||
| 532 | # ------------------------- html_filter_form ------------------------- | ||
| 533 | sub html_filter_form( $ ) { | ||
| 534 | my( $o ) = @_; | ||
| 535 | |||
| 536 | my %columnLBL = $o->getColumnNames(); | ||
| 537 | my @columnLOV = sort { $columnLBL{$a} cmp $columnLBL{$b} } keys %columnLBL; | ||
| 538 | # TODO: create named_functions from pre-exising filters and use them | ||
| 539 | # my @functionLOV = map {"$_".'()'} keys %{$o->{schema}->{'named_filters'}}; | ||
| 540 | # my @functionLOV = keys %{$o->{schema}->{'named_filters'}}; | ||
| 541 | my %functionLBL = $o->getFunctionNames(); | ||
| 542 | my @functionLOV = sort { $functionLBL{$a} cmp $functionLBL{$b} } keys %functionLBL; | ||
| 543 | # (t1=>'Test One', t2=>"Test Two"); | ||
| 544 | my @andorLOV = ('AND', 'OR'); | ||
| 545 | |||
| 546 | |||
| 547 | my $js=" | ||
| 548 | |||
| 549 | function toggle_value(basenam) { | ||
| 550 | var f = window.document.filterForm; | ||
| 551 | if( f.elements[basenam+'ISVAL'].value ) { | ||
| 552 | f.elements[basenam+'ISVAL'].value = ''; | ||
| 553 | f.elements[basenam+'BTN'].value = 'column:'; | ||
| 554 | f.elements[basenam+'VALUE'].className = 'hide'; | ||
| 555 | f.elements[basenam+'COLMN'].className = 'col'; | ||
| 556 | } else { | ||
| 557 | f.elements[basenam+'ISVAL'].value = 1; | ||
| 558 | f.elements[basenam+'BTN'].value = 'value:'; | ||
| 559 | f.elements[basenam+'VALUE'].className = 'val'; | ||
| 560 | f.elements[basenam+'COLMN'].className = 'hide'; | ||
| 561 | } | ||
| 562 | return true; | ||
| 563 | } | ||
| 564 | |||
| 565 | function update_paren_vis(basenam) { | ||
| 566 | var f = window.document.filterForm; | ||
| 567 | if( f.elements[basenam+'PAREN'].options[0].selected ) { | ||
| 568 | f.elements[basenam+'PARBTN'].className = 'noparen'; | ||
| 569 | f.elements[basenam+'PAREN'].className = 'hide'; | ||
| 570 | } else { | ||
| 571 | f.elements[basenam+'PARBTN'].className = 'hide'; | ||
| 572 | f.elements[basenam+'PAREN'].className = 'paren'; | ||
| 573 | } | ||
| 574 | window.check_paren(); | ||
| 575 | return true; | ||
| 576 | } | ||
| 577 | |||
| 578 | function toggle_paren(basenam) { | ||
| 579 | var f = window.document.filterForm; | ||
| 580 | if( f.elements[basenam+'PAREN'].options[0].selected ) { | ||
| 581 | f.elements[basenam+'PAREN'].options[1].selected = true; | ||
| 582 | } else { | ||
| 583 | f.elements[basenam+'PAREN'].options[0].selected = true; | ||
| 584 | } | ||
| 585 | window.update_paren_vis(basenam); | ||
| 586 | return true; | ||
| 587 | } | ||
| 588 | |||
| 589 | function show_submit_del() { | ||
| 590 | var f = window.document.filterForm; | ||
| 591 | var i = f.elements['FINDX'].value; | ||
| 592 | for(; i>0; i--){ | ||
| 593 | if( f.elements['F'+i+'_DELME'].checked ) { | ||
| 594 | f.elements['SUBMIT_DEL'].className = 'delbtn'; | ||
| 595 | window.document.getElementById('submit_text').className = 'submit_off'; | ||
| 596 | window.document.getElementById('submit_add').className = 'add_off'; | ||
| 597 | return true; | ||
| 598 | } | ||
| 599 | } | ||
| 600 | f.elements['SUBMIT_DEL'].className = 'hide'; | ||
| 601 | f.elements['CHECKALL'].checked = false; | ||
| 602 | window.document.getElementById('submit_add').className = 'add_ok'; | ||
| 603 | window.check_paren(); | ||
| 604 | return true; | ||
| 605 | } | ||
| 606 | |||
| 607 | function submit_act(actval) { | ||
| 608 | var f = window.document.filterForm; | ||
| 609 | f.elements.act.value = actval; | ||
| 610 | f.submit(); | ||
| 611 | return true; | ||
| 612 | } | ||
| 613 | |||
| 614 | function checkall_delme() { | ||
| 615 | var f = window.document.filterForm; | ||
| 616 | var newval = f.elements['CHECKALL'].checked; | ||
| 617 | var i = f.elements['FINDX'].value; | ||
| 618 | for(; i>0; i--){ | ||
| 619 | f.elements['F'+i+'_DELME'].checked = newval; | ||
| 620 | } | ||
| 621 | window.show_submit_del(); | ||
| 622 | return true; | ||
| 623 | } | ||
| 624 | |||
| 625 | function check_paren() { | ||
| 626 | var f = window.document.filterForm; | ||
| 627 | var i = f.elements['FINDX'].value; | ||
| 628 | var ocnt = 0; | ||
| 629 | for(; i>0; i--){ | ||
| 630 | ocnt += f.elements['F'+i+'_R_PAREN'].value - f.elements['F'+i+'_L_PAREN'].value; | ||
| 631 | if( ocnt < 0 ) { | ||
| 632 | i = -3; | ||
| 633 | } | ||
| 634 | } | ||
| 635 | if( ocnt == 0 ) { | ||
| 636 | window.document.getElementById('submit_text').className = 'submit_ok'; | ||
| 637 | window.document.getElementById('paren_warn').className = 'paren_match'; | ||
| 638 | } else { | ||
| 639 | window.document.getElementById('submit_text').className = 'submit_off'; | ||
| 640 | window.document.getElementById('paren_warn').className = 'paren_warn'; | ||
| 641 | } | ||
| 642 | return true; | ||
| 643 | } | ||
| 644 | |||
| 645 | "; | ||
| 646 | |||
| 647 | |||
| 648 | |||
| 649 | my $q = $o->{q}; | ||
| 650 | |||
| 651 | # pull out the fuctions arguments from the form | ||
| 652 | my %funct_args = (); | ||
| 653 | foreach my $fak ( $q->param() ){ | ||
| 654 | my @ary = split 'ARG_', $fak; | ||
| 655 | $funct_args{$ary[0]}{$ary[1]} = $q->param($fak) | ||
| 656 | if defined $ary[1]; | ||
| 657 | } | ||
| 658 | |||
| 659 | |||
| 660 | my $html = | ||
| 661 | $q->start_html ( -title=>"Interactive Filter - $$o{schema}{title}", | ||
| 662 | -script=> $js, | ||
| 663 | -head=> | ||
| 664 | $$o{schema}{options}{'CGI::OptimalQuery::InteractiveFilter'}{css} ). | ||
| 665 | "<center>". | ||
| 666 | (($$o{error}) ? "<strong>".$q->escapeHTML($$o{error})."</strong>" : ""). | ||
| 667 | $q->start_form ( -action=> $$o{schema}{URI_standalone}, -name=>'filterForm', | ||
| 668 | -class=>'filterForm'); | ||
| 669 | |||
| 670 | |||
| 671 | if (ref($$o{schema}{state_params}) eq 'ARRAY') { | ||
| 672 | foreach my $p (@{ $$o{schema}{state_params} }) { | ||
| 673 | $html .= "<input type='hidden' name='$p' value='".$o->escape_html($q->param($p))."'>"; | ||
| 674 | } | ||
| 675 | } | ||
| 676 | |||
| 677 | $html .= | ||
| 678 | $q->hidden ( -name=>'module', -value=>'InteractiveFilter', | ||
| 679 | -override=>1 ) | ||
| 680 | . $q->hidden ( -name=>'act', -value=>'submit_filter', | ||
| 681 | -override=>1 ) | ||
| 682 | . $q->hidden ( -name=>'hideParen', -value=>1 ) | ||
| 683 | . $q->hidden ( -name=>'FINDX', -value=>'0') ; | ||
| 684 | |||
| 685 | |||
| 686 | $html .= "<TABLE COLUMNS=6>\n"; | ||
| 687 | |||
| 688 | |||
| 689 | my $hideParen = $q->param('hideParen'); | ||
| 690 | my $pnp; # parameter name prefix | ||
| 691 | |||
| 692 | my $thing_to_focus_on; | ||
| 693 | |||
| 694 | for( my $findx = 1; $findx <= $q->param('FINDX'); $findx++ ) { | ||
| 695 | $pnp = 'F' . $findx . '_'; | ||
| 696 | $html .= '<TR><TD class="lp_col">' | ||
| 697 | . $q->button ( -name=>$pnp.'L_PARBTN', -label=>'(', | ||
| 698 | -onClick=>"toggle_paren('$pnp"."L_');", | ||
| 699 | -class=> $hideParen | ||
| 700 | ? 'hide' : ( $q->param($pnp.'L_PAREN') > 0 | ||
| 701 | ? 'hide' : 'noparen' ) ) | ||
| 702 | . $q->popup_menu | ||
| 703 | ( -name=>$pnp.'L_PAREN', -values=>[0 .. 3], -default=>'0', | ||
| 704 | -labels=>{'0'=>'','1'=>'(','2'=>'((','3'=>'((('}, | ||
| 705 | -onChange=>"update_paren_vis('$pnp"."L_');", | ||
| 706 | -class=>$q->param($pnp.'L_PAREN')<1 ?'hide':'paren' ) | ||
| 707 | . '</TD>'; | ||
| 708 | |||
| 709 | if( defined $q->param($pnp.'FUNCT') ) { | ||
| 710 | my $func_nam = $q->param($pnp.'FUNCT'); | ||
| 711 | $func_nam =~ s/\(\)//; | ||
| 712 | |||
| 713 | # if a predefined named filter | ||
| 714 | if (ref($o->{schema}->{'named_filters'}{$func_nam}) eq 'ARRAY') { | ||
| 715 | $html .= '<TD class="f_col" colspan=3>' | ||
| 716 | . $q->popup_menu( -name=>$pnp.'FUNCT', | ||
| 717 | -values=> \ @functionLOV, | ||
| 718 | -labels=> \ %functionLBL, | ||
| 719 | -default=> $q->param($pnp.'FUNCT'), | ||
| 720 | -onChange=>"submit_act('refresh');" ) ; | ||
| 721 | $html .= '</TD>'; | ||
| 722 | } | ||
| 723 | |||
| 724 | # if named filter has an html generator | ||
| 725 | elsif (exists $o->{schema}->{'named_filters'}{$func_nam}{html_generator}) { | ||
| 726 | $html .= '<TD class="f_col" colspan=3>' | ||
| 727 | . $q->popup_menu( -name=>$pnp.'FUNCT', | ||
| 728 | -values=> \ @functionLOV, | ||
| 729 | -labels=> \ %functionLBL, | ||
| 730 | -default=> $q->param($pnp.'FUNCT'), | ||
| 731 | -onChange=>"submit_act('refresh');" ) ; | ||
| 732 | $html .= | ||
| 733 | $o->{schema}->{'named_filters'}{$func_nam}{'html_generator'}->($q, $pnp.'ARG_'); | ||
| 734 | $html .= '</TD>'; | ||
| 735 | } | ||
| 736 | |||
| 737 | # else if named filter does not have a html_generator | ||
| 738 | else { | ||
| 739 | $html .= "<TD class='f_col' colspan=3><input type=hidden name='$pnp"."FUNCT' value='".$o->escape_html("$func_nam()")."' />"; | ||
| 740 | my %args; | ||
| 741 | my $arg_prefix = quotemeta($pnp.'ARG_'); | ||
| 742 | foreach my $param (grep { /^$arg_prefix/ } $q->param) { | ||
| 743 | my $k = $param; $k =~ s/$arg_prefix//; | ||
| 744 | my $v = $q->param($param); | ||
| 745 | $args{$k} = $v; | ||
| 746 | $html .= "<input type=hidden name='$param' value='".$o->escape_html($q->param($param))."' />"; | ||
| 747 | } | ||
| 748 | |||
| 749 | my $rv = $o->{schema}->{'named_filters'}{$func_nam}{'sql_generator'}->(%args); | ||
| 750 | $html .= "<div class=RO_NAMED_FILTER>".$o->escape_html($$rv[2])."</div></TD>"; | ||
| 751 | } | ||
| 752 | } | ||
| 753 | |||
| 754 | else { | ||
| 755 | $html .= '<TD class="l_col">' | ||
| 756 | . &cmp_val($q, $pnp.'L_', \ @columnLOV, \ %columnLBL) | ||
| 757 | . '</TD><TD class="c_col">' | ||
| 758 | . $q->popup_menu ( | ||
| 759 | -name=>$pnp.'CMPOP', -values=> cmpopLOV(), -class=>'cmpop') | ||
| 760 | . '</TD><TD class="r_col">' | ||
| 761 | . &cmp_val($q, $pnp.'R_', \ @columnLOV, \ %columnLBL ) | ||
| 762 | . '</TD>'; | ||
| 763 | } | ||
| 764 | |||
| 765 | $html .= '<TD class="rp_col">' | ||
| 766 | . $q->popup_menu ( -name=>$pnp.'R_PAREN', | ||
| 767 | -values=>[0 .. 3], -default=>'0', | ||
| 768 | -labels=> | ||
| 769 | {'0'=>'', '1'=>')', '2'=>'))', '3'=>')))'}, | ||
| 770 | -onChange=>"update_paren_vis('$pnp"."R_');", | ||
| 771 | -class=> ( $q->param($pnp.'R_PAREN')<1 | ||
| 772 | ? 'hide' : 'paren' ) ) | ||
| 773 | . $q->button ( -name=>$pnp.'R_PARBTN', -label=>')', | ||
| 774 | -onClick=>"toggle_paren('$pnp"."R_');", | ||
| 775 | -class=> $hideParen | ||
| 776 | ? 'hide' | ||
| 777 | : ( $q->param($pnp.'R_PAREN')>0 | ||
| 778 | ? 'hide' : 'noparen') ) | ||
| 779 | . '</TD><TD class="d_col">' | ||
| 780 | . $q->checkbox ( -name=>$pnp.'DELME', -label=>'remove', | ||
| 781 | -value=>'1', -checked=>0, -override=>1, | ||
| 782 | -onClick=>'show_submit_del();', | ||
| 783 | -class=>'delbox' ) | ||
| 784 | . "</TD></TR>\n<TR><TD colspan=5 align=center>" | ||
| 785 | . $q->popup_menu ( -name=>$pnp.'ANDOR', -values=> \ @andorLOV, | ||
| 786 | -class=>$findx == $q->param('FINDX')?'hide':'') | ||
| 787 | . "</TD></TR>\n" ; | ||
| 788 | |||
| 789 | } | ||
| 790 | |||
| 791 | |||
| 792 | $html .= '<TR><TD colspan=5><HR width="90%" /></TD><TD class="d_col">' | ||
| 793 | . $q->checkbox ( -name=>'CHECKALL', -label=>'ALL', -value=>'1', | ||
| 794 | -checked=>0, -override=>1, | ||
| 795 | -onClick=>'checkall_delme();', | ||
| 796 | -class=>'delbox' ) | ||
| 797 | . '</TD></TR> <TR class="footer"><TD colspan=5></TD><TD class="d_col">' | ||
| 798 | . $q->button ( -name=>'SUBMIT_DEL', -label=>'REMOVE', | ||
| 799 | -onClick=>"submit_act('submit_del');", | ||
| 800 | -class=> 'hide' ) | ||
| 801 | . '</TD></TR>' | ||
| 802 | if( $q->param('FINDX') > 0 ); # we printed something above here | ||
| 803 | |||
| 804 | my @sel_opts = ('-- add new filter element --', $q->optgroup ( -name=>'Column to compare:', | ||
| 805 | -values=> \ @columnLOV , | ||
| 806 | -labels=> \ %columnLBL ) ); | ||
| 807 | if (@functionLOV) { | ||
| 808 | push @sel_opts, $q->optgroup ( -name=>'Named Filters:', | ||
| 809 | -values=> \ @functionLOV , | ||
| 810 | -labels=> \ %functionLBL ); | ||
| 811 | } | ||
| 812 | |||
| 813 | $html .= "</TABLE>\n" | ||
| 814 | . '<DIV id="paren_warn" class="paren_match">( Parenthesis must be matching pairs )</DIV>' | ||
| 815 | . '<SPAN id="submit_add" class="submit_ok">' | ||
| 816 | . $q->popup_menu ( -name=>'NEXT_EXPR', | ||
| 817 | -default=>'--- Choose Next Filter ---', | ||
| 818 | -override=>1, | ||
| 819 | -values=>\@sel_opts, | ||
| 820 | -onChange=>"submit();" ) | ||
| 821 | . '</SPAN><SPAN id="submit_text" class="submit_ok"> or ' | ||
| 822 | . $q->submit( -name=>'SUBMIT', -class=>'submit_ok' ) | ||
| 823 | . "</SPAN> " | ||
| 824 | . $q->end_form() | ||
| 825 | . "\n</center> | ||
| 826 | <script type='text/javascript'> | ||
| 827 | check_paren(); | ||
| 828 | if (window.document.forms[0].elements['F".$q->param('FINDX')."_R_VALUE']) | ||
| 829 | window.document.forms[0].elements['F".$q->param('FINDX')."_R_VALUE'].focus(); | ||
| 830 | </script><noscript>Javascript is required when viewing this page.</noscript> | ||
| 831 | ".$q->end_html(); | ||
| 832 | |||
| 833 | return $html; | ||
| 834 | |||
| 835 | } | ||
| 836 | |||
| 837 | |||
| 838 | 1; | ||