word edit: list and link translation references
[sheet.git] / writer.plp
1 <(common.inc.plp)><:
2
3 Html({
4         title => 'words cheat sheet admin',
5         version => '1.0',
6         nocache => 1,
7         raw => <<'EOT',
8 <style>
9 dl {
10         display: inline-grid;
11         grid: auto-flow / min-content repeat(10, auto);
12 }
13
14 form > ul {
15         display: table;
16         border-spacing: 0 2px;
17 }
18 form > ul > li {
19         display: table-row;
20 }
21 form > ul > li > * {
22         display: table-cell;
23         padding-right: .5em;
24 }
25 form > ul > li > label {
26         /* th */
27         text-align: right;
28 }
29 form > ul > li > label + * {
30         /* td */
31         width: 32em;
32 }
33
34 input:not([type]) {
35         box-sizing: border-box;
36         width: 100%;
37         padding: .4rem;
38         font-family: monospace;
39 }
40 input[type=number] {
41         max-width: 7em;
42 }
43 select {
44         padding: .3rem .2rem; /* TODO: input */
45 }
46
47 ul.popup {
48         display: flex;
49         flex-wrap: wrap;
50         align-items: end;
51         position: fixed;
52         left: 0;
53         top: 0;
54         margin: auto;
55         max-height: 90%;
56         max-width: 90%;
57         overflow: auto;
58         background: rgba(0, 0, 0, .8);
59         border: 1px solid #CCC;
60 }
61
62 h1 {
63         margin-bottom: 1ex;
64 }
65 .inline {
66         display: inline-flex;
67         align-items: start;
68         margin: 0 -1ex; /* inner gap */
69 }
70 .inline > * {
71         margin: 0 1ex;
72 }
73 .inline .inline {
74         display: flex;
75 }
76
77 #nav {
78         -margin-left: 1em; /* flex gap */
79 }
80 #nav > ul,
81 #nav > ul strong,
82 #nav form {
83         margin: 1ex 0;
84         display: inline-block;
85 }
86 </style>
87
88 <script src="/writer.js"></script>
89 EOT
90 });
91
92 use List::Util qw( pairs pairkeys );
93
94 my $db = eval {
95         my @dbinfo = (
96                 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
97         ) or die "database not configured\n";
98         require DBIx::Simple;
99         DBIx::Simple->new(@dbinfo[0..2], {
100                 RaiseError => 1,
101                 pg_enable_utf8 => 1,
102         });
103 } or Abort('Database error', 501, $@);
104
105 my @wordcols = (
106         lang    => 'Language',
107         cat     => 'Category',
108         ref     => undef, # included with cat
109         grade   => undef, # "
110         prio    => 'Level',
111         cover   => undef, # included with prio
112         form    => 'Title',
113         alt     => 'Synonyms',
114         wptitle => 'Wikipedia',
115         source  => 'Image',
116         thumb   => 'Convert options',
117 );
118 my @prioenum = qw( essential basic common distinctive rare invisible );
119 my %langflag = (
120         nld => "\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}",
121         eng => "\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}",
122         epo => '<span style="color:green">★</span>',
123 );
124 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
125
126 my $row;
127 if ($find) {
128         $row = $db->select(word => '*', $find)->hash
129                 or Abort("Word not found", 404);
130 }
131
132 if (exists $get{copy}) {
133         $row = {%{$row}{ qw(prio lang cat) }};
134 }
135 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
136         my $replace = $row;
137         $row = {%post{ pairkeys @wordcols }};
138         $_ = length ? $_ : undef for values %{$row};
139
140         eval {
141                 my %res = (returning => '*');
142                 my $query = $find ? $db->update(word => $row, $find, \%res) :
143                         $db->insert(word => $row, \%res);
144                 $row = $query->hash;
145         } or do {
146                 Alert("Entry could not be saved", $@);
147                 next;
148         };
149
150         my $imgpath = "data/word/org/$row->{id}.jpg";
151         my $reimage = eval {
152                 ($row->{source} // '') ne ($replace->{source} // '') or return;
153                 # copy changed remote url to local file
154                 unlink $imgpath if -e $imgpath;
155                 my $download = $row->{source} or return 1;
156                 require LWP::UserAgent;
157                 my $ua = LWP::UserAgent->new;
158                 $ua->agent('/');
159                 my $status = $ua->mirror($download, $imgpath);
160                 $status->is_success
161                         or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
162         };
163         !$@ or Alert(["Source image not found", $@]);
164
165         $reimage ||= $row->{thumb} ~~ $replace->{thumb};  # different convert
166         $reimage ||= $row->{cover} ~~ $replace->{cover};  # resize
167         $reimage++ if $fields{rethumb};  # force refresh
168
169         my $thumbpath = "data/word/eng/$row->{form}.jpg";
170         if ($reimage) {
171                 if (-e $imgpath) {
172                         my $xyres = $row->{cover} ? '600x400' : '300x200';
173                         my @cmds = @{ $row->{thumb} // [] };
174                         @cmds = (
175                                 'convert',
176                                 -delete => '1--1', -background => 'white',
177                                 -gravity => @cmds ? 'northwest' : 'center',
178                                 @cmds,
179                                 -resize => "$xyres^", -extent => $xyres,
180                                 '-strip', -quality => '60%', -interlace => 'plane',
181                                 $imgpath => $thumbpath
182                         );
183                         eval {
184                                 require IPC::Run;
185                                 my $output;
186                                 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
187                                         or die $output ||
188                                                 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
189                         } or Alert([
190                                 "Thumbnail image not generated",
191                                 "Failed to convert source image.",
192                         ], "@cmds\n$@");
193                 }
194                 else {
195                         unlink $thumbpath;
196                 }
197         }
198 }}
199 else {
200         $row->{prio} //= 1;
201         $row->{$_} = $get{$_} for keys %get;
202 }
203
204 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
205 :>
206 <h1>Words <:= $title :></h1>
207
208 <div class="inline">
209
210 <form action="?" method="post">
211 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
212 <ul>
213 <:
214
215 for my $colinfo (pairs @wordcols) {
216         my ($col, $title) = @{$colinfo};
217         defined $title or next;
218         my $val = $row->{$col} // '';
219         $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
220         printf '<li><label for="%s">%s</label><p>', $col, $title;
221                 printf '<span class=inline>';
222         if ($col eq 'prio') {
223                 printf '<select id="%s" name="%1$s">', $col;
224                 printf('<option value="%s"%s>%s</option>',
225                         $_, $row->{$col} eq $_ && ' selected', $prioenum[$_]
226                 ) for 0 .. $#prioenum;
227                 print '</select>';
228                 printf(
229                         join('',
230                                 '<label>',
231                                 '<input id="%1$s" name="%1$s" value="0" type="hidden" />',
232                                 '<input id="%s" name="%1$s" value="1" type="checkbox"%s>',
233                                 ' %s</label>',
234                         ),
235                         'cover', !!$row->{cover} && ' checked', 'Highlighted'
236                 );
237                 printf('<label for="%s">%s</label><input id="%1$s" name="%1$s" value="%s" type="number" />',
238                         'grade', 'Order', Entity($row->{grade})
239                 );
240         }
241         else {
242                 printf '<input id="%s" name="%1$s" value="%s" />', $col, Entity($val);
243                 -e and printf '<img src="/%s" alt="%s" />', $_, $row->{form}
244                         for $col eq 'thumb'  ? "data/word/eng/$row->{form}.jpg" : ();
245                 printf('<label for="%s">%s</label><input id="%1$s" name="%1$s" value="%s" />',
246                         'ref', 'Reference', Entity($row->{ref})
247                 ) if $col eq 'cat';
248         }
249                 print '</span>';
250         -e and printf('<img id="%spreview" src="/%s" alt="%s" hidden />',
251                 $col, $_, $row->{form}
252         ) for $col eq 'source' ? "data/word/org/$row->{id}.jpg" : ();
253         say '</p></li>';
254 }
255
256 if ($row->{id}) {
257         my $children = $db->select(word => '*', {ref => $row->{id}}, 'lang, id');
258         printf '<li><label>%s</label><div><ul class="inline">', 'Translations';
259         while (my $row = $children->hash) {
260                 printf '<li><label for="%s">%s</label> <a id="%1$s" href="%s">%s</a></li>',
261                         "trans-$row->{lang}", $langflag{$row->{lang}},
262                         "/writer/$row->{id}", Entity($row->{form});
263         }
264         say '</ul></div></li>';
265 }
266 :>
267 </ul>
268 <p>
269         <input type="submit" value="Save" />
270         <input type="submit" value="New" formaction="/writer?copy=cat" />
271 </p>
272 </form>
273
274 <:
275 if ($row->{id}) {
276 :>
277 <section id="nav">
278 <h2>Hierarchy</h2>
279
280 <:
281 say '<ul>';
282 my $parents = $db->select(word => '*', {id => $row->{cat}});
283 while (my $ref = $parents->hash) {
284         printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
285 }
286 say "<li><strong>$row->{form}</strong></li>";
287 my $children = $db->select(word => '*', {cat => $row->{id}}, 'grade, id');
288 while (my $ref = $children->hash) {
289         printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
290 }
291 :>
292 <li><form action="/writer">
293         <input type="hidden" name="cat" value="<:= $row->{id} :>" />
294         <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
295         <input type="submit" value="Add" />
296 </form></li>
297 </ul>
298 </section>
299 <:
300 }
301 :>
302 </div>