1 package SL::DB::Helper::TranslatedAttributes;
 
   5 use SL::DB::GenericTranslation;
 
   7 use parent qw(Exporter);
 
   8 our @EXPORT = qw(translated_attribute save_attribute_translation);
 
  12 sub translated_attribute {
 
  13   my ($self, $attribute, $language_id, $verbatim) = @_;
 
  15   $language_id        = _check($self, $attribute, $language_id, $verbatim);
 
  16   my $translation_obj = _find_translation($self, $attribute, $language_id, 0);
 
  17   my $translation     = $translation_obj ? $translation_obj->translation : '';
 
  19   return $translation if $verbatim || $translation;
 
  21   $translation_obj = _find_translation($self, $attribute, undef, 0);
 
  22   $translation     = $translation_obj ? $translation_obj->translation : '';
 
  24   return $translation || $self->$attribute;
 
  27 sub save_attribute_translation {
 
  28   my ($self, $attribute, $language_id, $value) = @_;
 
  30   $language_id = _check($self, $attribute, $language_id);
 
  32   return _find_translation($self, $attribute, $language_id, 1)->update_attributes(translation => $value);
 
  36   my ($self, $attribute, $language_id, $verbatim) = @_;
 
  38   croak "Invalid attribute '${attribute}'" unless $self->can($attribute);
 
  39   croak "Object has not been saved yet"    unless $self->id || $verbatim;
 
  41   return (ref($language_id) eq 'SL::DB::Language' ? $language_id->id : $language_id) || undef;
 
  44 sub _find_translation {
 
  45   my ($self, $attribute, $language_id, $new_if_not_found) = @_;
 
  47   my %params = (language_id      => $language_id,
 
  48                 translation_type => ref($self). '/' . $attribute,
 
  49                 translation_id   => $self->id);
 
  51   return SL::DB::Manager::GenericTranslation->find_by(%params) || ($new_if_not_found ? SL::DB::GenericTranslation->new(%params) : undef);
 
  62 SL::DB::Helper::TranslatedAttributes - Mixin for retrieving and saving
 
  63 translations for certain model attributes in the table
 
  64 I<generic_translations>
 
  70   package SL::DB::SomeObject;
 
  71   use SL::DB::Helper::Translated;
 
  75   my $object   = SL::DB::SomeObject->new(id => $::form->{id})->load;
 
  76   my $language = SL::DB::Manager::Language->find_by(description => 'Deutsch');
 
  77   print "Untranslated name: " . $object->name . " translated: " . $object->translated_attribute('name', $language) . "\n";
 
  79   print "Now saving new value\n";
 
  80   my $save_ok = $object->save_attribute_translation('name', $language, 'Lieferung frei Haus');
 
  86 =item C<translated_attribute $attribute, $language_id, $verbatim>
 
  88 Returns the translation stored for the attribute C<$attribute> and the
 
  89 language C<$language_id> (either an ID or an instance of
 
  92 If C<$verbatim> is falsish and either no translation exists for
 
  93 C<$language_id> or if C<$language_id> is undefined then the default
 
  94 translation is looked up.
 
  96 If C<$verbatim> is falsish and neither translation exists then the
 
  97 value of C<< $self->$attribute >> is returned.
 
  99 Requires that C<$self> has a primary ID column named C<id> and that
 
 100 the object has been saved.
 
 102 =item C<save_attribute_translation $attribute, $language_id, $value>
 
 104 Saves the translation C<$value> for the attribute C<$attribute> and
 
 105 the language C<$language_id> (either an ID or an instance of
 
 106 L<SL::DB::Language>).
 
 108 If C<$language_id> is undefined then the default translation will be
 
 111 Requires that C<$self> has a primary ID column named C<id> and that
 
 112 the object has been saved.
 
 114 Returns the same value as C<save>.
 
 120 This mixin exports the functions L</translated_attribute> and
 
 121 L</save_attribute_translation>.
 
 129 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>