1 |
package Frey::DBIC::Designer; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
with 'Frey::Web'; |
6 |
with 'Frey::Config'; |
7 |
with 'Frey::Storage'; |
8 |
with 'Frey::jQuery'; |
9 |
|
10 |
has dbic_class => ( |
11 |
is => 'rw', |
12 |
isa => 'Str', |
13 |
required => 1, |
14 |
default => 'Reblog::Schema', |
15 |
); |
16 |
|
17 |
has dsn => ( |
18 |
is => 'rw', |
19 |
isa => 'Str', |
20 |
required => 1, |
21 |
default => 'DBI:mysql:database=reblog;host=127.0.0.1;port=13306', |
22 |
); |
23 |
|
24 |
has result_set => ( |
25 |
is => 'rw', |
26 |
isa => 'Str', |
27 |
required => 1, |
28 |
default => 'Items', |
29 |
); |
30 |
|
31 |
has order_by => ( |
32 |
is => 'rw', |
33 |
isa => 'Str', |
34 |
required => 1, |
35 |
default => 'insert_timestamp desc', |
36 |
); |
37 |
|
38 |
has page => ( |
39 |
is => 'rw', |
40 |
isa => 'Int', |
41 |
required => 1, |
42 |
default => 1, |
43 |
); |
44 |
|
45 |
has columns => ( |
46 |
is => 'rw', |
47 |
isa => 'ArrayRef[Str]', |
48 |
); |
49 |
|
50 |
has dnd_serialize => ( |
51 |
is => 'rw', |
52 |
isa => 'Str', |
53 |
); |
54 |
|
55 |
sub as_markup { |
56 |
my ($self) = @_; |
57 |
|
58 |
my $dbic_class = $self->dbic_class; |
59 |
my $dsn = $self->dsn; |
60 |
my $schema; |
61 |
|
62 |
my $code = qq{ |
63 |
use $dbic_class ; |
64 |
\$schema = $dbic_class->connect("$dsn", '', ''); |
65 |
}; |
66 |
|
67 |
eval $code; |
68 |
die $@ if $@; |
69 |
|
70 |
$schema->storage->debug(1); # XXX dump storage generated SQL |
71 |
|
72 |
my $attrs; |
73 |
|
74 |
$attrs->{ $_ } = $self->$_ foreach ( grep { $self->$_ } ( qw/page order_by/ ) ); |
75 |
warn "# attrs ", $self->dump( $attrs ); |
76 |
|
77 |
my $rs = $schema->resultset( $self->result_set ) |
78 |
->published |
79 |
->search( undef, $attrs ) |
80 |
; |
81 |
|
82 |
my @columns = @{ $self->columns } if $self->columns; |
83 |
my @all_columns = $rs->first->columns; |
84 |
@columns = @all_columns unless @columns; |
85 |
warn "# columns ", $self->dump( @columns ); |
86 |
|
87 |
my $rows; |
88 |
|
89 |
while ( my $row = $rs->next ) { |
90 |
# my %row = $row->get_columns; |
91 |
|
92 |
my $row; |
93 |
|
94 |
foreach my $name ( @columns ) { |
95 |
my $v = $row->$name; |
96 |
$v = '<code>NULL</code>' if ! defined $v; |
97 |
$row .= qq|<td title="$name">$v</td>|; |
98 |
} |
99 |
|
100 |
my $feed; |
101 |
if ( $feed = $row->feed ) { |
102 |
$row .= qq|<td> $feed->title </td>|; |
103 |
} |
104 |
|
105 |
$rows .= qq|<tr>$row</tr>\n|; |
106 |
} |
107 |
|
108 |
$self->add_css(qq| |
109 |
#column-editor { |
110 |
border: 1px solid #888; |
111 |
background: #ffc; |
112 |
position: absolute; |
113 |
top: 1em; |
114 |
right: 1em; |
115 |
z-index: 10; |
116 |
opacity: .2; |
117 |
filter: alpha(opacity=20); |
118 |
} |
119 |
#column-editor:hover { |
120 |
opacity: 1; |
121 |
filter: alpha(opacity=100); |
122 |
} |
123 |
#column-editor small { |
124 |
color: #888; |
125 |
} |
126 |
|); |
127 |
|
128 |
my $visible_columns; |
129 |
$visible_columns->{$_}++ foreach @columns; |
130 |
|
131 |
@all_columns = ( |
132 |
# dnd_serialize is in php format id[]=foo&id[]=bar |
133 |
map { s/[^=]+=// ; $_ } split(/&/, $self->dnd_serialize ) |
134 |
) if $self->dnd_serialize; |
135 |
warn "# column-editor for ", $self->dump( @all_columns ); |
136 |
|
137 |
my $dnd_serialize = join('&', @all_columns); |
138 |
my $column_editor = qq| |
139 |
<form id="column-editor" method="get"><!-- FIXME we really should do post, but it doesn't work! --> |
140 |
<table id="column-table"> |
141 |
<input type="submit" value="refresh"> |
142 |
| |
143 |
|
144 |
. join("\n", map { |
145 |
my $checked = ''; |
146 |
$checked = 'checked=1' if $visible_columns->{$_}; |
147 |
qq| |
148 |
<tr id="$_"> |
149 |
<td><input type="checkbox" name="columns" value="$_" $checked></td> |
150 |
<td>$_</td> |
151 |
</tr> |
152 |
| |
153 |
} @all_columns ) |
154 |
|
155 |
. qq| |
156 |
</table> |
157 |
<input type="submit" value="refresh"> |
158 |
<input type="hidden" name="dnd_serialize" id="dnd_serialize" value="$dnd_serialize"> |
159 |
</form> |
160 |
| |
161 |
; |
162 |
|
163 |
$self->add_js( 'static/Frey/jQuery/jquery.tablednd_0_5.js' ); |
164 |
$self->add_js(q| |
165 |
$(document).ready(function() { |
166 |
// Initialise the table |
167 |
$("#column-table").tableDnD({ |
168 |
onDrop: function(table, row) { |
169 |
$('#dnd_serialize').val( |
170 |
$('#column-table').tableDnDSerialize() |
171 |
); |
172 |
}, |
173 |
}); |
174 |
}); |
175 |
|); |
176 |
|
177 |
my $total = $rs->pager->total_entries; |
178 |
|
179 |
my $header = qq|<tr><th>| . join(qq|</th><th>|, @columns) . qq|</th></tr>|; |
180 |
|
181 |
my $html = qq| |
182 |
Rows: <b>$total</b> |
183 |
$column_editor |
184 |
<table> |
185 |
$header |
186 |
$rows |
187 |
</table> |
188 |
|; |
189 |
|
190 |
return $html; |
191 |
} |
192 |
|
193 |
=head1 SEE ALSO |
194 |
|
195 |
DBIx::Master Class |
196 |
|
197 |
L<http://www.shadowcat.co.uk/catalyst/-talks/yapc-na-2008/dbix-masterclass.xul> presentation |
198 |
|
199 |
L<http://www.shadowcat.co.uk/archive/conference-video/yapc-eu-2008/dbic-masterclass/> video |
200 |
|
201 |
=cut |
202 |
|
203 |
1; |