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 $feed = $rs->next ) { |
90 |
# my %row = $feed->get_columns; |
91 |
|
92 |
my $row; |
93 |
|
94 |
foreach my $name ( @columns ) { |
95 |
my $v = $feed->$name; |
96 |
$v = '<code>NULL</code>' if ! defined $v; |
97 |
$row .= qq|<td title="$name">$v</td>|; |
98 |
} |
99 |
|
100 |
$row .= qq|<td>| . $feed->feed->title . qq|</td>| if $feed->can('feed'); |
101 |
|
102 |
$rows .= qq|<tr>$row</tr>\n|; |
103 |
} |
104 |
|
105 |
$self->add_css(qq| |
106 |
#column-editor { |
107 |
border: 1px solid #888; |
108 |
background: #ffc; |
109 |
position: absolute; |
110 |
top: 1em; |
111 |
right: 1em; |
112 |
z-index: 10; |
113 |
opacity: .2; |
114 |
filter: alpha(opacity=20); |
115 |
} |
116 |
#column-editor:hover { |
117 |
opacity: 1; |
118 |
filter: alpha(opacity=100); |
119 |
} |
120 |
#column-editor small { |
121 |
color: #888; |
122 |
} |
123 |
|); |
124 |
|
125 |
my $visible_columns; |
126 |
$visible_columns->{$_}++ foreach @columns; |
127 |
|
128 |
@all_columns = ( |
129 |
# dnd_serialize is in php format id[]=foo&id[]=bar |
130 |
map { s/[^=]+=// ; $_ } split(/&/, $self->dnd_serialize ) |
131 |
) if $self->dnd_serialize; |
132 |
warn "# column-editor for ", $self->dump( @all_columns ); |
133 |
|
134 |
my $dnd_serialize = join('&', @all_columns); |
135 |
my $column_editor = qq| |
136 |
<form id="column-editor" method="get"><!-- FIXME we really should do post, but it doesn't work! --> |
137 |
<table id="column-table"> |
138 |
<input type="submit" value="refresh"> |
139 |
| |
140 |
|
141 |
. join("\n", map { |
142 |
my $checked = ''; |
143 |
$checked = 'checked=1' if $visible_columns->{$_}; |
144 |
qq| |
145 |
<tr id="$_"> |
146 |
<td><input type="checkbox" name="columns" value="$_" $checked></td> |
147 |
<td>$_</td> |
148 |
</tr> |
149 |
| |
150 |
} @all_columns ) |
151 |
|
152 |
. qq| |
153 |
</table> |
154 |
<input type="submit" value="refresh"> |
155 |
<input type="hidden" name="dnd_serialize" id="dnd_serialize" value="$dnd_serialize"> |
156 |
</form> |
157 |
| |
158 |
; |
159 |
|
160 |
$self->add_js( 'static/Frey/jQuery/jquery.tablednd_0_5.js' ); |
161 |
$self->add_js(q| |
162 |
$(document).ready(function() { |
163 |
// Initialise the table |
164 |
$("#column-table").tableDnD({ |
165 |
onDrop: function(table, row) { |
166 |
$('#dnd_serialize').val( |
167 |
$('#column-table').tableDnDSerialize() |
168 |
); |
169 |
}, |
170 |
}); |
171 |
}); |
172 |
|); |
173 |
|
174 |
my $total = $rs->pager->total_entries; |
175 |
|
176 |
my $header = qq|<tr><th>| . join(qq|</th><th>|, @columns) . qq|</th></tr>|; |
177 |
|
178 |
my $html = qq| |
179 |
Rows: <b>$total</b> |
180 |
$column_editor |
181 |
<table> |
182 |
$header |
183 |
$rows |
184 |
</table> |
185 |
|; |
186 |
|
187 |
return $html; |
188 |
} |
189 |
|
190 |
=head1 SEE ALSO |
191 |
|
192 |
DBIx::Master Class |
193 |
|
194 |
L<http://www.shadowcat.co.uk/catalyst/-talks/yapc-na-2008/dbix-masterclass.xul> presentation |
195 |
|
196 |
L<http://www.shadowcat.co.uk/archive/conference-video/yapc-eu-2008/dbic-masterclass/> video |
197 |
|
198 |
=cut |
199 |
|
200 |
1; |