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

  ViewVC Help
Powered by ViewVC 1.1.26