1 |
dpavlin |
1024 |
package Frey::DBIC::Designer; |
2 |
|
|
use Moose; |
3 |
|
|
|
4 |
|
|
extends 'Frey'; |
5 |
|
|
with 'Frey::Web'; |
6 |
|
|
with 'Frey::Config'; |
7 |
|
|
with 'Frey::Storage'; |
8 |
dpavlin |
1026 |
with 'Frey::jQuery'; |
9 |
dpavlin |
1024 |
|
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 remove_column => ( |
46 |
|
|
is => 'rw', |
47 |
|
|
isa => 'Str', |
48 |
|
|
); |
49 |
|
|
|
50 |
|
|
sub as_markup { |
51 |
|
|
my ($self) = @_; |
52 |
|
|
|
53 |
|
|
my $dbic_class = $self->dbic_class; |
54 |
|
|
my $dsn = $self->dsn; |
55 |
|
|
my $schema; |
56 |
|
|
|
57 |
|
|
my $code = qq{ |
58 |
|
|
use $dbic_class ; |
59 |
|
|
\$schema = $dbic_class->connect("$dsn", '', ''); |
60 |
|
|
}; |
61 |
|
|
|
62 |
|
|
eval $code; |
63 |
|
|
die $@ if $@; |
64 |
|
|
|
65 |
|
|
$schema->storage->debug(1); # XXX dump storage generated SQL |
66 |
|
|
|
67 |
|
|
my $attrs; |
68 |
|
|
|
69 |
|
|
$attrs->{ $_ } = $self->$_ foreach ( grep { $self->$_ } ( qw/page order_by/ ) ); |
70 |
|
|
warn "# attrs ", $self->dump( $attrs ); |
71 |
|
|
|
72 |
|
|
my $rs = $schema->resultset( $self->result_set ) |
73 |
|
|
->published |
74 |
|
|
->search( undef, $attrs ) |
75 |
|
|
; |
76 |
|
|
|
77 |
|
|
my $path = 'var/DBIC/' . $self->result_set . '.yaml'; |
78 |
|
|
my @columns; |
79 |
|
|
{ |
80 |
|
|
my $c = $self->load( $path ); |
81 |
|
|
@columns = @$c if $c; |
82 |
|
|
} |
83 |
|
|
warn "# load $path columns ", $self->dump( @columns ); |
84 |
dpavlin |
1026 |
my @all_columns = $rs->first->columns; |
85 |
|
|
@columns = @all_columns unless @columns; |
86 |
dpavlin |
1024 |
|
87 |
|
|
@columns = grep { $_ ne $self->remove_column } @columns if $self->remove_column; |
88 |
|
|
|
89 |
|
|
warn "# columns ", $self->dump( @columns ); |
90 |
|
|
$self->store( $path, [ @columns ] ); |
91 |
|
|
|
92 |
|
|
my $rows; |
93 |
|
|
|
94 |
|
|
while ( my $feed = $rs->next ) { |
95 |
|
|
# my %row = $feed->get_columns; |
96 |
|
|
|
97 |
|
|
my $row; |
98 |
|
|
|
99 |
|
|
foreach my $name ( @columns ) { |
100 |
|
|
my $v = $feed->$name; |
101 |
|
|
$v = '<code>NULL</code>' if ! defined $v; |
102 |
|
|
$row .= qq|<td title="$name">$v</td>|; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
$rows .= qq|<tr>$row</tr>\n|; |
106 |
|
|
} |
107 |
|
|
|
108 |
dpavlin |
1026 |
$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 |
|
|
} |
116 |
|
|
|); |
117 |
|
|
|
118 |
|
|
my @hidden_cols; |
119 |
|
|
{ |
120 |
|
|
my $col; |
121 |
|
|
$col->{$_}++ foreach @columns; |
122 |
|
|
@hidden_cols = grep { ! $col->{$_} } @all_columns; |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
my $column_editor = qq| |
126 |
|
|
<table id="column-editor"> |
127 |
|
|
<tr class="nodrag nodrop"><th>Visible</th></tr> |
128 |
|
|
| . join("\n", map { qq| |
129 |
|
|
<tr id="$_"><td>$_</td></tr> |
130 |
|
|
| } @columns ) |
131 |
|
|
. qq| |
132 |
|
|
<tr class="nodrag nodrop"><th>Hidden</th></tr> |
133 |
|
|
| . join("\n", map { qq| |
134 |
|
|
<tr id="$_"><td>$_</td></tr> |
135 |
|
|
| } @hidden_cols ) |
136 |
|
|
. qq| |
137 |
|
|
</table> |
138 |
|
|
| |
139 |
|
|
; |
140 |
|
|
|
141 |
|
|
$self->add_js( 'static/Frey/jQuery/jquery.tablednd_0_5.js' ); |
142 |
|
|
$self->add_js(q| |
143 |
|
|
$(document).ready(function() { |
144 |
|
|
// Initialise the table |
145 |
|
|
$("#column-editor").tableDnD(); |
146 |
|
|
}); |
147 |
|
|
|); |
148 |
|
|
|
149 |
dpavlin |
1024 |
my $total = $rs->pager->total_entries; |
150 |
|
|
|
151 |
|
|
sub form { |
152 |
|
|
my $column = shift || die; |
153 |
|
|
qq| |
154 |
|
|
<form> |
155 |
|
|
<input type="submit" value="-"> |
156 |
|
|
<input type="hidden" name="remove_column" value="$column"> |
157 |
|
|
</form> |
158 |
|
|
| |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
my $header = qq|<tr><th>| . join(qq|</th><th>|, map { $_ . form($_) } @columns) . qq|</th></tr>|; |
162 |
|
|
|
163 |
|
|
my $html = qq| |
164 |
|
|
Rows: <b>$total</b> |
165 |
dpavlin |
1026 |
$column_editor |
166 |
dpavlin |
1024 |
<table> |
167 |
|
|
$header |
168 |
|
|
$rows |
169 |
|
|
</table> |
170 |
|
|
|; |
171 |
|
|
|
172 |
|
|
return $html; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
=head1 SEE ALSO |
176 |
|
|
|
177 |
|
|
DBIx::Master Class |
178 |
|
|
|
179 |
|
|
L<http://www.shadowcat.co.uk/catalyst/-talks/yapc-na-2008/dbix-masterclass.xul> presentation |
180 |
|
|
|
181 |
|
|
L<http://www.shadowcat.co.uk/archive/conference-video/yapc-eu-2008/dbic-masterclass/> video |
182 |
|
|
|
183 |
|
|
=cut |
184 |
|
|
|
185 |
|
|
1; |