line
stmt
bran
cond
sub
pod
time
code
1
package CGI::OptimalQuery::LoadSearchTool;
2
3
8
8
52
use strict;
8
19
8
212
4
8
8
41
use JSON::XS();
8
19
8
113
5
8
8
35
use CGI::OptimalQuery::Base();
8
17
8
6541
6
7
0
0
0
sub escapeHTML { CGI::OptimalQuery::Base::escapeHTML(@_) }
8
9
sub load_default_saved_search {
10
0
0
0
my ($o) = @_;
11
0
0
return undef unless exists $$o{canSaveDefaultSearches};
12
0
local $$o{dbh}{LongReadLen};
13
0
0
if ($$o{dbh}{Driver}{Name} eq 'Oracle') {
14
0
$$o{dbh}{LongReadLen} = 900000;
15
0
my ($readLen) = $$o{dbh}->selectrow_array("SELECT dbms_lob.getlength(params) FROM oq_saved_search WHERE uri=? AND is_default=1", undef, $$o{schema}{URI});
16
0
0
$$o{dbh}{LongReadLen} = $readLen if $readLen > $$o{dbh}{LongReadLen};
17
}
18
my ($params) = $$o{dbh}->selectrow_array("
19
SELECT params
20
FROM oq_saved_search
21
WHERE uri=?
22
0
AND is_default=1", undef, $$o{schema}{URI});
23
24
0
0
if ($params) {
25
0
$params = eval '{'.$params.'}';
26
0
0
if (ref($params) eq 'HASH') {
27
0
delete $$params{module};
28
0
while (my ($k,$v) = each %$params) {
29
0
0
if(!defined($$o{q}->param($k))) {
30
0
$$o{q}->param( -name => $k, -values => $v );
31
}
32
}
33
}
34
}
35
0
return undef;
36
}
37
38
39
sub load_saved_search {
40
0
0
0
my ($o, $id) = @_;
41
0
local $$o{dbh}{LongReadLen};
42
0
0
if ($$o{dbh}{Driver}{Name} eq 'Oracle') {
43
0
$$o{dbh}{LongReadLen} = 900000;
44
0
my ($readLen) = $$o{dbh}->selectrow_array("SELECT dbms_lob.getlength(params) FROM oq_saved_search WHERE id = ?", undef, $id);
45
0
0
$$o{dbh}{LongReadLen} = $readLen if $readLen > $$o{dbh}{LongReadLen};
46
}
47
my ($params) = $$o{dbh}->selectrow_array(
48
0
"SELECT params FROM oq_saved_search WHERE id=?", undef, $id);
49
50
0
0
if ($params) {
51
0
$params = eval '{'.$params.'}';
52
0
0
if (ref($params) eq 'HASH') {
53
0
delete $$params{module};
54
0
while (my ($k,$v) = each %$params) {
55
0
0
if(!defined($$o{q}->param($k))) {
56
0
$$o{q}->param( -name => $k, -values => $v );
57
}
58
}
59
}
60
61
# remember saved search ID
62
0
$$o{q}->param('OQss', $id);
63
}
64
0
return undef;
65
}
66
67
sub on_init {
68
0
0
0
my ($o) = @_;
69
70
0
0
my $delete_id = $$o{q}->param('OQDeleteSavedSearch') || $$o{q}->url_param('OQDeleteSavedSearch');
71
72
# request to delete a saved search
73
0
0
if ($delete_id) {
0
0
74
0
$$o{dbh}->do("DELETE FROM oq_saved_search WHERE user_id=? AND id=?", undef, $$o{schema}{savedSearchUserID}, $delete_id);
75
0
$$o{output_handler}->($$o{httpHeader}->('text/html')."report deleted");
76
0
return undef;
77
}
78
79
# request to load a saved search?
80
elsif ($$o{q}->param('OQLoadSavedSearch') =~ /^\d+$/) {
81
0
load_saved_search($o, $$o{q}->param('OQLoadSavedSearch'));
82
}
83
84
# if intial request, load default saved search if it exists
85
elsif (! defined $$o{q}->param('module')) {
86
0
load_default_saved_search($o);
87
}
88
}
89
90
sub on_open {
91
0
0
0
my ($o) = @_;
92
my $ar = $$o{dbh}->selectall_arrayref("
93
SELECT id, uri, user_title
94
FROM oq_saved_search
95
WHERE user_id = ?
96
AND upper(uri) = upper(?)
97
AND oq_title = ?
98
ORDER BY 2", undef, $$o{schema}{savedSearchUserID},
99
0
$$o{schema}{URI}, $$o{schema}{title});
100
0
my $buf;
101
# must include state params because server code may not run without them defined
102
my $args;
103
0
0
if ($$o{schema}{state_params}) {
104
0
my @args;
105
0
foreach my $p (@{ $$o{schema}{state_params} }) {
0
106
0
my $v = $$o{q}->param($p);
107
0
0
push @args, "$p=".$o->escape_uri($v) if $v;
108
}
109
0
0
$args = '&'.join('&', @args) if $#args > -1;
110
}
111
0
foreach my $x (@$ar) {
112
0
my ($id, $uri, $user_title) = @$x;
113
0
$buf .= " ".escapeHTML($user_title)." x ";
114
}
115
0
0
if (! $buf) {
116
0
$buf = "none ";
117
} else {
118
0
$buf = "";
119
}
120
0
return $buf;
121
}
122
123
sub activate {
124
0
0
0
my ($o) = @_;
125
$$o{schema}{tools}{loadreport} ||= {
126
0
0
title => "Load Report",
127
on_init => \&on_init,
128
on_open => \&on_open
129
};
130
}
131
132
1;