Debian Codegraph @

eg/web/db/epwebapp.pl

raw
1
2
3 use DBIx::Recordset ;
4 use Data::Dumper ;
5 use Embperl::Mail ;
6 use File::Basename ;
7 use Embperl::Form::Validate;
8
9 BEGIN { Execute ({isa => '../epwebapp.pl', syntax => 'Perl'}) ; }
10
11
12 sub init
13 {
14 my $self = shift ;
15 my $r = shift ;
16
17 my $ret ;
18
19 $r -> {error} = $fdat{-error} ;
20 $r -> {success} = $fdat{-success} ;
21
22 $self -> SUPER::init ($r) ;
23
24 $self -> initdb ($r) ;
25
26 my $db = $r -> {db} ;
27
28 $r->{warning} = [];
29
30
31 my $login = $self -> checkuser ($r) ;
32 if ($config->{always_need_login} && $login < 1)
33 {
34 $r -> {need_login} = 1 ;
35 return ;
36 }
37 return 0 if ($r->{done}) ;
38
39 # warn "fdat = ", Data::Dumper->Dump ([\%fdat]);
40
41 $r -> {language_set} = DBIx::Recordset -> Search ({'!DataSource' => $db,
42 '!Table' => 'language'}) ;
43
44 if ($fdat{-add_category})
45 {
46 $self -> add_category($r) ;
47 $self -> get_category($r, 2) ;
48 }
49 elsif ($fdat{-add_item})
50 {
51 $self -> get_category($r, 2) ;
52 $ret = $self -> add_item($r) ;
53 }
54 elsif ($fdat{-update_item})
55 {
56 $self -> get_category($r, 2) ;
57 $ret = $self -> update_item ($r) ;
58 }
59 elsif ($fdat{-delete_item})
60 {
61 $self -> get_category($r, 2) ;
62 $ret = $self -> delete_item($r) ;
63 }
64 elsif ($fdat{-edit_item})
65 {
66 $self -> get_category($r, 2) ;
67 $self -> get_item_lang($r) ;
68 $self -> setup_edit_item($r) ;
69 }
70 elsif ($fdat{-show_item})
71 {
72 $self -> get_category($r, 2) ;
73 $self -> get_item_lang($r) ;
74 }
75 elsif ($fdat{-update_user})
76 {
77 $self -> update_user($r) ;
78 }
79 else
80 {
81 $self -> get_category($r) ;
82 $self -> get_item($r) ;
83 #$self -> get_user($r);
84 }
85
86
87 #d# if ($r->param->uri =~ m|/user\.epl$|)
88 #d# {
89 # $self -> get_users($r) if $r->{user_admin};
90 # }
91
92 return defined ($ret)?$ret:0 ;
93 }
94
95
96 # ----------------------------------------------------------------------------
97
98 sub initdb
99 {
100 my $self = shift ;
101 my $r = shift ;
102 my $config = $r -> {config} ;
103
104 $DBIx::Recordset::Debug = $config -> {dbdebug} || 1 ;
105 *DBIx::Recordset::LOG = \*STDERR ;
106 my $db = DBIx::Database -> new ({'!DataSource' => $config -> {dbdsn},
107 '!Username' => $config -> {dbuser},
108 '!Password' => $config -> {dbpassword},
109 '!DBIAttr' => { RaiseError => 1, PrintError => 1, LongReadLen => 32765, LongTruncOk => 0, },
110
111 }) ;
112
113 $db -> TableAttr ('*', '!SeqClass', "DBIx::Recordset::FileSeq,$config->{root}/db") if ($^O eq 'MSWin32') ;
114 $db -> TableAttr ('*', '!PrimKey', 'id') ;
115 $db -> TableAttr ('*', '!Filter',
116 {
117 'creationtime' => [\&current_time, undef, DBIx::Recordset::rqINSERT ],
118 'modtime' => [\&current_time, undef, DBIx::Recordset::rqINSERT + DBIx::Recordset::rqUPDATE ],
119 }) ;
120
121 $r -> {db} = $db ;
122
123 }
124
125 # ----------------------------------------------------------------------------
126
127 sub current_time
128
129 {
130 return $_[0] if ($_[0]) ;
131
132 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
133 localtime(time);
134
135 $mon++ ;
136 $year += 1900 ;
137 return "$year-$mon-$mday $hour:$min:$sec" ;
138 }
139
140
141
142 # ----------------------------------------------------------------------------
143 #
144 # Get url for postings forms
145 #
146 # $dest = path relativ to current uri
147 #
148
149 sub posturl
150 {
151 my ($self, $dest) = @_ ;
152
153 my $r = $self -> curr_req ;
154
155 return $dest if (!$r -> {action_prefix}) ;
156
157 my $config = $r->{config} ;
158 my $buri = $config -> {baseuri} ;
159 $buri .= '/' if (!$buri =~ m#/$#) ;
160 my $uri = $r-> param -> uri ;
161 my $path = ($uri =~ /\Q$buri\E(.*?)$/)?$1:$uri ;
162 my $lang = (@{$config -> {supported_languages}} > 1)?$r -> param -> language . '/':'' ;
163
164 my $url ;
165 if (!$dest)
166 {
167 $url = $r -> {action_prefix} . $buri . $lang . $path ;
168 }
169 else
170 {
171 $path =~ m#^/?(.*)/# ;
172 my $dir = $1 ;
173 $url = $r -> {action_prefix} . $buri . $lang . $dir . '/' . $dest ;
174 }
175
176 return $url ;
177 }
178
179
180
181 # ----------------------------------------------------------------------------
182 #
183 # check if user is loged in, handle login/out and createing of new users
184 #
185 # allowed actions parameters:
186 # -login
187 # -logout
188 # -newuser
189 # -newpassword
190 # formfields expected:
191 # user_email
192 # user_password
193 #
194 # returns:
195 # undef not logged in
196 # 1 user logged in
197 # 2 admin logged in
198 #
199
200 sub checkuser_light
201 {
202 my $self = shift ;
203 my $r = shift ;
204
205 if ($udat{user_id} && $udat{user_email} && !$fdat{-logout})
206 {
207 $r -> {user_id} = $udat{user_id} ;
208 $r -> {user_email} = $udat{user_email} ;
209 $r -> {user_name} = $udat{user_name} ;
210 $r -> {user_admin} = $udat{user_admin} ;
211 return $r -> {user_admin}?2:1 ;
212 }
213 return 0;
214 }
215
216 sub checkuser
217 {
218 my $self = shift ;
219 my $r = shift ;
220
221
222 if ($udat{user_id} && $udat{user_email} && !$fdat{-logout})
223 {
224 $r -> {user_id} = $udat{user_id} ;
225 $r -> {user_email} = $udat{user_email} ;
226 $r -> {user_name} = $udat{user_name} ;
227 $r -> {user_admin} = $udat{user_admin} ;
228 return $r -> {user_admin}?2:1 ;
229 }
230
231 if (($fdat{-login} || $fdat{-newuser} || $fdat{-newpassword})
232 && !$fdat{user_email})
233 {
234 $r -> {error} = 'err_email_needed' ;
235 return ;
236 }
237
238 my $user ;
239
240 if ($fdat{user_email})
241 {
242 $user = DBIx::Recordset -> Search ({'!DataSource' => $r -> {db},
243 '!Table' => 'user',
244 'email' => $fdat{user_email}}) ;
245 }
246
247 if ($fdat{-login} && $fdat{user_email})
248 {
249 if ($user -> {id} && $user -> {password} eq $fdat{user_password})
250 {
251 $r -> {user_id} = $udat{user_id} = $user -> {id} ;
252 $r -> {user_email} = $udat{user_email} = $user -> {email} ;
253 $r -> {user_name} = $udat{user_name} = $user -> {user_name} ;
254 $r -> {user_admin} = $udat{user_admin} = $user -> {admin} ;
255 $r -> {success} = "suc_login";
256 return $r -> {user_admin}?2:1 ;
257 }
258
259 $r -> {error} = 'err_access_denied' ;
260 $r -> {need_login} = 1 ;
261 return ;
262 }
263
264 if ($fdat{-logout})
265 {
266 $r -> {user_id} = $udat{user_id} = undef ;
267 $r -> {user_email} = $udat{user_email} = undef ;
268 $r -> {user_name} = $udat{user_name} = undef ;
269 $r -> {user_admin} = $udat{user_admin} = undef ;
270 $r -> {success} = 'suc_logout';
271 return ;
272 }
273
274 if ($fdat{-newuser} && $user -> {id})
275 {
276 $r -> {error} = 'err_user_exists';
277 return ;
278 }
279
280 if ($fdat{-newpassword} && !$user -> {id})
281 {
282 $r -> {error} = 'err_user_not_exists' ;
283 return ;
284 }
285
286 my $user_password = '' ;
287 if ($fdat{-newuser} || $fdat{-newpassword})
288 {
289 my $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-+$!#*=@1234567890-+$!#*=@' ;
290 for (my $i = 0; $i < 6; $i++)
291 {
292 $user_password .= substr($chars, rand(length($chars)), 1) ;
293 }
294 }
295
296
297 if ($fdat{-newuser} && $fdat{user_email})
298 {
299 my @errors_user = ();
300 my @errors_admin = ();
301 my $set = DBIx::Recordset -> Insert ({'!DataSource' => $r -> {db},
302 '!Table' => 'user',
303 'user_name' => $fdat{user_name},
304 'password' => $user_password,
305 'email' => $fdat{user_email}}) ;
306 if (DBIx::Recordset -> LastError)
307 {
308 $r -> {error} = 'err_db';
309 $r -> {error_details} = DBIx::Recordset -> LastError;
310 }
311
312 my $usermail = Embperl::Mail::Execute ({
313 inputfile => 'newuser.mail',
314 from => $r->{config}->{emailfrom},
315 to => $fdat{user_email},
316 subject => $r->gettext('mail_subj_newuser'),
317 param => [$user_password],
318 errors => \@errors_user});
319 if ($usermail)
320 {
321 $r->{error} = 'err_user_mail';
322 $r->{error_details} = join("\n",@errors_user);
323 }
324 else
325 {
326 $r->{success} = 'suc_password_sent';
327 }
328
329 my $adminmail = Embperl::Mail::Execute ({
330 inputfile => 'newuser.admin.mail',
331 from => $r->{config}->{emailfrom},
332 to => $r->{config}->{adminemail},
333 subject => ($r->{error} ?
334 "Error while creating new website user '$fdat{user_email}'" :
335 "New website user: $fdat{user_email}"),
336 errors => \@errors_admin});
337
338 if ($adminmail)
339 {
340 $r->{error} = 'err_user_admin_mail';
341 $r->{error_details} = join('; ',@errors_admin);
342 }
343
344 $r -> {done} = 1 ;
345 $r -> {need_login} = 1 ;
346
347 return ;
348 }
349
350 if ($fdat{-newpassword} && $fdat{user_email})
351 {
352 my @errors_pw;
353 my $set = DBIx::Recordset -> Update ({'!DataSource' => $r -> {db},
354 '!Table' => 'user',
355 'password' => $user_password,
356 'email' => $fdat{user_email}},
357 {'id' => $user -> {id}}) ;
358
359 my $newpw_mail = Embperl::Mail::Execute ({
360 inputfile => 'newpw.mail',
361 from => $r->{config}->{emailfrom},
362 to => $fdat{user_email},
363 subject => $r->gettext('mail_subj_newpw'),
364 param => [$user_password],
365 errors => \@errors_pw});
366 if ($newpw_mail)
367 {
368 $r->{error} .= 'err_pw_mail';
369 $r->{error_details} .= join("\n",@errors_pw);
370 }
371 else
372 {
373 $r->{success} = 'suc_password_sent';
374 }
375 $r -> {need_login} = 1 ;
376 $r -> {done} = 1 ;
377
378 return ;
379 }
380
381 return ;
382 }
383
384 # ----------------------------------------------------------------------------
385
386 ###
387 ### Not yet working with new db-scheme
388 ###
389
390 sub add_category
391 {
392 my $self = shift ;
393 my $r = shift ;
394
395 if ($self -> checkuser($r) < 2)
396 {
397 $r -> {need_login} = 1 ;
398 return ;
399 }
400
401 my $set = DBIx::Recordset -> Insert ({'!DataSource' => $r -> {db},
402 '!Table' => 'category',
403 '!Serial' => 'id',
404 state => 0}) ;
405 my $id = $$set -> LastSerial ;
406 my $langset = $r -> {language_set} ;
407 my $txtset = DBIx::Recordset -> Setup ({'!DataSource' => $r -> {db},
408 '!Table' => 'categorytext'}) ;
409
410 $$langset -> Reset ;
411 while ($rec = $$langset -> Next)
412 {
413 $$txtset -> Insert ({category_id => $id,
414 language_id => $rec->{id},
415 category => $fdat{"category_$rec->{id}"}}) if ($fdat{"category_$rec->{id}"}) ;
416 delete $fdat{"category_$rec->{id}"} ;
417 }
418 }
419
420
421 # ----------------------------------------------------------------------------
422
423 sub add_item
424 {
425 my $self = shift ;
426 my $r = shift ;
427
428 die "No category" if (!defined ($r->{category_set}{edit_level})) ;
429
430 if ($self -> checkuser($r) < $r->{category_set}{edit_level})
431 {
432 $r -> {need_login} = 1 ;
433 return ;
434 }
435
436 # Check the URL
437
438 my $tt = $r->{category_set}{table_type};
439 my $cf = $r->{category_fields};
440 my $cfnl = $r->{category_fields_nolang};
441
442 foreach ((@$cf, @$cfnl))
443 {
444 next unless $r->{category_types}{$_} =~ /url/;
445
446 if ($fdat{$_} && $fdat{$_} =~ /\s/)
447 {
448 $fdat{$_} =~ s/\s//g;
449 push(@{$r->{warning}}, 'warn_url_removed_white_space');
450 }
451
452 if ($fdat{$_} && $fdat{$_} !~ m{http://})
453 {
454 $fdat{$_} =~ s{^}{http://};
455 push(@{$r->{warning}}, 'warn_url_added_http');
456 }
457
458 }
459
460 my $set = DBIx::Recordset -> Insert ({'!DataSource' => $r -> {db},
461 '!Table' => $tt,
462 '!Serial' => 'id',
463 (map { $_ => $fdat{$_} } @$cfnl),
464 url => $fdat{url},
465 $fdat{modtime} ? (modtime => $fdat{modtime}) : (),
466 category_id => $fdat{category_id},
467 user_id => $r -> {user_id},
468 state => $r ->{user_admin} ? ($fdat{state}?1:0):0}) ;
469
470 my $id = $$set -> LastSerial ;
471 my $langset = $r -> {language_set} ;
472 my $txtset = DBIx::Recordset -> Setup ({'!DataSource' => $r -> {db},
473 '!Table' => "${tt}text"}) ;
474
475 $$langset -> Reset ;
476 while ($rec = $$langset -> Next)
477 {
478 # Check the URL
479
480 my $lang = $rec->{id};
481
482 foreach (@$cf)
483 {
484 next unless $r->{category_types}{$_.'_'.$lang} =~ /url/;
485
486 if ($fdat{$_.'_'.$lang} && $fdat{$_.'_'.$lang} =~ /\s/)
487 {
488 $fdat{$_.'_'.$lang} =~ s/\s//g;
489 push(@{$r->{warning}}, 'warn_url_removed_white_space');
490 }
491
492 if ($fdat{$_.'_'.$lang} && $fdat{$_.'_'.$lang} !~ m{http://})
493 {
494 $fdat{$_.'_'.$lang} =~ s{^}{http://};
495 push(@{$r->{warning}}, 'warn_url_added_http');
496 }
497
498 }
499
500 $$txtset -> Insert ({ (map { $_ => $fdat{$_.'_'.$lang} || $fdat{$_} } @$cf),
501 "${tt}_id" => $id,
502 language_id => $lang })
503 if (grep { $fdat{$_.'_'.$lang} || $fdat{$_} } @$cf) ;
504 }
505
506 $fdat{"${tt}_id"} = $id ;
507
508 $r->{item_set} = undef ;
509 $self->get_item_lang($r);
510
511 if (!$udat{user_admin})
512 {
513 my @errors;
514 my $newitemmail = Embperl::Mail::Execute ({
515 inputfile => 'updateditem.mail',
516 from => $r->{config}->{emailfrom},
517 to => $r->{config}->{adminemail},
518 subject => 'New item on Embperl Website (Category '.$r->{category_set}{category}.')'.($udat{user_email}?" by $udat{user_email}":''),
519 errors => \@errors});
520 if ($newitemmail)
521 {
522 $r->{error} = 'err_item_admin_mail';
523 $r->{error_details} = join("\n",@errors);
524
525 return;
526 }
527 }
528
529 $r->{success} = 'suc_item_created';
530
531 return $self -> redir_to_show ($r) ;
532 }
533
534 # ----------------------------------------------------------------------------
535
536 sub update_item
537 {
538 my $self = shift ;
539 my $r = shift ;
540
541 die "No category" if (!defined ($r->{category_set}{edit_level})) ;
542
543 if ($self -> checkuser($r) < $r->{category_set}{edit_level})
544 {
545 $r -> {need_login} = 1 ;
546 return ;
547 }
548
549 my $tt = $r->{category_set}{table_type};
550 my $cf = $r->{category_fields};
551 my $cfnl = $r->{category_fields_nolang};
552
553 # make sure we have an id
554 if (!$fdat{"${tt}_id"})
555 {
556 $r -> {error} = 'err_cannot_update_no_id';
557 return ;
558 }
559
560 my $set = DBIx::Recordset -> Setup ({'!DataSource' => $r -> {db},
561 '!Table' => $tt }) ;
562
563 # update the entry, but only if it has the correct user id or the has admin rights
564 my $rows = $$set -> Select ({ id => $fdat{"${tt}_id"},
565 $r ->{user_admin} ? () : (user_id => $r->{user_id}) }) ;
566 if ($rows <= 0)
567 { # error if nothing was found (this will happen when the record isdn't owned by the user)
568 $r -> {error} = 'err_cannot_update_maybe_wrong_user' ;
569 return ;
570 }
571
572 $$set -> Update ({ url => $fdat{url},
573 (map { $_ => $fdat{$_} } @$cfnl),
574 $fdat{modtime} ? (modtime => $fdat{modtime}) : (),
575 $fdat{category_id} ? (category_id => $fdat{category_id}) : (),
576 $r->{user_admin} ? (state => $fdat{state}) : () },
577 { id => $fdat{"${tt}_id"},
578 $r ->{user_admin} ? () : (user_id => $r->{user_id}) }) ;
579
580
581 my $id = $fdat{"${tt}_id"} ;
582 my $langset = $r -> {language_set} ;
583 my $txtset = DBIx::Recordset -> Setup ({'!DataSource' => $r -> {db},
584 '!Table' => "${tt}text"}) ;
585
586 if (DBIx::Recordset->LastError)
587 {
588 $r -> {error} = 'err_update_db' ;
589 return ;
590 }
591
592 # Update the texts for every languange, but only if they belong to
593 # the item we have updated above
594
595 $$langset -> Reset ;
596 while ($rec = $$langset -> Next)
597 {
598 my $lang = $rec->{id};
599 if (grep { $fdat{$_.'_'.$lang} || $fdat{$_} } @$cf)
600 {
601 $rows = $$txtset -> Search ({"${tt}_id" => $id,
602 language_id => $lang
603 }) ;
604 if (DBIx::Recordset->LastError)
605 {
606 $r -> {error} = 'err_update_lang_db' ;
607 return ;
608 }
609 elsif ($rows == 0)
610 {
611 $$txtset -> Insert ({ (map { $_ => $fdat{$_.'_'.$lang} || $fdat{$_} } @$cf),
612 language_id => $lang,
613 "${tt}_id" => $id,
614 }) ;
615
616 if (DBIx::Recordset->LastError)
617 {
618 $r -> {error} = 'err_update_lang_db' ;
619 return ;
620 }
621 }
622 else
623 {
624 $rows = $$txtset -> Update ({ (map { $_ => $fdat{$_.'_'.$lang} || $fdat{$_} } @$cf),
625 language_id => $lang,
626 }, {
627 "${tt}_id" => $id,
628 id => $fdat{"id_$lang"}
629 }) ;
630 if (DBIx::Recordset->LastError)
631 {
632 $r -> {error} = 'err_update_lang_db' ;
633 return ;
634 }
635 }
636 }
637 }
638
639 $r -> {item_set} = undef ;
640 $self->get_item_lang($r) ;
641
642 if (!$udat{user_admin})
643 {
644 my @errors;
645 $r->{is_update} = 1;
646 my $newitemmail = Embperl::Mail::Execute ({
647 inputfile => 'updateditem.mail',
648 from => $r->{config}->{emailfrom},
649 to => $r->{config}->{adminemail},
650 subject => 'Updated item on Embperl Website (Category '.$r->{category_set}{category}.')'.($udat{user_email}?" by $udat{user_email}":''),
651 errors => \@errors});
652 if ($newitemmail)
653 {
654 $r->{error} = 'err_item_admin_mail';
655 $r->{error_details} = join('; ',@errors);
656
657 return;
658 }
659 }
660
661 $r->{success} = 'suc_item_updated' ;
662
663 return $self -> redir_to_show ($r) ;
664 }
665
666
667 # ----------------------------------------------------------------------------
668
669 sub delete_item
670 {
671 my $self = shift ;
672 my $r = shift ;
673
674 if (!$self -> checkuser($r))
675 {
676 $r -> {need_login} = 1 ;
677 return ;
678 }
679
680 my $tt = $r->{category_set}{table_type};
681 my $cf = $r->{category_fields};
682
683 # make sure we have an id
684 if (!$fdat{"${tt}_id"})
685 {
686 $r -> {error} = 'err_cannot_delete_no_id' ;
687 return ;
688 }
689
690 # first see if the entry exists and has the correct user_id
691 my $set = DBIx::Recordset -> Search ({'!DataSource' => $r->{db},
692 '!Table' => $tt,
693 id => $fdat{"${tt}_id"},
694 $r->{user_admin} ? () : (user_id => $r->{user_id}) }) ;
695
696 if (!$$set -> MoreRecords())
697 { # error if nothing was found (this will happen when the record isdn't owned by the user
698 $r -> {error} = 'err_cannot_delete_maybe_wrong_user_or_no_such_item' ;
699 return ;
700 }
701
702 # delete the entry, but only if it has the correct user id or the has admin rights
703 $$set -> Delete ({id => $fdat{"${tt}_id"},
704 $r ->{user_admin}?():(user_id => $r->{user_id})}) ;
705
706 if (DBIx::Recordset->LastError)
707 {
708 $r->{error} = 'err_cannot_delete_db_error';
709 $r->{error_details} = DBIx::Recordset->LastError;
710 return;
711 }
712
713 my $id = $fdat{"${tt}_id"} ;
714 my $langset = $r -> {language_set} ;
715 my $txtset = DBIx::Recordset -> Setup ({'!DataSource' => $r -> {db},
716 '!Table' => "${tt}text"}) ;
717
718 # Delete the texts for every languange, but only if they belong to the item we have delete above
719 $$langset -> Reset ;
720 while ($rec = $$langset -> Next)
721 {
722 $$txtset -> Delete ({ "${tt}_id" => $id,
723 id => $fdat{"id_$rec->{id}"}}) ;
724
725 if (DBIx::Recordset->LastError)
726 {
727 $r->{error} = 'err_cannot_delete_db_error';
728 $r->{error_details} = DBIx::Recordset->LastError;
729 return;
730 }
731 }
732
733 if (!$udat{user_admin})
734 {
735 my @errors;
736 $r->{is_update} = -1;
737 my $newitemmail = Embperl::Mail::Execute ({
738 inputfile => 'updateditem.mail',
739 from => $r->{config}->{emailfrom},
740 to => $r->{config}->{adminemail},
741 subject => 'Delete item on Embperl Website (Category '.$r->{category_set}{category}.')'.($udat{user_email}?" by $udat{user_email}":''),
742 errors => \@errors});
743 if ($newitemmail)
744 {
745 $r->{error} = 'err_item_admin_mail';
746 $r->{error_details} = join('; ',@errors);
747
748 return;
749 }
750 }
751
752 $r->{success} = 'suc_item_deleted' ;
753
754 return $self -> redir_to_show ($r) ;
755 }
756
757
758 # ----------------------------------------------------------------------------
759
760 sub redir_to_show
761 {
762 my $self = shift ;
763 my $r = shift ;
764
765 my $tt = $r->{category_set}{table_type};
766
767 my %params =
768 (
769 -show_item => 1,
770 $fdat{category_id} ? (category_id => $fdat{category_id}) : (),
771 $fdat{"${tt}_id"} ? ("${tt}_id" => $fdat{"${tt}_id"}) : (),
772 $r -> {error} ? (-error => $r -> {error}) : (),
773 $r -> {success} ? (-success => $r -> {success}) : (),
774 ) ;
775
776 my $dest = join ('&', map { $_ . '=' . $r -> Escape (ref ($params{$_})?join("\t", @{$params{$_}}):$params{$_} , 2) } keys %params) ;
777
778 my ($uri) = split (/\?/, $r -> param -> unparsed_uri, 1) ;
779 $http_headers_out{'location'} = $r -> param -> server_addr . dirname ($uri) ."/show.epl?$dest" ;
780
781 return 302 ;
782 }
783
784
785
786 # ----------------------------------------------------------------------------
787
788
789 sub get_category
790 {
791 my $self = shift ;
792 my $r = shift ;
793 my $edit = shift || 0 ;
794
795 $r -> {category_set} = DBIx::Recordset -> Search ({'!DataSource' => $r -> {db},
796 '!Table' => 'category, categorytext',
797 '!TabRelation' => 'category_id = category.id',
798 'language_id' => $r -> param -> language,
799 $fdat{category_id}?(category_id => $fdat{category_id}):(),
800 $edit?(edit_level => $r -> {user_admin}?2:1, '*edit_level' => '<='):(),
801 $r -> {user_admin} || $edit?():(state => 1)}) ;
802
803 my $level = $r -> {user_admin}?2:1 ;
804 my $level_field = $edit?'categoryfields.edit_level':'categoryfields.view_level' ;
805
806
807 *fields = DBIx::Recordset -> Search ({'!DataSource' => $r -> {db},
808 '!Table' => 'category, categoryfields',
809 '!TabRelation' => 'category_id = category.id',
810 'language_id' => $r -> param -> language,
811 $fdat{category_id}?(category_id => $fdat{category_id}):(),
812 $edit?('category.edit_level' => $r -> {user_admin}?2:1, '*category.edit_level' => '<='):(),
813 $level_field => $level,
814 "*$level_field" => '<=',
815 $r -> {user_admin} || $edit?():(state => 1),
816 '$order' => 'position' }) ;
817
818 my %texts = ();
819 my %types = ();
820 my %remarks = ();
821 my @textfields = ();
822 my @textfields_nolang = ();
823 my @validate ;
824
825 while (my $field = $fields->Next)
826 {
827 if ($field->{nolang})
828 {
829 push(@textfields_nolang, $field->{fieldname});
830 }
831 else
832 {
833 push(@textfields, $field->{fieldname});
834 }
835 $texts{$field->{fieldname}.'_text'} = $field->{txt};
836 $types{$field->{fieldname}} = $field->{typeinfo};
837 $remarks{$field->{fieldname}} = $field->{remark};
838 if ($field -> {validate})
839 {
840 my @tests = split (/[=,]/, $field -> {validate}) ;
841 push @validate, ('-key', $field->{fieldname}) ;
842 push @validate, ('-name', $field->{txt}) ;
843 push @validate, @tests ;
844 }
845 }
846
847 $r -> {category_fields} = \@textfields;
848 $r -> {category_fields_nolang} = \@textfields_nolang;
849 $r -> {category_texts} = \%texts;
850 $r -> {category_types} = \%types;
851 $r -> {category_remarks} = \%remarks;
852
853 my $title_type = 'heading';
854 foreach my $f (@textfields)
855 {
856 if ($types{$f} =~ /title/)
857 {
858 $title_type = $f;
859 last;
860 }
861 }
862
863 $r -> {category_title_type} = $title_type;
864
865
866 $r -> {validate} = new Embperl::Form::Validate(\@validate, 'form') ;
867
868 }
869
870
871 # ----------------------------------------------------------------------------
872
873 sub get_item
874 {
875 my $self = shift ;
876 my $r = shift ;
877 my %state ;
878
879 if (!$r -> {user_admin})
880 {
881 if ($r -> {user_id})
882 {
883 %state = ('$expr' => { '$conj' => 'or', state => 1, user_id => $r -> {user_id} } ) ;
884 }
885 else
886 {
887 %state = (state => 1) ;
888 }
889 }
890
891 my $tt = $r->{category_set}{table_type};
892
893
894 my $currlang = $r->param->language ;
895 my $rec ;
896 my %idmap ;
897 my @langs ;
898 while ($rec = ${$r -> {language_set}} -> Next)
899 {
900 push @langs, $rec->{id} ;
901 }
902
903
904 ${$r -> {language_set}} -> Reset ;
905 @langs = grep {$_ ne $currlang} @langs ;
906 push @langs, $currlang ;
907
908
909 foreach my $lang (@langs)
910 {
911 my $set = DBIx::Recordset -> Search ({'!DataSource' => $r->{db},
912 '!Fields' => "$tt.id as id, ${tt}text.id as textid",
913 '!Table' => "user, ${tt}, ${tt}text",
914 '!TabJoin' => "($tt left join ${tt}text on (${tt}_id = ${tt}.id)), user",
915 '!TabRelation' => "${tt}.user_id = user.id",
916 '$expr1' => {
917 'language_id' => $lang,
918 '$conj' => 'or',
919 "${tt}_id" => undef,
920 },
921 $fdat{category_id} ? (category_id => $fdat{category_id}) : (),
922 $fdat{"${tt}_id"} ? ("${tt}_id" => $fdat{"${tt}_id"}) : (),
923 %state}) ;
924 while ($rec = $$set -> Next)
925 {
926 $idmap{$rec -> {id}} = $rec -> {textid} ;
927 }
928 }
929
930 warn 'dbg ' . __LINE__ . "tab = user, ${tt}, ${tt}text; fields = *, $tt.id as ${tt}_id; idmap = " .
931 join (',', keys %idmap) if ($r -> {config}{dbdebug} > 1);
932 $r -> {item_set} = DBIx::Recordset -> Search ({'!DataSource' => $r->{db},
933 '!Fields' => "*, $tt.id as ${tt}_id",
934 '!Table' => "user, ${tt}, ${tt}text",
935 '!TabJoin' => "($tt left join ${tt}text on (${tt}text.${tt}_id = ${tt}.id)), user",
936 '!TabRelation' => "${tt}.user_id = user.id",
937 #"$tt.id" => [keys %idmap],
938 '$expr1' => {
939 '$expr1' => { "${tt}text.id" => [values %idmap], },
940 #'language_id' => $currlang,
941 '$conj' => 'or',
942 '$expr2' => { "${tt}text.id" => undef },
943 },
944 '!Order' => $fdat{-order} || 'modtime desc',
945 $fdat{category_id} ? (category_id => $fdat{category_id}) : (),
946 $fdat{"${tt}_id"} ? ("${tt}_id" => $fdat{"${tt}_id"}) : (),
947 %state}) ;
948
949 }
950
951
952 # ----------------------------------------------------------------------------
953
954 sub get_item_lang
955 {
956 my $self = shift ;
957 my $r = shift ;
958
959 my %state ;
960
961 if (!$r -> {user_admin})
962 {
963 if ($r -> {user_id})
964 {
965 %state = ('$expr' => { '$conj' => 'or', state => 1, user_id => $r -> {user_id} } ) ;
966 }
967 else
968 {
969 %state = (state => 1) ;
970 }
971 }
972
973 $tt = $r->{category_set}{table_type};
974
975 $r -> {item_set} = DBIx::Recordset -> Search ({'!DataSource' => $r->{db},
976 '!Fields' => "*, ${tt}text.id as id, $tt.id as ${tt}_id",
977 '!Table' => "user, ${tt}, language, ${tt}text",
978 '!TabJoin' => "($tt left join ${tt}text on (${tt}_id = ${tt}.id)) left join language on (language_id = language.id), user",
979 '!TabRelation' => "${tt}.user_id = user.id",
980 '!Order' => 'modtime desc',
981 $fdat{category_id} ? (category_id => $fdat{category_id}) : (),
982 $fdat{"${tt}_id"} ? ("${tt}.id" => $fdat{"${tt}_id"}) : (),
983 %state}) ;
984
985
986
987 $r->{item_set} = undef unless ${$r->{item_set}}->MoreRecords;
988 ${$r->{item_set}} -> Reset if ($r->{item_set}) ;
989
990 }
991
992 # ----------------------------------------------------------------------------
993
994 sub setup_edit_item
995 {
996 my $self = shift ;
997 my $r = shift ;
998
999 if (!$self -> checkuser($r))
1000 {
1001 $r -> {need_login} = 1 ;
1002 return ;
1003 }
1004
1005 my $set = $r -> {item_set} ;
1006
1007 unless (defined $set)
1008 {
1009 $r->{error} = 'err_item_not_found_or_access_denied';
1010
1011 return;
1012 }
1013
1014 my $tt = $r->{category_set}{table_type};
1015 my $cf = $r->{category_fields};
1016 my $cfnl = $r->{category_fields_nolang};
1017
1018 $fdat{"${tt}_id"} = $set->{"${tt}_id"} if $set->{"${tt}_id"};
1019
1020 $$set -> Reset ;
1021 while ($rec = $$set -> Next)
1022 {
1023 my $lang = $rec -> {language_id} ;
1024 $fdat{'id_' . $lang} = $rec -> {id};
1025 foreach my $type (@$cf)
1026 {
1027 $fdat{$type . '_' . $lang} = $rec -> {$type} ;
1028 }
1029 foreach my $type (@$cfnl)
1030 {
1031 $fdat{$type} = $rec -> {$type} ;
1032 }
1033 }
1034
1035 $$set -> Reset ;
1036 $r -> {edit} = 1 ;
1037 }
1038
1039
1040 # ----------------------------------------------------------------------------
1041
1042 sub get_user
1043 {
1044 my $self = shift ;
1045 my $r = shift ;
1046
1047 $fdat{user_id} = undef unless $r -> {user_admin};
1048
1049 $r -> {user_set} = DBIx::Recordset -> Search ({'!DataSource' => $r->{db},
1050 '!Table' => "user",
1051 id => $fdat{user_id} || $udat{user_id}
1052 }) ;
1053 $r->{user_set} = undef unless ${$r->{user_set}}->MoreRecords;
1054 }
1055
1056 # ----------------------------------------------------------------------------
1057
1058 sub get_users
1059 {
1060 my $self = shift ;
1061 my $r = shift ;
1062
1063 if ($self -> checkuser_light($r) < 1)
1064 {
1065 $r -> {need_login} = 1 ;
1066 return ;
1067 }
1068
1069 return unless $r -> {user_admin};
1070
1071 $r -> {users} = DBIx::Recordset -> Search ({'!DataSource' => $r->{db},
1072 '!Table' => "user" }) ;
1073 $r->{users} = undef unless ${$r->{users}}->MoreRecords;
1074 }
1075
1076
1077 # ----------------------------------------------------------------------------
1078
1079 sub update_user
1080 {
1081 my $self = shift ;
1082 my $r = shift ;
1083
1084 if ($self -> checkuser_light($r) < 1)
1085 {
1086 $r -> {need_login} = 1 ;
1087 return ;
1088 }
1089
1090 unless (($fdat{user_id} == $udat{user_id}) or $r->{user_admin})
1091 {
1092 $r->{error} = 'err_cannot_update_wrong_user_xxx';
1093 return;
1094 }
1095
1096 eval { *set = DBIx::Recordset -> Update ({'!DataSource' => $r->{db},
1097 '!Table' => "user",
1098 'user_name' => $fdat{user_name},
1099 'pid' => $fdat{pid} },
1100 { id => $fdat{user_id} || $udat{user_id}}) ; };
1101
1102
1103 if ($@ and $@ =~ 'Duplicate entry')
1104 {
1105 $r->{error} = 'err_pid_exists';
1106 return;
1107 }
1108
1109 if (DBIx::Recordset->LastError)
1110 {
1111 $r->{error} = 'err_update_db';
1112 push(@{$r->{error_details}}, DBIx::Recordset->LastError
1113 );
1114 }
1115
1116 $r->{success} = 'suc_user_update';
1117
1118 }
1119
1120 # ----------------------------------------------------------------------------
1121 # Warning: This will not yet work as intended if there is more than
1122 # one category using $table as category type!
1123
1124 sub get_title
1125 {
1126 my ($self, $r, $col, $id) = @_;
1127
1128 (my $table = $col) =~ s/_id$// or die "Can't strip '_id' (col=$col)";
1129
1130 my $config = $r->{config};
1131 my $db = DBIx::Database -> new ({'!DataSource' => $config -> {dbdsn},
1132 '!Username' => $config -> {dbuser},
1133 '!Password' => $config -> {dbpassword},
1134 '!DBIAttr' => { RaiseError => 1, PrintError => 1, LongReadLen => 32765, LongTruncOk => 0, }});
1135
1136
1137 # SQL can't handle such kind soft links, so we need two requests
1138 *fields = DBIx::Recordset -> Search ({'!DataSource' => $db,
1139 '!Table' => 'category, categoryfields',
1140 '!TabRelation' => 'category_id = category.id',
1141 'table_type' => $table,
1142 #'state' => 1,
1143 'typeinfo' => 'title',
1144 '*typeinfo' => 'LIKE',
1145 '$order' => 'position' }) ;
1146
1147 *set = DBIx::Recordset -> Search ({'!DataSource' => $db,
1148 '!Table' => $table.'text',
1149 'language_id' => $r -> param -> language,
1150 $table.'_id' => $id }) ;
1151
1152
1153 return $set{$fields{fieldname}};
1154 }
1155
1156 # ----------------------------------------------------------------------------
1157 # Warning: This will not yet work as intended if there is more than
1158 # one category using $table as category type!
1159
1160 sub get_titles
1161 {
1162 my ($self, $r, $table) = @_;
1163
1164 # *set = DBIx::Recordset -> Search ({'!DataSource' => $r->{db},
1165 # '!Fields' => "id,$r->{category_title_type} as title",
1166 # '!Table' => $table, }) ;
1167 # print OUT Dumper $config;
1168 #
1169 # return;
1170
1171 my $config = $r->{config};
1172 my $db = DBIx::Database -> new ({'!DataSource' => $config -> {dbdsn},
1173 '!Username' => $config -> {dbuser},
1174 '!Password' => $config -> {dbpassword},
1175 '!DBIAttr' => { RaiseError => 1, PrintError => 1, LongReadLen => 32765, LongTruncOk => 0, },
1176 }) ;
1177
1178 # SQL can't handle such kind soft links, so we need two requests
1179 # warn "tab=\"${table}\" searching for title\n" ;
1180 *fields = DBIx::Recordset -> Search ({'!DataSource' => $db,
1181 '!Table' => 'category, categoryfields',
1182 '!TabRelation' => 'category_id = category.id',
1183 'table_type' => $table,
1184 #'state' => 1,
1185 'typeinfo' => 'title',
1186 '*typeinfo' => 'LIKE',
1187 '$order' => 'position' }) ;
1188 my $title_type = $fields{fieldname};
1189 # warn "tt=\"$title_type\" tab=\"${table}text\" ${table}_id as id, $title_type as title" . $fields -> LastSQLStatement . "\n" ;
1190
1191 *set = DBIx::Recordset -> Search ({'!DataSource' => $db,
1192 '!Table' => $table.'text',
1193 'language_id' => $r -> param -> language,
1194 '!Fields' => $table."_id as id, $title_type as title",
1195 }) ;
1196
1197
1198 return \@set;
1199 }
1200
1201 # ----------------------------------------------------------------------------
1202
1203 sub set_xslt_param
1204 {
1205 my ($class, $r, $config, $param) = @_ ;
1206
1207 $class -> SUPER::set_xslt_param ($r, $config, $param) ;
1208
1209 my $p = $param -> xsltparam ;
1210 $p -> {category_id} = $fdat{category_id} || 0 ;
1211 }
1212
1213
1214