« lib/CGI/OptimalQuery/InteractiveQuery.pm
↑ Summary
lib/CGI/OptimalQuery/InteractiveQuery2Tools.pm »
| 1 | package CGI::OptimalQuery::InteractiveQuery2; | ||
| 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 CGI(); | ||
| 8 | |||
| 9 | sub escapeHTML { | ||
| 10 | return defined $_[0] ? CGI::escapeHTML($_[0]) : ''; | ||
| 11 | } | ||
| 12 | |||
| 13 | sub output { | ||
| 14 | my $o = shift; | ||
| 15 | |||
| 16 | my %opts = %{ $o->get_opts() }; | ||
| 17 | |||
| 18 | # evalulate code refs | ||
| 19 | for (qw(httpHeader htmlFooter htmlHeader OQdocTop | ||
| 20 | OQdocBottom OQformTop OQformBottom )) { | ||
| 21 | $opts{$_} = $opts{$_}->($o) if ref($opts{$_}) eq 'CODE'; | ||
| 22 | } | ||
| 23 | |||
| 24 | # define defaults | ||
| 25 | $opts{OQdocTop} ||= ''; | ||
| 26 | $opts{OQdocBottom} ||= ''; | ||
| 27 | $opts{OQformTop} ||= ''; | ||
| 28 | $opts{OQformBottom} ||= ''; | ||
| 29 | $opts{editButtonLabel}||= 'edit'; | ||
| 30 | $opts{disable_sort} ||= 0; | ||
| 31 | $opts{disable_filter} ||= 0; | ||
| 32 | $opts{disable_select} ||= 0; | ||
| 33 | $opts{mutateRecord} ||= undef; | ||
| 34 | $opts{editLink} ||= undef; | ||
| 35 | $opts{htmlExtraHead} ||= ""; | ||
| 36 | if (! exists $opts{usePopups}) { | ||
| 37 | $opts{usePopups}=1; | ||
| 38 | } else { | ||
| 39 | $opts{usePopups}=($opts{usePopups}) ? 1 : 0; | ||
| 40 | } | ||
| 41 | if (! exists $opts{useAjax}) { | ||
| 42 | $opts{useAjax} = $opts{usePopups}; | ||
| 43 | } else { | ||
| 44 | $opts{useAjax}=($opts{useAjax}) ? 1 : 0; | ||
| 45 | } | ||
| 46 | |||
| 47 | $opts{httpHeader} = $$o{q}->header(-type=>'text/html',-expires=>'now') | ||
| 48 | unless exists $opts{httpHeader}; | ||
| 49 | $opts{htmlFooter} = "</body>\n</html>\n" | ||
| 50 | unless exists $opts{htmlFooter}; | ||
| 51 | |||
| 52 | my $newBut; | ||
| 53 | if ($opts{NewButton}) { | ||
| 54 | $newBut = (ref($opts{NewButton}) eq 'CODE') ? $opts{NewButton}->($o, \%opts) : $opts{NewButton}; | ||
| 55 | } | ||
| 56 | elsif (ref($opts{buildNewLink}) eq 'CODE') { | ||
| 57 | my $link = $opts{buildNewLink}->($o, \%opts); | ||
| 58 | if ($link ne '') { | ||
| 59 | $newBut = "<button type=button title=new class=OQnewBut"; | ||
| 60 | if ($opts{usePopups}) { | ||
| 61 | my $target = uc($link); $target =~ s/\W//g; | ||
| 62 | $newBut .= " data-target='$target'"; | ||
| 63 | } | ||
| 64 | $newBut .= " data-href='".escapeHTML($link)."'>new</button>"; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | elsif (exists $opts{buildNewLink} && $opts{buildNewLink} eq '') {} | ||
| 68 | elsif ($opts{editLink} ne '') { | ||
| 69 | my $link = $opts{editLink}.(($opts{editLink} =~ /\?/)?'&':'?')."on_update=OQrefresh&act=new"; | ||
| 70 | if ($link ne '') { | ||
| 71 | $newBut = "<button type=button title=new class=OQnewBut"; | ||
| 72 | if ($opts{usePopups}) { | ||
| 73 | my $target = uc($opts{editLink}); $target =~ s/\W//g; | ||
| 74 | $newBut .= " data-target='$target'"; | ||
| 75 | } | ||
| 76 | $newBut .= " data-href='".escapeHTML($link)."'>new</button>"; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | my $ver = "ver=$CGI::OptimalQuery::VERSION"; | ||
| 81 | my $buf; | ||
| 82 | my $dataonly = $$o{q}->param('dataonly'); | ||
| 83 | if ($dataonly) { | ||
| 84 | $buf = $opts{httpHeader}."<!DOCTYPE html>\n<html><body>"; | ||
| 85 | } else { | ||
| 86 | my $script; | ||
| 87 | $script .= "window.OQWindowHeight=$opts{WindowHeight};\n" if $opts{WindowHeight}; | ||
| 88 | $script .= "window.OQWindowWidth=$opts{WindowWidth};\n" if $opts{WindowWidth}; | ||
| 89 | $script .= "window.OQuseAjax=$opts{useAjax};\n"; | ||
| 90 | $script .= "window.OQusePopups=$opts{usePopups};\n"; | ||
| 91 | |||
| 92 | if (! exists $opts{htmlHeader}) { | ||
| 93 | $opts{htmlHeader} = | ||
| 94 | "<!DOCTYPE html> | ||
| 95 | <html> | ||
| 96 | <head> | ||
| 97 | <title>".escapeHTML($o->get_title)."</title> | ||
| 98 | <link id=OQIQ2CSS rel=stylesheet type=text/css href='$$o{schema}{resourceURI}/InteractiveQuery2.css?$ver'> | ||
| 99 | <meta name=viewport content='width=device-width, initial-scale=1.0, user-scalable=no'> | ||
| 100 | ".$opts{htmlExtraHead}."</head> | ||
| 101 | <body id=OQbody>"; | ||
| 102 | } else { | ||
| 103 | $script .= " | ||
| 104 | if (! document.getElementById('OQIQ2CSS')) { | ||
| 105 | var a = document.createElement('link'); | ||
| 106 | a.setAttribute('rel','stylesheet'); | ||
| 107 | a.setAttribute('type','text/css'); | ||
| 108 | a.setAttribute('id','OQIQ2CSS'); | ||
| 109 | a.setAttribute('href','$$o{schema}{resourceURI}/InteractiveQuery2.css?1'); | ||
| 110 | document.getElementsByTagName('head')[0].appendChild(a); | ||
| 111 | }\n"; | ||
| 112 | } | ||
| 113 | |||
| 114 | if ($opts{color}) { | ||
| 115 | $script .= " | ||
| 116 | var d = document.createElement('style'); | ||
| 117 | var r = document.createTextNode('.OQhead { background-color: $opts{color}; }'); | ||
| 118 | d.type = 'text/css'; | ||
| 119 | if (d.styleSheet) | ||
| 120 | d.styleSheet.cssText = r.nodeValue; | ||
| 121 | else d.appendChild(r); | ||
| 122 | document.getElementsByTagName('head')[0].appendChild(d);\n"; | ||
| 123 | } | ||
| 124 | |||
| 125 | $buf = $opts{httpHeader}.$opts{htmlHeader}; | ||
| 126 | $buf .= "<script src=$$o{schema}{resourceURI}/jquery.js?$ver></script><noscript>Javascript is required when viewing this page.</noscript>" unless $opts{jquery_already_sent}; | ||
| 127 | $buf .= " | ||
| 128 | <script src=$$o{schema}{resourceURI}/InteractiveQuery2.js?$ver></script><noscript>Javascript is required when viewing this page.</noscript> | ||
| 129 | <script> | ||
| 130 | (function(){ | ||
| 131 | $script | ||
| 132 | })(); | ||
| 133 | </script><noscript>Javascript is required when viewing this page.</noscript>"; | ||
| 134 | $buf .= " | ||
| 135 | <div class=OQdocTop>$opts{OQdocTop}</div>"; | ||
| 136 | |||
| 137 | # ouput tools panel | ||
| 138 | my @tools = sort keys %{$$o{schema}{tools}}; | ||
| 139 | if ($#tools > -1) { | ||
| 140 | $buf .= "<div class=OQToolsPanel-pos-div><div class=OQToolsPanel-align-div><div class=OQToolsPanel><ul>"; | ||
| 141 | my $opened_tool_key = $$o{q}->param('tool'); | ||
| 142 | foreach my $key (sort keys %{$$o{schema}{tools}}) { | ||
| 143 | my $tool = $$o{schema}{tools}{$key}; | ||
| 144 | |||
| 145 | my $openedClass = ''; | ||
| 146 | my $toolContent = ''; | ||
| 147 | if ($opened_tool_key eq $key) { | ||
| 148 | $openedClass = ' opened'; | ||
| 149 | $toolContent = "<div class=OQToolContent>".$$tool{handler}->($o)."</div>"; | ||
| 150 | } | ||
| 151 | $buf .= "<li data-toolkey='$key' class='OQToolExpander $openedClass'><h3>".escapeHTML($$tool{title})."</h3>$toolContent</li>"; | ||
| 152 | } | ||
| 153 | $buf .= "</ul><button class=OQToolsCancelBut type=button>✕</button></div></div></div>"; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | $buf .= " | ||
| 158 | <form class='OQform mode-$$o{mode}' name=OQform action='".escapeHTML($$o{schema}{URI_standalone}||$$o{schema}{URI})."' method=get> | ||
| 159 | <input type=hidden name=show value='".escapeHTML(join(',',@{$$o{show}}))."'> | ||
| 160 | <input type=hidden name=filter value='".escapeHTML($$o{filter})."'> | ||
| 161 | <input type=hidden name=mode value='".escapeHTML($$o{mode})."'> | ||
| 162 | <input type=hidden name=hiddenFilter value='".escapeHTML($$o{hiddenFilter})."'> | ||
| 163 | <input type=hidden name=queryDescr value='".escapeHTML($$o{queryDescr})."'> | ||
| 164 | <input type=hidden name=sort value='".escapeHTML($$o{'sort'})."'> | ||
| 165 | <input type=hidden name=module value='".escapeHTML($$o{module})."'> | ||
| 166 | <input type=hidden name=OQss value='".escapeHTML($$o{q}->param('OQss'))."'> | ||
| 167 | <input type=hidden name=on_select value='".escapeHTML($$o{q}->param('on_select'))."'>\n"; | ||
| 168 | if (ref($$o{schema}{state_params}) eq 'ARRAY') { | ||
| 169 | $buf .= "<input type=hidden name='".escapeHTML($_)."' value='" | ||
| 170 | .escapeHTML($$o{q}->param($_))."'>\n" for @{$$o{schema}{state_params}}; | ||
| 171 | } | ||
| 172 | $buf .= | ||
| 173 | "<a name=OQtop></a> | ||
| 174 | <div class=OQformTop>$opts{OQformTop}</div> | ||
| 175 | |||
| 176 | <div class=OQhead> | ||
| 177 | <div class=OQtitle>".escapeHTML($o->get_title)."</div> | ||
| 178 | <div class=OQsummary>Result(s) (".$o->commify($o->get_lo_rec)." - " | ||
| 179 | .$o->commify($o->get_hi_rec).") of ".$o->commify($o->get_count)."</div> | ||
| 180 | <div class=OQcmds> | ||
| 181 | $newBut | ||
| 182 | |||
| 183 | <button type=button title='refresh data' class=OQrefreshBut>refresh</button> | ||
| 184 | <button type=button title='tools' class=OQToolsBut>tools</button> | ||
| 185 | <button type=button title='help' class=OQhelpBut>help</button> | ||
| 186 | </div> | ||
| 187 | </div> | ||
| 188 | |||
| 189 | <table class=OQinfo>"; | ||
| 190 | $buf .= "<tr class=OQQueryDescr><td class=OQlabel>Query:</td><td>".escapeHTML($$o{queryDescr})."</td></tr>" if $$o{queryDescr}; | ||
| 191 | |||
| 192 | my $filter = $o->get_filter(); | ||
| 193 | if ($filter) { | ||
| 194 | $buf .= "<tr class=OQFilterDescr title='click to edit filter'"; | ||
| 195 | $buf .= " data-nofilter" if $opts{disable_filter}; | ||
| 196 | $buf .= "><td class=OQlabel>Filter:</td><td>".escapeHTML($filter)."</td></tr>"; | ||
| 197 | } | ||
| 198 | |||
| 199 | my @sort = $o->sth->sort_descr; | ||
| 200 | if ($#sort > -1) { | ||
| 201 | $buf .= "<tr class=OQSortDescr><td class=OQlabel>Sort:</td><td>"; | ||
| 202 | my $comma = ''; | ||
| 203 | foreach my $c (@sort) { | ||
| 204 | $buf .= $comma; | ||
| 205 | $comma = ', '; | ||
| 206 | $buf .= "<a title=remove class=OQRemoveSortBut>" unless $opts{disable_sort}; | ||
| 207 | $buf .= escapeHTML($c); | ||
| 208 | $buf .= "</a>" unless $opts{disable_sort}; | ||
| 209 | } | ||
| 210 | $buf .= "</tr>"; | ||
| 211 | } | ||
| 212 | |||
| 213 | $buf .= "</table>"; | ||
| 214 | |||
| 215 | |||
| 216 | if ($$o{mode} eq 'recview') { | ||
| 217 | $buf .= "<div class=OQRecViewCmds><button type=button class=OQAddColumnsBut>columns</button></div>"; | ||
| 218 | } | ||
| 219 | |||
| 220 | $buf .= "<table class=OQdata>"; | ||
| 221 | |||
| 222 | if ($$o{mode} eq 'recview') { | ||
| 223 | } else { | ||
| 224 | $buf .= " | ||
| 225 | <thead title='click to hide, sort, filter, or add columns'> | ||
| 226 | <tr> | ||
| 227 | <td class=OQdataLHead></td>"; | ||
| 228 | foreach my $colAlias (@{ $o->get_usersel_cols }) { | ||
| 229 | my $colOpts = $$o{schema}{select}{$colAlias}[3]; | ||
| 230 | $buf .= "<td data-col='".escapeHTML($colAlias)."'"; | ||
| 231 | $buf .= " data-noselect" if $$colOpts{disable_select} || $opts{disable_select}; | ||
| 232 | $buf .= " data-nosort" if $$colOpts{disable_sort} || $opts{disable_sort}; | ||
| 233 | $buf .= " data-nofilter" if $$colOpts{disable_filter} || $opts{disable_filter}; | ||
| 234 | $buf .= ">".escapeHTML($o->get_nice_name($colAlias))."</td>"; | ||
| 235 | } | ||
| 236 | $buf .= " | ||
| 237 | <td class=OQdataRHead></td> | ||
| 238 | </tr> | ||
| 239 | </thead>"; | ||
| 240 | } | ||
| 241 | |||
| 242 | $buf .= " | ||
| 243 | <tbody>\n"; | ||
| 244 | |||
| 245 | my $recs_in_buffer = 0; | ||
| 246 | my $typeMap = $o->{oq}->get_col_types('select'); | ||
| 247 | while (my $r = $o->fetch()) { | ||
| 248 | $buf .= "<tr data-uid='".escapeHTML($$r{U_ID})."'><td class=OQdataLCol>"; | ||
| 249 | if (ref($opts{OQdataLCol}) eq 'CODE') { | ||
| 250 | $buf .= $opts{OQdataLCol}->($r); | ||
| 251 | } elsif (ref($opts{buildEditLink}) eq 'CODE') { | ||
| 252 | my $link = $opts{buildEditLink}->($o, $r, \%opts); | ||
| 253 | if ($link ne '') { | ||
| 254 | $buf .= "<a href='".escapeHTML($link)."' title='open record' class=OQeditBut>".$opts{editButtonLabel}."</a>"; | ||
| 255 | } | ||
| 256 | } elsif ($opts{editLink} ne '' && $$r{U_ID} ne '') { | ||
| 257 | my $link = $opts{editLink}.(($opts{editLink} =~ /\?/)?'&':'?')."on_update=OQrefresh&act=load&id=$$r{U_ID}"; | ||
| 258 | $buf .= "<a href='".escapeHTML($link)."' title='open record' class=OQeditBut>".$opts{editButtonLabel}."</a>"; | ||
| 259 | } | ||
| 260 | $buf .= "</td>"; | ||
| 261 | |||
| 262 | if ($$o{mode} eq 'recview') { | ||
| 263 | $buf .= "<td>"; | ||
| 264 | foreach my $col (@{ $o->get_usersel_cols }) { | ||
| 265 | my $val = $o->get_html_val($col); | ||
| 266 | if ($val ne '') { | ||
| 267 | my $label = $o->get_nice_name($col); | ||
| 268 | $buf .= "<div class=OQrecviewLabel>".escapeHTML($label).":</div><div class=OQrecviewVal>$val</div>"; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | $buf .= "</td>"; | ||
| 272 | } | ||
| 273 | |||
| 274 | else { | ||
| 275 | foreach my $col (@{ $o->get_usersel_cols }) { | ||
| 276 | my $val = $o->get_html_val($col); | ||
| 277 | my $type = $$typeMap{$col} || 'char'; | ||
| 278 | $buf .= "<td".(($type ne 'char')?" class=$type":"").">$val</td>"; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | $buf .= "<td class=OQdataRCol>"; | ||
| 282 | if (ref($opts{OQdataRCol}) eq 'CODE') { | ||
| 283 | $buf .= $opts{OQdataRCol}->($r); | ||
| 284 | } elsif ($o->{q}->param('on_select') ne '') { | ||
| 285 | my $on_select = $o->{q}->param('on_select'); | ||
| 286 | $on_select =~ s/\~.*//; | ||
| 287 | my ($func,@argfields) = split /\,/, $on_select; | ||
| 288 | $argfields[0] = 'U_ID' if $#argfields==-1; | ||
| 289 | my @argvals = map { | ||
| 290 | my $v=$$r{$_}; | ||
| 291 | $v = join(', ', @$v) if ref($v) eq 'ARRAY'; | ||
| 292 | $v =~ s/\~\~\~//g; | ||
| 293 | $v; | ||
| 294 | } @argfields; | ||
| 295 | $buf .= "<button type=button title='select record' class=OQselectBut data-rv='" | ||
| 296 | .escapeHTML(join('~~~',@argvals))."'>select</button>"; | ||
| 297 | } | ||
| 298 | $buf .= "</td></tr>\n"; | ||
| 299 | if (++$recs_in_buffer == 10000) { | ||
| 300 | $$o{output_handler}->($buf); | ||
| 301 | $buf = ''; | ||
| 302 | $recs_in_buffer = 0; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | $o->finish(); | ||
| 306 | |||
| 307 | $buf .= "</tbody></table>\n"; | ||
| 308 | |||
| 309 | my $numpages = $o->get_num_pages(); | ||
| 310 | |||
| 311 | $buf .= "<div class=OQPager>\n"; | ||
| 312 | if ($numpages != 1) { | ||
| 313 | $buf .= "<button type=button title='previous page' class=OQPrevBut"; | ||
| 314 | $buf .= " disabled" if $$o{page}==1; | ||
| 315 | $buf .= "><</button>"; | ||
| 316 | } | ||
| 317 | $buf .= " <select name=rows_page>"; | ||
| 318 | foreach my $p (@{ $$o{schema}{results_per_page_picker_nums} }) { | ||
| 319 | $buf .= "<option value=$p".(($p eq $$o{rows_page})?" selected":"").">view $p results per page"; | ||
| 320 | } | ||
| 321 | $buf .= "</select>"; | ||
| 322 | if ($numpages != 1) { | ||
| 323 | $buf .= " <label>Page <input type=number min=1 max=$numpages step=1 name=page value='" | ||
| 324 | .escapeHTML($$o{page})."'> of $numpages</label> <button type=button title='next page' class=OQNextBut>></button>" | ||
| 325 | } | ||
| 326 | $buf .= " | ||
| 327 | </div> | ||
| 328 | <div class=OQformBottom>$opts{OQformBottom}</div> | ||
| 329 | <div class=OQBlocker></div> | ||
| 330 | <div class=OQColumnCmdPanel> | ||
| 331 | <button type=button class=OQLeftBut title='move column left'>move left</button> | ||
| 332 | <button type=button class=OQRightBut title='move column right'>move right</button> | ||
| 333 | <button type=button class=OQSortBut title='sort column A-Z'>sort</button> | ||
| 334 | <button type=button class=OQReverseSortBut title='reverse sort column Z-A'>sort reverse</button> | ||
| 335 | <button type=button class=OQFilterBut title='filter column'>filter</button> | ||
| 336 | <button type=button class=OQAddColumnsBut title='add columns'>add columns</button> | ||
| 337 | <button type=button class=OQCloseBut title='hide column'>hide column</button> | ||
| 338 | </div> | ||
| 339 | </form>"; | ||
| 340 | |||
| 341 | if ($dataonly) { | ||
| 342 | $buf .= "</body></html>"; | ||
| 343 | } else { | ||
| 344 | |||
| 345 | $buf .= "<div class=OQdocBottom>$opts{OQdocBottom}</div>"; | ||
| 346 | $buf .= $opts{htmlFooter}; | ||
| 347 | } | ||
| 348 | |||
| 349 | $$o{output_handler}->($buf); | ||
| 350 | |||
| 351 | return undef; | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | 1; | ||