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>
16 use List::Util qw( pairs pairkeys );
17 use Shiar_Sheet::FormRow;
21 require Shiar_Sheet::DB;
22 Shiar_Sheet::DB->connect;
23 } or Abort('Database error', 501, $@);
26 if (defined $post{username}) {
27 $cookie{login} = EncodeURI(join ':', @post{qw( username pass )});
29 elsif (exists $fields{logout}) {
31 if (AddCookie(CGI::Cookie->new(
37 delete $cookie{login};
38 die "Logged out as requested\n";
40 Alert("Failed to log out", "Login cookie could not be removed.");
43 my $cookiedata = $cookie{login} or return;
44 my ($name, $key) = split /[:\v]/, DecodeURI($cookiedata);
45 my %rowmatch = (username => $name, pass => $key);
46 my $found = $db->select(login => '*', \%rowmatch)->hash
47 or die "Invalid user or password\n";
51 my $httpcookie = CGI::Cookie->new(
53 -value => join(':', @{$found}{qw( username pass )}),
55 ) or die "prepared object is empty\n";
56 AddCookie($httpcookie->as_string);
57 } or Abort(["Unable to create login cookie", $@], 403);
61 say '<h1>Login to edit words</h1>';
62 Alert('Access denied', $@) if $@;
63 say '<form action="?" method="post" class="inline"><ul>';
64 my $loginform = bless {%post}, 'Shiar_Sheet::FormRow';
65 say '<li>', $loginform->input(@{$_}), '</li>' for pairs (
66 username => {-label => 'User name'},
67 pass => {-label => 'Password', type => 'password'},
69 say '<li><input type="submit" value="Login" /></li>';
75 nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
76 en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
77 eo => [qq'<span style="color:green">\N{BLACK STAR}</span>', 'esperanto'],
78 ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'],
79 zh => ["\N{REGIONAL INDICATOR SYMBOL LETTER C}\N{REGIONAL INDICATOR SYMBOL LETTER N}", '中文'],
80 la => ["\N{PUSHPIN}", 'latin'],
82 my @wordcols = pairkeys
84 lang => {-label => 'Language', -select => {
85 map { $_ => "@{$lang{$_}}" } keys %lang
87 cat => [{-label => 'Category'}, 'ref'],
88 ref => {-label => 'Reference'},
90 {-label => 'Level', -select => sub {
92 my @enum = qw[ essential basic common distinctive optional invisible ];
94 ('' => 'parent') x (defined $row->{ref}),
95 map { $_ => $enum[$_] } 0 .. $#enum
100 cover => {-label => 'Highlighted', type => 'checkbox'},
101 grade => {-label => 'Order', type => 'number'},
102 form => {-label => 'Title'},
103 alt => {-label => 'Synonyms', -multiple => 1},
104 wptitle => {-label => 'Wikipedia'},
105 source => {-label => 'Image', -json => 'image', -src => sub {
106 return "data/word/org/$_[0]->{id}.jpg";
108 convert => {-label => 'Convert options', -json => 'image', -multiple => 1, -src => sub {
109 return "data/word/en/$_[0]->{id}.jpg";
111 story => {-label => 'Story', type => 'textarea', hidden => 'hidden'},
114 if (my $search = $fields{q}) {
115 my %filter = $search eq '^' ? (cat => undef, ref => undef) :
116 (form => {ilike => '%'.parseinput($search).'%'});
117 my $results = $db->select(word => '*', \%filter);
118 say '<h1>Search</h1><ul>';
119 printf("<li><small>%s</small> %s %s</li>\n",
120 $_->{id}, showlink($_->{form}, "$editorurl/$_->{id}"),
121 sprintf('<img src="/%s" style="height:3ex; width:auto" />', $wordcol{convert}->{-src}->($_)) x defined $_->{image}
122 ) for $results->hashes;
127 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
130 $row = $db->select(word => '*', $find)->hash
131 or Abort("Word not found", 404);
134 if (exists $get{copy}) {
135 $row = {%{$row}{ qw(prio lang cat) }};
137 elsif (defined $post{form}) {{
139 return if not length $_[0];
141 return Encode::decode_utf8($_[0]);
144 my $replace = $row; # currently stored
145 $row = {}; # proposed update
146 while (my ($col, $colinfo) = each %wordcol) {
147 ref $colinfo eq 'HASH' or $colinfo = {};
148 my @val = map { parseinput($_) } $post{'@'.$col}->@*;
149 my $val = $colinfo->{-multiple} && @val ? \@val : $val[-1];
150 if (my $jsoncol = $colinfo->{-json}) {
152 $row->{$jsoncol}->{$col} = $val; # hash will be encoded
158 my $imagecol = $row->{image}; # backup image subcolumns
159 ref $_ eq 'HASH' and $_ = encode_json($_) for values %{$row};
162 if ($row->{ref} ne 'delete') {
164 "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
168 $db->delete(word => $find);
169 Alert("Entry removed");
175 my %res = (returning => '*');
176 $row->{creator} = $user->{id} unless $find;
177 $row->{updated} = ['now()'];
178 my $query = $find ? $db->update(word => $row, $find, \%res) :
179 $db->insert(word => $row, \%res);
182 Alert("Entry could not be saved", $@);
187 while (my ($lang, $val) = each %post) {
189 $lang =~ s/^trans-// or next;
190 $val = parseinput($val) or next;
197 $subrow{wptitle} = $1 if $subrow{form} =~ s/\h*\[(.*)\]$//; # [Link] shorthand
198 $subrow{alt} = [split m{/}, $1] if $subrow{form} =~ s{/(\S.*)}{}; # /alternates shorthand
199 $db->insert(word => \%subrow);
200 delete $fields{$field};
203 } or Alert('Error creating translation entries', $@);
205 require Shiar_Sheet::ImagePrep;
206 my $image = Shiar_Sheet::ImagePrep->new($wordcol{source}->{-src}->($row));
208 ($imagecol->{source} // '') ne ($replace->{source} // '') or return;
209 $image->download($imagecol->{source});
211 !$@ or Alert(["Source image not found", $@]);
213 $reimage ||= $row->{image} ~~ $replace->{image}; # different source
214 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
215 $reimage++ if $fields{rethumb}; # force refresh
218 $image->convert($wordcol{convert}->{-src}->($row), $imagecol->{convert});
220 my ($warn, @details) = ref $@ ? @{$@} : $@;
221 Alert([ "Thumbnail image not generated", $warn ], @details);
226 $row->{lang} //= $user->{editlang}->[0];
227 $row->{$_} = $get{$_} for keys %get;
228 $row->{prio} = defined $row->{ref} ? undef : 1 unless exists $row->{prio};
232 my $imagerow = $row->{image} && JSON->new->decode(delete $row->{image}) || {};
233 while (my ($col, $val) = each %{$imagerow}) {
237 } or Alert("Error decoding image metadata", $@);
239 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
240 bless $row, 'Shiar_Sheet::FormRow';
242 <h1>Words <:= $title :></h1>
246 <form action="?" method="post">
247 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
250 for my $col (@wordcols) {
251 my $info = $wordcol{$col} or next;
252 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
253 next if delete $attr->{hidden} and not $row->{$col};
254 my $title = ref $attr ? delete $attr->{-label} : $attr;
255 printf '<li><label for="%s">%s</label><p>', $col, $title;
256 printf '<span class=inline>';
257 print $row->input($col => $attr);
258 if (my $imgsrc = $attr->{-src}) {
259 printf('<img id="%spreview" src="/%s" alt="%s"%s />',
260 $col, $_, $row->{form}, $col eq 'source' && ' hidden'
261 ) for grep { -e } $imgsrc->($row);
263 print $row->input($_ => delete $wordcol{$_}) for @span;
268 if (not $row->{ref}) {
269 printf '<li><label for="%s">%s</label><div><ul class="inline multiinput" id="%1$s">',
270 'trans', 'Translations';
271 my @children = !$row->{id} ? () :
272 $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
273 while (my ($lang, $val) = each %fields) {
274 $lang =~ s/^trans-// or next;
275 push @children, { lang => $lang, form => $val };
277 my %existing = map { $_->{lang} => 1 } $row, @children;
278 $existing{$_} or push @children, { lang => $_ } for @{$user->{editlang}};
280 for my $ref (@children) {
282 '<li><label for="%s" title="%3$s">%s </label>',
283 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
286 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
287 '<input id="%s" name="%1$s" value="%3$s" />',
288 "trans-$ref->{lang}", "$editorurl/$ref->{id}", Entity($ref->{form} // ''),
291 say '</ul></div></li>';
296 <input type="submit" value="Save" />
297 <input type="submit" value="New" formaction="<:= $editorurl :>?copy=cat" />
309 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
310 while (my $ref = $parents->hash) {
311 printf '<li><a href="%s/%d">%s</a></li>', $editorurl, $ref->{id}, Entity($ref->{form});
313 say "<li><strong>$_</strong></li>" for Entity($row->{form});
314 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
315 while (my $ref = $children->hash) {
316 printf '<li><a href="%s/%d">%s</a></li>', $editorurl, $ref->{id}, Entity($ref->{form});
319 <li><form action="<:= $editorurl :>">
320 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
321 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
322 <input type="submit" value="Add" />