+ my ($self, $myconfig, $form, $prefix) = @_;
+ $prefix ||= '';
+
+ my $dbh = $form->get_standard_dbh;
+
+ my $query = "SELECT *, base_unit AS original_base_unit FROM units";
+
+ my $sth = prepare_execute_query($form, $dbh, $query);
+
+ my $units = {};
+ while (my $ref = $sth->fetchrow_hashref()) {
+ $units->{$ref->{"name"}} = $ref;
+ }
+ $sth->finish();
+
+ my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
+ $sth = $dbh->prepare($query_lang);
+ $sth->execute() || $form->dberror($query_lang);
+ my @languages;
+ while (my $ref = $sth->fetchrow_hashref()) {
+ push(@languages, $ref);
+ }
+ $sth->finish();
+
+ $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
+ "FROM units_language ul " .
+ "LEFT JOIN language l ON ul.language_id = l.id " .
+ "WHERE ul.unit = ?";
+ $sth = $dbh->prepare($query_lang);
+
+ foreach my $unit (values(%{$units})) {
+ ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
+
+ $unit->{"LANGUAGES"} = {};
+ foreach my $lang (@languages) {
+ $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
+ }
+
+ $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
+ while (my $ref = $sth->fetchrow_hashref()) {
+ map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
+ }
+ }
+ $sth->finish;
+
+ $main::lxdebug->leave_sub();
+
+ return $units;
+}
+
+sub retrieve_all_units {
+ $main::lxdebug->enter_sub();
+
+ my $self = shift;
+
+ if (!$::request->{cache}{all_units}) {
+ $::request->{cache}{all_units} = $self->retrieve_units(\%main::myconfig, $main::form);
+ }
+
+ $main::lxdebug->leave_sub();
+
+ return $::request->{cache}{all_units};
+}
+
+
+sub translate_units {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $form, $template_code, $unit, $amount) = @_;
+
+ my $units = $self->retrieve_units(\%main::myconfig, $form);
+
+ my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
+ my $new_unit = $unit;
+ if ($h) {
+ if (($amount != 1) && $h->{"localized_plural"}) {
+ $new_unit = $h->{"localized_plural"};
+ } elsif ($h->{"localized"}) {
+ $new_unit = $h->{"localized"};
+ }
+ }
+
+ $main::lxdebug->leave_sub();
+
+ return $new_unit;
+}
+
+sub units_in_use {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form, $units) = @_;
+