[WTF CVS] wtf/lib/WTF/Pages Admin.pm,1.4,1.5
Brought to you by:
gryphonshafer
|
From: Gryphon S. <gry...@us...> - 2006-11-28 18:47:57
|
Update of /cvsroot/wtf-tracker/wtf/lib/WTF/Pages In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv9489/lib/WTF/Pages Modified Files: Admin.pm Log Message: Changes to support full Table Data administration features Index: Admin.pm =================================================================== RCS file: /cvsroot/wtf-tracker/wtf/lib/WTF/Pages/Admin.pm,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Admin.pm 20 Nov 2006 02:53:07 -0000 1.4 --- Admin.pm 28 Nov 2006 18:47:50 -0000 1.5 *************** *** 22,25 **** --- 22,26 ---- 'has_project' => 0, 'has_groups' => 0, + 'has_ordering' => 1, }, { *************** *** 31,34 **** --- 32,36 ---- 'has_project' => 0, 'has_groups' => 0, + 'has_ordering' => 1, }, { *************** *** 40,43 **** --- 42,46 ---- 'has_project' => 1, 'has_groups' => 1, + 'has_ordering' => 1, }, { *************** *** 49,52 **** --- 52,56 ---- 'has_project' => 0, 'has_groups' => 1, + 'has_ordering' => 1, }, ], *************** *** 411,418 **** --- 415,425 ---- 'tables' => \@tables, 'table_data' => $table_data, + 'table' => $table->{'table'}, 'has_parent_id' => $table->{'has_parent_id'}, 'has_description' => $table->{'has_description'}, 'has_active' => $table->{'has_active'}, 'has_project' => $table->{'has_project'}, + 'has_ordering' => $table->{'has_ordering'}, + 'show_save' => ( $table->{'has_active'} or $table->{'has_project'} ) ? 1 : 0, ); *************** *** 424,433 **** my $req = WTF::Utils::get_req($r); ! $r->headers_out->set( 'Location' => ROOT_URL . '/view/admin/tables' ); return $r->status( Apache2::Const::REDIRECT ); } --- 431,565 ---- my $req = WTF::Utils::get_req($r); + # store locally the data about the table we're going to edit + my $table = ( grep { + defined $req->param('table') and $req->param('table') eq $_->{'table'} + } @{ (DATA_TABLES) } )[0]; + # if there was some goof-up and we're not able to get table information, + # return to the main Table Data page + if ( not defined $table ) { + $r->headers_out->set( 'Location' => ROOT_URL . '/view/admin/tables' ); + return $r->status( Apache2::Const::REDIRECT ); + } + # reorder the ordering column so that the numbers are in a clean series + if ( $req->param('button') and $req->param('button') eq 'reorder' ) { + my $sth_upd = $dbh->prepare( + 'UPDATE ' . $table->{'table'} . ' SET ordering = ? WHERE id = ?' + ) or die $dbh->errstr(); + my $sql_sth = 'table_data_' . $table->{'table'}; + $sth->{$sql_sth}->execute() or die $dbh->errstr(); + my $ordering = 0; + while ( $_ = $sth->{$sql_sth}->fetchrow_hashref() ) { + $ordering++; + $sth_upd->execute( $ordering, $_->{'id'} ); + } + $sth->{$sql_sth}->finish(); + } ! # updating the boolean columns for an existing table record ! elsif ( not defined $req->param('id') ) { ! $dbh->do( ! 'UPDATE ' . $table->{'table'} . ' SET ' . join( ', ', grep { defined } ( ! ( $table->{'has_active'} ) ? 'active = 0' : undef, ! ( $table->{'has_project'} ) ? 'project = 0' : undef, ! ) ) ! ) or die $dbh->errstr(); ! ! if ( $table->{'has_active'} ) { ! $dbh->do( ! 'UPDATE ' . $table->{'table'} . ' SET active = 1 WHERE id IN ( ' . join( ', ', ! map { substr( $_, 7 ) } grep { substr( $_, 0, 7 ) eq 'active_' } $req->param() ) . ')' ! ) or die $dbh->errstr(); ! } ! ! if ( $table->{'has_project'} ) { ! $dbh->do( ! 'UPDATE ' . $table->{'table'} . ' SET project = 1 WHERE id IN ( ' . join( ', ', ! map { substr( $_, 8 ) } grep { substr( $_, 0, 8 ) eq 'project_' } $req->param() ) . ')' ! ) or die $dbh->errstr(); ! } ! } ! ! # inserting a new record into the table ! elsif ( $req->param('id') < 1 ) { ! ! # setup an array of column names we're going to use in our query; ! # "name" is always going to exist, the rest come from the ! # table definition structure ! my @columns = ( 'name', grep { defined } ( ! ( $table->{'has_parent_id'} ) ? $table->{'table'} . '_id' : undef, ! ( $table->{'has_ordering'} ) ? 'ordering' : undef, ! ( $table->{'has_description'} ) ? 'description' : undef, ! ) ); ! ! my $sth = $dbh->prepare( ! 'INSERT INTO ' . $table->{'table'} . ' ( ' . ! join( ', ', @columns ) . ' ) VALUES ( ' . join( ', ', ('?') x @columns ) . ' )' ! ) or die $dbh->errstr(); ! ! # set parent_id and description for SQL insert based on what the ! # Javascript sends into this method ! my $pid = ( ! not defined $req->param('pid') or ! lc( $req->param('pid') ) eq 'null' ! ) ? undef : $req->param('pid'); ! my $description = ( ! not defined $req->param('description') or ! length( $req->param('description') ) == 0 ! ) ? undef : $req->param('description'); ! ! # execute the query, grepping-out input values for columns that ! # don't exist ! $sth->execute( grep { $_ ne '-1' } ( ! $req->param('name'), ! ( $table->{'has_parent_id'} ) ? $pid : -1, ! ( $table->{'has_ordering'} ) ? $req->param('ordering') : -1, ! ( $table->{'has_description'} ) ? $description : -1, ! ) ) or die $dbh->errstr(); ! } ! ! # updating an existing record in the table ! elsif ( $req->param('id') > 0 ) { ! ! # setup an array of column names we're going to use in our query; ! # "name" is always going to exist, the rest come from the ! # table definition structure ! my @columns = ( 'name = ?', grep { defined } ( ! ( $table->{'has_parent_id'} ) ? $table->{'table'} . '_id = ?' : undef, ! ( $table->{'has_ordering'} ) ? 'ordering = ?' : undef, ! ( $table->{'has_description'} ) ? 'description = ?' : undef, ! ) ); ! ! my $sth = $dbh->prepare( ! 'UPDATE ' . $table->{'table'} . ' SET ' . join( ', ', @columns ) . ' WHERE id = ?' ! ) or die $dbh->errstr(); ! ! # set parent_id and description for SQL insert based on what the ! # Javascript sends into this method ! my $pid = ( ! not defined $req->param('pid') or ! lc( $req->param('pid') ) eq 'null' ! ) ? undef : $req->param('pid'); ! my $description = ( ! not defined $req->param('description') or ! length( $req->param('description') ) == 0 ! ) ? undef : $req->param('description'); ! ! # execute the query, grepping-out input values for columns that ! # don't exist ! $sth->execute( grep { $_ ne '-1' } ( ! $req->param('name'), ! ( $table->{'has_parent_id'} ) ? $pid : -1, ! ( $table->{'has_ordering'} ) ? $req->param('ordering') : -1, ! ( $table->{'has_description'} ) ? $description : -1, ! $req->param('id'), ! ) ) or die $dbh->errstr(); ! } ! ! # return to the Table Data view for the table to which we just made changes ! $r->headers_out->set( 'Location' => ROOT_URL . '/view/admin/tables?table=' . $table->{'table'} ); return $r->status( Apache2::Const::REDIRECT ); } |