Improve category completion
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
index 90c5806..d1ad045 100755 (executable)
@@ -201,17 +201,6 @@ sub get_categories
     return @arr;
 }
 
-sub categories
-{
-    my ($text, $line, $start) = @_;
-
-    return () if $line =~ /\s/;
-
-    @categories = get_categories unless @categories;
-
-    return @categories;
-}
-
 sub list_categories
 {
     my $field = shift;
@@ -280,6 +269,26 @@ sub complete_category
     return @complete;
 }
 
+sub complete_categoryname
+{
+    my ($text, $line, $start) = @_;
+
+    return () unless length $line;
+
+    my $sql = sprintf("SELECT DISTINCT category FROM %s WHERE category LIKE '%s%%' ORDER BY category",
+                     $table,
+                     $line);
+    my $sth = $dbh->prepare($sql);
+    $sth->execute;
+    my @complete;
+    while (my $row = $sth->fetchrow_hashref) {
+       $row->{category} = substr $row->{category}, $start if $start;
+       push @complete, $row->{category};
+    }
+
+    return @complete;
+}
+
 sub default_year
 {
     my $answers = shift;
@@ -402,7 +411,7 @@ sub buchung_input
            'title' => 'Kategorie',
            'lookup' => \&list_categories,
            'default' => 'last',
-           'complete' => \&categories},
+           'complete' => \&complete_categoryname},
        'description' => {
            'title' => 'Beschreibung',
            'default' => 'last',