| 1 | package CGI::OptimalQuery::InteractiveFilter2; | ||
| 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 | |||
| 8 | sub escapeHTML { CGI::OptimalQuery::Base::escapeHTML(@_) } | ||
| 9 | |||
| 10 | sub output { | ||
| 11 | my $o = shift; | ||
| 12 | my $buf = $$o{httpHeader}->('text/html')."<!DOCTYPE html>\n<html><body><div class=OQFilterPanel><h1>filter</h1><table>"; | ||
| 13 | my $types = $$o{oq}->get_col_types('filter'); | ||
| 14 | my $s = $$o{schema}{select}; | ||
| 15 | |||
| 16 | my $filter = $$o{q}->param('filter'); | ||
| 17 | |||
| 18 | # add new field to filter? | ||
| 19 | if ($$o{q}->param('field') ne '') { | ||
| 20 | my $field = $$o{q}->param('field'); | ||
| 21 | |||
| 22 | # if named filter exists for this field, use the named filter | ||
| 23 | if (exists $$o{schema}{named_filters}{$field}) { | ||
| 24 | $filter .= " AND " if $filter; | ||
| 25 | $filter .= $field.'()'; | ||
| 26 | } | ||
| 27 | elsif (! $$s{$field}[3]{disable_filter}) { | ||
| 28 | $filter .= " AND " if $filter; | ||
| 29 | if ($$types{$field} eq 'char' || $$types{$field} eq 'clob') { | ||
| 30 | $filter .= "$field contains ''"; | ||
| 31 | } else { | ||
| 32 | $filter .= "$field=''"; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | my @cols = grep { | ||
| 39 | $$s{$_}[2] ne '' && ! $$s{$_}[3]{disable_filter} && ! $$s{$_}[3]{is_hidden} | ||
| 40 | } sort { $$s{$a}[2] cmp $$s{$b}[2] } keys %$s; | ||
| 41 | my @op = (qw( = != < <= > >= like ), 'not like', 'contains', 'not contains'); | ||
| 42 | |||
| 43 | my $seq = 0; | ||
| 44 | |||
| 45 | my $parsedFilter = $$o{oq}->parseFilter($filter); | ||
| 46 | foreach my $f (@$parsedFilter) { | ||
| 47 | $buf .= "<tr class=filterexp>"; | ||
| 48 | |||
| 49 | my $typenum; | ||
| 50 | $typenum = $$f[0] if ref($f) eq 'ARRAY'; | ||
| 51 | |||
| 52 | if (! $typenum) { | ||
| 53 | $buf .= "<td colspan=6><select class=logicop><option>AND<option"; | ||
| 54 | $buf .= " selected" if $f eq 'OR'; | ||
| 55 | $buf .= ">OR</select></td>"; | ||
| 56 | } | ||
| 57 | |||
| 58 | # else if (selectalias operator literal) | ||
| 59 | elsif ($typenum == 1 || $typenum == 3) { | ||
| 60 | $buf .= "<td>"; | ||
| 61 | my ($type,$numLeftParen,$leftExp,$operator,$rightExp,$numRightParen) = @$f; | ||
| 62 | |||
| 63 | if ($numLeftParen == 0) { | ||
| 64 | $buf .= "<button type=button class=lp>(</button>"; | ||
| 65 | } else { | ||
| 66 | $buf .= "<select class=lp><option value=''> </option><option"; | ||
| 67 | $buf .= " selected" if $numLeftParen==1; | ||
| 68 | $buf .= ">(<option"; | ||
| 69 | $buf .= " selected" if $numLeftParen==2; | ||
| 70 | $buf .= ">((<option"; | ||
| 71 | $buf .= " selected" if $numLeftParen==3; | ||
| 72 | $buf .= ">(((</select>"; | ||
| 73 | } | ||
| 74 | $buf .= "</td>"; | ||
| 75 | |||
| 76 | my $colOpts = $$o{schema}{select}{$leftExp}[3]; | ||
| 77 | |||
| 78 | # if lexp is not something that user is allowed to filter or is hidden | ||
| 79 | if ($$colOpts{disable_filter} || $$colOpts{is_hidden}) { | ||
| 80 | my $exp = $leftExp; | ||
| 81 | my $label = $o->get_nice_name($leftExp); | ||
| 82 | if ($operator =~ /\w/) { | ||
| 83 | $exp .= ' '.$operator.' '; | ||
| 84 | $label .= ' '.$operator.' '; | ||
| 85 | } else { | ||
| 86 | $exp .= $operator; | ||
| 87 | $label .= $operator; | ||
| 88 | } | ||
| 89 | # if rightExp is a literal | ||
| 90 | if ($typenum == 1) { | ||
| 91 | if ($rightExp =~ /[\ \'\"]/) { | ||
| 92 | if ($rightExp !~ /\'/) { | ||
| 93 | $exp .= "'".$rightExp."'"; | ||
| 94 | $label .= "'".$rightExp."'"; | ||
| 95 | } else { | ||
| 96 | my $x = $rightExp; $x =~ s/\"//g; | ||
| 97 | $exp .= '"'.$x.'"'; | ||
| 98 | $label .= '"'.$x.'"'; | ||
| 99 | } | ||
| 100 | } else { | ||
| 101 | $exp .= $rightExp; | ||
| 102 | $label .= $rightExp; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | # else lexp is col | ||
| 106 | else { | ||
| 107 | $exp .= $rightExp; | ||
| 108 | $label .= $o->get_nice_name($rightExp); | ||
| 109 | } | ||
| 110 | $buf .= "<td colspan=3><input type=hidden value='".escapeHTML($exp)."'><span>".escapeHTML($label)."</span></td>"; | ||
| 111 | } | ||
| 112 | |||
| 113 | # else present a widget to modify expression | ||
| 114 | else { | ||
| 115 | $buf .= "<td><select class=lexp>"; | ||
| 116 | foreach my $c (@cols) { | ||
| 117 | $buf .= "<option value='".escapeHTML($c)."'"; | ||
| 118 | $buf .= " data-type=".$$types{$c} if $$types{$c} ne 'char' && $$types{$c} ne 'clob'; | ||
| 119 | $buf .= " selected" if $c eq $leftExp; | ||
| 120 | $buf .= ">".escapeHTML($o->get_nice_name($c)); | ||
| 121 | } | ||
| 122 | $buf .= "</select></td><td><select class=op>"; | ||
| 123 | foreach my $op (@op) { | ||
| 124 | $buf .= "<option"; | ||
| 125 | $buf .= " selected" if $op eq $operator; | ||
| 126 | $buf .= ">$op"; | ||
| 127 | } | ||
| 128 | $buf .= "</select></td><td><div class=rexptypesel><select class=rexp><optgroup label='Either: '><option value=''> type in a value </optgroup><optgroup label='OR select another field'>"; | ||
| 129 | my $rightSelectedField; | ||
| 130 | $rightSelectedField = $rightExp if $type == 3; | ||
| 131 | foreach my $c (@cols) { | ||
| 132 | $buf .= "<option value='".escapeHTML($c)."'"; | ||
| 133 | $buf .= " data-type=".$$types{$c} if $$types{$c} ne 'char'; | ||
| 134 | $buf .= " selected" if $c eq $rightSelectedField; | ||
| 135 | $buf .= ">".escapeHTML($o->get_nice_name($c)); | ||
| 136 | } | ||
| 137 | $buf .= "</optgroup></select><input type=text class=rexp"; | ||
| 138 | if ($rightSelectedField) { | ||
| 139 | $buf .= " style='display: none;'"; | ||
| 140 | } else { | ||
| 141 | $buf .= " value='".escapeHTML($rightExp)."'"; | ||
| 142 | } | ||
| 143 | $buf .= "></div></td>"; | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | $buf .= "<td>"; | ||
| 148 | if ($numRightParen == 0) { | ||
| 149 | $buf .= "<button type=button class=rp>)</button>"; | ||
| 150 | } else { | ||
| 151 | $buf .= "<select class=rp><option value=''> </option><option"; | ||
| 152 | $buf .= " selected" if $numRightParen==1; | ||
| 153 | $buf .= ">)<option"; | ||
| 154 | $buf .= " selected" if $numRightParen==2; | ||
| 155 | $buf .= ">))<option"; | ||
| 156 | $buf .= " selected" if $numRightParen==3; | ||
| 157 | $buf .= ">)))</select>"; | ||
| 158 | } | ||
| 159 | $buf .= "</td><td><button type=button class=DeleteFilterElemBut>x</button></td>"; | ||
| 160 | } | ||
| 161 | |||
| 162 | # else if (namedfilter, arguments) | ||
| 163 | elsif ($typenum == 2) { | ||
| 164 | $buf .= "<td>"; | ||
| 165 | my ($type,$numLeftParen,$namedFilter,$argArray,$numRightParen) = @$f; | ||
| 166 | if ($numLeftParen == 0) { | ||
| 167 | $buf .= "<button type=button class=lp>(</button>"; | ||
| 168 | } else { | ||
| 169 | $buf .= "<select class=lp><option value=''> </option><option"; | ||
| 170 | $buf .= " selected" if $numLeftParen==1; | ||
| 171 | $buf .= ">(<option"; | ||
| 172 | $buf .= " selected" if $numLeftParen==2; | ||
| 173 | $buf .= ">((<option"; | ||
| 174 | $buf .= " selected" if $numLeftParen==3; | ||
| 175 | $buf .= ">(((</select>"; | ||
| 176 | } | ||
| 177 | $buf .= "</td><td colspan=3>"; | ||
| 178 | my $nf = $$o{schema}{named_filters}{$namedFilter}; | ||
| 179 | if (ref($nf) eq 'ARRAY') { | ||
| 180 | my $title = $$nf[2] || $namedFilter; | ||
| 181 | $buf .= '<span>'.escapeHTML($title).'</span>' | ||
| 182 | .'<input type=hidden value="' | ||
| 183 | .escapeHTML("$namedFilter(" | ||
| 184 | .join(',', map { '"'.$_.'"' } @$argArray).")").'">'; | ||
| 185 | } | ||
| 186 | elsif (ref($nf) eq 'HASH') { | ||
| 187 | if (ref($$nf{html_generator}) eq 'CODE') { | ||
| 188 | #before we call the html_generator, set the params up | ||
| 189 | my %args; | ||
| 190 | for (my $i=0; $i <= $#$argArray; $i+=2) { | ||
| 191 | my $name = $$argArray[$i]; | ||
| 192 | my $val = $$argArray[$i + 1]; | ||
| 193 | $args{$name}||=[]; | ||
| 194 | push @{$args{$name}}, $val; | ||
| 195 | } | ||
| 196 | my $prefix = '_nfarg'.++$seq; | ||
| 197 | |||
| 198 | while (my ($name,$vals) = each %args) { | ||
| 199 | $$o{q}->param($prefix.$name, @$vals); | ||
| 200 | } | ||
| 201 | $buf .= | ||
| 202 | '<input type=hidden value="'.escapeHTML("$namedFilter(").'">' | ||
| 203 | .$$nf{html_generator}->($$o{q}, $prefix) | ||
| 204 | .'<input type=hidden value="'.escapeHTML(")").'">'; | ||
| 205 | } else { | ||
| 206 | my $title; | ||
| 207 | if (ref($$nf{sql_generator}) eq 'CODE') { | ||
| 208 | my $ar = $$nf{sql_generator}->(@$argArray); | ||
| 209 | $title = $$ar[2]; | ||
| 210 | } | ||
| 211 | $title ||= $$nf{title} || $namedFilter; | ||
| 212 | $buf .= '<span>'.escapeHTML($title).'</span>' | ||
| 213 | .'<input type=hidden value="' | ||
| 214 | .escapeHTML("$namedFilter(" | ||
| 215 | .join(',', map { '"'.$_.'"' } @$argArray).")").'">'; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | $buf .= "</td><td>"; | ||
| 219 | if ($numRightParen == 0) { | ||
| 220 | $buf .= "<button type=button class=rp>)</button>"; | ||
| 221 | } else { | ||
| 222 | $buf .= "<select class=rp><option value=''> </option><option"; | ||
| 223 | $buf .= " selected" if $numRightParen==1; | ||
| 224 | $buf .= ">)<option"; | ||
| 225 | $buf .= " selected" if $numRightParen==2; | ||
| 226 | $buf .= ">))<option"; | ||
| 227 | $buf .= " selected" if $numRightParen==3; | ||
| 228 | $buf .= ">)))</select>"; | ||
| 229 | } | ||
| 230 | $buf .= "</td><td><button type=button class=DeleteFilterElemBut>x</button></td>"; | ||
| 231 | } | ||
| 232 | |||
| 233 | else { | ||
| 234 | die "invalid typenum: $typenum; this should never happen"; | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | $buf .= "</tr>"; | ||
| 239 | } | ||
| 240 | $buf .= "</table><br>"; | ||
| 241 | |||
| 242 | $buf .= "<select class=newfilter><option value=''>-- add new filter element</option><optgroup label='Column to compare:'>"; | ||
| 243 | |||
| 244 | foreach my $c (@cols) { | ||
| 245 | # if there is a named filter for this field, skip it and make user use named filter instead | ||
| 246 | next if exists $$o{schema}{named_filters}{$c}; | ||
| 247 | |||
| 248 | $buf .= "<option value='".escapeHTML("$c")."'"; | ||
| 249 | $buf .= " data-type=".$$types{$c} if $$types{$c} ne 'char'; | ||
| 250 | $buf .= ">".escapeHTML($$o{schema}{select}{$c}[2]); | ||
| 251 | } | ||
| 252 | $buf .= "</optgroup>"; | ||
| 253 | my $f = $$o{schema}{named_filters}; | ||
| 254 | my @k = sort { | ||
| 255 | ((ref($$f{$a}) eq 'ARRAY') ? $$f{$a}[2] : $$f{$a}{title}) cmp | ||
| 256 | ((ref($$f{$b}) eq 'ARRAY') ? $$f{$b}[2] : $$f{$b}{title}) } keys %$f; | ||
| 257 | if ($#k > -1) { | ||
| 258 | $buf .= "<optgroup label='Named Filters:'>"; | ||
| 259 | foreach my $alias (@k) { | ||
| 260 | my $label; | ||
| 261 | if (ref($$f{$alias}) eq 'ARRAY') { | ||
| 262 | $label = $$f{$alias}[2]; | ||
| 263 | } else { | ||
| 264 | $label = $$f{$alias}{title}; | ||
| 265 | } | ||
| 266 | next unless $label; | ||
| 267 | $buf .= "<option value='".escapeHTML($alias)."()'>".escapeHTML($label); | ||
| 268 | } | ||
| 269 | $buf .= "</optgroup>"; | ||
| 270 | } | ||
| 271 | $buf .= "</select><br><button type=button class=CancelFilterBut>cancel</button><button type=button class=OKFilterBut>ok</button></div></body></html>"; | ||
| 272 | |||
| 273 | $$o{output_handler}->($buf); | ||
| 274 | return undef; | ||
| 275 | } | ||
| 276 | |||
| 277 | 1; | ||