Skip to content
Browse files

Submit Query

  • Loading branch information...
1 parent fc7498d commit 67de0c9834c1df67a967e100eb24a5bac2c5f48c @kthakore committed Jan 30, 2011
View
57 implementation/SIMS/SIMS/lib/SIMS/Controller/Report.pm
@@ -42,23 +42,41 @@ sub index : Chained('base') : PathPart('') : Args(0) {
sub add_query : Chained('base') : PathPart('add_query') : Args(0) {
my ( $self, $c ) = @_;
+ if( $c->req->param('datum') )
+ {
+ try{
+
+ my $query = _generate_query($c);
+ my $report = $c->model('DB::Report')->create(
+ {
+ name => $c->req->param('query_name'),
+ query => Dumper ($query)
+ }
+ );
+ $c->stash(message => "Created ".$report->id() );
+ }
+ catch
+ {
+ $c->stash( message => "Problem $_");
+
+ };
+
+ }
+
$c->stash(template => 'report/index.tt' );
}
sub test_query : Chained('base') : PathPart('test_query') : Args(0) {
my ( $self, $c ) = @_;
+
+
if( $c->req->param('datum') )
{
try{
- my $query = {};
- foreach(0..($c->req->param('count')-1))
- {
- $query->{$c->req->{parameters}->{"column_$_"}} =
- { $c->req->{parameters}->{"condition_$_"} => $c->req->{parameters}->{"text_$_"} };
- }
+ my $query = _generate_query($c);
my @cols = $c->req->param('columns');
$c->log->debug( $c->req->param('datum'). " and ". Dumper ($query )) ;
my @foo = $c->model($c->req->param('datum'))->search( $query );
@@ -113,6 +131,33 @@ sub _prepare_columns
return $student_cols;
}
+sub _generate_query
+{
+ my $c = shift;
+ my $query = {};
+
+
+ foreach(0..($c->req->param('count')-1))
+ {
+ my $prev_query = $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/ )
+ {
+ $query = {"-$cur_op" => [$prev_query , {$cur_col => $cur_query}] }
+
+ }
+ else
+ {
+ $query->{$cur_col} = $cur_query;
+ }
+
+ }
+
+ return $query;
+}
+
sub _get_datums
{
my ($c) = shift;
View
42 implementation/SIMS/SIMS/root/report/index.tt
@@ -3,6 +3,12 @@
<script>
var condition = 0;
var datum = '';
+function reset( table )
+{
+ condition = 0;
+ datum = '';
+ table.html('');
+}
function select_data( data, table)
{
@@ -12,7 +18,7 @@ function select_data( data, table)
if( data == '[%datum.value%]' )
{
row = $('<tr> </th>');
- row.append('<tr><th>Columns To Show</th>');
+ row.append('<tr><th>Columns To Show</th>');
row.append('<th><select multiple="multiple" name="columns" size="4">'+
@@ -64,7 +70,11 @@ function add_condition( table,op)
if( op != '')
{
- row.append('<th>'+op+'</th>');
+ row.append('<th>'+op+'<input type="hidden" value="'+op+'" name="op_'+condition+'" /></th>');
+ }
+ else
+ {
+ row.append('<th />');
}
row.append( options() );
row.append('<th>'+
@@ -82,28 +92,31 @@ function add_condition( table,op)
<form id="make_query" action="[%c.uri_for('add_query')%]" method="POST">
<input type="hidden" id="count" />
-<table id="query_table">
+<table id="con">
<tr>
<th>
-Data Type: <select id="datum" name="datum">
+Data Type:
+</th><th><select id="datum" name="datum">
<option value="" >----</option>
[% FOREACH datum IN datums %]
<option value="[%datum.value%]">[%datum.text%]</option>
[% END %]
</select>
</th>
</tr>
-
<tr>
<th>Add More Options:</th>
-<th><input class="cond_but" disabled="true" type="button" value="And" onclick="add_condition($('#query_table'), 'and')"/></th>
-<th><input class="cond_but" disabled="true" type="button" value="OR" onclick="add_condition($('#query_table'), 'or')"/></th>
+<th><input class="cond_but" disabled="true" type="button" value="And" onclick="add_condition($('#query_table'), 'and')"/><input class="cond_but" disabled="true" type="button" value="OR" onclick="add_condition($('#query_table'), 'or')"/></th>
</tr>
+
+
+</table>
+<table id="query_table">
</table>
<script>
$('#datum').change( function(){
var value = $('#datum').val();
-
+ reset($('#query_table'));
if( value != '' )
{
select_data( value, $('#query_table'));
@@ -115,8 +128,19 @@ $('#datum').change( function(){
}
});
</script>
-<input type="submit" value="save query" />
+<table>
+<tr>
+<th>
+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();" />
+</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()" />
+</th>
+</tr>
+</table>
</form>
[% IF result_col %]
View
BIN implementation/SIMS/SIMS/sims.db
Binary file not shown.

0 comments on commit 67de0c9

Please sign in to comment.
Something went wrong with that request. Please try again.