summaryrefslogtreecommitdiff
path: root/form.cgi
blob: d4d7c4e2308bf98e93666ff184b937015ebae556 (plain)
  1. #!/usr/bin/perl -w
  2. use CGI::FormBuilder;
  3. use utf8::all;
  4. # TODO: check if protocol-agnostic URL works
  5. my $webroot_url = 'http://byvandring.nu/feedback/';
  6. my $webroot_path = '/home/bynu/public_websites/byvandring.nu/feedback';
  7. my $req_path = $webroot_path . '/index.html';
  8. my $ack_path = $webroot_path . '/tak/index.html';
  9. # Set this to 1 for a separate confirmation page
  10. my ($confirm) = 1;
  11. # Built-in email validation is too simplistic
  12. my $valid_email = '/^[+_A-Za-z0-9-]+(\.[+_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[_A-Za-z0-9-]+)$/';
  13. my $form = CGI::FormBuilder->new(
  14. lang => 'da',
  15. title => 'Feedback til Byvandring.nu',
  16. method => 'POST',
  17. fields => [qw/
  18. kommentar
  19. postnummer
  20. email
  21. /],
  22. validate => {
  23. postnummer => INT,
  24. email => $valid_email,
  25. _email => $valid_email, #fake check to silence warning of variable used only once
  26. },
  27. # required => [qw/
  28. # email
  29. # /],
  30. messages => ':da_DK',
  31. submit => ['Send oplysningerne'],
  32. action => $webroot_url, #avoids loosing submitted values when redirected from other site
  33. );
  34. $form->field(
  35. name => 'postnr',
  36. label => 'Postnummer',
  37. size => 5,
  38. comment => '(hvis du har lyst)'
  39. );
  40. $form->field(
  41. name => 'kommentar',
  42. type => 'textarea',
  43. rows => 3,
  44. );
  45. my ($form_required_text) = $form->{opt}{messages}{form_required_text};
  46. my ($mail_from) = $form->field('email') || undef;
  47. my ($mail_to, $mail_subject);
  48. if ($form->submitted) {
  49. $infostring = "Tak for din feedback!";
  50. if ($form->validate) {
  51. # $mail_to = 'info@byvandring.nu';
  52. $mail_to = 'siri@jones.dk';
  53. $mail_subject = 'Feedback til Byvandring.nu';
  54. } else {
  55. # $mail_to = 'siri@jones.dk, info@byvandring.nu';
  56. $mail_to = 'siri@jones.dk';
  57. $mail_subject = 'FEJL ved feedback til Byvandring.nu';
  58. $infostring .= "\n<P>OBS! Der var fejl i et eller flere af felterne. Informationerne er sendt uanset, men det anbefales at gå tilbage, checke at alle felter er udfyldt korrekt, og derefter sende igen.";
  59. $confirm = 0;
  60. };
  61. # if ($form->field('email')) {
  62. # $infostring .= "\n<P>Der er sendt en kopi af tilmeldingen til " . $form->field('email') . ".";
  63. # $mail_to .= ', ' . $form->field('email');
  64. # }
  65. # Abuse subject to add additional headers
  66. # $mail_subject .= "\n" . 'Content-Type: text/plain; charset="ISO-8859-1";';
  67. # $mail_subject .= "\n" . 'Content-Transfer-Encoding: 8bit';
  68. $form->mailresults(
  69. to => "$mail_to",
  70. from => $mail_from,
  71. # subject => "$mail_subject: " . $form->field('brugergruppe_navn'),
  72. subject => "$mail_subject",
  73. );
  74. if ($confirm) {
  75. $infostring .= "\n<P>Følgende informationer er blevet sendt:";
  76. $form->{opt}{messages}{form_confirm_text} = "$infostring";
  77. print $form->confirm(
  78. header => 1,
  79. template => $ack_path
  80. );
  81. } else {
  82. print $form->render(
  83. header => 1,
  84. sticky => (! $form->validate),
  85. #FIXME text => $infostring,
  86. template => $req_path
  87. );
  88. }
  89. } else {
  90. print $form->render(
  91. header => 1,
  92. template => $req_path
  93. );
  94. }