word edit: separate javascript include
[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 form > ul li img {
44         max-width: 300px;
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         form    => 'Translation',
109         alt     => 'Synonyms',
110         wptitle => 'Wikipedia',
111         source  => 'Image URL',
112         thumb   => 'Convert options',
113         prio    => 'Level',
114         cover   => undef, # included with prio
115         ref     => 'Reference',
116 );
117 my @prioenum = qw( essential basic common distinctive rare invisible );
118 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
119
120 my $row;
121 if ($find) {
122         $row = $db->select(word => '*', $find)->hash
123                 or Abort("Word not found", 404);
124 }
125
126 if (exists $get{copy}) {
127         $row = {%{$row}{ qw(prio lang cat) }};
128 }
129 elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
130         my $replace = $row;
131         $row = {%post{ pairkeys @wordcols }};
132         $_ = length ? $_ : undef for values %{$row};
133
134         eval {
135                 my %res = (returning => '*');
136                 my $query = $find ? $db->update(word => $row, $find, \%res) :
137                         $db->insert(word => $row, \%res);
138                 $row = $query->hash;
139         } or do {
140                 Alert("Entry could not be saved", $@);
141                 next;
142         };
143
144         my $imgpath = "data/word/org/$row->{id}.jpg";
145         my $reimage = eval {
146                 ($row->{source} // '') ne ($replace->{source} // '') or return;
147                 # copy changed remote url to local file
148                 unlink $imgpath if -e $imgpath;
149                 my $download = $row->{source} or return 1;
150                 require LWP::UserAgent;
151                 my $ua = LWP::UserAgent->new;
152                 $ua->agent('/');
153                 my $status = $ua->mirror($download, $imgpath);
154                 $status->is_success
155                         or die "Download from <q>$download</q> failed: ".$status->status_line."\n";
156         };
157         !$@ or Alert(["Source image not found", $@]);
158
159         $reimage ||= $row->{thumb} ~~ $replace->{thumb};  # different convert
160         $reimage ||= $row->{cover} ~~ $replace->{cover};  # resize
161         $reimage++ if $fields{rethumb};  # force refresh
162
163         my $thumbpath = "data/word/eng/$row->{form}.jpg";
164         if ($reimage) {
165                 if (-e $imgpath) {
166                         my $xyres = $row->{cover} ? '600x400' : '300x200';
167                         my @cmds = @{ $row->{thumb} // [] };
168                         @cmds = (
169                                 'convert',
170                                 -delete => '1--1', -background => 'white',
171                                 -gravity => @cmds ? 'northwest' : 'center',
172                                 @cmds,
173                                 -resize => "$xyres^", -extent => $xyres,
174                                 '-strip', -quality => '60%', -interlace => 'plane',
175                                 $imgpath => $thumbpath
176                         );
177                         eval {
178                                 require IPC::Run;
179                                 my $output;
180                                 IPC::Run::run(\@cmds, '<' => \undef, '>&' => \$output)
181                                         or die $output ||
182                                                 ($? & 127 ? "signal $?" : "error code ".($? >> 8))."\n";
183                         } or Alert([
184                                 "Thumbnail image not generated",
185                                 "Failed to convert source image.",
186                         ], "@cmds\n$@");
187                 }
188                 else {
189                         unlink $thumbpath;
190                 }
191         }
192 }}
193 else {
194         $row->{prio} //= 1;
195         $row->{$_} = $get{$_} for keys %get;
196 }
197
198 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
199 :>
200 <h1>Words <:= $title :></h1>
201
202 <div class="inline">
203
204 <form action="?" method="post">
205 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
206 <ul>
207 <:
208
209 for my $colinfo (pairs @wordcols) {
210         my ($col, $title) = @{$colinfo};
211         defined $title or next;
212         my $val = $row->{$col} // '';
213         $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
214         printf '<li><label for="%s">%s</label><p>', $col, $title;
215                 printf '<span class=inline>';
216         if ($col eq 'prio') {
217                 printf '<select id="%s" name="%1$s">', $col;
218                 printf('<option value="%s"%s>%s</option>',
219                         $_, $row->{$col} eq $_ && ' selected', $prioenum[$_]
220                 ) for 0 .. $#prioenum;
221                 print '</select>';
222                 printf(
223                         join('',
224                                 '<label>',
225                                 '<input id="%1$s" name="%1$s" value="0" type="hidden" />',
226                                 '<input id="%s" name="%1$s" value="1" type="checkbox"%s>',
227                                 ' %s</label>',
228                         ),
229                         'cover', !!$row->{cover} && ' checked', 'Highlighted'
230                 );
231         }
232         else {
233                 printf '<input id="%s" name="%1$s" value="%s" />', $col, Entity($val);
234                 -e and printf '<img src="/%s" alt="%s" />', $_, $row->{form} for
235                         $col eq 'source' ? "data/word/org/$row->{id}.jpg" :
236                         $col eq 'thumb'  ? "data/word/eng/$row->{form}.jpg" :
237                         ();
238         }
239                 print '</span>';
240         say '</p></li>';
241 }
242 :>
243 </ul>
244 <p>
245         <input type="submit" value="Save" />
246         <input type="submit" value="New" formaction="/writer?copy=cat" />
247 </p>
248 </form>
249
250 <:
251 if ($row->{id}) {
252 :>
253 <section id="nav">
254 <h2>Hierarchy</h2>
255
256 <:
257 say '<ul>';
258 my $parents = $db->select(word => '*', {id => $row->{cat}});
259 while (my $ref = $parents->hash) {
260         printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
261 }
262 say "<li><strong>$row->{form}</strong></li>";
263 my $children = $db->select(word => '*', {cat => $row->{id}});
264 while (my $ref = $children->hash) {
265         printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
266 }
267 :>
268 <li><form action="/writer">
269         <input type="hidden" name="cat" value="<:= $row->{id} :>" />
270         <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
271         <input type="submit" value="Add" />
272 </form></li>
273 </ul>
274 </section>
275 <:
276 }
277 :>
278 </div>