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 '' => ['(reference)'],
76 nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
77 en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
78 eo => [qq'<span style="color:green">\N{BLACK STAR}</span>', 'esperanto'],
79 ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'],
80 zh => ["\N{REGIONAL INDICATOR SYMBOL LETTER C}\N{REGIONAL INDICATOR SYMBOL LETTER N}", '中文'],
81 la => ["\N{PUSHPIN}", 'latin'],
83 my @wordcols = pairkeys
85 lang => {-label => 'Language', -select => {
86 map { $_ => "@{$lang{$_}}" } keys %lang
88 cat => [{-label => 'Category'}, 'ref'],
89 ref => {-label => 'Reference'},
91 {-label => 'Level', -select => sub {
94 essential ubiquitous basic common distinctive specialised rare invisible
97 ('' => 'parent') x (defined $row->{ref}),
98 map { $_ => $enum[$_] } 0 .. $#enum
103 cover => {-label => 'Highlighted', type => 'checkbox'},
104 grade => {-label => 'Order', type => 'number'},
105 form => {-label => 'Title'},
106 alt => {-label => 'Synonyms', -multiple => 1},
107 wptitle => {-label => 'Wikipedia'},
108 source => {-label => 'Image', -json => 'image', -src => sub {
109 return "data/word/org/$_[0]->{id}.jpg";
111 convert => {-label => 'Convert options', -json => 'image', -multiple => 1, -src => sub {
112 return "data/word/32/$_[0]->{id}.jpg";
114 crop32 => {-json => 'image', type => 'hidden'}, # set by javascript interface
115 story => {-label => 'Story', type => 'textarea', hidden => 'hidden'},
118 if (my $search = $fields{q}) {
119 my %filter = $search eq '^' ? (cat => undef, ref => undef) :
120 (form => {ilike => '%'.parseinput($search).'%'});
121 my $results = $db->select(word => '*', \%filter);
122 say '<h1>Search</h1><ul>';
123 printf("<li><small>%s</small> %s %s</li>\n",
124 $_->{id}, showlink($_->{form}, "$editorurl/$_->{id}"),
125 sprintf('<img src="/%s" style="height:3ex; width:auto" />', $wordcol{convert}->{-src}->($_)) x defined $_->{image}
126 ) for $results->hashes;
131 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
134 $row = $db->select(word => '*', $find)->hash
135 or Abort("Word not found", 404);
138 if (exists $get{copy}) {
139 $row = {%{$row}{ qw(prio lang cat) }};
141 elsif (defined $post{form}) {{
143 return if not length $_[0];
145 return Encode::decode_utf8($_[0]);
148 my $replace = $row; # currently stored
149 $row = {}; # proposed update
150 while (my ($col, $colinfo) = each %wordcol) {
151 ref $colinfo eq 'HASH' or $colinfo = {};
152 my @val = map { parseinput($_) } $post{'@'.$col}->@*;
153 my $val = $colinfo->{-multiple} && @val ? \@val : $val[-1];
154 if (my $jsoncol = $colinfo->{-json}) {
155 $row->{$jsoncol}->{$col} = $val; # hash will be encoded
156 ref $_ eq 'HASH' or $_ = decode_json($_) for $replace->{$jsoncol} // ();
162 my $imagecol = $row->{image}; # backup image subcolumns
163 while (my ($col, $val) = each %{$row}) {
164 # convert json subcolumns to database string
165 ref $val eq 'HASH' or next;
166 $val = { %{$_}, %{$val} } for $replace->{$col} // (); # preserve unknown
167 defined $val->{$_} or delete $val->{$_} for keys %{$val}; # delete emptied
168 $row->{$col} = encode_json($val);
171 if (!$row->{form} and $row->{lang}) {
172 if ($row->{ref} ne 'delete') {
174 "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
178 $db->delete(word => $find);
179 Alert("Entry removed");
185 my %res = (returning => '*');
186 $row->{creator} = $user->{id} unless $find;
187 $row->{updated} = ['now()'];
188 my $query = $find ? $db->update(word => $row, $find, \%res) :
189 $db->insert(word => $row, \%res);
192 Alert("Entry could not be saved", $@);
197 while (my ($lang, $val) = each %post) {
199 $lang =~ s/^trans-// or next;
200 $val = parseinput($val) or next;
207 $subrow{wptitle} = $1 if $subrow{form} =~ s/\h*\[(.*)\]$//; # [Link] shorthand
208 $subrow{alt} = [split m{/}, $1] if $subrow{form} =~ s{/(\S.*)}{}; # /alternates shorthand
209 $db->insert(word => \%subrow);
210 delete $fields{$field};
213 } or Alert('Error creating translation entries', $@);
215 require Shiar_Sheet::ImagePrep;
216 my $image = Shiar_Sheet::ImagePrep->new($wordcol{source}->{-src}->($row));
218 ($imagecol->{source} // '') ne ($replace->{image}->{source} // '') or return;
219 $image->download($imagecol->{source});
221 !$@ or Alert(["Source image not found", $@]);
223 $reimage ||= $row->{convert} ~~ $replace->{image}->{convert}; # different
224 $reimage ||= $row->{cover} ~~ $replace->{image}->{cover}; # resize
225 $reimage++ if $fields{rethumb}; # force refresh
228 $image->generate($wordcol{convert}->{-src}->($row), $imagecol);
230 my ($warn, @details) = ref $@ ? @{$@} : $@;
231 Alert([ "Thumbnail image not generated", $warn ], @details);
236 $row->{lang} //= $user->{editlang}->[0] unless exists $row->{lang};
237 $row->{$_} = $get{$_} for keys %get;
238 $row->{prio} = defined $row->{ref} ? undef : 4 unless exists $row->{prio};
242 my $imagerow = $row->{image} && JSON->new->decode(delete $row->{image}) || {};
243 while (my ($col, $val) = each %{$imagerow}) {
247 } or Alert("Error decoding image metadata", $@);
249 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
250 bless $row, 'Shiar_Sheet::FormRow';
252 <h1>Words <:= $title :></h1>
256 <form action="?" method="post">
257 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
260 for my $col (@wordcols) {
261 my $info = $wordcol{$col} or next;
262 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
263 next if delete $attr->{hidden} and not $row->{$col};
264 my $title = ref $attr ? delete $attr->{-label} : $attr;
265 printf '<li><label for="%s">%s</label><div>', $col, $title;
266 printf '<span class=inline>';
267 print $row->input($col => $attr);
268 if (my $imgsrc = $attr->{-src}) {
269 my $hide = $col eq 'source';
270 printf '<figure id="%spreview">', $col unless $hide;
271 printf('<img src="/%s" alt="%s"%s />',
272 $_, $row->{form}, $hide && qq( id="${col}preview" hidden)
273 ) for grep { -e } $imgsrc->($row);
274 printf '</figure>' unless $hide;
276 print $row->input($_ => delete $wordcol{$_}) for @span;
281 if (not $row->{ref}) {
282 printf '<li><label for="%s">%s</label><div><ul class="inline multiinput" id="%1$s">',
283 'trans', 'Translations';
284 my @children = !$row->{id} ? () :
285 $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
286 while (my ($lang, $val) = each %fields) {
287 $lang =~ s/^trans-// or next;
288 push @children, { lang => $lang, form => $val };
290 my %existing = map { $_->{lang} => 1 } $row, @children;
291 $existing{$_} or push @children, { lang => $_ } for @{$user->{editlang}};
293 for my $ref (@children) {
295 '<li><label for="%s" title="%3$s">%s </label>',
296 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
299 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
300 '<input id="%s" name="%1$s" value="%3$s" />',
301 "trans-$ref->{lang}", "$editorurl/$ref->{id}", Entity($ref->{form} // ''),
304 say '</ul></div></li>';
309 <input type="submit" value="Save" />
310 <input type="submit" value="New" formaction="<:= $editorurl :>?copy=cat" />
322 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
323 while (my $ref = $parents->hash) {
324 printf '<li><a href="%s/%d">%s</a></li>', $editorurl, $ref->{id}, Entity($ref->{form});
326 say "<li><strong>$_</strong></li>" for Entity($row->{form});
327 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
328 while (my $ref = $children->hash) {
329 printf '<li><a href="%s/%d">%s</a></li>', $editorurl, $ref->{id}, Entity($ref->{form});
332 <li><form action="<:= $editorurl :>">
333 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
334 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
335 <input type="submit" value="Add" />
340 <input type="search" name="q" value="" placeholder="search" /><button type="submit">🔍</button>