/[Frey]/trunk/lib/Frey/DBIC/Designer.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/DBIC/Designer.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1027 - (show annotations)
Wed Jan 28 21:41:34 2009 UTC (15 years, 3 months ago) by dpavlin
File size: 3780 byte(s)
first working version of designer (column and order selection)

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

  ViewVC Help
Powered by ViewVC 1.1.26