/[libdata-portal]/branches/mysql/trunk/Portal.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /branches/mysql/trunk/Portal.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/Portal.pm revision 1 by dpavlin, Sun Mar 7 18:22:26 2004 UTC branches/mysql/trunk/Portal.pm revision 11 by dpavlin, Sun Mar 7 21:32:53 2004 UTC
# Line 5  use strict; Line 5  use strict;
5    
6  use Config::IniFiles;  use Config::IniFiles;
7  use DBI;  use DBI;
8    use Carp;
9    
10  use Data::Dumper;  use Data::Dumper;
11    
12  use lib '..';  use lib '..';
13    
14  my $dsn = 'Pg:dbname=libdata';  my @persistent_vars = qw(p ms s);
 my ($user,$passwd) = ('dpavlin','');  
   
 my @persistent_vars = qw(p);  
15    
16  # read global.conf configuration  # read global.conf configuration
17  my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";  my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
18    
19  # configuration options from global.conf  # configuration options from global.conf
20  my $TEMPLATE_PATH = $cfg_global->val('webpac', 'template_html') || die "need template_html in global.conf, section webpac";  my $TEMPLATE_PATH = $cfg_global->val('portal', 'template_html') || die "need template_html in global.conf, section portal";
21  my $CHARSET = $cfg_global->val('webpac', 'charset') || 'ISO-8859-1';  my $CHARSET = $cfg_global->val('portal', 'charset') || 'ISO-8859-1';
22    my $dsn = $cfg_global->val('portal', 'dbi_dsn') || die "need dsn in global.conf, section portal";
23    my ($user,$passwd) = ($cfg_global->val('portal', 'dbi_user'), $cfg_global->val('portal', 'dbi_passwd'));
24    my $locale = $cfg_global->val('portal', 'locale') || 'C';
25    
26    
27  my $dbh = DBI->connect("DBI:$dsn",$user,$passwd, { RaiseError => 1 });  my $dbh = DBI->connect("DBI:".$dsn,$user,$passwd, { RaiseError => 1 });
28    
29  use POSIX qw(locale_h);  use POSIX qw(locale_h);
30  setlocale(LC_CTYPE, "hr_HR");  setlocale(LC_CTYPE, $locale);
31  use locale;  use locale;
32    
33  sub setup {  sub setup {
# Line 33  sub setup { Line 35  sub setup {
35          $self->tmpl_path($TEMPLATE_PATH);          $self->tmpl_path($TEMPLATE_PATH);
36          $self->run_modes(          $self->run_modes(
37                  'home' => 'show_home',                  'home' => 'show_home',
38                  'ms' => 'show_ms',                  'ms' => 'show_mastersubject',
39                  'it' => 'show_home',                  's' => 'show_subject',
40                  's' => 'show_home',                  'r' => 'search_resources',
41          );          );
42          $self->start_mode('home');          $self->start_mode('home');
43          $self->mode_param('p');          $self->mode_param('p');
# Line 44  sub setup { Line 46  sub setup {
46  }  }
47    
48    
49    # home page
50  sub show_home {  sub show_home {
51          my $self = shift;          my $self = shift;
52    
# Line 64  sub show_home { Line 67  sub show_home {
67    
68  }  }
69    
70  sub show_ms {  
71    # MasterSubject
72    sub show_mastersubject {
73          my $self = shift;          my $self = shift;
74    
75          my $q = $self->query();          my $q = $self->query();
# Line 73  sub show_ms { Line 78  sub show_ms {
78    
79          $tmpl->param('MasterSubjects' => $self->get_mastersubjects() );          $tmpl->param('MasterSubjects' => $self->get_mastersubjects() );
80    
81          my $ms = $self->get_mastersubjects_by_id($q->param('ms'));          my $ms = $self->get_mastersubject_by_id($q->param('ms'));
82    
83          $tmpl->param('title' => uc($ms->{'mastersubject'}) );          $tmpl->param('title' => uc($ms->{'mastersubject'}) );
84          $tmpl->param('mastersubject_lc' => lc($ms->{'mastersubject'}) );          $tmpl->param('search_field' => lc($ms->{'mastersubject'}) );
85    
86          $tmpl->param('InfoTypes' => $self->get_infotypes() );          $tmpl->param('InfoTypes' => $self->get_infotypes() );
87    
# Line 86  sub show_ms { Line 91  sub show_ms {
91    
92  }  }
93    
94    
95    # Subject
96    sub show_subject {
97            my $self = shift;
98    
99            my $q = $self->query();
100    
101            my $tmpl = $self->use_template('s.html');
102    
103            $tmpl->param('MasterSubjects' => $self->get_mastersubjects() );
104    
105            my $s = $self->get_subject_by_id($q->param('s'));
106    
107            $tmpl->param('title' => uc($s->{'subject'}) );
108            $tmpl->param('search_field' => lc($s->{'subject'}) );
109    
110            $tmpl->param('InfoTypes' => $self->get_infotypes() );
111    
112            return $tmpl->output;
113    
114    }
115    
116    
117    # search for resources and display results
118    sub search_resources {
119            my $self = shift;
120    
121            my $q = $self->query();
122    
123            my $tmpl = $self->use_template('r.html');
124    
125            $tmpl->param('MasterSubjects' => $self->get_mastersubjects() );
126    
127            if ($q->param('s')) {
128                    my $s = $self->get_subject_by_id($q->param('s'));
129    
130                    $tmpl->param('title' => uc($s->{'subject'}) );
131                    $tmpl->param('search_field' => lc($s->{'subject'}) );
132            } elsif ($q->param('ms')) {
133                    my $s = $self->get_mastersubject_by_id($q->param('ms'));
134    
135                    $tmpl->param('title' => uc($s->{'mastersubject'}) );
136                    $tmpl->param('search_field' => lc($s->{'mastersubject'}) );
137            }
138    
139            my $res = $self->get_resources();
140            $tmpl->param('resource_results' => $res);
141            $tmpl->param('nr_results' => scalar @$res);
142    
143            $tmpl->param('limit_infotype' => $self->get_infotype_by_id($q->param('it'))->{'infotype'});
144    
145            return $tmpl->output;
146    
147    }
148    
149    #
150  # load template and generate permanent valirables in template  # load template and generate permanent valirables in template
151    #
152    
153  sub use_template {  sub use_template {
154          my $self = shift;          my $self = shift;
# Line 105  sub use_template { Line 167  sub use_template {
167          return $tmpl;          return $tmpl;
168  }  }
169    
170    
171    #
172  # get data from database  # get data from database
173    #
174    
175    # get all MasterSubjects
176  sub get_mastersubjects {  sub get_mastersubjects {
177          my $self = shift;          my $self = shift;
178    
# Line 125  sub get_mastersubjects { Line 191  sub get_mastersubjects {
191          return $sth->fetchall_arrayref({});          return $sth->fetchall_arrayref({});
192  }  }
193    
194  sub get_mastersubjects_by_id {  # get one MasterSubject by it's ID
195    sub get_mastersubject_by_id {
196          my $self = shift;          my $self = shift;
197    
198          my $id = shift || croak("need mastersubject id");          my $id = shift || croak("need mastersubject id");
# Line 142  sub get_mastersubjects_by_id { Line 209  sub get_mastersubjects_by_id {
209          return $sth->fetchrow_hashref();          return $sth->fetchrow_hashref();
210  }  }
211    
212    # get all InfoTypes
213  sub get_infotypes {  sub get_infotypes {
214          my $self = shift;          my $self = shift;
215    
# Line 156  sub get_infotypes { Line 224  sub get_infotypes {
224                  where res_sub_infotype.infotype_id=infotype.infotype_id and infotype.infotype_id > 1                  where res_sub_infotype.infotype_id=infotype.infotype_id and infotype.infotype_id > 1
225          };          };
226    
227          if ($q->param('ms')) {  
228            # first check if subject is defined and limit by that, and only if it's not
229            # fallback to mastersubject
230            if ($q->param('s')) {
231                    $sql .= qq{
232                            and res_sub_infotype.subject_id = ?
233                    };
234                    push @args, $q->param('s');
235            } elsif ($q->param('ms')) {
236                  $sql .= qq{                  $sql .= qq{
237                          and res_sub_infotype.subject_id in                          and res_sub_infotype.subject_id in
238                                  (select subject_id from sub_mastersubject where mastersubject_id = ?)                                  (select subject_id from sub_mastersubject where mastersubject_id = ?)
# Line 179  sub get_infotypes { Line 255  sub get_infotypes {
255          return $arr;          return $arr;
256  }  }
257    
258    # get first letters for all Subjects
259  sub get_subjects_letters {  sub get_subjects_letters {
260          my $self = shift;          my $self = shift;
261    
# Line 195  sub get_subjects_letters { Line 272  sub get_subjects_letters {
272          return $sth->fetchall_arrayref({});          return $sth->fetchall_arrayref({});
273  }  }
274    
275    # get all Subjects
276  sub get_subjects {  sub get_subjects {
277          my $self = shift;          my $self = shift;
278    
# Line 232  sub get_subjects { Line 310  sub get_subjects {
310          return $sth->fetchall_arrayref({});          return $sth->fetchall_arrayref({});
311  }  }
312    
313    # get one Subject by it's ID
314    sub get_subject_by_id {
315            my $self = shift;
316    
317            my $id = shift || croak("need subject id");
318    
319            my $sql = qq{
320            select subject
321                    from subject
322                    where subject_id = ?
323            };
324    
325            my $sth = $dbh->prepare($sql);
326            $sth->execute($id);
327    
328            return $sth->fetchrow_hashref();
329    }
330    
331    # get one InfoType by it's ID
332    sub get_infotype_by_id {
333            my $self = shift;
334    
335            my $id = shift || croak("need infotype id");
336    
337            my $sql = qq{
338            select infotype
339                    from infotype
340                    where infotype_id = ?
341            };
342    
343            my $sth = $dbh->prepare($sql);
344            $sth->execute($id);
345    
346            return $sth->fetchrow_hashref();
347    }
348    
349    # get add resources for given criteria
350    sub get_resources {
351            my $self = shift;
352    
353            my $q = $self->query();
354            my @args;
355    
356            my $sql = qq{
357                    select distinct resource.resource_id, title, infotype.infotype, coverage_detail, url
358            };
359    
360            my $sql_from = qq{
361                            from resource,infotype
362            };
363    
364            my $sql_where = qq{
365                            where resource.infotype_id=infotype.infotype_id
366            };
367    
368            # limits
369            if ($q->param('s')) {
370                    $sql_from .= qq{ , res_sub_infotype };
371                    $sql_where .= qq{
372                            and res_sub_infotype.resource_id = resource.resource_id
373                            and res_sub_infotype.subject_id = ?
374                    };
375                    push @args, $q->param('s');
376            } elsif ($q->param('ms')) {
377                    $sql_from .= qq{ , res_sub_infotype };
378                    $sql_where .= qq{
379                            and res_sub_infotype.resource_id = resource.resource_id
380                            and res_sub_infotype.subject_id in
381                                    (select subject_id from sub_mastersubject where mastersubject_id = ?)
382                    };
383                    push @args, $q->param('ms');
384            }
385            if ($q->param('it')) {
386                    if ($sql_from !~ m/res_sub_infotype/) {
387                            $sql_from .= qq{ , res_sub_infotype };
388                            $sql_where .= qq{ and res_sub_infotype.resource_id = resource.resource_id };
389                    }
390                    $sql_where .= qq{ and res_sub_infotype.infotype_id = ? };
391                    push @args, $q->param('it');
392            }
393    
394            my $sth = $dbh->prepare($sql.$sql_from.$sql_where);
395            $sth->execute(@args);
396    
397            my $arr = $sth->fetchall_arrayref({});
398    
399            # now fill-in features
400            $sql = qq{
401                    select feature
402                            from res_feature,feature
403                            where res_feature.feature_id = feature.feature_id and res_feature.resource_id = ?
404            };
405            $sth = $dbh->prepare($sql);
406    
407            foreach my $i ( 0 .. (scalar @$arr)-1 ) {
408                    $sth->execute($arr->[$i]->{'resource_id'});
409                    $arr->[$i]->{'res_features'} = $sth->fetchall_arrayref({});
410            }
411    
412            return $arr;
413    }
414  1;  1;

Legend:
Removed from v.1  
changed lines
  Added in v.11

  ViewVC Help
Powered by ViewVC 1.1.26