--- trunk/Portal.pm 2004/03/07 18:48:13 3 +++ trunk/Portal.pm 2004/03/07 20:40:47 6 @@ -14,7 +14,7 @@ my $dsn = 'Pg:dbname=libdata'; my ($user,$passwd) = ('dpavlin',''); -my @persistent_vars = qw(p ms); +my @persistent_vars = qw(p ms s); # read global.conf configuration my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'"; @@ -34,9 +34,9 @@ $self->tmpl_path($TEMPLATE_PATH); $self->run_modes( 'home' => 'show_home', - 'ms' => 'show_ms', - 'it' => 'show_home', - 's' => 'show_s', + 'ms' => 'show_mastersubject', + 's' => 'show_subject', + 'r' => 'search_resources', ); $self->start_mode('home'); $self->mode_param('p'); @@ -68,7 +68,7 @@ # MasterSubject -sub show_ms { +sub show_mastersubject { my $self = shift; my $q = $self->query(); @@ -77,7 +77,7 @@ $tmpl->param('MasterSubjects' => $self->get_mastersubjects() ); - my $ms = $self->get_mastersubjects_by_id($q->param('ms')); + my $ms = $self->get_mastersubject_by_id($q->param('ms')); $tmpl->param('title' => uc($ms->{'mastersubject'}) ); $tmpl->param('search_field' => lc($ms->{'mastersubject'}) ); @@ -92,7 +92,7 @@ # Subject -sub show_s { +sub show_subject { my $self = shift; my $q = $self->query(); @@ -101,7 +101,7 @@ $tmpl->param('MasterSubjects' => $self->get_mastersubjects() ); - my $s = $self->get_subjects_by_id($q->param('s')); + my $s = $self->get_subject_by_id($q->param('s')); $tmpl->param('title' => uc($s->{'subject'}) ); $tmpl->param('search_field' => lc($s->{'subject'}) ); @@ -113,6 +113,38 @@ } +# search for resources and display results +sub search_resources { + my $self = shift; + + my $q = $self->query(); + + my $tmpl = $self->use_template('r.html'); + + $tmpl->param('MasterSubjects' => $self->get_mastersubjects() ); + + if ($q->param('s')) { + my $s = $self->get_subject_by_id($q->param('s')); + + $tmpl->param('title' => uc($s->{'subject'}) ); + $tmpl->param('search_field' => lc($s->{'subject'}) ); + } elsif ($q->param('ms')) { + my $s = $self->get_mastersubject_by_id($q->param('ms')); + + $tmpl->param('title' => uc($s->{'mastersubject'}) ); + $tmpl->param('search_field' => lc($s->{'mastersubject'}) ); + } + + my $res = $self->get_resources(); + $tmpl->param('resource_results' => $res); + $tmpl->param('nr_results' => scalar @$res); + + $tmpl->param('limit_infotype' => $self->get_infotype_by_id($q->param('it'))->{'infotype'}); + + return $tmpl->output; + +} + # # load template and generate permanent valirables in template # @@ -159,7 +191,7 @@ } # get one MasterSubject by it's ID -sub get_mastersubjects_by_id { +sub get_mastersubject_by_id { my $self = shift; my $id = shift || croak("need mastersubject id"); @@ -278,7 +310,7 @@ } # get one Subject by it's ID -sub get_subjects_by_id { +sub get_subject_by_id { my $self = shift; my $id = shift || croak("need subject id"); @@ -295,4 +327,87 @@ return $sth->fetchrow_hashref(); } +# get one InfoType by it's ID +sub get_infotype_by_id { + my $self = shift; + + my $id = shift || croak("need infotype id"); + + my $sql = qq{ + select infotype + from infotype + where infotype_id = ? + }; + + my $sth = $dbh->prepare($sql); + $sth->execute($id); + + return $sth->fetchrow_hashref(); +} + +# get add resources for given criteria +sub get_resources { + my $self = shift; + + my $q = $self->query(); + my @args; + + my $sql = qq{ + select distinct resource.resource_id, title, infotype.infotype, coverage_detail, url + }; + + my $sql_from = qq{ + from resource,infotype + }; + + my $sql_where = qq{ + where resource.infotype_id=infotype.infotype_id + }; + + # limits + if ($q->param('s')) { + $sql_from .= qq{ , res_sub_infotype }; + $sql_where .= qq{ + and res_sub_infotype.resource_id = resource.resource_id + and res_sub_infotype.subject_id = ? + }; + push @args, $q->param('s'); + } elsif ($q->param('ms')) { + $sql_from .= qq{ , res_sub_infotype }; + $sql_where .= qq{ + and res_sub_infotype.resource_id = resource.resource_id + and res_sub_infotype.subject_id in + (select subject_id from sub_mastersubject where mastersubject_id = ?) + }; + push @args, $q->param('ms'); + } + if ($q->param('it')) { + if ($sql_from !~ m/res_sub_infotype/) { + $sql_from .= qq{ , res_sub_infotype }; + $sql_where .= qq{ and res_sub_infotype.resource_id = resource.resource_id }; + } + $sql_where .= qq{ and res_sub_infotype.infotype_id = ? }; + push @args, $q->param('it'); + } + + my $sth = $dbh->prepare($sql.$sql_from.$sql_where); + $sth->execute(@args); + + my $arr = $sth->fetchall_arrayref({}); + + # now fill-in features + $sql = qq{ + select feature + from res_feature,feature + where res_feature.feature_id = feature.feature_id and res_feature.resource_id = ? + }; + $sth = $dbh->prepare($sql); + + foreach my $i ( 0 .. (scalar @$arr)-1 ) { + $sth->execute($arr->[$i]->{'resource_id'}); + $arr->[$i]->{'res_features'} = $sth->fetchall_arrayref({}); + } + + return $arr; +} 1;