| 1 | package CGI::OptimalQuery::LoadSearchTool; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use JSON::XS(); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use CGI qw(escapeHTML); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | |||
| 7 | |||
| 8 | sub load_saved_search { | ||
| 9 | my ($o, $id) = @_; | ||
| 10 | local $$o{dbh}{LongReadLen}; | ||
| 11 | if ($$o{dbh}{Driver}{Name} eq 'Oracle') { | ||
| 12 | $$o{dbh}{LongReadLen} = 900000; | ||
| 13 | my ($readLen) = $$o{dbh}->selectrow_array("SELECT dbms_lob.getlength(params) FROM oq_saved_search WHERE id = ?", undef, $id); | ||
| 14 | $$o{dbh}{LongReadLen} = $readLen if $readLen > $$o{dbh}{LongReadLen}; | ||
| 15 | } | ||
| 16 | my ($params) = $$o{dbh}->selectrow_array( | ||
| 17 | "SELECT params FROM oq_saved_search WHERE id=?", undef, $id); | ||
| 18 | if ($params) { | ||
| 19 | $params = eval '{'.$params.'}'; | ||
| 20 | if (ref($params) eq 'HASH') { | ||
| 21 | delete $$params{module}; | ||
| 22 | while (my ($k,$v) = each %$params) { | ||
| 23 | $$o{q}->param( -name => $k, -values => $v ); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | # remember saved search ID | ||
| 27 | $$o{q}->param('OQss', $id); | ||
| 28 | } | ||
| 29 | return undef; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub on_init { | ||
| 33 | my ($o) = @_; | ||
| 34 | |||
| 35 | # request to delete a saved search | ||
| 36 | if ($$o{q}->param('OQdeleteSavedSearch') =~ /^\d+$/) { | ||
| 37 | my $id = $$o{q}->param('OQdeleteSavedSearch'); | ||
| 38 | $$o{dbh}->do("DELETE FROM oq_saved_search WHERE user_id=? AND id=?", undef, $$o{schema}{savedSearchUserID}, $id); | ||
| 39 | $$o{output_handler}->(CGI::header('text/html')."report deleted"); | ||
| 40 | return undef; | ||
| 41 | } | ||
| 42 | |||
| 43 | # request to load a saved search? | ||
| 44 | elsif ($$o{q}->param('OQLoadSavedSearch') =~ /^\d+$/) { | ||
| 45 | load_saved_search($o, $$o{q}->param('OQLoadSavedSearch')); | ||
| 46 | } | ||
| 47 | |||
| 48 | # else if default saved searches are enabled and this isn't intial load, load default saved search if it exists | ||
| 49 | elsif (exists $$o{schema}{canSaveDefaultSearches} && ! defined $$o{q}->param('filter')) { | ||
| 50 | my ($id) = $$o{dbh}->selectrow_array(" | ||
| 51 | SELECT id | ||
| 52 | FROM oq_saved_search | ||
| 53 | WHERE is_default=1 | ||
| 54 | AND uri=? | ||
| 55 | LIMIT 1", undef, $$o{schema}{URI}); | ||
| 56 | load_saved_search($o, $id) if $id; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub on_open { | ||
| 61 | my ($o) = @_; | ||
| 62 | my $ar = $$o{dbh}->selectall_arrayref(" | ||
| 63 | SELECT id, uri, user_title | ||
| 64 | FROM oq_saved_search | ||
| 65 | WHERE user_id = ? | ||
| 66 | AND upper(uri) = upper(?) | ||
| 67 | AND oq_title = ? | ||
| 68 | ORDER BY 2", undef, $$o{schema}{savedSearchUserID}, | ||
| 69 | $$o{schema}{URI}, $$o{schema}{title}); | ||
| 70 | my $buf; | ||
| 71 | # must include state params because server code may not run without them defined | ||
| 72 | my $args; | ||
| 73 | if ($$o{schema}{state_params}) { | ||
| 74 | my @args; | ||
| 75 | foreach my $p (@{ $$o{schema}{state_params} }) { | ||
| 76 | my $v = $$o{q}->param($p); | ||
| 77 | push @args, "$p=".$o->escape_uri($v) if $v; | ||
| 78 | } | ||
| 79 | $args = '&'.join('&', @args) if $#args > -1; | ||
| 80 | } | ||
| 81 | foreach my $x (@$ar) { | ||
| 82 | my ($id, $uri, $user_title) = @$x; | ||
| 83 | $buf .= "<tr><td><a href='$uri?OQLoadSavedSearch=$id".$args."'>".escapeHTML($user_title)."</a></td><td><button type=button class=OQDeleteSavedSearchBut data-id=$id>x</button></td></tr>"; | ||
| 84 | } | ||
| 85 | if (! $buf) { | ||
| 86 | $buf = "<em>none</em>"; | ||
| 87 | } else { | ||
| 88 | $buf = "<table>".$buf."</table>"; | ||
| 89 | } | ||
| 90 | return $buf; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub activate { | ||
| 94 | my ($o) = @_; | ||
| 95 | $$o{schema}{tools}{loadreport} ||= { | ||
| 96 | title => "Load Report", | ||
| 97 | on_init => \&on_init, | ||
| 98 | on_open => \&on_open | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; | ||