/[libdata-portal]/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

Contents of /trunk/Portal.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations)
Sun Mar 7 20:40:47 2004 UTC (20 years, 1 month ago) by dpavlin
File size: 8903 byte(s)
added results page (search isn't implemented yet!)

1 package Portal;
2
3 use base 'CGI::Application';
4 use strict;
5
6 use Config::IniFiles;
7 use DBI;
8 use Carp;
9
10 use Data::Dumper;
11
12 use lib '..';
13
14 my $dsn = 'Pg:dbname=libdata';
15 my ($user,$passwd) = ('dpavlin','');
16
17 my @persistent_vars = qw(p ms s);
18
19 # read global.conf configuration
20 my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
21
22 # configuration options from global.conf
23 my $TEMPLATE_PATH = $cfg_global->val('webpac', 'template_html') || die "need template_html in global.conf, section webpac";
24 my $CHARSET = $cfg_global->val('webpac', 'charset') || 'ISO-8859-1';
25
26 my $dbh = DBI->connect("DBI:$dsn",$user,$passwd, { RaiseError => 1 });
27
28 use POSIX qw(locale_h);
29 setlocale(LC_CTYPE, "hr_HR");
30 use locale;
31
32 sub setup {
33 my $self = shift;
34 $self->tmpl_path($TEMPLATE_PATH);
35 $self->run_modes(
36 'home' => 'show_home',
37 'ms' => 'show_mastersubject',
38 's' => 'show_subject',
39 'r' => 'search_resources',
40 );
41 $self->start_mode('home');
42 $self->mode_param('p');
43
44 $self->header_props(-charset=>$CHARSET);
45 }
46
47
48 # home page
49 sub show_home {
50 my $self = shift;
51
52 # Get the CGI.pm query object
53 my $q = $self->query();
54 # template
55
56 # read master template
57 my $tmpl = $self->use_template('home.html');
58
59 $tmpl->param('MasterSubjects' => $self->get_mastersubjects() );
60 $tmpl->param('InfoTypes' => $self->get_infotypes() );
61
62 $tmpl->param('Subjects_letters' => $self->get_subjects_letters() );
63 $tmpl->param('Subjects' => $self->get_subjects() );
64
65 return $tmpl->output;
66
67 }
68
69
70 # MasterSubject
71 sub show_mastersubject {
72 my $self = shift;
73
74 my $q = $self->query();
75
76 my $tmpl = $self->use_template('ms.html');
77
78 $tmpl->param('MasterSubjects' => $self->get_mastersubjects() );
79
80 my $ms = $self->get_mastersubject_by_id($q->param('ms'));
81
82 $tmpl->param('title' => uc($ms->{'mastersubject'}) );
83 $tmpl->param('search_field' => lc($ms->{'mastersubject'}) );
84
85 $tmpl->param('InfoTypes' => $self->get_infotypes() );
86
87 $tmpl->param('Subjects' => $self->get_subjects() );
88
89 return $tmpl->output;
90
91 }
92
93
94 # Subject
95 sub show_subject {
96 my $self = shift;
97
98 my $q = $self->query();
99
100 my $tmpl = $self->use_template('s.html');
101
102 $tmpl->param('MasterSubjects' => $self->get_mastersubjects() );
103
104 my $s = $self->get_subject_by_id($q->param('s'));
105
106 $tmpl->param('title' => uc($s->{'subject'}) );
107 $tmpl->param('search_field' => lc($s->{'subject'}) );
108
109 $tmpl->param('InfoTypes' => $self->get_infotypes() );
110
111 return $tmpl->output;
112
113 }
114
115
116 # search for resources and display results
117 sub search_resources {
118 my $self = shift;
119
120 my $q = $self->query();
121
122 my $tmpl = $self->use_template('r.html');
123
124 $tmpl->param('MasterSubjects' => $self->get_mastersubjects() );
125
126 if ($q->param('s')) {
127 my $s = $self->get_subject_by_id($q->param('s'));
128
129 $tmpl->param('title' => uc($s->{'subject'}) );
130 $tmpl->param('search_field' => lc($s->{'subject'}) );
131 } elsif ($q->param('ms')) {
132 my $s = $self->get_mastersubject_by_id($q->param('ms'));
133
134 $tmpl->param('title' => uc($s->{'mastersubject'}) );
135 $tmpl->param('search_field' => lc($s->{'mastersubject'}) );
136 }
137
138 my $res = $self->get_resources();
139 $tmpl->param('resource_results' => $res);
140 $tmpl->param('nr_results' => scalar @$res);
141
142 $tmpl->param('limit_infotype' => $self->get_infotype_by_id($q->param('it'))->{'infotype'});
143
144 return $tmpl->output;
145
146 }
147
148 #
149 # load template and generate permanent valirables in template
150 #
151
152 sub use_template {
153 my $self = shift;
154 my $q = $self->query();
155
156 my $tmpl_file = shift || croak("perm_vars need tempate file");
157 my $tmpl = $self->load_tmpl($tmpl_file, global_vars => 1, die_on_bad_params => 0);
158
159 $tmpl->param('self_url_full', $q->url(-relative=>1,-query=>1));
160 $tmpl->param('self_url', $q->url(-relative=>1));
161
162 foreach my $var (@persistent_vars) {
163 $tmpl->param($var, $q->param($var));
164 }
165
166 return $tmpl;
167 }
168
169
170 #
171 # get data from database
172 #
173
174 # get all MasterSubjects
175 sub get_mastersubjects {
176 my $self = shift;
177
178 my $q = $self->query();
179
180 my $sql = qq{
181 select mastersubject_id,upper(mastersubject) as mastersubject,(mastersubject_id = ?) as selected
182 from mastersubject
183 where mastersubject_id > 2
184 order by mastersubject
185 };
186
187 my $sth = $dbh->prepare($sql);
188 $sth->execute($q->param('ms') || undef);
189
190 return $sth->fetchall_arrayref({});
191 }
192
193 # get one MasterSubject by it's ID
194 sub get_mastersubject_by_id {
195 my $self = shift;
196
197 my $id = shift || croak("need mastersubject id");
198
199 my $sql = qq{
200 select mastersubject
201 from mastersubject
202 where mastersubject_id = ?
203 };
204
205 my $sth = $dbh->prepare($sql);
206 $sth->execute($id);
207
208 return $sth->fetchrow_hashref();
209 }
210
211 # get all InfoTypes
212 sub get_infotypes {
213 my $self = shift;
214
215 my $q = $self->query();
216 my @args;
217
218 push @args,$q->param('it') || undef; # for selected
219
220 my $sql = qq{
221 select distinct infotype.infotype_id,infotype.infotype, 0 as half, (infotype.infotype_id = ?) as selected
222 from res_sub_infotype,infotype
223 where res_sub_infotype.infotype_id=infotype.infotype_id and infotype.infotype_id > 1
224 };
225
226
227 # first check if subject is defined and limit by that, and only if it's not
228 # fallback to mastersubject
229 if ($q->param('s')) {
230 $sql .= qq{
231 and res_sub_infotype.subject_id = ?
232 };
233 push @args, $q->param('s');
234 } elsif ($q->param('ms')) {
235 $sql .= qq{
236 and res_sub_infotype.subject_id in
237 (select subject_id from sub_mastersubject where mastersubject_id = ?)
238 };
239 push @args, $q->param('ms');
240 }
241
242 $sql .= qq{
243 order by infotype
244 };
245
246 my $sth = $dbh->prepare($sql);
247 $sth->execute(@args);
248
249 my $arr = $sth->fetchall_arrayref({});
250
251 # find element which is on half of list
252 my $half = int(scalar @$arr / 2) - 1;
253 $arr->[$half]->{half} = 1 if ($half > 0);
254 return $arr;
255 }
256
257 # get first letters for all Subjects
258 sub get_subjects_letters {
259 my $self = shift;
260
261 my $sql = qq{
262 select distinct substr(subject,1,1) as letter
263 from subject
264 where subject_id > 1
265 order by substr(subject,1,1)
266 };
267
268 my $sth = $dbh->prepare($sql);
269 $sth->execute();
270
271 return $sth->fetchall_arrayref({});
272 }
273
274 # get all Subjects
275 sub get_subjects {
276 my $self = shift;
277
278 my $q = $self->query();
279 my @args;
280
281 my $sql = qq{
282 select subject.subject_id,subject.subject,sub_mastersubject.mastersubject_id
283 from subject,sub_mastersubject
284 where subject.subject_id=sub_mastersubject.subject_id
285 and subject.subject_id > 1
286 };
287
288 if ($q->param('s_letter')) {
289 push @args,$q->param('s_letter') . '%';
290 $sql .= qq{
291 and upper(subject.subject) like upper(?)
292 };
293 }
294
295 if ($q->param('ms')) {
296 push @args,$q->param('ms');
297 $sql .= qq{
298 and sub_mastersubject.mastersubject_id = ?
299 };
300 }
301
302 $sql .= qq{
303 order by subject.subject
304 };
305
306 my $sth = $dbh->prepare($sql);
307 $sth->execute(@args);
308
309 return $sth->fetchall_arrayref({});
310 }
311
312 # get one Subject by it's ID
313 sub get_subject_by_id {
314 my $self = shift;
315
316 my $id = shift || croak("need subject id");
317
318 my $sql = qq{
319 select subject
320 from subject
321 where subject_id = ?
322 };
323
324 my $sth = $dbh->prepare($sql);
325 $sth->execute($id);
326
327 return $sth->fetchrow_hashref();
328 }
329
330 # get one InfoType by it's ID
331 sub get_infotype_by_id {
332 my $self = shift;
333
334 my $id = shift || croak("need infotype id");
335
336 my $sql = qq{
337 select infotype
338 from infotype
339 where infotype_id = ?
340 };
341
342 my $sth = $dbh->prepare($sql);
343 $sth->execute($id);
344
345 return $sth->fetchrow_hashref();
346 }
347
348 # get add resources for given criteria
349 sub get_resources {
350 my $self = shift;
351
352 my $q = $self->query();
353 my @args;
354
355 my $sql = qq{
356 select distinct resource.resource_id, title, infotype.infotype, coverage_detail, url
357 };
358
359 my $sql_from = qq{
360 from resource,infotype
361 };
362
363 my $sql_where = qq{
364 where resource.infotype_id=infotype.infotype_id
365 };
366
367 # limits
368 if ($q->param('s')) {
369 $sql_from .= qq{ , res_sub_infotype };
370 $sql_where .= qq{
371 and res_sub_infotype.resource_id = resource.resource_id
372 and res_sub_infotype.subject_id = ?
373 };
374 push @args, $q->param('s');
375 } elsif ($q->param('ms')) {
376 $sql_from .= qq{ , res_sub_infotype };
377 $sql_where .= qq{
378 and res_sub_infotype.resource_id = resource.resource_id
379 and res_sub_infotype.subject_id in
380 (select subject_id from sub_mastersubject where mastersubject_id = ?)
381 };
382 push @args, $q->param('ms');
383 }
384 if ($q->param('it')) {
385 if ($sql_from !~ m/res_sub_infotype/) {
386 $sql_from .= qq{ , res_sub_infotype };
387 $sql_where .= qq{ and res_sub_infotype.resource_id = resource.resource_id };
388 }
389 $sql_where .= qq{ and res_sub_infotype.infotype_id = ? };
390 push @args, $q->param('it');
391 }
392
393 my $sth = $dbh->prepare($sql.$sql_from.$sql_where);
394 $sth->execute(@args);
395
396 my $arr = $sth->fetchall_arrayref({});
397
398 # now fill-in features
399 $sql = qq{
400 select feature
401 from res_feature,feature
402 where res_feature.feature_id = feature.feature_id and res_feature.resource_id = ?
403 };
404 $sth = $dbh->prepare($sql);
405
406 foreach my $i ( 0 .. (scalar @$arr)-1 ) {
407 $sth->execute($arr->[$i]->{'resource_id'});
408 $arr->[$i]->{'res_features'} = $sth->fetchall_arrayref({});
409 }
410
411 return $arr;
412 }
413 1;

  ViewVC Help
Powered by ViewVC 1.1.26