1 <(../common.inc.plp)><:
3 my $editorurl = '/word/edit';
4 s{\Aedit(/|\z)}{} for $Request // ();
7 title => 'words cheat sheet admin',
11 <link rel="stylesheet" type="text/css" media="all" href="/word/editor.css" />
12 <script src="/word/editor.js"></script>
13 <script src="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.5/croppie.min.js"></script>
14 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.5/croppie.min.css" />
18 use List::Util qw( pairs pairkeys );
19 use Shiar_Sheet::FormRow;
23 require Shiar_Sheet::DB;
24 Shiar_Sheet::DB->connect;
25 } or Abort('Database error', 501, $@);
28 if (defined $post{username}) {
29 $cookie{login} = EncodeURI(join ':', @post{qw( username pass )});
31 elsif (exists $fields{logout}) {
33 if (AddCookie(CGI::Cookie->new(
39 delete $cookie{login};
40 die "Logged out as requested\n";
42 Alert("Failed to log out", "Login cookie could not be removed.");
45 my $cookiedata = $cookie{login} or return;
46 my ($name, $key) = split /[:\v]/, DecodeURI($cookiedata);
47 my %rowmatch = (username => $name, pass => $key);
48 my $found = $db->select(login => '*', \%rowmatch)->hash
49 or die "Invalid user or password\n";
53 my $httpcookie = CGI::Cookie->new(
55 -value => join(':', @{$found}{qw( username pass )}),
57 ) or die "prepared object is empty\n";
58 AddCookie($httpcookie->as_string);
59 } or Abort(["Unable to create login cookie", $@], 403);
63 say '<h1>Login to edit words</h1>';
64 Alert('Access denied', $@) if $@;
65 say '<form action="?" method="post" class="inline"><ul>';
66 my $loginform = bless {%post}, 'Shiar_Sheet::FormRow';
67 say '<li>', $loginform->input(@{$_}), '</li>' for pairs (
68 username => {-label => 'User name'},
69 pass => {-label => 'Password', type => 'password'},
71 say '<li><input type="submit" value="Login" /></li>';
77 '' => ['(reference)'],
78 nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
79 en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
80 eo => [qq'<span style="color:green">\N{BLACK STAR}</span>', 'esperanto'],
81 ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'],
82 zh => ["\N{REGIONAL INDICATOR SYMBOL LETTER C}\N{REGIONAL INDICATOR SYMBOL LETTER N}", '中文'],
83 la => ["\N{PUSHPIN}", 'latin'],
85 my @wordcols = pairkeys
87 lang => {-label => 'Language', -select => {
88 map { $_ => "@{$lang{$_}}" } keys %lang
90 cat => [{-label => 'Category'}, 'ref'],
91 ref => {-label => 'Reference'},
93 {-label => 'Level', -select => sub {
96 essential ubiquitous basic common distinctive specialised rare invisible
99 ('' => 'parent') x (defined $row->{ref}),
100 map { $_ => $enum[$_] } 0 .. $#enum
105 cover => {-label => 'Highlighted', type => 'checkbox'},
106 grade => {-label => 'Order', type => 'number'},
107 form => {-label => 'Title'},
108 alt => {-label => 'Synonyms', -multiple => 1},
109 wptitle => {-label => 'Wikipedia'},
110 source => {-label => 'Image', -json => 'image', -src => sub {
111 return "data/word/org/$_[0]->{id}.jpg";
113 convert => {-label => 'Convert options', -json => 'image', -multiple => 1, -src => sub {
114 return "data/word/32/$_[0]->{id}.jpg";
116 crop32 => {-label => 'Crop 3:2', -json => 'image'},
117 story => {-label => 'Story', type => 'textarea', hidden => 'hidden'},
120 if (my $search = $fields{q}) {
121 my %filter = $search eq '^' ? (cat => undef, ref => undef) :
122 (form => {ilike => '%'.parseinput($search).'%'});
123 my $results = $db->select(word => '*', \%filter);
124 say '<h1>Search</h1><ul>';
125 printf("<li><small>%s</small> %s %s</li>\n",
126 $_->{id}, showlink($_->{form}, "$editorurl/$_->{id}"),
127 sprintf('<img src="/%s" style="height:3ex; width:auto" />', $wordcol{convert}->{-src}->($_)) x defined $_->{image}
128 ) for $results->hashes;
133 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
136 $row = $db->select(word => '*', $find)->hash
137 or Abort("Word not found", 404);
140 if (exists $get{copy}) {
141 $row = {%{$row}{ qw(prio lang cat) }};
143 elsif (defined $post{form}) {{
145 return if not length $_[0];
147 return Encode::decode_utf8($_[0]);
150 my $replace = $row; # currently stored
151 $row = {}; # proposed update
152 while (my ($col, $colinfo) = each %wordcol) {
153 ref $colinfo eq 'HASH' or $colinfo = {};
154 my @val = map { parseinput($_) } $post{'@'.$col}->@*;
155 my $val = $colinfo->{-multiple} && @val ? \@val : $val[-1];
156 if (my $jsoncol = $colinfo->{-json}) {
158 $row->{$jsoncol}->{$col} = $val; # hash will be encoded
164 my $imagecol = $row->{image}; # backup image subcolumns
165 ref $_ eq 'HASH' and $_ = encode_json($_) for values %{$row};
167 if (!$row->{form} and $row->{lang}) {
168 if ($row->{ref} ne 'delete') {
170 "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
174 $db->delete(word => $find);
175 Alert("Entry removed");
181 my %res = (returning => '*');
182 $row->{creator} = $user->{id} unless $find;
183 $row->{updated} = ['now()'];
184 my $query = $find ? $db->update(word => $row, $find, \%res) :
185 $db->insert(word => $row, \%res);
188 Alert("Entry could not be saved", $@);
193 while (my ($lang, $val) = each %post) {
195 $lang =~ s/^trans-// or next;
196 $val = parseinput($val) or next;
203 $subrow{wptitle} = $1 if $subrow{form} =~ s/\h*\[(.*)\]$//; # [Link] shorthand
204 $subrow{alt} = [split m{/}, $1] if $subrow{form} =~ s{/(\S.*)}{}; # /alternates shorthand
205 $db->insert(word => \%subrow);
206 delete $fields{$field};
209 } or Alert('Error creating translation entries', $@);
211 require Shiar_Sheet::ImagePrep;
212 my $image = Shiar_Sheet::ImagePrep->new($wordcol{source}->{-src}->($row));
214 ($imagecol->{source} // '') ne ($replace->{source} // '') or return;
215 $image->download($imagecol->{source});
217 !$@ or Alert(["Source image not found", $@]);
219 $reimage ||= $row->{image} ~~ $replace->{image}; # different source
220 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
221 $reimage++ if $fields{rethumb}; # force refresh
224 $image->generate($wordcol{convert}->{-src}->($row), $imagecol);
226 my ($warn, @details) = ref $@ ? @{$@} : $@;
227 Alert([ "Thumbnail image not generated", $warn ], @details);
232 $row->{lang} //= $user->{editlang}->[0] unless exists $row->{lang};
233 $row->{$_} = $get{$_} for keys %get;
234 $row->{prio} = defined $row->{ref} ? undef : 4 unless exists $row->{prio};
238 my $imagerow = $row->{image} && JSON->new->decode(delete $row->{image}) || {};
239 while (my ($col, $val) = each %{$imagerow}) {
243 } or Alert("Error decoding image metadata", $@);
245 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
246 bless $row, 'Shiar_Sheet::FormRow';
248 <h1>Words <:= $title :></h1>
252 <form action="?" method="post">
253 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
256 for my $col (@wordcols) {
257 my $info = $wordcol{$col} or next;
258 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
259 next if delete $attr->{hidden} and not $row->{$col};
260 my $title = ref $attr ? delete $attr->{-label} : $attr;
261 printf '<li><label for="%s">%s</label><p>', $col, $title;
262 printf '<span class=inline>';
263 print $row->input($col => $attr);
264 if (my $imgsrc = $attr->{-src}) {
265 printf('<img id="%spreview" src="/%s" alt="%s"%s />',
266 $col, $_, $row->{form}, $col eq 'source' && ' hidden'
267 ) for grep { -e } $imgsrc->($row);
269 print $row->input($_ => delete $wordcol{$_}) for @span;
274 if (not $row->{ref}) {
275 printf '<li><label for="%s">%s</label><div><ul class="inline multiinput" id="%1$s">',
276 'trans', 'Translations';
277 my @children = !$row->{id} ? () :
278 $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
279 while (my ($lang, $val) = each %fields) {
280 $lang =~ s/^trans-// or next;
281 push @children, { lang => $lang, form => $val };
283 my %existing = map { $_->{lang} => 1 } $row, @children;
284 $existing{$_} or push @children, { lang => $_ } for @{$user->{editlang}};
286 for my $ref (@children) {
288 '<li><label for="%s" title="%3$s">%s </label>',
289 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
292 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
293 '<input id="%s" name="%1$s" value="%3$s" />',
294 "trans-$ref->{lang}", "$editorurl/$ref->{id}", Entity($ref->{form} // ''),
297 say '</ul></div></li>';
302 <input type="submit" value="Save" />
303 <input type="submit" value="New" formaction="<:= $editorurl :>?copy=cat" />
315 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
316 while (my $ref = $parents->hash) {
317 printf '<li><a href="%s/%d">%s</a></li>', $editorurl, $ref->{id}, Entity($ref->{form});
319 say "<li><strong>$_</strong></li>" for Entity($row->{form});
320 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
321 while (my $ref = $children->hash) {
322 printf '<li><a href="%s/%d">%s</a></li>', $editorurl, $ref->{id}, Entity($ref->{form});
325 <li><form action="<:= $editorurl :>">
326 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
327 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
328 <input type="submit" value="Add" />
333 <input type="search" name="q" value="" placeholder="search" /><button type="submit">🔍</button>