--- lib/A3C/View/Strix.pm 2008/05/30 23:47:29 142 +++ lib/A3C/View/Strix.pm 2008/06/23 00:12:56 221 @@ -6,7 +6,9 @@ =head1 DESCRIPTION -Display information about Strix sites +Display information about Strix instances + +=head1 TEMPLATES =cut @@ -16,41 +18,513 @@ use Jifty::View::Declare -base; use Data::Dump qw/dump/; +=head2 / + +Display instaces search and some stats + +=cut + template 'index.html' => page { - h1 { _('Statistics') } + title is _('Strix instances'); - my $orgs = A3C::Model::StrixSiteCollection->new; + my $orgs = A3C::Model::StrixInstanceCollection->new; $orgs->unlimit; - dt { _('Number of Strix sites') } - dd { $orgs->count } + div { _('Number of instances in Strix: %1', $orgs->count ) }; + + render_region( + name => 'selected-instances', + path => '/strix/selected-instances' + ); + + render_region( + name => 'search-instances', + path => '/strix/search-instances', + ); + +}; + +=head2 name_diff + +=cut + +template 'name_diff' => page { + + title is _('Strix name differences'); + + render_region( + name => 'selected-instances', + path => '/strix/selected-instances' + ); my $name_diff = A3C::SQL->new({ query => qq{ select - site,hreduorgurl, + instance,hreduorgurl, _site_name,o - from strix_sites - join hr_edu_orgs on cn = site + from strix_instances + join hr_edu_orgs on cn = instance where o != _site_name }}); - h1 { _('Name differences') } - dd { + if ( $name_diff->count > 0 ) { + table { row { - th { _('Site') } - th { _('Strix site name') } + th {} + th { _('Instance') } + th { _('Strix instance name') } th { _('hrEduOrg.o') } }; while ( my $row = $name_diff->next ) { row { - cell { hyperlink( url => 'http://' . $row->hreduorgurl, label => $row->site ) } + cell { show( 'instance-op', 'Create', '+', $row->instance ) } + cell { $row->instance } cell { $row->_site_name } cell { $row->o } } } } + + } else { + div { _("Can't find any instance of strix which has different name than data from LDAP") } + } + +}; + +=head2 sql + +Execute SQL query on instance + +=cut + +template 'sql' => page { + + title is _('Execute SQL'); + + render_region( + name => 'selected-instances', + path => '/strix/selected-instances' + ); + + render_region( + name => 'execute-sql', + path => '/strix/execute-sql', + ); + +}; + +sub strix { + my $instance = get('instance'); + return Strix->new({ instance => $instance }); +} + +sub strix_link { + my ( $url, $label ) = @_; + hyperlink( + url => 'http://' . get('instance') . '.cms-qa.skole.hr' . $url, + label => $label || $url, + target => 'strix', + ); +} + +=head2 navigation + +=cut + +template 'navigation' => page { + + title is _('Site navigation'); + + render_region( + name => 'selected-instances', + path => '/strix/selected-instances' + ); + + render_region( + name => 'strix-site', + path => '/strix/site' + ); + +}; + +=head1 REGIONS + +=head2 execute-sql + +Execute SQL query on instance + +=cut + +template 'execute-sql' => sub { + + my $sql = get('sql') || Jifty->web->session->get('sql'); + warn ">>>> sql = $sql"; + my $instance = get('instance'); + warn ">>>> instance = $instance"; + + my $action = new_action( + class => 'StrixSQL', + moniker => 'strix-sql', + sticky_on_success => 1, + sticky_on_failure => 1, + arguments => { + instance => $instance, + sql => $sql, + }, + ); + + form { + render_action( $action, [ 'instance', 'sql' ] ); + form_submit( label => _('Execute SQL') ); + }; + + if ( my $sql = $action->result->content('sql') ) { + Jifty->web->session->set( sql => $sql->query ); + div { _('Found %1 results for %2', $sql->count, $instance ) } + table { + row { map { th { $_ } } $sql->_column_names }; + while (my $row = $sql->next) { + row { + foreach my $col ( $sql->_column_names ) { + cell { $row->$col } + } + } + } + } + } + warn ">>>> sql (at end) = ",Jifty->web->session->get('sql'); +}; + +=head2 search-instances + +=cut + +template 'search-instances' => sub { + + h1 { _('Find instance') } + + my $action = new_action( + class => 'SearchStrixInstance', + moniker => 'search-strix-instance', + sticky_on_success => 1, + sticky_on_failure => 1, + ); + + + form { + render_action( $action => [ 'instance_contains', '_site_name_contains' ] ); + form_submit( label => _('Search') ); + }; + +# warn dump( $action->result->content ); + + if ( my $search = $action->result->content('search') ) { + div { _('Found %1 results', $search->count ) } + table { + while (my $strix = $search->next) { + row { + cell { show( 'instance-op', 'Create', '+', $strix->instance ) } + cell { tt { $strix->instance } } + cell { $strix->_site_name } + } + } + } + } + +}; + +=head2 selected-instances + +Show Selected instances for current user + +=cut + +template 'selected-instances' => sub { + my $self = shift; + +# warn "## IN selected-instances ",dump( @_ ); + + if ( my $op = get 'op' ) { + my $op_instance = get 'op_instance'; + return unless $op_instance; + warn "# selected-instances $op on $op_instance"; + + my $a; + + if ( $op eq 'Create' ) { + + $a = new_action( + class => $op . 'StrixInstanceSelection', + moniker => $op, + arguments => { + instance => $op_instance, + by_user => $self->current_user->id, + }, + ); + + } elsif ( $op eq 'Delete' ) { + + my $strix = A3C::Model::StrixInstanceSelection->new; + $strix->load_by_cols( instance => $op_instance, by_user => $self->current_user->id ); + warn "can't find instance $op_instance" unless $strix->id; + $a = $strix->as_delete_action; + + } +# warn "# argument_values = ",dump( $a->argument_values ); + $a->run; + + if ( $a->result->error ) { + div { + { class is 'note error' } + $a->result->error; + } + } + + set( op => '' ); + } + + my $selected = A3C::Model::StrixInstanceSelectionCollection->new; + $selected->limit( column => 'by_user', value => Jifty->web->current_user->id ); + + if ( $selected->count > 0 ) { + + my $instance = get('instance'); + warn "# selected-instances -- selected: $instance\n"; + + div { _('%1 instances selected', $selected->count ) }; + table { + while (my $s = $selected->next) { + row { + cell { tt { + if ( $s->instance->instance eq $instance ) { + b { $instance } + } else { + hyperlink( + url => '?instance=' . $s->instance->instance, + label => $s->instance->instance + ) + } + } } + cell { $s->instance->_site_name } + cell { show( 'instance-op', 'Delete', '-', $s->instance->instance ) } + } + } + } + } else { + div { _('No instances selected') } + } +}; + +=head2 instance-op + +Display button to add/remove instance from selection + + show( 'instance-op', 'Delete', '-', $strix->instace ); + +=cut + +template 'instance-op' => sub { + my $self = shift; + +# warn "# instance-op = ",dump( @_ ); + + my ( $op, $label, $instance ) = @_; + + form { + hyperlink( + label => $label, + onclick => { + refresh => 'selected-instances', + path => '/strix/selected-instances', + args => { + op_instance => $instance, + op => $op, + } + }, + ); + } + +}; + +=head2 site + +=cut + +template 'site' => sub { + + my $action = new_action( + class => 'StrixSelectSite', + moniker => 'strix-select-site', + ); + + warn "# action = ", dump( $action ); + + warn "# argument_values = ", dump( $action->argument_values ); + + if ( ! $action->argument_value('instance') ) { + $action->argument_value( 'instance', get('instance') ); + warn "# run action with instance\n"; + $action->run; + } + + my $magic = [ + { submit => $action, refresh_self => 1 }, + # this is basically a closure + { refresh => 'selected-instances', path => '/strix/selected-instances', args => { + instance => { result_of => $action, name => 'instance' } + } }, + { refresh => 'strix-site-layout', path => '/__jifty/empty' }, + ]; + + form { + render_param( $action, 'instance', onchange => $magic ); + render_param( $action, 'site_id', onchange => $magic ); + form_submit( label => _('Show navigation'), onclick => $magic ); + }; + + warn "## select-site action ",dump( $action->result ); + + render_region( + name => 'layout', + path => '/__jifty/empty', + ); + + if ( my $site_id = $action->result->content('site_id') ) { + show('navigation-tree', $action->result->content('instance'), $site_id); + } +}; + +=head2 layout + +Shows layout for C + +=cut + +template 'layout' => sub { + + my $url = get('url') || '/'; + my $category = strix->category( $url ); + my $layout = strix->layout( $url ); + + h1 { $category->{naziv} } + pre { + dump( $layout ); + } + +}; + +=head2 category + +Show category data for C + +=cut + +template 'category' => sub { + + my $url = get('url') || '/'; + my $category = strix->category( $url ); + + h1 { $category->{naziv} } + pre { + dump( $category ); + } + +}; + +=head1 PRIVATE TEMPLATES + +=head2 navigation-tree-category + + show('navigation-tree-category',$kat_row); + +=cut + +private template 'navigation-tree-category' => sub { + my $self = shift; + #warn "## navigation-tree-category",dump( @_ ); + my $p = shift; + hyperlink( + onclick => { + region => 'strix-site-layout', # FIXME do we have to hard-code region name here? + replace_with => '/strix/category', + args => { + url => $p->{url}, + instance => get('instance'), + } + }, + label => $p->{naziv}, + ); + outs_raw(' '); + if ( $p->{type} eq 'category' ) { + hyperlink( +# url => '/strix/layout?url=' . $p->{url} . ';instance=' . get('instance'), + onclick => { + region => 'strix-site-layout', # FIXME do we have to hard-code region name here? + replace_with => '/strix/layout', + args => { + url => $p->{url}, + instance => get('instance'), + } + }, + label => _('layout'), + class => 'layout', + ); + } + outs_raw(' '); + strix_link( $p->{url}, '>>' ); +}; + +=head2 navigation-tree + + show('navigation-tree',$instance,$site_id); + +=cut + +private template 'navigation-tree' => sub { + my $self = shift; + my ( $instance, $site_id ) = @_; + + warn "## navigation-tree instance: $instance site_id: $site_id"; + + set 'instance' => $instance; + + sub children { + my $c = shift; + return unless defined $c->{children}; + ul { + foreach my $p ( @{ $c->{children} } ) { + li { + if ( defined( $p->{class} ) ) { + { class is $p->{class} }; + } + show( 'navigation-tree-category', $p ); + children( $p ); + } + } + } + } + + my $strix = Strix->new({ instance => $instance }); + + my $navigation = $strix->site_navigation( $site_id ); + #warn "## navigation = ",dump( $navigation ); + if ( $navigation ) { + ul { + { class is 'navigation' }; + foreach my $p ( @$navigation ) { + li { + show( 'navigation-tree-category', $p ); + children( $p ); + } + } + } + } else { + div { + { class is 'note error' } + _('No navigation found for instance %1 site_id %2', $instance, $site_id) + } + } };