word edit: copy translations from wikipedia download
[sheet.git] / writer.plp
index ef377a0afb0db329026956ff5548ed68c012e4ba..ba81e8a4d4ee42e1841ae2e0d56d67aa4a379b71 100644 (file)
@@ -106,9 +106,10 @@ my $db = eval {
 } or Abort('Database error', 501, $@);
 
 my %lang = (
-       nld => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'dutch'],
-       eng => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
-       epo => ['<span style="color:green">★</span>', 'esperanto'],
+       nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
+       en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
+       eo => ['<span style="color:green">★</span>', 'esperanto'],
+       ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'],
 );
 my @wordcols = pairkeys
 my %wordcol = (
@@ -147,6 +148,19 @@ elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
        $row = {%post{keys %wordcol}};
        $_ = length ? $_ : undef for values %{$row};
 
+       if (!$row->{form}) {
+               if ($row->{ref} ne 'delete') {
+                       Alert("Empty title",
+                               "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
+                       );
+               }
+               else {
+                       $db->delete(word => $find);
+                       Alert("Entry removed");
+               }
+               next;
+       }
+
        eval {
                my %res = (returning => '*');
                my $query = $find ? $db->update(word => $row, $find, \%res) :
@@ -157,6 +171,20 @@ elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
                next;
        };
 
+       eval {
+               while (my ($lang, $val) = each %post) {
+                       my $field = $lang;
+                       $lang =~ s/^trans-// or next;
+                       $db->insert(word => {
+                               ref   => $row->{id},
+                               lang  => $lang,
+                               form  => $val,
+                       });
+                       delete $fields{$field};
+               }
+               return 1;
+       } or Alert('Error creating translation entries', $@);
+
        my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
        my $reimage = eval {
                ($row->{source} // '') ne ($replace->{source} // '') or return;
@@ -263,7 +291,7 @@ package Shiar_Sheet::FormRow {
        sub imagepath {
                my ($row, $col) = @_;
                return "data/word/org/$row->{id}.jpg"   if $col eq 'source';
-               return "data/word/eng/$row->{form}.jpg" if $col eq 'thumb';
+               return "data/word/en/$row->{form}.jpg"  if $col eq 'thumb';
                return;
        }
 }
@@ -289,15 +317,23 @@ for my $col (@wordcols) {
        say '</p></li>';
 }
 
-if ($row->{id}) {
-       my $children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id');
-       printf '<li><label>%s</label><div><ul class="inline">', 'Translations';
-       while (my $row = $children->hash) {
+if ($row->{id} and not $row->{ref}) {
+       printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
+               'trans', 'Translations';
+       my @children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
+       while (my ($lang, $val) = each %fields) {
+               $lang =~ s/^trans-// or next;
+               push @children, { lang => $lang, form => $val };
+       }
+       for my $ref (@children) {
+               printf(
+                       '<li><label for="%s" title="%3$s">%s </label>',
+                       "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
+               );
                printf(
-                       '<li><label for="%1$s" title="%5$s">%4$s</label>' .
-                               ' <a id="%s" href="%s">%s</a></li>',
-                       "trans-$row->{lang}", "/writer/$row->{id}", Entity($row->{form}),
-                       @{$lang{ $row->{lang} }}, # flag, name
+                       $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
+                       '<input id="%s" name="%1$s" value="%3$s" />',
+                       "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form}),
                );
        }
        say '</ul></div></li>';
@@ -318,12 +354,12 @@ if ($row->{id}) {
 
 <:
 say '<ul>';
-my $parents = $db->select(word => '*', {id => $row->{cat}});
+my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
 while (my $ref = $parents->hash) {
        printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
 }
 say "<li><strong>$row->{form}</strong></li>";
-my $children = $db->select(word => '*', {cat => $row->{id}}, 'grade, id');
+my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
 while (my $ref = $children->hash) {
        printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
 }