Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 0f63a447

Von Moritz Bunkus vor mehr als 13 Jahren hinzugefügt

  • ID 0f63a447e4ba5d3c85741dda984a0236f7e79abd
  • Vorgänger 93be9aa2
  • Nachfolger 76da5bf5

Helper mixin for retrieving/saving translations for attributes for models

Unterschiede anzeigen:

SL/DB/Helper/TranslatedAttributes.pm
1
package SL::DB::Helper::TranslatedAttributes;
2

  
3
use strict;
4

  
5
use SL::DB::GenericTranslation;
6

  
7
use parent qw(Exporter);
8
our @EXPORT = qw(translated_attribute save_attribute_translation);
9

  
10
use Carp;
11

  
12
sub translated_attribute {
13
  my ($self, $attribute, $language_id, %params) = @_;
14

  
15
  $language_id      = _check($self, $attribute, $language_id);
16
  my $translation   = _find_translation($self, $attribute, $language_id, 0);
17
  $translation    ||= _find_translation($self, $attribute, undef,        0);
18

  
19
  return $translation ? $translation->translation : $self->$attribute;
20
}
21

  
22
sub save_attribute_translation {
23
  my ($self, $attribute, $language_id, $value) = @_;
24

  
25
  $language_id = _check($self, $attribute, $language_id);
26

  
27
  return _find_translation($self, $attribute, $language_id, 1)->update_attributes(translation => $value);
28
}
29

  
30
sub _check {
31
  my ($self, $attribute, $language_id) = @_;
32

  
33
  croak "Invalid attribute '${attribute}'" unless $self->can($attribute);
34
  croak "Object has not been saved yet"    unless $self->id;
35

  
36
  return ref($language_id) eq 'SL::DB::Language' ? $language_id->id : $language_id;
37
}
38

  
39
sub _find_translation {
40
  my ($self, $attribute, $language_id, $new_if_not_found) = @_;
41

  
42
  my %params = (language_id      => $language_id,
43
                translation_type => ref($self). '/' . $attribute,
44
                translation_id   => $self->id);
45

  
46
  return SL::DB::Manager::GenericTranslation->find_by(%params) || ($new_if_not_found ? SL::DB::GenericTranslation->new(%params) : undef);
47
}
48

  
49
1;
50

  
51
__END__
52

  
53
=encoding utf8
54

  
55
=head1 NAME
56

  
57
SL::DB::Helper::TranslatedAttributes - Mixin for retrieving and saving
58
translations for certain model attributes in the table
59
I<generic_translations>
60

  
61
=head1 SYNOPSIS
62

  
63
Declaration:
64

  
65
  package SL::DB::SomeObject;
66
  use SL::DB::Helper::Translated;
67

  
68
Usage:
69

  
70
  my $object   = SL::DB::SomeObject->new(id => $::form->{id})->load;
71
  my $language = SL::DB::Manager::Language->find_by(description => 'Deutsch');
72
  print "Untranslated name: " . $object->name . " translated: " . $object->translated_attribute('name', $language) . "\n";
73

  
74
  print "Now saving new value\n";
75
  my $save_ok = $object->save_attribute_translation('name', $language, 'Lieferung frei Haus');
76

  
77
=head1 FUNCTIONS
78

  
79
=over 4
80

  
81
=item C<translated_attribute $attribute, $language_id>
82

  
83
Returns the translation stored for the attribute C<$attribute> and the
84
language C<$language_id> (either an ID or an instance of
85
L<SL::DB::Language>).
86

  
87
If no translation exists for C<$language_id> or if C<$language_id> is
88
undefined then the default translation is looked up.
89

  
90
If neither translation exists then the value of C<< $self->$attribute >>
91
is returned.
92

  
93
Requires that C<$self> has a primary ID column named C<id> and that
94
the object has been saved.
95

  
96
=item C<save_attribute_translation $attribute, $language_id, $value>
97

  
98
Saves the translation C<$value> for the attribute C<$attribute> and
99
the language C<$language_id> (either an ID or an instance of
100
L<SL::DB::Language>).
101

  
102
If C<$language_id> is undefined then the default translation will be
103
saved.
104

  
105
Requires that C<$self> has a primary ID column named C<id> and that
106
the object has been saved.
107

  
108
Returns the same value as C<save>.
109

  
110
=back
111

  
112
=head1 EXPORTS
113

  
114
This mixin exports the functions L</translated_attribute> and
115
L</save_attribute_translation>.
116

  
117
=head1 BUGS
118

  
119
Nothing here yet.
120

  
121
=head1 AUTHOR
122

  
123
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
124

  
125
=cut

Auch abrufbar als: Unified diff