word edit: copycat button to save and start a similar entry
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 24 May 2020 04:15:57 +0000 (06:15 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 6 Jun 2020 01:49:20 +0000 (03:49 +0200)
Option to retain language and category.

writer.plp

index 59bacacbd5eaf89f1b7586d51c70e73ec45928de..37424a8b65bf2cf00fc7c317a7fe1c4b1506ea55 100644 (file)
@@ -51,10 +51,13 @@ my @wordcols = (
        thumb   => 'Convert options',
        wptitle => 'Wikipedia',
 );
-my $find = $Request ? {id => $Request} : undef;
+my ($find) = map {{id => $_}} $fields{id} || $Request || ();
 
 my $row;
-if ($ENV{REQUEST_METHOD} eq 'POST') {
+if (exists $get{copy}) {
+       $row = {%fields{ qw(lang cat) }};
+}
+elsif ($ENV{REQUEST_METHOD} eq 'POST') {
        $row = {%post{ pairkeys @wordcols }};
        $_ = length ? $_ : undef for values %{$row};
        eval {
@@ -90,19 +93,18 @@ if ($ENV{REQUEST_METHOD} eq 'POST') {
                        "Failed to convert source image, error code ".($status >> 8),
                ], "@cmds");
        }
-
-       $row = {map {$_ => $row->{$_}} qw(lang cat)} if not $Request;
 }
 elsif ($find) {
        $row = $db->select(word => '*', $find)->hash
                or Abort("Word not found", 404);
 }
 
-my $title = $row ? "entry <small>#$row->{id}</small>" : 'new entry';
+my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
 :>
 <h1>Words <:= $title :></h1>
 
-<form action="" method="post">
+<form action="?" method="post">
+<input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
 <dl>
 <:
 
@@ -111,7 +113,7 @@ for my $col (pairs @wordcols) {
        $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
        printf '<dt><label for="%s">%s</label></dt>'
                . '<dd><input id="%1$s" name="%1$s" value="%s" />',
-               $col->key, $col->value, $val;
+               $col->key, $col->value, Entity($val);
        -e and printf ' <img src="/%s" alt="%s" />', $_, $row->{form} for
                $col->key eq 'source' ? "data/word/org/$row->{id}.jpg" :
                $col->key eq 'thumb'  ? "data/word/eng/$row->{form}.jpg" :
@@ -120,5 +122,8 @@ for my $col (pairs @wordcols) {
 }
 :>
 </dl>
-<p><input type="submit" value="Save" /></p>
+<p>
+       <input type="submit" value="Save" />
+       <input type="submit" value="New" formaction="/writer?copy=cat" />
+</p>
 </form>