| 1 | package CGI::OptimalQuery::InteractiveFilter; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | no warnings qw( uninitialized ); | ||
| 6 | use base 'CGI::OptimalQuery::Base'; | ||
| 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 = @{ $_[4] } if ref($_[4]) eq 'ARRAY'; | ||
| 207 | foreach my $key (keys %args) { $$rv{'ARG_'.$key} = $args{$key}; } | ||
| 208 | return $rv; | ||
| 209 | }, | ||
| 210 | |||
| 211 | # just return the first token for all other rules not specified | ||
| 212 | '*default*' => sub { $_[2] } | ||
| 213 | ); | ||
| 214 | |||
| 215 | |||
| 216 | |||
| 217 | # ------------------------- process actions ------------------------- | ||
| 218 | sub process_actions { | ||
| 219 | my $o = shift; | ||
| 220 | my $q = $$o{q}; | ||
| 221 | $$o{view} = 'html_filter_form'; | ||
| 222 | |||
| 223 | # should we load a fresh filter into the appropriate params | ||
| 224 | # representing the filter? | ||
| 225 | if ($q->param('filter') ne '') { | ||
| 226 | my $expression_array = $$o{oq}->parse($DBIx::OptimalQuery::filterGrammar, | ||
| 227 | $q->param('filter'), \%translations); | ||
| 228 | die "bad filter!\nfilter= ".$q->param('filter'). | ||
| 229 | "\nexp=".Dumper( $expression_array )."\n" unless ref($expression_array) eq 'ARRAY'; | ||
| 230 | |||
| 231 | # fill in the params representing the filter state | ||
| 232 | my $i = 0; | ||
| 233 | foreach my $exp (@$expression_array) { | ||
| 234 | $i++; | ||
| 235 | while (my ($k,$v) = each %$exp) { $q->param('F'.$i.'_'.$k,$v); } | ||
| 236 | } | ||
| 237 | $q->param('FINDX', $i); | ||
| 238 | $q->param('hideParen', ($i < 3)); | ||
| 239 | $$o{view} = 'html_filter_form'; | ||
| 240 | } | ||
| 241 | |||
| 242 | |||
| 243 | # did the user request a new expression? | ||
| 244 | if ( defined $q->param('NEXT_EXPR') | ||
| 245 | && scalar $q->param('NEXT_EXPR') ne '-- add new filter element --') { | ||
| 246 | my $val = scalar $q->param('NEXT_EXPR'); | ||
| 247 | my $findx = $q->param('FINDX'); | ||
| 248 | $findx = 0 unless $findx > 0; | ||
| 249 | $findx++; | ||
| 250 | my $pn = 'F' . $findx . '_'; | ||
| 251 | if( $val =~ /\(\)\Z/ ) { # ends with a () | ||
| 252 | $q->param($pn.'FUNCT', $val); | ||
| 253 | } else { | ||
| 254 | $q->param($pn.'L_COLMN', $val); | ||
| 255 | $q->param($pn.'L_VALUE', ''); | ||
| 256 | if ($o->typ4clm($val) eq 'char' || | ||
| 257 | $o->typ4clm($val) eq 'clob') { | ||
| 258 | $q->param($pn.'CMPOP', 'contains'); | ||
| 259 | } else { | ||
| 260 | $q->param($pn.'CMPOP', '='); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | $q->param('FINDX', $findx); | ||
| 264 | $q->param('hideParen', ( $findx < 3 ) ); | ||
| 265 | $q->param('NEXT_EXPR', '--- Choose Next Filter ---'); | ||
| 266 | $$o{view} = 'html_filter_form'; | ||
| 267 | |||
| 268 | } | ||
| 269 | |||
| 270 | # did user submit the filter? | ||
| 271 | elsif ($q->param('act') eq 'submit_filter') { | ||
| 272 | my $ftxt = $o->recreateFilterString(); | ||
| 273 | $q->param('filter', $ftxt); | ||
| 274 | $$o{view} = 'html_parent_update'; | ||
| 275 | if ($$o{error}) { | ||
| 276 | $$o{view} = 'html_filter_form'; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | # did user request to delete filter | ||
| 281 | elsif ($q->param('act') eq 'submit_del') { | ||
| 282 | delselForm( $q ); | ||
| 283 | $$o{view} = 'html_filter_form'; | ||
| 284 | } | ||
| 285 | |||
| 286 | return undef; | ||
| 287 | } | ||
| 288 | |||
| 289 | |||
| 290 | # ------------------------- cmp_val ------------------------- | ||
| 291 | sub cmp_val ( $$$$ ) { | ||
| 292 | my( $q, $pnam, $vals, $lbls ) = @_; | ||
| 293 | |||
| 294 | |||
| 295 | my $isUserVal = ( $q->param($pnam.'COLMN') eq '' | ||
| 296 | || $q->param($pnam.'ISVAL') ); | ||
| 297 | |||
| 298 | return | ||
| 299 | $q->button( -name=>$pnam.'BTN', | ||
| 300 | -label=>$isUserVal?'value:':'column:', | ||
| 301 | -onClick=>"toggle_value('$pnam');", | ||
| 302 | -class=>'colvalbtn') | ||
| 303 | . $q->hidden( -name=>$pnam.'ISVAL', -default=>$isUserVal ) | ||
| 304 | . $q->textfield( -name=>$pnam.'VALUE', | ||
| 305 | -class=> ( $isUserVal ? 'val' : 'hide' ) ) | ||
| 306 | . $q->popup_menu( -name=>$pnam.'COLMN', | ||
| 307 | -values=> $vals, -labels=> $lbls, | ||
| 308 | -onChange=>"submit_act('refresh');", | ||
| 309 | -class=> $isUserVal ? 'hide' : 'col'); | ||
| 310 | |||
| 311 | } | ||
| 312 | |||
| 313 | # ------------------------- recreateFilterString ------------------------- | ||
| 314 | sub recreateFilterString { | ||
| 315 | my $o = shift; | ||
| 316 | my $q = $$o{q}; | ||
| 317 | |||
| 318 | # pull out the fuctions arguments from the form | ||
| 319 | my %funct_args = (); | ||
| 320 | foreach my $fak ( $q->param() ){ | ||
| 321 | my @ary = split 'ARG_', $fak; | ||
| 322 | $funct_args{$ary[0]}{$ary[1]} = $q->param($fak) | ||
| 323 | if defined $ary[1]; | ||
| 324 | } | ||
| 325 | |||
| 326 | my $ftext = ''; | ||
| 327 | my $ei = scalar $q->param('FINDX'); | ||
| 328 | for( my $i = 1; $i <= $ei; $i++ ) { | ||
| 329 | my $p = 'F' . $i . '_'; | ||
| 330 | |||
| 331 | my $parcnt = $q->param($p.'L_PAREN'); | ||
| 332 | $ftext .= ($parcnt < 1 ? '' : '('x$parcnt . ' ' ); | ||
| 333 | |||
| 334 | if( defined $q->param($p.'FUNCT') | ||
| 335 | && $q->param($p.'FUNCT') ne '' ) { | ||
| 336 | |||
| 337 | # TODO: Grab the $p.'ARG_' and Dump it. | ||
| 338 | my $f = $q->param($p.'FUNCT'); | ||
| 339 | $f =~ s/\(\)\Z//; | ||
| 340 | my $args = ''; | ||
| 341 | while (my ($k,$v) = each %{ $funct_args{$p} }) { | ||
| 342 | $args .= ',' if $args; | ||
| 343 | $v = "'".$v."'" if $v =~ /\W/; | ||
| 344 | $args .= "$k,$v"; | ||
| 345 | } | ||
| 346 | $ftext .= " $f($args) "; | ||
| 347 | } else { | ||
| 348 | if( $q->param($p.'L_ISVAL') ) { | ||
| 349 | $ftext .= '\'' . $q->param($p.'L_VALUE') . '\''; | ||
| 350 | } else { | ||
| 351 | $ftext .= '[' . $q->param($p.'L_COLMN') . ']'; | ||
| 352 | |||
| 353 | # force operator to be "like/not like" if a numeric operator | ||
| 354 | if ($o->typ4clm(uc($q->param($p.'L_COLMN'))) eq 'clob' && | ||
| 355 | $q->param($p.'CMPOP') !~ /\w/) { | ||
| 356 | if ($q->param($p.'CMPOP') =~ /\!/) { | ||
| 357 | $q->param($p.'CMPOP', "not like"); | ||
| 358 | } else { | ||
| 359 | $q->param($p.'CMPOP', "like"); | ||
| 360 | } | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | $ftext .= ' ' . $q->param($p.'CMPOP') . ' '; | ||
| 365 | |||
| 366 | if( $q->param($p.'R_ISVAL') ) { | ||
| 367 | my $val = $q->param($p.'R_VALUE'); | ||
| 368 | if ($val =~ /\'/ || $val =~ /\"/) { | ||
| 369 | if ($val !~ /\"/) { $val = '"'.$val.'"'; } | ||
| 370 | elsif ($val !~ /\'/) { $val = "'".$val."'"; } | ||
| 371 | else { $val =~ s/\'|\"//g; } | ||
| 372 | } else { | ||
| 373 | $val = "'$val'"; | ||
| 374 | } | ||
| 375 | $ftext .= $val; | ||
| 376 | |||
| 377 | # if date comparison and right side is value and numeric operator | ||
| 378 | # ensure the right side valud fits date_format string | ||
| 379 | if ($q->param($p.'L_COLMN') && | ||
| 380 | $q->param($p.'CMPOP') !~ /\w/ && | ||
| 381 | exists $$o{schema}{select}{$q->param($p.'L_COLMN')} && | ||
| 382 | exists $$o{schema}{select}{$q->param($p.'L_COLMN')}[3]{date_format}) { | ||
| 383 | my $date_format = $$o{schema}{select}{$q->param($p.'L_COLMN')}[3]{date_format}; | ||
| 384 | local $$o{dbh}->{RaiseError} = 0; | ||
| 385 | local $$o{dbh}->{PrintError} = 0; | ||
| 386 | if ($$o{dbh}{Driver}{Name} eq 'Oracle') { | ||
| 387 | my $dt = $q->param($p.'R_VALUE'); | ||
| 388 | if ($dt ne '') { | ||
| 389 | my ($rv) = $$o{dbh}->selectrow_array("SELECT 1 FROM dual WHERE to_date(?,'$date_format') IS NOT NULL", undef, $dt); | ||
| 390 | if (! $rv) { | ||
| 391 | $$o{error} = "invalid date: \"$dt\", must be in format: \"$date_format\""; | ||
| 392 | return undef; | ||
| 393 | } | ||
| 394 | } | ||
| 395 | } | ||
| 396 | } | ||
| 397 | } else { | ||
| 398 | $ftext .= '[' . $q->param($p.'R_COLMN') . ']'; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | $parcnt = $q->param($p.'R_PAREN'); | ||
| 403 | $ftext .= ( $parcnt<1 ? '' : ')'x$parcnt .' ' ) . "\n"; | ||
| 404 | |||
| 405 | $ftext .= $q->param($p.'ANDOR') . "\n" unless( $i == $ei ); | ||
| 406 | |||
| 407 | } | ||
| 408 | |||
| 409 | $ftext =~ s/\n//g; | ||
| 410 | return $ftext; | ||
| 411 | } | ||
| 412 | |||
| 413 | # ------------------------- delselForm ------------------------- | ||
| 414 | sub delselForm( $ ) { | ||
| 415 | my( $q ) = @_; | ||
| 416 | |||
| 417 | my $oei = scalar $q->param('FINDX'); | ||
| 418 | my $ni=1; | ||
| 419 | for( my $oi = 1; $oi <= $oei; $oi++ ) { | ||
| 420 | my $op = 'F' . $oi . '_'; | ||
| 421 | unless( $q->param($op.'DELME') ) { | ||
| 422 | if( $oi != $ni ){ | ||
| 423 | my $np = 'F' . $ni . '_'; | ||
| 424 | $q->delete($np.'FUNCT'); # clear so NOT assumed a func | ||
| 425 | |||
| 426 | |||
| 427 | foreach my $par ( $q->param() ) { | ||
| 428 | if( $par =~ s/\A$op// ){ | ||
| 429 | $q->param( $np.$par, $q->param($op.$par) ); | ||
| 430 | } | ||
| 431 | } | ||
| 432 | } | ||
| 433 | $ni++; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | $ni--; | ||
| 437 | $q->param('FINDX', $ni); | ||
| 438 | return $oei - $ni; | ||
| 439 | } | ||
| 440 | |||
| 441 | |||
| 442 | # ------------------------- typ4clm ------------------------- | ||
| 443 | sub typ4clm ($$) { | ||
| 444 | my( $o, $clm ) = @_; | ||
| 445 | $clm =~ s/\A\[//; | ||
| 446 | $clm =~ s/\]\Z//; | ||
| 447 | return $o->{oq}->get_col_types('filter')->{uc($clm)}; | ||
| 448 | } | ||
| 449 | |||
| 450 | # ------------------------- cmpopLOV ------------------------- | ||
| 451 | sub cmpopLOV { ['=','!=','<','<=','>','>=','like','not like','contains','not contains'] } | ||
| 452 | |||
| 453 | |||
| 454 | # ------------------------- html_parent_update ------------------------- | ||
| 455 | sub html_parent_update( $ ) { | ||
| 456 | |||
| 457 | my ($o) = @_; | ||
| 458 | |||
| 459 | my $q = $o->{q}; | ||
| 460 | |||
| 461 | my $filter = $q->param('filter'); | ||
| 462 | $filter =~ s/\n/\ /g; | ||
| 463 | |||
| 464 | my $js = " | ||
| 465 | if( window.opener | ||
| 466 | && !window.opener.closed | ||
| 467 | && window.opener.OQval ) { | ||
| 468 | var w = window.opener; | ||
| 469 | w.OQval('filter', '".$o->escape_js($filter)."'); | ||
| 470 | if (w.OQval('rows_page') == 'All') w.OQval('rows_page', 10); | ||
| 471 | w.OQrefresh(); | ||
| 472 | window.close(); | ||
| 473 | } | ||
| 474 | |||
| 475 | function show_defaultOQ() { | ||
| 476 | window.document.failedFilterForm.submit(); | ||
| 477 | return true; | ||
| 478 | } | ||
| 479 | "; | ||
| 480 | |||
| 481 | my $doc = $q->start_html( -title=>'OQFilter', -script=> $js ) | ||
| 482 | . '<H3>Unable to contact this filters parent.</H3>' | ||
| 483 | . $q->start_form( -name=>'failedFilterForm', -class=>'filterForm', -action => $$o{schema}{URI_standalone}); | ||
| 484 | |||
| 485 | |||
| 486 | if (ref($$o{schema}{state_params}) eq 'ARRAY') { | ||
| 487 | foreach my $p (@{ $$o{schema}{state_params} }) { | ||
| 488 | $doc .= "<input type='hidden' name='$p' value='".$o->escape_html($q->param($p))."'>"; | ||
| 489 | } | ||
| 490 | } | ||
| 491 | |||
| 492 | $doc .= $q->hidden( -name=>'filter', -value=>'') | ||
| 493 | . '<A HREF="" onclick="show_defaultOQ();return false;">' | ||
| 494 | . 'Click here for a default view</A> of the following RAW filter.' | ||
| 495 | . '<HR /><PRE>' | ||
| 496 | . $o->escape_html( $q->param('filter') ) | ||
| 497 | . '</PRE><HR />' | ||
| 498 | . $q->end_html() ; | ||
| 499 | |||
| 500 | return $doc; | ||
| 501 | } | ||
| 502 | |||
| 503 | # ------------------------- getFunctionNames ------------------------- | ||
| 504 | sub getFunctionNames( $ ) { | ||
| 505 | my( $o ) = @_; | ||
| 506 | my %functs = (); # ( t1=>'Test One', t2=>"Test Two" ); | ||
| 507 | foreach my $k ( keys %{$o->{schema}->{'named_filters'}} ) { | ||
| 508 | my $fref = $o->{schema}->{'named_filters'}{$k}; | ||
| 509 | if (ref $fref eq 'ARRAY') { $functs{"$k".'()'} = $fref->[2]; } | ||
| 510 | elsif (ref $fref eq 'HASH' && $fref->{'title'} ne '') { | ||
| 511 | $functs{"$k".'()'} = $fref->{'title'}; | ||
| 512 | } | ||
| 513 | } | ||
| 514 | return %functs; | ||
| 515 | } | ||
| 516 | |||
| 517 | # ------------------------- getColumnNames ------------------------- | ||
| 518 | sub getColumnNames( $ ) { | ||
| 519 | my( $o ) = @_; | ||
| 520 | my %cols = (); # ( t1=>'Test One', t2=>"Test Two" ); | ||
| 521 | foreach my $k ( keys %{$o->{schema}->{'select'}} ) { | ||
| 522 | next if $$o{schema}{select}{$k}[3]{is_hidden}; | ||
| 523 | my $cref = $o->{schema}->{'select'}{$k}; | ||
| 524 | $cols{"$k"} = | ||
| 525 | ( ref $cref eq 'ARRAY' ) ? $cref->[2] : 'bad:'.(ref $cref) ; | ||
| 526 | } | ||
| 527 | return %cols; | ||
| 528 | } | ||
| 529 | |||
| 530 | # ------------------------- html_filter_form ------------------------- | ||
| 531 | sub html_filter_form( $ ) { | ||
| 532 | my( $o ) = @_; | ||
| 533 | |||
| 534 | my %columnLBL = $o->getColumnNames(); | ||
| 535 | my @columnLOV = sort { $columnLBL{$a} cmp $columnLBL{$b} } keys %columnLBL; | ||
| 536 | # TODO: create named_functions from pre-exising filters and use them | ||
| 537 | # my @functionLOV = map {"$_".'()'} keys %{$o->{schema}->{'named_filters'}}; | ||
| 538 | # my @functionLOV = keys %{$o->{schema}->{'named_filters'}}; | ||
| 539 | my %functionLBL = $o->getFunctionNames(); | ||
| 540 | my @functionLOV = sort { $functionLBL{$a} cmp $functionLBL{$b} } keys %functionLBL; | ||
| 541 | # (t1=>'Test One', t2=>"Test Two"); | ||
| 542 | my @andorLOV = ('AND', 'OR'); | ||
| 543 | |||
| 544 | |||
| 545 | my $js=" | ||
| 546 | |||
| 547 | function toggle_value(basenam) { | ||
| 548 | var f = window.document.filterForm; | ||
| 549 | if( f.elements[basenam+'ISVAL'].value ) { | ||
| 550 | f.elements[basenam+'ISVAL'].value = ''; | ||
| 551 | f.elements[basenam+'BTN'].value = 'column:'; | ||
| 552 | f.elements[basenam+'VALUE'].className = 'hide'; | ||
| 553 | f.elements[basenam+'COLMN'].className = 'col'; | ||
| 554 | } else { | ||
| 555 | f.elements[basenam+'ISVAL'].value = 1; | ||
| 556 | f.elements[basenam+'BTN'].value = 'value:'; | ||
| 557 | f.elements[basenam+'VALUE'].className = 'val'; | ||
| 558 | f.elements[basenam+'COLMN'].className = 'hide'; | ||
| 559 | } | ||
| 560 | return true; | ||
| 561 | } | ||
| 562 | |||
| 563 | function update_paren_vis(basenam) { | ||
| 564 | var f = window.document.filterForm; | ||
| 565 | if( f.elements[basenam+'PAREN'].options[0].selected ) { | ||
| 566 | f.elements[basenam+'PARBTN'].className = 'noparen'; | ||
| 567 | f.elements[basenam+'PAREN'].className = 'hide'; | ||
| 568 | } else { | ||
| 569 | f.elements[basenam+'PARBTN'].className = 'hide'; | ||
| 570 | f.elements[basenam+'PAREN'].className = 'paren'; | ||
| 571 | } | ||
| 572 | window.check_paren(); | ||
| 573 | return true; | ||
| 574 | } | ||
| 575 | |||
| 576 | function toggle_paren(basenam) { | ||
| 577 | var f = window.document.filterForm; | ||
| 578 | if( f.elements[basenam+'PAREN'].options[0].selected ) { | ||
| 579 | f.elements[basenam+'PAREN'].options[1].selected = true; | ||
| 580 | } else { | ||
| 581 | f.elements[basenam+'PAREN'].options[0].selected = true; | ||
| 582 | } | ||
| 583 | window.update_paren_vis(basenam); | ||
| 584 | return true; | ||
| 585 | } | ||
| 586 | |||
| 587 | function show_submit_del() { | ||
| 588 | var f = window.document.filterForm; | ||
| 589 | var i = f.elements['FINDX'].value; | ||
| 590 | for(; i>0; i--){ | ||
| 591 | if( f.elements['F'+i+'_DELME'].checked ) { | ||
| 592 | f.elements['SUBMIT_DEL'].className = 'delbtn'; | ||
| 593 | window.document.getElementById('submit_text').className = 'submit_off'; | ||
| 594 | window.document.getElementById('submit_add').className = 'add_off'; | ||
| 595 | return true; | ||
| 596 | } | ||
| 597 | } | ||
| 598 | f.elements['SUBMIT_DEL'].className = 'hide'; | ||
| 599 | f.elements['CHECKALL'].checked = false; | ||
| 600 | window.document.getElementById('submit_add').className = 'add_ok'; | ||
| 601 | window.check_paren(); | ||
| 602 | return true; | ||
| 603 | } | ||
| 604 | |||
| 605 | function submit_act(actval) { | ||
| 606 | var f = window.document.filterForm; | ||
| 607 | f.elements.act.value = actval; | ||
| 608 | f.submit(); | ||
| 609 | return true; | ||
| 610 | } | ||
| 611 | |||
| 612 | function checkall_delme() { | ||
| 613 | var f = window.document.filterForm; | ||
| 614 | var newval = f.elements['CHECKALL'].checked; | ||
| 615 | var i = f.elements['FINDX'].value; | ||
| 616 | for(; i>0; i--){ | ||
| 617 | f.elements['F'+i+'_DELME'].checked = newval; | ||
| 618 | } | ||
| 619 | window.show_submit_del(); | ||
| 620 | return true; | ||
| 621 | } | ||
| 622 | |||
| 623 | function check_paren() { | ||
| 624 | var f = window.document.filterForm; | ||
| 625 | var i = f.elements['FINDX'].value; | ||
| 626 | var ocnt = 0; | ||
| 627 | for(; i>0; i--){ | ||
| 628 | ocnt += f.elements['F'+i+'_R_PAREN'].value - f.elements['F'+i+'_L_PAREN'].value; | ||
| 629 | if( ocnt < 0 ) { | ||
| 630 | i = -3; | ||
| 631 | } | ||
| 632 | } | ||
| 633 | if( ocnt == 0 ) { | ||
| 634 | window.document.getElementById('submit_text').className = 'submit_ok'; | ||
| 635 | window.document.getElementById('paren_warn').className = 'paren_match'; | ||
| 636 | } else { | ||
| 637 | window.document.getElementById('submit_text').className = 'submit_off'; | ||
| 638 | window.document.getElementById('paren_warn').className = 'paren_warn'; | ||
| 639 | } | ||
| 640 | return true; | ||
| 641 | } | ||
| 642 | |||
| 643 | "; | ||
| 644 | |||
| 645 | |||
| 646 | |||
| 647 | my $q = $o->{q}; | ||
| 648 | |||
| 649 | # pull out the fuctions arguments from the form | ||
| 650 | my %funct_args = (); | ||
| 651 | foreach my $fak ( $q->param() ){ | ||
| 652 | my @ary = split 'ARG_', $fak; | ||
| 653 | $funct_args{$ary[0]}{$ary[1]} = $q->param($fak) | ||
| 654 | if defined $ary[1]; | ||
| 655 | } | ||
| 656 | |||
| 657 | |||
| 658 | my $html = | ||
| 659 | $q->start_html ( -title=>"Interactive Filter - $$o{schema}{title}", | ||
| 660 | -script=> $js, | ||
| 661 | -head=> | ||
| 662 | $$o{schema}{options}{'CGI::OptimalQuery::InteractiveFilter'}{css} ). | ||
| 663 | "<center>". | ||
| 664 | (($$o{error}) ? "<strong>".$q->escapeHTML($$o{error})."</strong>" : ""). | ||
| 665 | $q->start_form ( -action=> $$o{schema}{URI_standalone}, -name=>'filterForm', | ||
| 666 | -class=>'filterForm'); | ||
| 667 | |||
| 668 | |||
| 669 | if (ref($$o{schema}{state_params}) eq 'ARRAY') { | ||
| 670 | foreach my $p (@{ $$o{schema}{state_params} }) { | ||
| 671 | $html .= "<input type='hidden' name='$p' value='".$o->escape_html($q->param($p))."'>"; | ||
| 672 | } | ||
| 673 | } | ||
| 674 | |||
| 675 | $html .= | ||
| 676 | $q->hidden ( -name=>'module', -value=>'InteractiveFilter', | ||
| 677 | -override=>1 ) | ||
| 678 | . $q->hidden ( -name=>'act', -value=>'submit_filter', | ||
| 679 | -override=>1 ) | ||
| 680 | . $q->hidden ( -name=>'hideParen', -value=>1 ) | ||
| 681 | . $q->hidden ( -name=>'FINDX', -value=>'0') ; | ||
| 682 | |||
| 683 | |||
| 684 | $html .= "<TABLE COLUMNS=6>\n"; | ||
| 685 | |||
| 686 | |||
| 687 | my $hideParen = $q->param('hideParen'); | ||
| 688 | my $pnp; # parameter name prefix | ||
| 689 | |||
| 690 | my $thing_to_focus_on; | ||
| 691 | |||
| 692 | for( my $findx = 1; $findx <= $q->param('FINDX'); $findx++ ) { | ||
| 693 | $pnp = 'F' . $findx . '_'; | ||
| 694 | $html .= '<TR><TD class="lp_col">' | ||
| 695 | . $q->button ( -name=>$pnp.'L_PARBTN', -label=>'(', | ||
| 696 | -onClick=>"toggle_paren('$pnp"."L_');", | ||
| 697 | -class=> $hideParen | ||
| 698 | ? 'hide' : ( $q->param($pnp.'L_PAREN') > 0 | ||
| 699 | ? 'hide' : 'noparen' ) ) | ||
| 700 | . $q->popup_menu | ||
| 701 | ( -name=>$pnp.'L_PAREN', -values=>[0 .. 3], -default=>'0', | ||
| 702 | -labels=>{'0'=>'','1'=>'(','2'=>'((','3'=>'((('}, | ||
| 703 | -onChange=>"update_paren_vis('$pnp"."L_');", | ||
| 704 | -class=>$q->param($pnp.'L_PAREN')<1 ?'hide':'paren' ) | ||
| 705 | . '</TD>'; | ||
| 706 | |||
| 707 | if( defined $q->param($pnp.'FUNCT') ) { | ||
| 708 | my $func_nam = $q->param($pnp.'FUNCT'); | ||
| 709 | $func_nam =~ s/\(\)//; | ||
| 710 | |||
| 711 | # if a predefined named filter | ||
| 712 | if (ref($o->{schema}->{'named_filters'}{$func_nam}) eq 'ARRAY') { | ||
| 713 | $html .= '<TD class="f_col" colspan=3>' | ||
| 714 | . $q->popup_menu( -name=>$pnp.'FUNCT', | ||
| 715 | -values=> \ @functionLOV, | ||
| 716 | -labels=> \ %functionLBL, | ||
| 717 | -default=> $q->param($pnp.'FUNCT'), | ||
| 718 | -onChange=>"submit_act('refresh');" ) ; | ||
| 719 | $html .= '</TD>'; | ||
| 720 | } | ||
| 721 | |||
| 722 | # if named filter has an html generator | ||
| 723 | elsif (exists $o->{schema}->{'named_filters'}{$func_nam}{html_generator}) { | ||
| 724 | $html .= '<TD class="f_col" colspan=3>' | ||
| 725 | . $q->popup_menu( -name=>$pnp.'FUNCT', | ||
| 726 | -values=> \ @functionLOV, | ||
| 727 | -labels=> \ %functionLBL, | ||
| 728 | -default=> $q->param($pnp.'FUNCT'), | ||
| 729 | -onChange=>"submit_act('refresh');" ) ; | ||
| 730 | $html .= | ||
| 731 | $o->{schema}->{'named_filters'}{$func_nam}{'html_generator'}->($q, $pnp.'ARG_'); | ||
| 732 | $html .= '</TD>'; | ||
| 733 | } | ||
| 734 | |||
| 735 | # else if named filter does not have a html_generator | ||
| 736 | else { | ||
| 737 | $html .= "<TD class='f_col' colspan=3><input type=hidden name='$pnp"."FUNCT' value='".$o->escape_html("$func_nam()")."' />"; | ||
| 738 | my %args; | ||
| 739 | my $arg_prefix = quotemeta($pnp.'ARG_'); | ||
| 740 | foreach my $param (grep { /^$arg_prefix/ } $q->param) { | ||
| 741 | my $k = $param; $k =~ s/$arg_prefix//; | ||
| 742 | my $v = $q->param($param); | ||
| 743 | $args{$k} = $v; | ||
| 744 | $html .= "<input type=hidden name='$param' value='".$o->escape_html($q->param($param))."' />"; | ||
| 745 | } | ||
| 746 | |||
| 747 | my $rv = $o->{schema}->{'named_filters'}{$func_nam}{'sql_generator'}->(%args); | ||
| 748 | $html .= "<div class=RO_NAMED_FILTER>".$o->escape_html($$rv[2])."</div></TD>"; | ||
| 749 | } | ||
| 750 | } | ||
| 751 | |||
| 752 | else { | ||
| 753 | $html .= '<TD class="l_col">' | ||
| 754 | . &cmp_val($q, $pnp.'L_', \ @columnLOV, \ %columnLBL) | ||
| 755 | . '</TD><TD class="c_col">' | ||
| 756 | . $q->popup_menu ( | ||
| 757 | -name=>$pnp.'CMPOP', -values=> cmpopLOV(), -class=>'cmpop') | ||
| 758 | . '</TD><TD class="r_col">' | ||
| 759 | . &cmp_val($q, $pnp.'R_', \ @columnLOV, \ %columnLBL ) | ||
| 760 | . '</TD>'; | ||
| 761 | } | ||
| 762 | |||
| 763 | $html .= '<TD class="rp_col">' | ||
| 764 | . $q->popup_menu ( -name=>$pnp.'R_PAREN', | ||
| 765 | -values=>[0 .. 3], -default=>'0', | ||
| 766 | -labels=> | ||
| 767 | {'0'=>'', '1'=>')', '2'=>'))', '3'=>')))'}, | ||
| 768 | -onChange=>"update_paren_vis('$pnp"."R_');", | ||
| 769 | -class=> ( $q->param($pnp.'R_PAREN')<1 | ||
| 770 | ? 'hide' : 'paren' ) ) | ||
| 771 | . $q->button ( -name=>$pnp.'R_PARBTN', -label=>')', | ||
| 772 | -onClick=>"toggle_paren('$pnp"."R_');", | ||
| 773 | -class=> $hideParen | ||
| 774 | ? 'hide' | ||
| 775 | : ( $q->param($pnp.'R_PAREN')>0 | ||
| 776 | ? 'hide' : 'noparen') ) | ||
| 777 | . '</TD><TD class="d_col">' | ||
| 778 | . $q->checkbox ( -name=>$pnp.'DELME', -label=>'remove', | ||
| 779 | -value=>'1', -checked=>0, -override=>1, | ||
| 780 | -onClick=>'show_submit_del();', | ||
| 781 | -class=>'delbox' ) | ||
| 782 | . "</TD></TR>\n<TR><TD colspan=5 align=center>" | ||
| 783 | . $q->popup_menu ( -name=>$pnp.'ANDOR', -values=> \ @andorLOV, | ||
| 784 | -class=>$findx == $q->param('FINDX')?'hide':'') | ||
| 785 | . "</TD></TR>\n" ; | ||
| 786 | |||
| 787 | } | ||
| 788 | |||
| 789 | |||
| 790 | $html .= '<TR><TD colspan=5><HR width="90%" /></TD><TD class="d_col">' | ||
| 791 | . $q->checkbox ( -name=>'CHECKALL', -label=>'ALL', -value=>'1', | ||
| 792 | -checked=>0, -override=>1, | ||
| 793 | -onClick=>'checkall_delme();', | ||
| 794 | -class=>'delbox' ) | ||
| 795 | . '</TD></TR> <TR class="footer"><TD colspan=5></TD><TD class="d_col">' | ||
| 796 | . $q->button ( -name=>'SUBMIT_DEL', -label=>'REMOVE', | ||
| 797 | -onClick=>"submit_act('submit_del');", | ||
| 798 | -class=> 'hide' ) | ||
| 799 | . '</TD></TR>' | ||
| 800 | if( $q->param('FINDX') > 0 ); # we printed something above here | ||
| 801 | |||
| 802 | my @sel_opts = ('-- add new filter element --', $q->optgroup ( -name=>'Column to compare:', | ||
| 803 | -values=> \ @columnLOV , | ||
| 804 | -labels=> \ %columnLBL ) ); | ||
| 805 | if (@functionLOV) { | ||
| 806 | push @sel_opts, $q->optgroup ( -name=>'Named Filters:', | ||
| 807 | -values=> \ @functionLOV , | ||
| 808 | -labels=> \ %functionLBL ); | ||
| 809 | } | ||
| 810 | |||
| 811 | $html .= "</TABLE>\n" | ||
| 812 | . '<DIV id="paren_warn" class="paren_match">( Parenthesis must be matching pairs )</DIV>' | ||
| 813 | . '<SPAN id="submit_add" class="submit_ok">' | ||
| 814 | . $q->popup_menu ( -name=>'NEXT_EXPR', | ||
| 815 | -default=>'--- Choose Next Filter ---', | ||
| 816 | -override=>1, | ||
| 817 | -values=>\@sel_opts, | ||
| 818 | -onChange=>"submit();" ) | ||
| 819 | . '</SPAN><SPAN id="submit_text" class="submit_ok"> or ' | ||
| 820 | . $q->submit( -name=>'SUBMIT', -class=>'submit_ok' ) | ||
| 821 | . "</SPAN> " | ||
| 822 | . $q->end_form() | ||
| 823 | . "\n</center> | ||
| 824 | <script type='text/javascript'> | ||
| 825 | check_paren(); | ||
| 826 | if (window.document.forms[0].elements['F".$q->param('FINDX')."_R_VALUE']) | ||
| 827 | window.document.forms[0].elements['F".$q->param('FINDX')."_R_VALUE'].focus(); | ||
| 828 | </script><noscript>Javascript is required when viewing this page.</noscript> | ||
| 829 | ".$q->end_html(); | ||
| 830 | |||
| 831 | return $html; | ||
| 832 | |||
| 833 | } | ||
| 834 | |||
| 835 | |||
| 836 | 1; | ||
| 837 | |||
| 838 |