blib/lib/CGI/OptimalQuery/InteractiveQuery2.pm untested

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