X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/3d0e1e761a53934cfdadf6e7e28ced4ec6e603e3..e26741ab236cbfaa89aa82d121c224137801a63b:/writer.plp
diff --git a/writer.plp b/writer.plp
index f4819b7..b5f5032 100644
--- a/writer.plp
+++ b/writer.plp
@@ -5,119 +5,122 @@ Html({
version => '1.0',
nocache => 1,
raw => <<'EOT',
-
-
+
EOT
});
use List::Util qw( pairs pairkeys );
+use Shiar_Sheet::FormRow;
my $db = eval {
- my @dbinfo = (
- 'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
- ) or die "database not configured\n";
- require DBIx::Simple;
- DBIx::Simple->new(@dbinfo[0..2], {
- RaiseError => 1,
- pg_enable_utf8 => 1,
- });
+ require Shiar_Sheet::DB;
+ Shiar_Sheet::DB->connect;
} or Abort('Database error', 501, $@);
-my @wordcols = (
- lang => 'Language',
- cat => 'Category',
- ref => undef, # included with cat
- grade => undef, # "
- prio => 'Level',
- cover => undef, # included with prio
- form => 'Translation',
- alt => 'Synonyms',
- wptitle => 'Wikipedia',
- source => 'Image',
- thumb => 'Convert options',
+my $user = eval {
+ my $rootpath = ($ENV{REQUEST_URI} // '/writer') =~ s{(?new(
+ -name => 'login',
+ -value => '',
+ -path => $rootpath,
+ -expires => 'now',
+ )->as_string)) {
+ delete $cookie{login};
+ die "Logged out as requested\n";
+ }
+ Alert("Failed to log out", "Login cookie could not be removed.");
+ }
+
+ my $cookiedata = $cookie{login} or return;
+ my ($name, $key) = split /[:\v]/, DecodeURI($cookiedata);
+ my %rowmatch = (username => $name, pass => $key);
+ my $found = $db->select(login => '*', \%rowmatch)->hash
+ or die "Invalid user or password\n";
+
+ eval {
+ require CGI::Cookie;
+ my $httpcookie = CGI::Cookie->new(
+ -name => 'login',
+ -value => join(':', @{$found}{qw( username pass )}),
+ -path => $rootpath,
+ ) or die "prepared object is empty\n";
+ AddCookie($httpcookie->as_string);
+ } or Abort(["Unable to create login cookie", $@], 403);
+
+ return $found;
+} or do {
+ say '
Login to edit words
';
+ Alert('Access denied', $@) if $@;
+ say '';
+ exit;
+};
+
+my %lang = (
+ nl => ["\N{REGIONAL INDICATOR SYMBOL LETTER N}\N{REGIONAL INDICATOR SYMBOL LETTER L}", 'nederlands'],
+ en => ["\N{REGIONAL INDICATOR SYMBOL LETTER G}\N{REGIONAL INDICATOR SYMBOL LETTER B}", 'english'],
+ eo => ['â ', 'esperanto'],
+ ru => ["\N{REGIONAL INDICATOR SYMBOL LETTER R}\N{REGIONAL INDICATOR SYMBOL LETTER U}", 'ÑÑÑÑкий'],
+ zh => ["\N{REGIONAL INDICATOR SYMBOL LETTER C}\N{REGIONAL INDICATOR SYMBOL LETTER N}", 'ä¸æ'],
+ la => ["\N{PUSHPIN}", 'latin'],
);
-my @prioenum = qw( essential basic common distinctive rare invisible );
-my ($find) = map {{id => $_}} $fields{id} || $Request || ();
+my @wordcols = pairkeys
+my %wordcol = (
+ lang => {-label => 'Language', -select => {
+ map { $_ => "@{$lang{$_}}" } keys %lang
+ }},
+ cat => [{-label => 'Category'}, 'ref'],
+ ref => {-label => 'Reference'},
+ prio => [
+ {-label => 'Level', -select => sub {
+ my ($row) = @_;
+ my @enum = qw[ essential basic common distinctive optional invisible ];
+ return {
+ ('' => 'parent') x (defined $row->{ref}),
+ map { $_ => $enum[$_] } 0 .. $#enum
+ };
+ }},
+ 'cover', 'grade',
+ ],
+ cover => {-label => 'Highlighted', type => 'checkbox'},
+ grade => {-label => 'Order', type => 'number'},
+ form => {-label => 'Title'},
+ alt => {-label => 'Synonyms', -multiple => 1},
+ wptitle => {-label => 'Wikipedia'},
+ source => {-label => 'Image', -src => sub {
+ return "data/word/org/$_[0]->{id}.jpg";
+ }},
+ thumb => {-label => 'Convert options', -multiple => 1, -src => sub {
+ return "data/word/en/$_[0]->{id}.jpg";
+ }},
+ story => {-label => 'Story', type => 'textarea'},
+);
+
+if (my $search = $fields{q}) {
+ my %filter = (form => {ilike => '%'.$search.'%'});
+ my $results = $db->select(word => '*', \%filter);
+ say '
Search
';
+ printf("
%s %s %s
\n",
+ $_->{id}, showlink($_->{form}, "/writer/$_->{id}"),
+ sprintf('', $wordcol{thumb}->{-src}->($_)) x defined $_->{thumb}
+ ) for $results->hashes;
+ say "