package II1b4055e43647c5076890f5400944c314::mod_keysearchobj; use strict; use base qw/II720d6e18aa34327713eb014408fc9144/; use Zen::Config; sub process { my $class = shift; my ($action, $value, $config, $element_id, $edit_class, $edit_rec_id, $edit_field) = @_; my $cfg = _load_config($config); my $code; if ($cfg->{priv_check_handler}) { if (my $denied_msg = Zen::PerlHandler->get($cfg->{priv_check_handler})->do) { return $denied_msg; } } if ($action eq 'change') { eval "require $edit_class"; die $@ if $@; if ($value == -1) { $action = 'search'; } elsif ($value == -2) { $value = $edit_class->get($edit_rec_id)->{$edit_field}; $action = undef; } else { $edit_class->get($edit_rec_id)->set($edit_field => ($value + 0))->save; $action = undef; } } if (! $action) { my $title; if ($value) { my $obj = $cfg->{persistent_class}->get($value); $title = "$obj->{$cfg->{view_field}}&nbsp;(#$obj->{id})"; } else { $title = $cfg->{no_value_title}; } $title ||= "[NO TITLE]"; $code = qq(<a href="#" onClick="ajax$element_id('search', ''); return false">[$title]</a>); } elsif ($action eq 'search') { my $msg_go = $class->message('go'); my $msg_reset = $class->message('reset'); my $msg_sea_size = $class->message('sea_size'); $code = qq(<input type="text" id="$element_id\_sea" size="10" style="size:70%">&nbsp;) . qq(<input type="button" value="$msg_go" onClick="var sea = document.getElementById('$element_id\_sea').value; if (sea.length >= 3) ajax$element_id('select', sea); else alert('$msg_sea_size')">) . qq(<input type="button" value="$msg_reset" onClick="ajax$element_id('change', '0');">); } elsif ($action eq 'select') { my @sea; push @sea, ORDER => $cfg->{order_field} if $cfg->{order_field}; push @sea, @{ $cfg->{addon_search_parsed} } if $cfg->{addon_search_parsed}; my $dbc = $cfg->{persistent_class}->dbc; (my $keyword_search = $cfg->{keyword_search}) =~ s/\#([A-Z]+)/$1 eq 'V' ? $value : $1 eq 'QV' ? $dbc->quote($value) : $1 eq 'LQV' ? $dbc->quoteLike($value) : die("unknown mask meta [$1]")/ge; push @sea, WHERE => $keyword_search; my $objects = $cfg->{persistent_class}->search(@sea); my @values = map { $_->{id} } @$objects; my %labels = map { $_->{id} => "$_->{$cfg->{view_field}} (#$_->{id})" } @$objects; $labels{''} = $class->message('results', scalar(@$objects)); $labels{'-1'} = $class->message('new_search'); $labels{'-2'} = $class->message('cancel'); unshift @values, 0 if @$objects; unshift @values, '', -1, -2; $code = II3562149e53818b6637b38680e87ba918::perllist::buildList('select', $element_id.'_sel', '', \@values, \%labels, undef, qq(onChange="var v = this.options[this.selectedIndex].value; if (v != '0' && v != '') ajax$element_id('change', v)")); } else { die "unknown action [$action]"; } $code .= Zen::PerlHandler->get($cfg->{attendant_content_handler})->do($action, $value, $element_id, $edit_class, $edit_rec_id, $edit_field) if $cfg->{attendant_content_handler}; $code; } sub _load_config { my $config = shift; my $cfg = Zen::Config->get($1)->{$2} if $config =~ /^(.+?)::(\w+)$/; die "config not found [$config]" unless defined $cfg; die "persistent_class not found in config [$config]" unless $cfg->{persistent_class}; die "keyword_search not found in config [$config]" unless $cfg->{keyword_search}; die "view_field not found in config [$config]" unless $cfg->{view_field}; eval "require $cfg->{persistent_class}"; die $@ if $@; if (my $addon_search = $cfg->{addon_search}) { my $in = Zen->getRequest->getInput; $addon_search =~ s!\#\{INPUT\.(\w+)\}!$in->{$1}!g; eval "\$cfg->{addon_search_parsed} = [$addon_search]"; } $cfg; } 1; 