1 |
package Strix::View; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Template::Declare::Tags; # defaults to 'HTML' |
7 |
use base 'Template::Declare'; |
8 |
use Data::Dump qw/dump/; |
9 |
|
10 |
=head1 NAME |
11 |
|
12 |
Strix::View - examine Strix data |
13 |
|
14 |
=cut |
15 |
|
16 |
=head1 METHODS |
17 |
|
18 |
=head2 user |
19 |
|
20 |
=cut |
21 |
|
22 |
use Strix::User; |
23 |
use Frey::HTML; |
24 |
|
25 |
my @callbacks; |
26 |
|
27 |
sub gen_link { |
28 |
my ($text, $code) = @_; |
29 |
push @callbacks, $code; |
30 |
return qq{<a href="?cb=$#callbacks">$text</a>}; |
31 |
} |
32 |
|
33 |
sub process_links { |
34 |
my $request = shift; |
35 |
my $cb = $request->param('cb'); |
36 |
if (exists $callbacks[$cb]) { |
37 |
$callbacks[$cb]->($request); |
38 |
delete $callbacks[$cb]; |
39 |
} |
40 |
} |
41 |
|
42 |
use Data::Dumper; |
43 |
$Data::Dumper::Deparse = 1; |
44 |
|
45 |
template 'user' => sub { |
46 |
my ( $self, $req ) = @_; |
47 |
my $id = $req->param('id'); |
48 |
|
49 |
my $continue = 1; |
50 |
while ( $continue ) { |
51 |
|
52 |
my $user = eval { Strix::User->new( id => $id ); }; |
53 |
warn "## user $id: $@", dump( $user ); |
54 |
|
55 |
if ( ! $user ) { |
56 |
$req->conn->send_status_line( 404, "user $id" ); |
57 |
$req->print( Frey::HTML->page( 'error', $req, "Can't find user with id $id\n$@" ) ); |
58 |
} else { |
59 |
$req->print( Frey::HTML->page( 'show-user', $req, $user ) ); |
60 |
# $req->print( '<pre>' . dump( $user ) . '</pre>' ); |
61 |
}; |
62 |
|
63 |
@callbacks = (); |
64 |
$req->print('<tt>'.join(' ', |
65 |
$id, |
66 |
gen_link('+' => sub { $id++ }), |
67 |
gen_link('-' => sub { $id-- }), |
68 |
gen_link('X' => sub { $continue = 0 }), |
69 |
).'</tt>'); |
70 |
$req->print('<pre>'.Dumper( @callbacks ).'</pre>'); |
71 |
$req->next; |
72 |
process_links($req); |
73 |
} |
74 |
}; |
75 |
|
76 |
template 'show-user' => sub { |
77 |
my ( $self, $req, $user ) = @_; |
78 |
table { |
79 |
foreach my $f ( keys %$user ) { |
80 |
row { |
81 |
cell { $f } |
82 |
cell { $user->{$f} } |
83 |
} |
84 |
} |
85 |
} |
86 |
}; |
87 |
|
88 |
1; |