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

filter: Statementstmt 0.0% 0 / 188 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 42 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 343 CC 53 SCAR 78.0
CC
53
Cov
0%
output
2756.0
escapeHTML
6.0
BEGIN
2.0
CRAP 2429.4
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 {
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>&#10005;</button></div></div></div>";
154 }
155 }
156 $buf .= "
157 <form class=OQform name=OQform action='".escapeHTML($$o{schema}{URI_standalone}||$$o{schema}{URI})."' method=get>
158 <input type=hidden name=show value='".escapeHTML(join(',',@{$$o{show}}))."'>
159 <input type=hidden name=filter value='".escapeHTML($$o{filter})."'>
160 <input type=hidden name=hiddenFilter value='".escapeHTML($$o{hiddenFilter})."'>
161 <input type=hidden name=queryDescr value='".escapeHTML($$o{queryDescr})."'>
162 <input type=hidden name=sort value='".escapeHTML($$o{'sort'})."'>
163 <input type=hidden name=module value='".escapeHTML($$o{module})."'>
164 <input type=hidden name=OQss value='".escapeHTML($$o{q}->param('OQss'))."'>
165 <input type=hidden name=on_select value='".escapeHTML($$o{q}->param('on_select'))."'>\n";
166 if (ref($$o{schema}{state_params}) eq 'ARRAY') {
167 $buf .= "<input type=hidden name='".escapeHTML($_)."' value='"
168 .escapeHTML($$o{q}->param($_))."'>\n" for @{$$o{schema}{state_params}};
169 }
170 $buf .=
171 "<a name=OQtop></a>
172 <div class=OQformTop>$opts{OQformTop}</div>
173  
174 <div class=OQhead>
175 <div class=OQtitle>".escapeHTML($o->get_title)."</div>
176 <div class=OQsummary>Result(s) (".$o->commify($o->get_lo_rec)." - "
177 .$o->commify($o->get_hi_rec).") of ".$o->commify($o->get_count)."</div>
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 </div>
185  
186 <table class=OQinfo>";
187 $buf .= "<tr class=OQQueryDescr><td class=OQlabel>Query:</td><td>".escapeHTML($$o{queryDescr})."</td></tr>" if $$o{queryDescr};
188  
189 my $filter = $o->get_filter();
190 if ($filter) {
191 $buf .= "<tr class=OQFilterDescr title='click to edit filter'";
192 $buf .= " data-nofilter" if $opts{disable_filter};
193 $buf .= "><td class=OQlabel>Filter:</td><td>".escapeHTML($filter)."</td></tr>";
194 }
195  
196 my @sort = $o->sth->sort_descr;
197 if ($#sort > -1) {
198 $buf .= "<tr class=OQSortDescr><td class=OQlabel>Sort:</td><td>";
199 my $comma = '';
200 foreach my $c (@sort) {
201 $buf .= $comma;
202 $comma = ', ';
203 $buf .= "<a title=remove class=OQRemoveSortBut>" unless $opts{disable_sort};
204 $buf .= escapeHTML($c);
205 $buf .= "</a>" unless $opts{disable_sort};
206 }
207 $buf .= "</tr>";
208 }
209 $buf .=
210 "</table>
211 <table class=OQdata>
212 <thead title='click to hide, sort, filter, or add columns'>
213 <tr>
214 <td class=OQdataLHead></td>";
215 foreach my $colAlias (@{ $o->get_usersel_cols }) {
216 my $colOpts = $$o{schema}{select}{$colAlias}[3];
217 $buf .= "<td data-col='".escapeHTML($colAlias)."'";
218 $buf .= " data-noselect" if $$colOpts{disable_select} || $opts{disable_select};
219 $buf .= " data-nosort" if $$colOpts{disable_sort} || $opts{disable_sort};
220 $buf .= " data-nofilter" if $$colOpts{disable_filter} || $opts{disable_filter};
221 $buf .= ">".escapeHTML($o->get_nice_name($colAlias))."</td>";
222 }
223 $buf .= "
224 <td class=OQdataRHead></td>
225 </tr>
226 </thead>
227 <tbody>\n";
228  
229 my $recs_in_buffer = 0;
230 my $typeMap = $o->{oq}->get_col_types('select');
231 while (my $r = $o->fetch()) {
232 $buf .= "<tr data-uid='".escapeHTML($$r{U_ID})."'><td class=OQdataLCol>";
233 if (ref($opts{OQdataLCol}) eq 'CODE') {
234 $buf .= $opts{OQdataLCol}->($r);
235 } elsif (ref($opts{buildEditLink}) eq 'CODE') {
236 my $link = $opts{buildEditLink}->($o, $r, \%opts);
237 if ($link ne '') {
238 $buf .= "<a href='".escapeHTML($link)."' title='open record' class=OQeditBut>".$opts{editButtonLabel}."</a>";
239 }
240 } elsif ($opts{editLink} ne '' && $$r{U_ID} ne '') {
241 my $link = $opts{editLink}.(($opts{editLink} =~ /\?/)?'&':'?')."on_update=OQrefresh&act=load&id=$$r{U_ID}";
242 $buf .= "<a href='".escapeHTML($link)."' title='open record' class=OQeditBut>".$opts{editButtonLabel}."</a>";
243 }
244 $buf .= "</td>";
245 foreach my $col (@{ $o->get_usersel_cols }) {
246 my $val = $o->get_html_val($col);
247 my $type = $$typeMap{$col} || 'char';
248 $buf .= "<td".(($type ne 'char')?" class=$type":"").">$val</td>";
249 }
250 $buf .= "<td class=OQdataRCol>";
251 if (ref($opts{OQdataRCol}) eq 'CODE') {
252 $buf .= $opts{OQdataRCol}->($r);
253 } elsif ($o->{q}->param('on_select') ne '') {
254 my $on_select = $o->{q}->param('on_select');
255 $on_select =~ s/\~.*//;
256 my ($func,@argfields) = split /\,/, $on_select;
257 $argfields[0] = 'U_ID' if $#argfields==-1;
258 my @argvals = map {
259 my $v=$$r{$_};
260 $v = join(', ', @$v) if ref($v) eq 'ARRAY';
261 $v =~ s/\~\~\~//g;
262 $v;
263 } @argfields;
264 $buf .= "<button type=button title='select record' class=OQselectBut data-rv='"
265 .escapeHTML(join('~~~',@argvals))."'>select</button>";
266 }
267 $buf .= "</td></tr>\n";
268 if (++$recs_in_buffer == 10000) {
269 $$o{output_handler}->($buf);
270 $buf = '';
271 $recs_in_buffer = 0;
272 }
273 }
274 $o->finish();
275  
276 $buf .= "</tbody></table>\n";
277  
278 my $numpages = $o->get_num_pages();
279  
280 $buf .= "<div class=OQPager>\n";
281 if ($numpages != 1) {
282 $buf .= "<button type=button title='previous page' class=OQPrevBut";
283 $buf .= " disabled" if $$o{page}==1;
284 $buf .= ">&lt;</button>";
285 }
286 $buf .= " <select name=rows_page>";
287 foreach my $p (@{ $$o{schema}{results_per_page_picker_nums} }) {
288 $buf .= "<option value=$p".(($p eq $$o{rows_page})?" selected":"").">view $p results per page";
289 }
290 $buf .= "</select>";
291 if ($numpages != 1) {
292 $buf .= " <label>Page <input type=number min=1 max=$numpages step=1 name=page value='"
293 .escapeHTML($$o{page})."'> of $numpages</label> <button type=button title='next page' class=OQNextBut>&gt;</button>"
294 }
295 $buf .= "
296 </div>
297 <div class=OQformBottom>$opts{OQformBottom}</div>
298 <div class=OQBlocker></div>
299 <div class=OQColumnCmdPanel>
300 <button type=button class=OQLeftBut title='move column left'>move left</button>
301 <button type=button class=OQRightBut title='move column right'>move right</button>
302 <button type=button class=OQSortBut title='sort column A-Z'>sort</button>
303 <button type=button class=OQReverseSortBut title='reverse sort column Z-A'>sort reverse</button>
304 <button type=button class=OQFilterBut title='filter column'>filter</button>
305 <button type=button class=OQAddColumnsBut title='add columns'>add columns</button>
306 <button type=button class=OQCloseBut title='hide column'>hide column</button>
307 </div>
308 </form>";
309  
310 if ($dataonly) {
311 $buf .= "</body></html>";
312 } else {
313  
314 $buf .= "<div class=OQdocBottom>$opts{OQdocBottom}</div>";
315 $buf .= $opts{htmlFooter};
316 }
317  
318 $$o{output_handler}->($buf);
319  
320 return undef;
321 }
322  
323  
324 1;