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

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