X-Git-Url: http://git.shiar.net/sheet.git/blobdiff_plain/df43fa85051ebf955c1f42dab2d789b5d6a68aa6..3207fe934ef25ea7980d123c83c2711cf8964646:/writer.plp?ds=inline
diff --git a/writer.plp b/writer.plp
index 62d26d7..cd2e2bf 100644
--- a/writer.plp
+++ b/writer.plp
@@ -5,112 +5,77 @@ Html({
version => '1.0',
nocache => 1,
raw => <<'EOT',
-
-
+
EOT
});
use List::Util qw( pairs pairkeys );
+use Shiar_Sheet::FormRow;
+use JSON;
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, $@);
-$db->abstract->{array_datatypes}++;
+
+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 @wordcols = pairkeys
my %wordcol = (
@@ -120,9 +85,14 @@ my %wordcol = (
cat => [{-label => 'Category'}, 'ref'],
ref => {-label => 'Reference'},
prio => [
- {-label => 'Level', -select => [qw(
- essential basic common distinctive rare invisible
- )]},
+ {-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'},
@@ -130,11 +100,28 @@ my %wordcol = (
form => {-label => 'Title'},
alt => {-label => 'Synonyms', -multiple => 1},
wptitle => {-label => 'Wikipedia'},
- source => {-label => 'Image'},
- thumb => {-label => 'Convert options', -multiple => 1},
+ source => {-label => 'Image', -json => 'image', -src => sub {
+ return "data/word/org/$_[0]->{id}.jpg";
+ }},
+ convert => {-label => 'Convert options', -json => 'image', -multiple => 1, -src => sub {
+ return "data/word/en/$_[0]->{id}.jpg";
+ }},
+ story => {-label => 'Story', type => 'textarea', hidden => 'hidden'},
);
-my ($find) = map {{id => $_}} $fields{id} || $Request || ();
+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{convert}->{-src}->($_)) x defined $_->{image}
+ ) for $results->hashes;
+ say "
@@ -329,23 +247,33 @@ bless $row, 'Shiar_Sheet::FormRow';
for my $col (@wordcols) {
my $info = $wordcol{$col} or next;
my ($attr, @span) = ref $info eq 'ARRAY' ? @{$info} : $info;
+ next if delete $attr->{hidden} and not $row->{$col};
my $title = ref $attr ? delete $attr->{-label} : $attr;
printf '