Permalink
Browse files

Can add reports

  • Loading branch information...
1 parent 67de0c9 commit 00a37d64c1b8344fa5437cab02ee771f6b45b598 @kthakore committed Feb 1, 2011
@@ -2,6 +2,7 @@ package SIMS::Controller::Report;
use Moose;
use namespace::autoclean;
use Data::Dumper;
+use JSON;
use Try::Tiny;
BEGIN {extends 'Catalyst::Controller'; }
@@ -30,7 +31,12 @@ sub base : Chained('/') PathPart('report') CaptureArgs(0) {
$c->response->redirect( $c->uri_for('/unauthorized') )
unless ( grep /(g_admin)/, @roles );
+ my @reports = $c->model('DB::Report')->all();
+
+ $c->stash->{reports} = \@reports;
+
$c->stash->{datums} = _get_datums($c) unless $c->stash->{datums};
+ $c->stash->{eval_arr} = sub { my $foo = decode_json( $_ ); return join( ',', @$foo) };
}
@@ -47,13 +53,18 @@ sub add_query : Chained('base') : PathPart('add_query') : Args(0) {
try{
my $query = _generate_query($c);
+ my $e_query = encode_json $query;
+ my @cols = $c->req->param('columns');
+ my $e_cols = encode_json \@cols;
my $report = $c->model('DB::Report')->create(
{
name => $c->req->param('query_name'),
- query => Dumper ($query)
+ query => $e_query,
+ datum => $c->req->param('datum'),
+ cols => $e_cols
}
);
- $c->stash(message => "Created ".$report->id() );
+ $c->response->redirect( $c->uri_for('/report') );
}
catch
{
@@ -67,6 +78,27 @@ sub add_query : Chained('base') : PathPart('add_query') : Args(0) {
}
+sub show_query : Chained('base') : PathPart('show_query') : Args(1) {
+ my ( $self, $c, $id ) = @_;
+
+ try{
+ my $report = $c->model('DB::Report')->find( $id );
+ my $datum = $report->datum();
+ my $cols = decode_json $report->cols();
+ my $query = decode_json $report->query();
+
+ my @results = $c->model( $datum )->search($query);
+
+ _show_records( $c, \@results, $cols);
+ }
+ catch
+ {
+ $c->stash( message => "Problem $_");
+ };
+
+ $c->stash(template => 'report/index.tt' );
+}
+
sub test_query : Chained('base') : PathPart('test_query') : Args(0) {
my ( $self, $c ) = @_;
@@ -77,20 +109,37 @@ sub test_query : Chained('base') : PathPart('test_query') : Args(0) {
try{
my $query = _generate_query($c);
- my @cols = $c->req->param('columns');
+ my @columns = $c->req->param('columns');
$c->log->debug( $c->req->param('datum'). " and ". Dumper ($query )) ;
- my @foo = $c->model($c->req->param('datum'))->search( $query );
+ my @results = $c->model($c->req->param('datum'))->search( $query );
+ my $cols = \@columns;
+ my $datum_search = \@results;
+ _show_records( $c, $datum_search, $cols );
+ }
+ catch
+ {
+ $c->stash( message => "Problem $_");
+
+ };
+ }
+ $c->stash(template => 'report/index.tt' );
+
+}
+
+sub _show_records
+{
+ my ($c, $datum_search, $cols) = @_;
my @records;
- foreach(@foo )
+ foreach( @$datum_search )
{
my $res = $_;
my @record;
$res->id();
- foreach( @cols)
+ foreach( @$cols)
{
push @record, ( $res->{_column_data}->{$_});
}
@@ -99,17 +148,8 @@ sub test_query : Chained('base') : PathPart('test_query') : Args(0) {
}
- $c->stash( result_col => \@cols);
+ $c->stash( result_col => $cols);
$c->stash( result_record => \@records );
- }
- catch
- {
- $c->stash( message => "Problem $_");
-
- };
- }
- $c->stash(template => 'report/index.tt' );
-
}
sub _prepare_columns
@@ -143,7 +183,7 @@ sub _generate_query
my $cur_col = $c->req->{parameters}->{"column_$_"};
my $cur_query = { $c->req->{parameters}->{"condition_$_"} => $c->req->{parameters}->{"text_$_"} };
my $cur_op = $c->req->{parameters}->{"op_$_"};
- if( $cur_op =~ /and|or/ )
+ if( $cur_op && $cur_op =~ /and|or/ )
{
$query = {"-$cur_op" => [$prev_query , {$cur_col => $cur_query}] }
@@ -39,6 +39,16 @@ __PACKAGE__->table("Report");
data_type: 'text'
is_nullable: 1
+=head2 datum
+
+ data_type: 'text'
+ is_nullable: 1
+
+=head2 cols
+
+ data_type: 'text'
+ is_nullable: 1
+
=cut
__PACKAGE__->add_columns(
@@ -48,12 +58,16 @@ __PACKAGE__->add_columns(
{ data_type => "text", is_nullable => 1 },
"query",
{ data_type => "text", is_nullable => 1 },
+ "datum",
+ { data_type => "text", is_nullable => 1 },
+ "cols",
+ { data_type => "text", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("id");
-# Created by DBIx::Class::Schema::Loader v0.07002 @ 2011-01-11 11:10:47
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:nWwoqIzKxgvJh1k/DrbhcA
+# Created by DBIx::Class::Schema::Loader v0.07002 @ 2011-01-31 13:37:56
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:HTX7PLUqJlsL+X33Q/PGQA
# You can replace this text with custom content, and it will be preserved on regeneration
@@ -88,7 +88,9 @@ function add_condition( table,op)
}
</script>
<div id="content">
-
+[% message %]
+<div id="left" style="float:left; width:60%">
+<h1>Make Report</h1>
<form id="make_query" action="[%c.uri_for('add_query')%]" method="POST">
<input type="hidden" id="count" />
@@ -134,7 +136,7 @@ $('#datum').change( function(){
Query Name: <input name="query_name" value="Query Name" />
</th>
<th>
-<input type="button" value="save query" onclick=" var form = $('#make_query').attr('action', '[%c.uri_for('add_query') %] '); form.submit();" />
+<input type="button" value="save query" onclick=" var form = $('#make_query').attr('action', '[%c.uri_for('add_query') %] '); $('<input>').attr({ type: 'hidden', name: 'count', value: condition }).appendTo(form); form.submit();" />
</th>
<th>
<input type="button" value="try query" onclick=" var form = $('#make_query').attr('action', '[%c.uri_for('test_query') %] '); $('<input>').attr({ type: 'hidden', name: 'count', value: condition }).appendTo(form); form.submit()" />
@@ -164,7 +166,19 @@ Query Name: <input name="query_name" value="Query Name" />
</table>
[% END %]
-
+</div>
+<div id="right" style="float:right; width:40%">
+<h1>Reports</h1>
+<table>
+ [% FOREACH report IN reports %]
+ <tr bgcolor="8349AC"><th> </th> <th></th></tr>
+ <tr> <th> Name: </th> <th> [% report.name %] </th></tr>
+ <tr> <th> Query: </th> <th> <a href="[%c.uri_for('show_query') %]/[%report.id%]" >Show Query Result</a> </th></tr>
+ <tr> <th> Datum: </th> <th> [% report.datum %] </th></tr>
+ <tr> <th> Show Columns: </th> <th> [% report.cols %] </th></tr>
+ [% END %]
+</table>
+</div>
</div>
[% PROCESS footer.tt %]
Binary file not shown.
@@ -149,7 +149,9 @@ CREATE TABLE MeetingComments (
CREATE TABLE Report (
id INTEGER PRIMARY KEY,
name TEXT,
- query TEXT
+ query TEXT,
+ datum TEXT,
+ cols TEXT
);
---

0 comments on commit 00a37d6

Please sign in to comment.