/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations)
Sun Mar 7 22:07:47 2004 UTC (20 years, 1 month ago) by dpavlin
File size: 9795 byte(s)
hacked version to use MySQL

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 @persistent_vars = qw(p ms s);
15
16 # read global.conf configuration
17 my $cfg_global = new Config::IniFiles( -file => '../global.conf' ) || die "can't open 'global.conf'";
18
19 # configuration options from global.conf
20 my $TEMPLATE_PATH = $cfg_global->val('portal', 'template_html') || die "need template_html in global.conf, section portal";
21 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 });
28
29 use POSIX qw(locale_h);
30 setlocale(LC_CTYPE, $locale);
31 use locale;
32
33 sub setup {
34 my $self = shift;
35 $self->tmpl_path($TEMPLATE_PATH);
36 $self->run_modes(
37 'home' => 'show_home',
38 'ms' => 'show_mastersubject',
39 's' => 'show_subject',
40 'r' => 'search_resources',
41 );
42 $self->start_mode('home');
43 $self->mode_param('p');
44
45 $self->header_props(-charset=>$CHARSET);
46 }
47
48
49 # home page
50 sub show_home {
51 my $self = shift;
52
53 # Get the CGI.pm query object
54 my $q = $self->query();
55 # template
56
57 # read master template
58 my $tmpl = $self->use_template('home.html');
59
60 $tmpl->param('MasterSubjects' => $self->get_mastersubjects() );
61 $tmpl->param('InfoTypes' => $self->get_infotypes() );
62
63 $tmpl->param('Subjects_letters' => $self->get_subjects_letters() );
64 $tmpl->param('Subjects' => $self->get_subjects() );
65
66 return $tmpl->output;
67
68 }
69
70
71 # MasterSubject
72 sub show_mastersubject {
73 my $self = shift;
74
75 my $q = $self->query();
76
77 my $tmpl = $self->use_template('ms.html');
78
79 $tmpl->param('MasterSubjects' => $self->get_mastersubjects() );
80
81 my $ms = $self->get_mastersubject_by_id($q->param('ms'));
82
83 $tmpl->param('title' => uc($ms->{'mastersubject'}) );
84 $tmpl->param('search_field' => lc($ms->{'mastersubject'}) );
85
86 $tmpl->param('InfoTypes' => $self->get_infotypes() );
87
88 $tmpl->param('Subjects' => $self->get_subjects() );
89
90 return $tmpl->output;
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
151 #
152
153 sub use_template {
154 my $self = shift;
155 my $q = $self->query();
156
157 my $tmpl_file = shift || croak("perm_vars need tempate file");
158 my $tmpl = $self->load_tmpl($tmpl_file, global_vars => 1, die_on_bad_params => 0);
159
160 $tmpl->param('self_url_full', $q->url(-relative=>1,-query=>1));
161 $tmpl->param('self_url', $q->url(-relative=>1));
162
163 foreach my $var (@persistent_vars) {
164 $tmpl->param($var, $q->param($var));
165 }
166
167 return $tmpl;
168 }
169
170
171 #
172 # get data from database
173 #
174
175 # get all MasterSubjects
176 sub get_mastersubjects {
177 my $self = shift;
178
179 my $q = $self->query();
180
181 my $sql = qq{
182 select mastersubject_id,upper(mastersubject) as mastersubject
183 from mastersubject
184 where mastersubject_id > 2
185 order by mastersubject
186 };
187
188 my $sth = $dbh->prepare($sql);
189 $sth->execute();
190 my $arr = $sth->fetchall_arrayref({});
191
192 return if (! $arr);
193 return $arr if (! $q->param('ms'));
194
195 # add selected column
196 foreach my $i ( 0 .. (scalar @$arr)-1 ) {
197 $arr->[$i]->{'selected'} = ($arr->[$i]->{'mastersubject_id'} == $q->param('ms'));
198 }
199 return $arr;
200 }
201
202 # get one MasterSubject by it's ID
203 sub get_mastersubject_by_id {
204 my $self = shift;
205
206 my $id = shift || croak("need mastersubject id");
207
208 my $sql = qq{
209 select mastersubject
210 from mastersubject
211 where mastersubject_id = ?
212 };
213
214 my $sth = $dbh->prepare($sql);
215 $sth->execute($id);
216
217 return $sth->fetchrow_hashref();
218 }
219
220 # WARNING: Cludge ahead!
221 #
222 # stupid MySQL doesn't support subselects.
223 #
224 sub select_subject_id_where_mastersubject_id {
225 my $id = shift || croak("sub select emulation needs mastersubject_id");
226 my $sql = qq{ select subject_id from sub_mastersubject where mastersubject_id = ? };
227
228 my $sth = $dbh->prepare($sql);
229 $sth->execute($id);
230
231 my $arr = $sth->fetchall_hashref('subject_id');
232
233 $sql = join(",",keys %$arr);
234 $sql = "null" if (! $sql);
235 return $sql;
236 }
237
238
239 # get all InfoTypes
240 sub get_infotypes {
241 my $self = shift;
242
243 my $q = $self->query();
244 my @args;
245
246 my $sql = qq{
247 select distinct infotype.infotype_id,infotype.infotype, 0 as half
248 from res_sub_infotype,infotype
249 where res_sub_infotype.infotype_id=infotype.infotype_id and infotype.infotype_id > 1
250 };
251
252
253 # first check if subject is defined and limit by that, and only if it's not
254 # fallback to mastersubject
255 if ($q->param('s')) {
256 $sql .= qq{
257 and res_sub_infotype.subject_id = ?
258 };
259 push @args, $q->param('s');
260 } elsif ($q->param('ms')) {
261 my $ss_sql = select_subject_id_where_mastersubject_id($q->param('ms'));
262 $sql .= qq{ and res_sub_infotype.subject_id in ($ss_sql) };
263 }
264
265 $sql .= qq{
266 order by infotype
267 };
268
269 my $sth = $dbh->prepare($sql);
270 $sth->execute(@args);
271
272 my $arr = $sth->fetchall_arrayref({});
273
274 # find element which is on half of list
275 my $half = int(scalar @$arr / 2) - 1;
276 $arr->[$half]->{half} = 1 if ($half > 0);
277
278 return $arr if (! $q->param('it'));
279
280 # add selected column
281 foreach my $i ( 0 .. (scalar @$arr)-1 ) {
282 $arr->[$i]->{'selected'} = ($arr->[$i]->{'infotype_id'} == $q->param('it'));
283 }
284 return $arr;
285 }
286
287 # get first letters for all Subjects
288 sub get_subjects_letters {
289 my $self = shift;
290
291 my $sql = qq{
292 select distinct substring(subject from 1 for 1) as letter
293 from subject
294 where subject_id > 1
295 order by substring(subject from 1 for 1)
296 };
297
298 my $sth = $dbh->prepare($sql);
299 $sth->execute();
300
301 return $sth->fetchall_arrayref({});
302 }
303
304 # get all Subjects
305 sub get_subjects {
306 my $self = shift;
307
308 my $q = $self->query();
309 my @args;
310
311 my $sql = qq{
312 select subject.subject_id,subject.subject,sub_mastersubject.mastersubject_id
313 from subject,sub_mastersubject
314 where subject.subject_id=sub_mastersubject.subject_id
315 and subject.subject_id > 1
316 };
317
318 if ($q->param('s_letter')) {
319 push @args,$q->param('s_letter') . '%';
320 $sql .= qq{
321 and upper(subject.subject) like upper(?)
322 };
323 }
324
325 if ($q->param('ms')) {
326 push @args,$q->param('ms');
327 $sql .= qq{
328 and sub_mastersubject.mastersubject_id = ?
329 };
330 }
331
332 $sql .= qq{
333 order by subject.subject
334 };
335
336 my $sth = $dbh->prepare($sql);
337 $sth->execute(@args);
338
339 return $sth->fetchall_arrayref({});
340 }
341
342 # get one Subject by it's ID
343 sub get_subject_by_id {
344 my $self = shift;
345
346 my $id = shift || croak("need subject id");
347
348 my $sql = qq{
349 select subject
350 from subject
351 where subject_id = ?
352 };
353
354 my $sth = $dbh->prepare($sql);
355 $sth->execute($id);
356
357 return $sth->fetchrow_hashref();
358 }
359
360 # get one InfoType by it's ID
361 sub get_infotype_by_id {
362 my $self = shift;
363
364 my $id = shift || croak("need infotype id");
365
366 my $sql = qq{
367 select infotype
368 from infotype
369 where infotype_id = ?
370 };
371
372 my $sth = $dbh->prepare($sql);
373 $sth->execute($id);
374
375 return $sth->fetchrow_hashref();
376 }
377
378 # get add resources for given criteria
379 sub get_resources {
380 my $self = shift;
381
382 my $q = $self->query();
383 my @args;
384
385 my $sql = qq{
386 select distinct resource.resource_id, title, infotype.infotype, coverage_detail, url
387 };
388
389 my $sql_from = qq{
390 from resource,infotype
391 };
392
393 my $sql_where = qq{
394 where resource.infotype_id=infotype.infotype_id
395 };
396
397 # limits
398 if ($q->param('s')) {
399 $sql_from .= qq{ , res_sub_infotype };
400 $sql_where .= qq{
401 and res_sub_infotype.resource_id = resource.resource_id
402 and res_sub_infotype.subject_id = ?
403 };
404 push @args, $q->param('s');
405 } elsif ($q->param('ms')) {
406 $sql_from .= qq{ , res_sub_infotype };
407 my $ss_sql = select_subject_id_where_mastersubject_id($q->param('ms'));
408 $sql_where .= qq{
409 and res_sub_infotype.resource_id = resource.resource_id
410 and res_sub_infotype.subject_id in ($ss_sql)
411 };
412 }
413 if ($q->param('it')) {
414 if ($sql_from !~ m/res_sub_infotype/) {
415 $sql_from .= qq{ , res_sub_infotype };
416 $sql_where .= qq{ and res_sub_infotype.resource_id = resource.resource_id };
417 }
418 $sql_where .= qq{ and res_sub_infotype.infotype_id = ? };
419 push @args, $q->param('it');
420 }
421
422 my $sth = $dbh->prepare($sql.$sql_from.$sql_where);
423 $sth->execute(@args);
424
425 my $arr = $sth->fetchall_arrayref({});
426
427 # now fill-in features
428 $sql = qq{
429 select feature
430 from res_feature,feature
431 where res_feature.feature_id = feature.feature_id and res_feature.resource_id = ?
432 };
433 $sth = $dbh->prepare($sql);
434
435 foreach my $i ( 0 .. (scalar @$arr)-1 ) {
436 $sth->execute($arr->[$i]->{'resource_id'});
437 $arr->[$i]->{'res_features'} = $sth->fetchall_arrayref({});
438 }
439
440 return $arr;
441 }
442 1;

  ViewVC Help
Powered by ViewVC 1.1.26