From: Moritz Bunkus Date: Wed, 8 Dec 2010 15:59:57 +0000 (+0100) Subject: Helper mixin for retrieving/saving translations for attributes for models X-Git-Tag: release-2.6.3~25^2~59 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=0f63a447e4ba5d3c85741dda984a0236f7e79abd;p=kivitendo-erp.git Helper mixin for retrieving/saving translations for attributes for models --- diff --git a/SL/DB/Helper/TranslatedAttributes.pm b/SL/DB/Helper/TranslatedAttributes.pm new file mode 100644 index 000000000..51e7e295f --- /dev/null +++ b/SL/DB/Helper/TranslatedAttributes.pm @@ -0,0 +1,125 @@ +package SL::DB::Helper::TranslatedAttributes; + +use strict; + +use SL::DB::GenericTranslation; + +use parent qw(Exporter); +our @EXPORT = qw(translated_attribute save_attribute_translation); + +use Carp; + +sub translated_attribute { + my ($self, $attribute, $language_id, %params) = @_; + + $language_id = _check($self, $attribute, $language_id); + my $translation = _find_translation($self, $attribute, $language_id, 0); + $translation ||= _find_translation($self, $attribute, undef, 0); + + return $translation ? $translation->translation : $self->$attribute; +} + +sub save_attribute_translation { + my ($self, $attribute, $language_id, $value) = @_; + + $language_id = _check($self, $attribute, $language_id); + + return _find_translation($self, $attribute, $language_id, 1)->update_attributes(translation => $value); +} + +sub _check { + my ($self, $attribute, $language_id) = @_; + + croak "Invalid attribute '${attribute}'" unless $self->can($attribute); + croak "Object has not been saved yet" unless $self->id; + + return ref($language_id) eq 'SL::DB::Language' ? $language_id->id : $language_id; +} + +sub _find_translation { + my ($self, $attribute, $language_id, $new_if_not_found) = @_; + + my %params = (language_id => $language_id, + translation_type => ref($self). '/' . $attribute, + translation_id => $self->id); + + return SL::DB::Manager::GenericTranslation->find_by(%params) || ($new_if_not_found ? SL::DB::GenericTranslation->new(%params) : undef); +} + +1; + +__END__ + +=encoding utf8 + +=head1 NAME + +SL::DB::Helper::TranslatedAttributes - Mixin for retrieving and saving +translations for certain model attributes in the table +I + +=head1 SYNOPSIS + +Declaration: + + package SL::DB::SomeObject; + use SL::DB::Helper::Translated; + +Usage: + + my $object = SL::DB::SomeObject->new(id => $::form->{id})->load; + my $language = SL::DB::Manager::Language->find_by(description => 'Deutsch'); + print "Untranslated name: " . $object->name . " translated: " . $object->translated_attribute('name', $language) . "\n"; + + print "Now saving new value\n"; + my $save_ok = $object->save_attribute_translation('name', $language, 'Lieferung frei Haus'); + +=head1 FUNCTIONS + +=over 4 + +=item C + +Returns the translation stored for the attribute C<$attribute> and the +language C<$language_id> (either an ID or an instance of +L). + +If no translation exists for C<$language_id> or if C<$language_id> is +undefined then the default translation is looked up. + +If neither translation exists then the value of C<< $self->$attribute >> +is returned. + +Requires that C<$self> has a primary ID column named C and that +the object has been saved. + +=item C + +Saves the translation C<$value> for the attribute C<$attribute> and +the language C<$language_id> (either an ID or an instance of +L). + +If C<$language_id> is undefined then the default translation will be +saved. + +Requires that C<$self> has a primary ID column named C and that +the object has been saved. + +Returns the same value as C. + +=back + +=head1 EXPORTS + +This mixin exports the functions L and +L. + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut