| 1 | package Jifty::Plugin::ModelMap::View; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Jifty::Plugin::ModelMap::View | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | #use Data::Dumper; | ||
| 14 | 1 | ▶ | use Jifty::View::Declare -base; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | use Jifty::View::Declare::Page; | ||
| 16 | use GraphViz; | ||
| 17 | use HTML::Entities; | ||
| 18 | use Memoize; | ||
| 19 | |||
| 20 | memoize('_g'); | ||
| 21 | memoize('_as_png'); | ||
| 22 | memoize('_as_cmapx'); | ||
| 23 | |||
| 24 | sub _g { | ||
| 25 | my @models = Jifty->class_loader->models; | ||
| 26 | |||
| 27 | my $g = GraphViz->new(); | ||
| 28 | |||
| 29 | for my $model (@models) { | ||
| 30 | $model =~ m/::(\w+)$/o; | ||
| 31 | $g->add_node($model, label => encode_entities(_str_model($model)), shape => 'record', | ||
| 32 | URL => '__jifty/admin/model/'.$1 | ||
| 33 | ); | ||
| 34 | for my $column ($model->columns) { | ||
| 35 | next unless $column->refers_to; | ||
| 36 | if ($column->refers_to =~ m/^(.*)Collection$/o) { | ||
| 37 | $g->add_edge($model => $1, labeldistance => 2, headlabel => 'n', taillabel => $column->name); | ||
| 38 | } | ||
| 39 | else { | ||
| 40 | $g->add_edge($model => $column->refers_to, labeldistance => 2, headlabel => '1', taillabel => $column->name); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | $g; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub _as_png { | ||
| 48 | _g->as_png; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _as_cmapx { | ||
| 52 | _g->as_cmapx; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _str_column { | ||
| 56 | my $column = shift; | ||
| 57 | |||
| 58 | my $tail = do { | ||
| 59 | if ($column->label) { | ||
| 60 | "--" . $column->label; | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | ""; | ||
| 64 | } | ||
| 65 | }; | ||
| 66 | join(" ", $column->name, uc($column->type), $tail); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _str_model { | ||
| 70 | my $model = shift; | ||
| 71 | |||
| 72 | $model =~ m/::(\w*)$/o; | ||
| 73 | |||
| 74 | # The label can contain embedded newlines with '\n', as well as | ||
| 75 | # '\c', '\l', '\r' for center, left, and right justified lines. | ||
| 76 | # http://search.cpan.org/~lbrocard/GraphViz-2.03/lib/GraphViz.pm | ||
| 77 | ($1.'\n') | ||
| 78 | .join('\l', map { _str_column($_) } $model->columns) | ||
| 79 | .'\l'; | ||
| 80 | } | ||
| 81 | |||
| 82 | =head1 TEMPLATE | ||
| 83 | |||
| 84 | =head2 /model_map | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | template 'model_map' => page { | ||
| 89 | { title is 'All tables in Jifty Database' }; | ||
| 90 | |||
| 91 | my @models = Jifty->class_loader->models; | ||
| 92 | if (@models) { | ||
| 93 | outs_raw(_as_cmapx()); | ||
| 94 | img { attr { src => "model_map/graph", alt => "Model Map Image", usemap => '#test' } }; | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | p { "model is empty." }; | ||
| 98 | } | ||
| 99 | }; | ||
| 100 | |||
| 101 | =head2 /model_map/graph | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | template 'model_map/graph' => sub { | ||
| 106 | Jifty->handler->apache->content_type('image/png'); | ||
| 107 | outs_raw(_as_png); | ||
| 108 | }; | ||
| 109 | |||
| 110 | =head1 AUTHOR | ||
| 111 | |||
| 112 | bokutin, C<< <bokutin at cpan.org> >> | ||
| 113 | |||
| 114 | =head1 COPYRIGHT & LICENSE | ||
| 115 | |||
| 116 | Copyright 2008 bokutin, all rights reserved. | ||
| 117 | |||
| 118 | This program is free software; you can redistribute it and/or modify it | ||
| 119 | under the same terms as Perl itself. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | 1; | ||