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