b0b1c2e6cfb1f9a1f11b05262ceb0436b6ac80d8
[kivitendo-erp.git] / 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, $verbatim) = @_;
14
15   $language_id      = _check($self, $attribute, $language_id, $verbatim);
16   my $translation   = _find_translation($self, $attribute, $language_id, 0);
17   $translation    ||= _find_translation($self, $attribute, undef,        0) unless $verbatim;
18
19   return $translation ? $translation->translation
20        : $verbatim    ? undef
21        :                $self->$attribute;
22 }
23
24 sub save_attribute_translation {
25   my ($self, $attribute, $language_id, $value) = @_;
26
27   $language_id = _check($self, $attribute, $language_id);
28
29   return _find_translation($self, $attribute, $language_id, 1)->update_attributes(translation => $value);
30 }
31
32 sub _check {
33   my ($self, $attribute, $language_id, $verbatim) = @_;
34
35   croak "Invalid attribute '${attribute}'" unless $self->can($attribute);
36   croak "Object has not been saved yet"    unless $self->id || $verbatim;
37
38   return ref($language_id) eq 'SL::DB::Language' ? $language_id->id : $language_id;
39 }
40
41 sub _find_translation {
42   my ($self, $attribute, $language_id, $new_if_not_found) = @_;
43
44   my %params = (language_id      => $language_id,
45                 translation_type => ref($self). '/' . $attribute,
46                 translation_id   => $self->id);
47
48   return SL::DB::Manager::GenericTranslation->find_by(%params) || ($new_if_not_found ? SL::DB::GenericTranslation->new(%params) : undef);
49 }
50
51 1;
52
53 __END__
54
55 =encoding utf8
56
57 =head1 NAME
58
59 SL::DB::Helper::TranslatedAttributes - Mixin for retrieving and saving
60 translations for certain model attributes in the table
61 I<generic_translations>
62
63 =head1 SYNOPSIS
64
65 Declaration:
66
67   package SL::DB::SomeObject;
68   use SL::DB::Helper::Translated;
69
70 Usage:
71
72   my $object   = SL::DB::SomeObject->new(id => $::form->{id})->load;
73   my $language = SL::DB::Manager::Language->find_by(description => 'Deutsch');
74   print "Untranslated name: " . $object->name . " translated: " . $object->translated_attribute('name', $language) . "\n";
75
76   print "Now saving new value\n";
77   my $save_ok = $object->save_attribute_translation('name', $language, 'Lieferung frei Haus');
78
79 =head1 FUNCTIONS
80
81 =over 4
82
83 =item C<translated_attribute $attribute, $language_id, $verbatim>
84
85 Returns the translation stored for the attribute C<$attribute> and the
86 language C<$language_id> (either an ID or an instance of
87 L<SL::DB::Language>).
88
89 If C<$verbatim> is falsish and either no translation exists for
90 C<$language_id> or if C<$language_id> is undefined then the default
91 translation is looked up.
92
93 If C<$verbatim> is falsish and neither translation exists then the
94 value of C<< $self->$attribute >> is returned.
95
96 Requires that C<$self> has a primary ID column named C<id> and that
97 the object has been saved.
98
99 =item C<save_attribute_translation $attribute, $language_id, $value>
100
101 Saves the translation C<$value> for the attribute C<$attribute> and
102 the language C<$language_id> (either an ID or an instance of
103 L<SL::DB::Language>).
104
105 If C<$language_id> is undefined then the default translation will be
106 saved.
107
108 Requires that C<$self> has a primary ID column named C<id> and that
109 the object has been saved.
110
111 Returns the same value as C<save>.
112
113 =back
114
115 =head1 EXPORTS
116
117 This mixin exports the functions L</translated_attribute> and
118 L</save_attribute_translation>.
119
120 =head1 BUGS
121
122 Nothing here yet.
123
124 =head1 AUTHOR
125
126 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
127
128 =cut