| 1 | package CGI::Widget::DBI::Search::Base; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 3 | ▶ | use Encode qw/decode/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use Encode::Detect; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Scalar::Util qw/blessed/; | ||
| 8 | use URI::Escape; | ||
| 9 | |||
| 10 | # --------------------- USER CUSTOMIZABLE VARIABLES ------------------------ | ||
| 11 | |||
| 12 | use constant DEBUG => 0; | ||
| 13 | |||
| 14 | # --------------------- END USER CUSTOMIZABLE VARIABLES -------------------- | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $this = shift; | ||
| 18 | my $class = ref($this) || $this; | ||
| 19 | my $self = ref($_[0]) =~ m/^CGI::Widget::DBI::/ && scalar(@_) == 1 | ||
| 20 | ? bless { %{ $_[0] } }, $class | ||
| 21 | : bless { @_ }, $class; | ||
| 22 | $self->initialize if $self->can('initialize'); | ||
| 23 | return $self; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub caller_function { | ||
| 27 | my ($self, $stacklvl) = @_; | ||
| 28 | my ($func) = ( (caller($stacklvl || 1))[3] =~ m/::([^:]+)\z/ ); | ||
| 29 | return $func || ''; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub log_error { | ||
| 33 | my ($self, $msg) = @_; | ||
| 34 | my $method = $self->caller_function(2) || $self->caller_function(3); # check one level higher in case called from eval | ||
| 35 | my $logmsg = (ref($self)||$self).'->'.$method.': '.$msg; | ||
| 36 | if (blessed($self->{r}) && $self->{r}->can('log_error')) { | ||
| 37 | $self->{r}->log_error($logmsg); | ||
| 38 | } elsif (ref $self->{parent} and ref $self->{parent}->{r} eq 'Apache') { | ||
| 39 | $self->{parent}->{r}->log_error($logmsg); | ||
| 40 | } else { | ||
| 41 | print STDERR '['.localtime().'] [error] [client '.$ENV{REMOTE_ADDR}.'] (STDERR) '.$logmsg."\n"; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | sub warn { | ||
| 46 | my ($self, $msg) = @_; | ||
| 47 | return unless $self->{_DEBUG} || DEBUG; | ||
| 48 | my $method = $self->caller_function(2) || $self->caller_function(3); | ||
| 49 | my $logmsg = (ref($self)||$self).'->'.$method.': '.$msg; | ||
| 50 | if (blessed($self->{r}) && $self->{r}->can('warn')) { | ||
| 51 | $self->{r}->warn($logmsg); | ||
| 52 | } elsif (ref $self->{parent} and ref $self->{parent}->{r} eq 'Apache') { | ||
| 53 | $self->{parent}->{r}->warn($logmsg); | ||
| 54 | } else { | ||
| 55 | print STDERR '['.localtime().'] [warn] [client '.$ENV{REMOTE_ADDR}.'] (STDERR) '.$logmsg."\n"; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub extra_vars_for_uri { | ||
| 60 | my ($self, $exclude_param_list) = @_; | ||
| 61 | return '' unless ref $self->{-href_extra_vars} eq 'HASH'; | ||
| 62 | my %exclude = map {$_=>1} @{$exclude_param_list||[]}; | ||
| 63 | return join('&', map { | ||
| 64 | $exclude{$_} ? () | ||
| 65 | : uri_escape($_).'='.uri_escape_utf8(defined $self->{-href_extra_vars}->{$_} | ||
| 66 | ? $self->{-href_extra_vars}->{$_} | ||
| 67 | : $self->{q}->param($_) // ''); | ||
| 68 | } keys %{$self->{-href_extra_vars}}); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub extra_vars_for_form { | ||
| 72 | my ($self) = @_; | ||
| 73 | return '' unless ref $self->{-form_extra_vars} eq 'HASH'; | ||
| 74 | return join('', map { $self->{q}->hidden($_) } sort keys %{$self->{-form_extra_vars}}); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub decode_utf8 { | ||
| 78 | my ($self, $input) = @_; | ||
| 79 | my $output = eval { decode('Detect', $input); }; | ||
| 80 | if ($@) { | ||
| 81 | return $input if $@ =~ m/^Unknown encoding:/; | ||
| 82 | die $@; | ||
| 83 | } | ||
| 84 | return $output; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | 1; | ||
| 89 | __END__ | ||