RDBO::I18N model for Form::Processor.
[blog.pm-common-perl-mods.git] / Common-Form-Processor-Model-RDBO-I18N / lib / Common / Form / Processor / Model / RDBO / I18N.pm
blob9e3ef1605ef021135519ced5305db0dac01416ad
1 package Common::Form::Processor::Model::RDBO::I18N;
2 use strict;
3 use warnings;
4 use Carp;
5 use Hash::Merge 'merge';
6 use base 'Common::Form::Processor::Model::RDBO';
8 our $VERSION = '0.01';
10 sub build_form {
11 my $self = shift;
13 my $profile = merge $self->profile,
15 required => {
16 orig_lang => {
17 type => 'Select',
18 options =>
19 [ map { { value => $_ } } $self->object_class->languages ]
24 croak "Please define 'profile' method in subclass"
25 unless ref $profile eq 'HASH';
27 ### $$$ look at all keys in profile and allow keys to be Field names.
29 for my $group ( qw/ required optional / ) {
30 my $required = 'required' eq $group;
32 $self->_build_fields( $profile->{ $group }, $required );
34 my $auto_fields = $profile->{ 'auto_' . $group } || next;
36 $self->_build_fields( $auto_fields, $required );
40 sub update_from_form {
41 my ( $self, $params ) = @_;
43 my $item = $self->item;
45 my $defined = defined $item ? 1 : 0;
47 $params->{ orig_lang } = $item->orig_lang if $item;
49 $item = $self->SUPER::update_from_form($params);
50 return unless $item;
52 if ( not $defined ) {
53 $item->orig_lang( $self->field( 'orig_lang' )->value );
55 my $rel_name = $item->get_i18n_rel_name();
57 my $i18n = $item->meta->relationship($rel_name)->class->new();
59 my $values = { istran => 0 };
60 while ( my ($field, $value) = each %$params ) {
61 if ( $i18n->can( $field ) ) {
62 $values->{ $field } = $value;
65 $item->$rel_name($values);
66 } else {
67 my $i18n =
68 $item->i18n
69 ? $item->i18n
70 : $item->i18n( [ lang => $params->{language} ] );
72 return $item unless $i18n;
74 while ( my ($field, $value) = each %$params ) {
75 if ( $i18n->can( $field ) ) {
76 $item->i18n->$field( $value );
81 return $item;
84 =head1 AUTHOR
86 vti
88 =head1 LICENSE
90 This library is free software, you can redistribute it and/or modify it under
91 the same terms as Perl itself.
93 =head1 SEE ALSO
95 L<Form::Processor>
97 =cut