| 1 | package Dancer::Plugin::HTML::FormsDj; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use Dancer ':syntax'; | ||
| 8 | use Dancer::Plugin; | ||
| 9 | |||
| 10 | use Data::Dumper; | ||
| 11 | use HTML::FormsDj; | ||
| 12 | |||
| 13 | our $AUTHORITY = 'TLINDEN'; | ||
| 14 | our $VERSION = '0.01'; | ||
| 15 | |||
| 16 | register formsdj => sub { | ||
| 17 | my ($path, $config) = @_; | ||
| 18 | #my $conf = plugin_setting; | ||
| 19 | |||
| 20 | |||
| 21 | my %dj = (save => 0, get => 0, post => 0, redirect => '/', template => 0); | ||
| 22 | foreach my $param (keys %dj) { | ||
| 23 | if (exists $config->{$param}) { | ||
| 24 | $dj{$param} = $config->{$param}; | ||
| 25 | delete $config->{$param}; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | my $form = HTML::FormsDj->new(%{$config}); | ||
| 30 | |||
| 31 | if ($dj{post}) { | ||
| 32 | post $path => $dj{post}; | ||
| 33 | } | ||
| 34 | else { | ||
| 35 | post $path => sub { | ||
| 36 | my %input = params; | ||
| 37 | if (exists $config->{csrf}) { | ||
| 38 | $form->csrfcookie(cookie 'csrftoken'); | ||
| 39 | } | ||
| 40 | my %clean = $form->cleandata(%input); | ||
| 41 | |||
| 42 | if ( $form->clean() ) { | ||
| 43 | if ($dj{save}) { | ||
| 44 | $dj{save}(%clean); | ||
| 45 | } | ||
| 46 | redirect $dj{redirect}; | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | template $dj{template}, { form => $form }; | ||
| 50 | } | ||
| 51 | }; | ||
| 52 | } | ||
| 53 | |||
| 54 | if ($dj{get}) { | ||
| 55 | get $path => $dj{get}; | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | get $path => sub { | ||
| 59 | if (exists $config->{csrf}) { | ||
| 60 | cookie csrftoken => $form->getcsrf, expires => "15 minutes"; | ||
| 61 | } | ||
| 62 | |||
| 63 | template $dj{template}, { form => $form }; | ||
| 64 | }; | ||
| 65 | } | ||
| 66 | |||
| 67 | }; | ||
| 68 | |||
| 69 | |||
| 70 | register_plugin; | ||
| 71 | 1; | ||
| 72 | |||
| 73 | __END__ |