| 1 | package CGI::OptimalQuery; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | no warnings qw( uninitialized ); | ||
| 6 | use CGI(); | ||
| 7 | |||
| 8 | BEGIN { | ||
| 9 | use Exporter (); | ||
| 10 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||
| 11 | $VERSION = '0.21'; | ||
| 12 | @ISA = qw(Exporter); | ||
| 13 | #Give a hoot don't pollute, do not export more than needed by default | ||
| 14 | @EXPORT = qw(); | ||
| 15 | @EXPORT_OK = qw(); | ||
| 16 | %EXPORT_TAGS = (); | ||
| 17 | } | ||
| 18 | |||
| 19 | |||
| 20 | # module registry - when loading a sub module, the module CGI param is | ||
| 21 | # consulted, and the value is loaded as a module. | ||
| 22 | our $DEFAULT_MODULE = 'InteractiveQuery2'; | ||
| 23 | |||
| 24 | our %DEFAULT_MODULES = ( | ||
| 25 | 'CustomOutput' => 'CGI::OptimalQuery::CustomOutput', | ||
| 26 | 'PrinterFriendly' => 'CGI::OptimalQuery::PrinterFriendly', | ||
| 27 | 'CSV' => 'CGI::OptimalQuery::CSV', | ||
| 28 | 'InteractiveFilter' => 'CGI::OptimalQuery::InteractiveFilter', | ||
| 29 | 'InteractiveQuery' => 'CGI::OptimalQuery::InteractiveQuery', | ||
| 30 | 'XML' => 'CGI::OptimalQuery::XML', | ||
| 31 | 'InteractiveQuery2' => 'CGI::OptimalQuery::InteractiveQuery2', | ||
| 32 | 'InteractiveFilter2' => 'CGI::OptimalQuery::InteractiveFilter2', | ||
| 33 | 'ShowColumns' => 'CGI::OptimalQuery::ShowColumns', | ||
| 34 | 'InteractiveQuery2Tools' => 'CGI::OptimalQuery::InteractiveQuery2Tools' | ||
| 35 | ); | ||
| 36 | |||
| 37 | |||
| 38 | # Constructor | ||
| 39 | # my $recset = CGI::OptimalQuery->new(\%schema ) | ||
| 40 | # This constructor instantiates the correct class based on the module param. | ||
| 41 | sub new { | ||
| 42 | my $pack = shift; | ||
| 43 | my $schema = $_[0]; | ||
| 44 | |||
| 45 | if ($CGI::OptimalQuery::q) { | ||
| 46 | $$schema{q} = $CGI::OptimalQuery::q; | ||
| 47 | } else { | ||
| 48 | $$schema{q} ||= new CGI(); | ||
| 49 | } | ||
| 50 | |||
| 51 | # if this is a mod_perl query object, turn it into a CGI object | ||
| 52 | if (! $$schema{q}->isa('CGI')) { | ||
| 53 | my @names = $$schema{q}->param(); | ||
| 54 | my %params; | ||
| 55 | foreach my $p (@names) { | ||
| 56 | my @v = $$schema{q}->param($p); | ||
| 57 | $params{$p} = \@v; | ||
| 58 | } | ||
| 59 | $$schema{q} = new CGI(\%params); | ||
| 60 | } | ||
| 61 | |||
| 62 | # set default handlers | ||
| 63 | $$schema{output_handler} ||= sub { print @_ }; | ||
| 64 | $$schema{error_handler} ||= sub { print STDERR @_; 0; }; | ||
| 65 | |||
| 66 | # find module & class | ||
| 67 | my $module = $$schema{q}->param('module') || $$schema{module} || $DEFAULT_MODULE; | ||
| 68 | my $class = $$schema{modules}{$module} || $DEFAULT_MODULES{$module}; | ||
| 69 | |||
| 70 | # dynamically load class | ||
| 71 | my $rv = eval "require $class"; | ||
| 72 | if ($@ =~ /Not\ Found/) { die "Could not find class $class"; } | ||
| 73 | elsif ($@) { die "Compile Error in class $class: $@"; } | ||
| 74 | elsif ($rv != 1) { die "Initialization error in class $class, should return 1"; } | ||
| 75 | |||
| 76 | # call appropriate constructor | ||
| 77 | return $class->new(@_); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub escape_js { CGI::OptimalQuery::Base::escape_js(@_); } | ||
| 81 | |||
| 82 | sub get_saved_search_list { | ||
| 83 | my $q = shift; | ||
| 84 | my $dbh = shift; | ||
| 85 | my $userid = shift; | ||
| 86 | |||
| 87 | if ($q->param('OQ_remove_saved_search_id') =~ /^\d+$/) { | ||
| 88 | $dbh->do("DELETE FROM oq_saved_search WHERE id = ? AND user_id = ?", undef, $q->param('OQ_remove_saved_search_id'), $userid); | ||
| 89 | } | ||
| 90 | |||
| 91 | my $oracleReadLen; | ||
| 92 | if ($$dbh{Driver}{Name} eq 'Oracle') { | ||
| 93 | ($oracleReadLen) = $dbh->selectrow_array("SELECT max(dbms_lob.getlength(params)) FROM oq_saved_search WHERE user_id = ?", undef, $userid); | ||
| 94 | } | ||
| 95 | local $dbh->{LongReadLen} = $oracleReadLen | ||
| 96 | if $oracleReadLen && $oracleReadLen > $dbh->{LongReadLen}; | ||
| 97 | |||
| 98 | my $sth = $dbh->prepare("SELECT id, uri, oq_title, user_title, params FROM oq_saved_search WHERE user_id = ? ORDER BY oq_title, user_title"); | ||
| 99 | $sth->execute($userid); | ||
| 100 | my $last_oq_title = ''; | ||
| 101 | my $buffer = ''; | ||
| 102 | while (my ($id, $uri, $oq_title, $user_title, $params) = $sth->fetchrow_array()) { | ||
| 103 | if ($last_oq_title ne $oq_title) { | ||
| 104 | $last_oq_title = $oq_title; | ||
| 105 | $buffer .= "</table>" if $buffer; | ||
| 106 | $buffer .= "<table class='OQ_ss'><tr><td colspan='2' class='OQ_ss_title'>".CGI::escapeHTML($oq_title)."</td></tr>"; | ||
| 107 | } | ||
| 108 | |||
| 109 | my $stateArgs = ''; | ||
| 110 | if ($params ne '') { | ||
| 111 | $params = eval '{'.$params.'}'; | ||
| 112 | if (ref($params) eq 'HASH') { | ||
| 113 | delete $$params{show}; | ||
| 114 | delete $$params{rows_page}; | ||
| 115 | delete $$params{page}; | ||
| 116 | delete $$params{hiddenFilter}; | ||
| 117 | delete $$params{filter}; | ||
| 118 | delete $$params{queryDescr}; | ||
| 119 | delete $$params{sort}; | ||
| 120 | delete $$params{module}; | ||
| 121 | while (my ($k,$v) = each %$params) { | ||
| 122 | $stateArgs .= "&$k="; | ||
| 123 | $stateArgs .= (ref($v) eq 'ARRAY') ? CGI::escape($$v[0]) : CGI::escape($v); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | $buffer .= "<tr><td class='OQ_ss_query_title'><a href=# onclick=\"opwin('$uri?OQLoadSavedSearch=$id".$stateArgs."#OQtop','OQLoadSavedSearch$id','resizable,scrollbars',1024,768); return false;\">".CGI::escapeHTML($user_title)."</a></td><td class='OQ_ss_cmds'><button onclick=\"this.form.OQ_remove_saved_search_id.value = '$id'; this.form.submit();\" type='button'>delete</button></td></tr>"; | ||
| 129 | } | ||
| 130 | $sth->finish(); | ||
| 131 | $buffer .= "</table><input type='hidden' name='OQ_remove_saved_search_id' /> | ||
| 132 | <script> | ||
| 133 | if (! window.opwin) { | ||
| 134 | window.opwin = function(lnk,target,opts) { | ||
| 135 | window.open(lnk,target,opts); | ||
| 136 | }; | ||
| 137 | } | ||
| 138 | </script>" if $buffer; | ||
| 139 | return $buffer; | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | 1; | ||
| 144 | __END__ |