lib/CGI/OptimalQuery/InteractiveQuery2.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« 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 { 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{httpHeader}->(-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>" unless $opts{jquery_already_sent};
121 $buf .= "
122 <script src=$$o{schema}{resourceURI}/InteractiveQuery2.js?$ver></script>
123 <script>
124 (function(){
125 $script
126 })();
127 </script>";
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=hiddenFilter value='".escapeHTML($$o{hiddenFilter})."'>
156 <input type=hidden name=queryDescr value='".escapeHTML($$o{queryDescr})."'>
157 <input type=hidden name=sort value='".escapeHTML($$o{'sort'})."'>\n";
158 $buf .= "<input type=hidden name=mode value='".escapeHTML($$o{mode})."'>\n" if $$o{mode};
159 $buf .= "<input type=hidden name=module value='".escapeHTML($$o{module})."'>\n" if $$o{module};
160  
161 my @p = qw( OQss on_select on_update );
162 push @p, @{ $$o{schema}{state_params} } if ref($$o{schema}{state_params}) eq 'ARRAY';
163 foreach my $p (@p) {
164 my $v = $$o{q}->param($p);
165 $buf .= "<input type=hidden name='".escapeHTML($p)."' value='".escapeHTML($v)."'>\n" if $v ne '';
166 }
167  
168 $buf .=
169 "<a name=OQtop></a>
170 <div class=OQformTop>$opts{OQformTop}</div>
171  
172 <div class=OQhead>
173 <div class=OQtitle>".escapeHTML($o->get_title)."</div>
174 <div class=OQsummary>Result(s) (".$o->commify($o->get_lo_rec)." - "
175 .$o->commify($o->get_hi_rec).") of ".$o->commify($o->get_count)."</div>";
176  
177 if ($$o{mode} ne 'recview') {
178 $buf .= "
179 <div class=OQcmds>
180 $newBut
181 <button type=button title='refresh data' class=OQrefreshBut>refresh</button>
182 <button type=button title='tools' class=OQToolsBut>tools</button>
183 <button type=button title='help' class=OQhelpBut>help</button>
184 </div>";
185 }
186 $buf .= "
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 .= "
218 <div class=OQRecViewCmds>
219 $newBut
220 <button type=button class=OQrefreshBut>refresh</button>
221 <button type=button class=OQAddColumnsBut>columns</button>
222 <button type=button class=OQFilterBut>filter</button>
223 <button type=button class=OQhelpBut>help</button>
224 <button type=button class=OQToolsBut>tools</button>
225 </div>";
226 }
227  
228  
229 # print update message
230 my $updated_uid = $o->{q}->param('updated_uid');
231 if ($updated_uid ne '') {
232 my $msg;
233 if (exists $opts{OQRecUpdateMsg}) {
234 if (ref($opts{OQRecUpdateMsg}) eq 'CODE') {
235 $msg = $opts{OQRecUpdateMsg}->($updated_uid);
236 } else {
237 $msg = $opts{OQRecUpdateMsg};
238 }
239 } elsif ($opts{editLink}) {
240 my $editLink = $opts{editLink}.(($opts{editLink} =~ /\?/)?'&':'?')."on_update=OQrefresh&act=load&id=".CGI::escape($updated_uid);
241 $msg = "Record <a class=opwin href='".escapeHTML($editLink)."'>".escapeHTML($updated_uid)."</a> updated.";
242 }
243 if ($msg) {
244 $buf .= "<div class=OQRecUpdateMsg data-uid='".escapeHTML($updated_uid)."'>$msg</div>";
245 }
246 }
247  
248  
249 $$o{output_handler}->($buf);
250 $buf = '';
251  
252 $buf .= "<table class=OQdata>";
253  
254 if ($$o{mode} eq 'recview') {
255 } else {
256 $buf .= "
257 <thead title='click to hide, sort, filter, or add columns'>
258 <tr>
259 <td class=OQdataLHead></td>";
260 foreach my $colAlias (@{ $o->get_usersel_cols }) {
261 my $colOpts = $$o{schema}{select}{$colAlias}[3];
262 $buf .= "<td data-col='".escapeHTML($colAlias)."'";
263 $buf .= " data-noselect" if $$colOpts{disable_select} || $opts{disable_select};
264 $buf .= " data-nosort" if $$colOpts{disable_sort} || $opts{disable_sort};
265 $buf .= " data-nofilter" if $$colOpts{disable_filter} || $opts{disable_filter};
266 $buf .= ">".escapeHTML($o->get_nice_name($colAlias))."</td>";
267 }
268 $buf .= "
269 <td class=OQdataRHead></td>
270 </tr>
271 </thead>";
272 }
273  
274 $buf .= "
275 <tbody>\n";
276  
277 my $recs_in_buffer = 0;
278 my $typeMap = $o->{oq}->get_col_types('select');
279 while (my $r = $o->fetch()) {
280 my $leftBut;
281 if (ref($opts{OQdataLCol}) eq 'CODE') {
282 $leftBut = $opts{OQdataLCol}->($r);
283 } elsif (ref($opts{buildEditLink}) eq 'CODE') {
284 my $link = $opts{buildEditLink}->($o, $r, \%opts);
285 if ($link ne '') {
286 $leftBut = "<a href='".escapeHTML($link)."' title='open record' class=OQeditBut>".$opts{editButtonLabel}."</a>";
287 }
288 } elsif ($opts{editLink} ne '' && $$r{U_ID} ne '') {
289 my $link = $opts{editLink}.(($opts{editLink} =~ /\?/)?'&':'?')."on_update=OQrefresh&act=load&id=$$r{U_ID}";
290 $leftBut = "<a href='".escapeHTML($link)."' title='open record' class=OQeditBut>".$opts{editButtonLabel}."</a>";
291 }
292  
293 my $rightBut;
294 if (ref($opts{OQdataRCol}) eq 'CODE') {
295 $rightBut = $opts{OQdataRCol}->($r);
296 } elsif ($o->{q}->param('on_select') ne '') {
297 my $on_select = $o->{q}->param('on_select');
298 $on_select =~ s/\~.*//;
299 my ($func,@argfields) = split /\,/, $on_select;
300 $argfields[0] = 'U_ID' if $#argfields==-1;
301 my @argvals = map {
302 my $v=$$r{$_};
303 $v = join(', ', @$v) if ref($v) eq 'ARRAY';
304 $v =~ s/\~\~\~//g;
305 $v;
306 } @argfields;
307 $rightBut = "<button type=button title='select record' class=OQselectBut data-rv='"
308 .escapeHTML(join('~~~',@argvals))."'>select</button>";
309 }
310  
311  
312 $buf .= "<tr data-uid='".escapeHTML($$r{U_ID})."'";
313 $buf .= " class=OQupdatedRow" if $updated_uid && $updated_uid eq $$r{U_ID};
314 $buf .= ">";
315  
316 if ($$o{mode} eq 'recview') {
317 $buf .= "<td>";
318 foreach my $col (@{ $o->get_usersel_cols }) {
319 my $val = $o->get_html_val($col);
320 if ($val ne '') {
321 my $label = $o->get_nice_name($col);
322 $buf .= "<div class=OQrecviewLabel>".escapeHTML($label).":</div><div class=OQrecviewVal>$val</div>";
323 }
324 }
325 $buf .= "$leftBut $rightBut</td>";
326 }
327  
328 else {
329 $buf .= "<td class=OQdataLCol>$leftBut</td>";
330 foreach my $col (@{ $o->get_usersel_cols }) {
331 my $val = $o->get_html_val($col);
332 my $type = $$typeMap{$col} || 'char';
333 $buf .= "<td";
334 $buf .= " class=$type" unless $type eq 'char';
335 $buf .= " nowrap" if $$o{schema}{select}{$col}[3]{nowrap};
336 $buf .= " align='".escapeHTML($$o{schema}{select}{$col}[3]{align})."'"
337 if $$o{schema}{select}{$col}[3]{align};
338 $buf .= ">$val</td>";
339 }
340 $buf .= "<td class=OQdataRCol>$rightBut</td>";
341 }
342  
343 $buf .= "</tr>\n";
344 if (++$recs_in_buffer == 1000) {
345 $$o{output_handler}->($buf);
346 $buf = '';
347 $recs_in_buffer = 0;
348 }
349 }
350 $o->finish();
351  
352 $buf .= "</tbody></table>\n";
353  
354 my $numpages = $o->get_num_pages();
355  
356 $buf .= "<div class=OQPager>\n";
357 if ($numpages != 1) {
358 $buf .= "<button type=button title='previous page' class=OQPrevBut";
359 $buf .= " disabled" if $$o{page}==1;
360 $buf .= ">&lt;</button>";
361 }
362 $buf .= " <select name=rows_page>";
363 foreach my $p (@{ $$o{schema}{results_per_page_picker_nums} }) {
364 $buf .= "<option value=$p".(($p eq $$o{rows_page})?" selected":"").">view $p results per page";
365 }
366 $buf .= "</select>";
367 if ($numpages != 1) {
368 $buf .= " <label>Page <input type=number min=1 max=$numpages step=1 name=page value='"
369 .escapeHTML($$o{page})."'> of $numpages</label> <button type=button title='next page' class=OQNextBut>&gt;</button>"
370 }
371 $buf .= "
372 </div>
373 <div class=OQformBottom>$opts{OQformBottom}</div>
374 <div class=OQBlocker></div>
375 <div class=OQColumnCmdPanel>
376 <button type=button class=OQLeftBut title='move column left'>move left</button>
377 <button type=button class=OQRightBut title='move column right'>move right</button>
378 <button type=button class=OQSortBut title='sort column A-Z'>sort</button>
379 <button type=button class=OQReverseSortBut title='reverse sort column Z-A'>sort reverse</button>
380 <button type=button class=OQFilterBut title='filter column'>filter</button>
381 <button type=button class=OQAddColumnsBut title='add columns'>add columns</button>
382 <button type=button class=OQCloseBut title='hide column'>hide column</button>
383 </div>
384 </form>";
385  
386 $buf .= "<div class=OQdocBottom>$opts{OQdocBottom}</div>";
387 $buf .= "</div>"; # div.OQdoc
388 $buf .= $opts{htmlFooter};
389  
390 $$o{output_handler}->($buf);
391  
392 return undef;
393 }
394  
395  
396 1;
« lib/CGI/OptimalQuery/InteractiveQuery.pm ↑ Summary lib/CGI/OptimalQuery/InteractiveQuery2Tools.pm »