4 title => 'words cheat sheet admin',
8 <link rel="stylesheet" type="text/css" media="all" href="/editor.css" />
9 <script src="/writer.js"></script>
13 use List::Util qw( pairs pairkeys );
14 use Shiar_Sheet::FormRow;
17 require Shiar_Sheet::DB;
18 Shiar_Sheet::DB->connect;
19 } or Abort('Database error', 501, $@);
22 my $rootpath = ($ENV{REQUEST_URI} // '/writer') =~ s{(?<!^)/.+}{}r;
23 if (defined $post{username}) {
24 $cookie{login} = EncodeURI(join ':', @post{qw( username pass )});
26 elsif (exists $fields{logout}) {
28 if (AddCookie(CGI::Cookie->new(
34 delete $cookie{login};
35 die "Logged out as requested\n";
37 Alert("Failed to log out", "Login cookie could not be removed.");
40 my $cookiedata = $cookie{login} or return;
41 my ($name, $key) = split /[:\v]/, DecodeURI($cookiedata);
42 my %rowmatch = (username => $name, pass => $key);
43 my $found = $db->select(login => '*', \%rowmatch)->hash
44 or die "Invalid user or password\n";
48 my $httpcookie = CGI::Cookie->new(
50 -value => join(':', @{$found}{qw( username pass )}),
52 ) or die "prepared object is empty\n";
53 AddCookie($httpcookie->as_string);
54 } or Abort(["Unable to create login cookie", $@], 403);
58 say '<h1>Login to edit words</h1>';
59 Alert('Access denied', $@) if $@;
60 say '<form action="?" method="post" class="inline"><ul>';
61 my $loginform = bless {%post}, 'Shiar_Sheet::FormRow';
62 say '<li>', $loginform->input(@{$_}), '</li>' for pairs (
63 username => {-label => 'User name'},
64 pass => {-label => 'Password', type => 'password'},
66 say '<li><input type="submit" value="Login" /></li>';
72 nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
73 en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
74 eo => ['<span style="color:green">★</span>', 'esperanto'],
75 ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'русский'],
76 zh => ["\N{REGIONAL INDICATOR SYMBOL LETTER C}\N{REGIONAL INDICATOR SYMBOL LETTER N}", '中文'],
77 la => ["\N{PUSHPIN}", 'latin'],
79 my @wordcols = pairkeys
81 lang => {-label => 'Language', -select => {
82 map { $_ => "@{$lang{$_}}" } keys %lang
84 cat => [{-label => 'Category'}, 'ref'],
85 ref => {-label => 'Reference'},
87 {-label => 'Level', -select => sub {
89 my @enum = qw[ essential basic common distinctive optional invisible ];
91 ('' => 'parent') x (defined $row->{ref}),
92 map { $_ => $enum[$_] } 0 .. $#enum
97 cover => {-label => 'Highlighted', type => 'checkbox'},
98 grade => {-label => 'Order', type => 'number'},
99 form => {-label => 'Title'},
100 alt => {-label => 'Synonyms', -multiple => 1},
101 wptitle => {-label => 'Wikipedia'},
102 source => {-label => 'Image', -src => sub {
103 return "data/word/org/$_[0]->{id}.jpg";
105 thumb => {-label => 'Convert options', -multiple => 1, -src => sub {
106 return "data/word/en/$_[0]->{id}.jpg";
108 story => {-label => 'Story', type => 'textarea'},
111 if (my $search = $fields{q}) {
112 my %filter = (form => {ilike => '%'.$search.'%'});
113 my $results = $db->select(word => '*', \%filter);
114 say '<h1>Search</h1><ul>';
115 printf("<li><small>%s</small> %s %s</li>\n",
116 $_->{id}, showlink($_->{form}, "/writer/$_->{id}"),
117 sprintf('<img src="/%s" style="height:3ex; width:auto" />', $wordcol{thumb}->{-src}->($_)) x defined $_->{thumb}
118 ) for $results->hashes;
123 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
126 $row = $db->select(word => '*', $find)->hash
127 or Abort("Word not found", 404);
130 if (exists $get{copy}) {
131 $row = {%{$row}{ qw(prio lang cat) }};
133 elsif (defined $post{form}) {{
135 return if not length $_[0];
137 return Encode::decode_utf8($_[0]);
142 ref $wordcol{$_} eq 'HASH' && $wordcol{$_}->{-multiple} ?
143 [ map { parseinput($_) } $post{'@'.$_}->@* ] :
144 scalar parseinput($post{$_})
148 if ($row->{ref} ne 'delete') {
150 "Confirm removal by setting <em>Reference</em> to <q>delete</q>."
154 $db->delete(word => $find);
155 Alert("Entry removed");
161 my %res = (returning => '*');
162 $row->{creator} = $user->{id} unless $find;
163 $row->{updated} = ['now()'];
164 my $query = $find ? $db->update(word => $row, $find, \%res) :
165 $db->insert(word => $row, \%res);
168 Alert("Entry could not be saved", $@);
173 while (my ($lang, $val) = each %post) {
175 $lang =~ s/^trans-// or next;
176 $val = parseinput($val) or next;
183 $subrow{wptitle} = $1 if $subrow{form} =~ s/\h*\[(.*)\]$//; # [Link] shorthand
184 $subrow{alt} = [split m{/}, $1] if $subrow{form} =~ s{/(\S.*)}{}; # /alternates shorthand
185 $db->insert(word => \%subrow);
186 delete $fields{$field};
189 } or Alert('Error creating translation entries', $@);
191 require Shiar_Sheet::ImagePrep;
192 my $image = Shiar_Sheet::ImagePrep->new($wordcol{source}->{-src}->($row));
194 ($row->{source} // '') ne ($replace->{source} // '') or return;
195 $image->download($row->{source});
197 !$@ or Alert(["Source image not found", $@]);
199 $reimage ||= $row->{thumb} ~~ $replace->{thumb}; # different convert
200 $reimage ||= $row->{cover} ~~ $replace->{cover}; # resize
201 $reimage++ if $fields{rethumb}; # force refresh
204 $image->convert($wordcol{thumb}->{-src}->($row), $row->{thumb});
206 my ($warn, @details) = ref $@ ? @{$@} : $@;
207 Alert([ "Thumbnail image not generated", $warn ], @details);
212 $row->{lang} //= $user->{editlang}->[0];
213 $row->{$_} = $get{$_} for keys %get;
214 $row->{prio} = defined $row->{ref} ? undef : 1 unless exists $row->{prio};
217 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
218 bless $row, 'Shiar_Sheet::FormRow';
220 <h1>Words <:= $title :></h1>
224 <form action="?" method="post">
225 <input id="id" name="id" value="<:= $row->{id} // '' :>" type="hidden" />
228 for my $col (@wordcols) {
229 my $info = $wordcol{$col} or next;
230 my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
231 my $title = ref $attr ? delete $attr->{-label} : $attr;
232 printf '<li><label for="%s">%s</label><p>', $col, $title;
233 printf '<span class=inline>';
234 print $row->input($col => $attr);
235 if (my $imgsrc = $attr->{-src}) {
236 printf('<img id="%spreview" src="/%s" alt="%s"%s />',
237 $col, $_, $row->{form}, $col eq 'source' && ' hidden'
238 ) for grep { -e } $imgsrc->($row);
240 print $row->input($_ => delete $wordcol{$_}) for @span;
245 if (not $row->{ref}) {
246 printf '<li><label for="%s">%s</label><div><ul class="inline" id="%1$s">',
247 'trans', 'Translations';
248 my @children = !$row->{id} ? () :
249 $db->select(word => '*', {ref => $row->{id}}, 'lang, id')->hashes;
250 while (my ($lang, $val) = each %fields) {
251 $lang =~ s/^trans-// or next;
252 push @children, { lang => $lang, form => $val };
254 my %existing = map { $_->{lang} => 1 } $row, @children;
255 $existing{$_} or push @children, { lang => $_ } for @{$user->{editlang}};
257 for my $ref (@children) {
259 '<li><label for="%s" title="%3$s">%s </label>',
260 "trans-$ref->{lang}", @{$lang{ $ref->{lang} }}, # flag, name
263 $ref->{id} ? '<a id="%s" href="%s">%s</a></li>' :
264 '<input id="%s" name="%1$s" value="%3$s" />',
265 "trans-$ref->{lang}", "/writer/$ref->{id}", Entity($ref->{form} // ''),
268 say '</ul></div></li>';
273 <input type="submit" value="Save" />
274 <input type="submit" value="New" formaction="/writer?copy=cat" />
286 my $parents = $db->select(word => '*', [{id => $row->{cat}}, {id => $row->{ref}}]);
287 while (my $ref = $parents->hash) {
288 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
290 say "<li><strong>$_</strong></li>" for Entity($row->{form});
291 my $children = $db->select(word => '*', {cat => $row->{id}, ref => undef}, 'grade, id');
292 while (my $ref = $children->hash) {
293 printf '<li><a href="/writer/%d">%s</a></li>', $ref->{id}, Entity($ref->{form});
296 <li><form action="/writer">
297 <input type="hidden" name="cat" value="<:= $row->{id} :>" />
298 <input type="hidden" name="lang" value="<:= $row->{lang} :>" />
299 <input type="submit" value="Add" />