push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
}
- foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
+ foreach my $unit (sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } keys(%{$units}))) {
push(@{$select}, { "name" => $unit,
"base_unit" => $units->{$unit}->{"base_unit"},
"factor" => $units->{$unit}->{"factor"},
my $select = "<select name=${name}>";
- foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
+ foreach my $unit (sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } keys(%{$units}))) {
if (!$convertible_into ||
($units->{$convertible_into} &&
($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
my $dbh = $form->dbconnect_noauto($myconfig);
- my $query = "INSERT INTO units (name, base_unit, factor, type) VALUES (?, ?, ?, ?)";
- $dbh->do($query, undef, $name, $base_unit, $factor, $type) || $form->dberror($query . " ($name, $base_unit, $factor, $type)");
+ my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
+ my ($sortkey) = selectrow_query($form, $dbh, $query);
+
+ $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
+ "VALUES (?, ?, ?, ?, ?)";
+ do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
if ($languages) {
$query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
$main::lxdebug->leave_sub();
}
+sub swap_units {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
+
+ my $dbh = $form->dbconnect_noauto($myconfig);
+
+ my $query;
+
+ $query = qq|SELECT sortkey FROM units WHERE name = ?|;
+ my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
+
+ $query =
+ qq|SELECT sortkey FROM units | .
+ qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
+ qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
+ my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
+
+ $main::lxdebug->message(0, "name1 $name_1 dir $dir sk1 $sortkey_1 sk2 $sortkey_2");
+
+ if (defined($sortkey_1)) {
+ $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
+ my ($name_2) = selectrow_query($form, $dbh, $query);
+
+ if (defined($name_2)) {
+ $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
+ my $sth = $dbh->prepare($query);
+
+ do_statement($form, $sth, $query, $sortkey_1, $name_2);
+ do_statement($form, $sth, $query, $sortkey_2, $name_1);
+ }
+ }
+
+ $dbh->commit();
+ $dbh->disconnect();
+
+ $main::lxdebug->leave_sub();
+}
+
1;
@languages = AM->language(\%myconfig, $form, 1);
- @unit_list = ();
- foreach $name (sort({ lc($a) cmp lc($b) } grep({ !$units->{$_}->{"base_unit"} } keys(%{$units})))) {
- map({ push(@unit_list, $units->{$_}); }
- sort({ ($units->{$a}->{"resolved_factor"} * 1) <=> ($units->{$b}->{"resolved_factor"} * 1) }
- grep({ $units->{$_}->{"resolved_base_unit"} eq $name } keys(%{$units}))));
- }
+ @unit_list = sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } values(%{$units}));
+
my $i = 1;
foreach (@unit_list) {
$_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"} * 1) if ($_->{"factor"});
$units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"});
$ddbox = AM->unit_select_data($units, undef, 1);
+ my $updownlink = build_std_url("action=swap_units", "unit_type");
+
$form->{"title"} = sprintf($locale->text("Add and edit %s"), $form->{"unit_type"} eq "dimension" ? $locale->text("dimension units") : $locale->text("service units"));
$form->header();
print($form->parse_html_template("am/edit_units",
{ "UNITS" => \@unit_list,
"NEW_BASE_UNIT_DDBOX" => $ddbox,
- "LANGUAGES" => \@languages }));
+ "LANGUAGES" => \@languages,
+ "updownlink" => $updownlink }));
$lxdebug->leave_sub();
}
$lxdebug->leave_sub();
}
+
+sub swap_units {
+ $lxdebug->enter_sub();
+
+ my $dir = $form->{"dir"} eq "down" ? "down" : "up";
+ my $unit_type = $form->{"unit_type"} eq "dimension" ?
+ "dimension" : "service";
+ AM->swap_units(\%myconfig, $form, $dir, $form->{"name"}, $unit_type);
+
+ edit_units();
+
+ $lxdebug->leave_sub();
+}
<table>
<tr>
- <th class="listheading"> </th>
+ <th class="listheading" width="32" align="center" valign="center"><img alt="hoch" src="image/up.png"><img alt="runter" src="image/down.png"></th>
<th class="listheading">Löschen</th>
<th class="listheading">Einheit</th>
<th class="listheading">Basiseinheit</th>
<TMPL_LOOP NAME=UNITS>
<tr>
+ <td width="32" align="center" valign="center"><TMPL_IF __first__><img src="image/transparent16x16.gif"><TMPL_ELSE><a href="<TMPL_VAR updownlink>&dir=up&name=<TMPL_VAR name ESCAPE=URL>"><img alt="hoch" src="image/up.png"></a></TMPL_IF><TMPL_IF __last__><img src="image/transparent16x16.gif"><TMPL_ELSE><a href="<TMPL_VAR updownlink>&dir=down&name=<TMPL_VAR name ESCAPE=URL>"><img alt="runter" src="image/down.png"></a></TMPL_IF></td>
<TMPL_IF NAME=in_use>
- <td><TMPL_VAR NAME=__counter__></td>
<td>
<input type="hidden" name="unchangeable_<TMPL_VAR NAME=__counter__>" value="1">
<input type="hidden" name="old_name_<TMPL_VAR NAME=__counter__>" value="<TMPL_VAR NAME=name>">
<TMPL_ELSE>
- <td><TMPL_VAR NAME=__counter__></td>
<td align="center"><input type="checkbox" name="delete_<TMPL_VAR NAME=__counter__>"></td>
<td>
<input type="hidden" name="old_name_<TMPL_VAR NAME=__counter__>" value="<TMPL_VAR NAME=name>">
<table>
<tr>
- <th class="listheading"> </th>
+ <th class="listheading" width="32" align="center" valign="center"><img alt="<translate>up</translate>" src="image/up.png"><img alt="<translate>down</translate>" src="image/down.png"></th>
<th class="listheading"><translate>Delete</translate></th>
<th class="listheading"><translate>Unit</translate></th>
<th class="listheading"><translate>Base unit</translate></th>
<TMPL_LOOP NAME=UNITS>
<tr>
+ <td width="32" align="center" valign="center"><TMPL_IF __first__><img src="image/transparent16x16.gif"><TMPL_ELSE><a href="<TMPL_VAR updownlink>&dir=up&name=<TMPL_VAR name ESCAPE=URL>"><img alt="<translate>up</translate>" src="image/up.png"></a></TMPL_IF><TMPL_IF __last__><img src="image/transparent16x16.gif"><TMPL_ELSE><a href="<TMPL_VAR updownlink>&dir=down&name=<TMPL_VAR name ESCAPE=URL>"><img alt="<translate>down</translate>" src="image/down.png"></a></TMPL_IF></td>
<TMPL_IF NAME=in_use>
- <td><TMPL_VAR NAME=__counter__></td>
<td>
<input type="hidden" name="unchangeable_<TMPL_VAR NAME=__counter__>" value="1">
<input type="hidden" name="old_name_<TMPL_VAR NAME=__counter__>" value="<TMPL_VAR NAME=name>">
<TMPL_ELSE>
- <td><TMPL_VAR NAME=__counter__></td>
<td align="center"><input type="checkbox" name="delete_<TMPL_VAR NAME=__counter__>"></td>
<td>
<input type="hidden" name="old_name_<TMPL_VAR NAME=__counter__>" value="<TMPL_VAR NAME=name>">