#!/usr/bin/perl # Copyright 2001-2008 Leslie Richardson # This file is part of Open Admin for Schools. # Open Admin for Schools is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. my %lex = ('Main' => 'Main', 'Error' => 'Error', 'Edit Translation' => 'Edit Translation', 'Eoy' => 'Eoy', 'Search' => 'Search', 'Original Text' => 'Original Text', 'Translated Text' => 'Translated Text', 'Both' => 'Both', 'Continue' => 'Continue', 'Start of Phrase' => 'Start of Phrase', 'Everywhere in Phrase' => 'Everywhere in Phrase', 'Translation' => 'Translation', 'Blank=All' => 'Blank=All', ); my $self = 'xlatTransEdit.pl'; my $maxfieldlength = 70; # max size for text input fields. use DBI; use CGI; eval require "../../etc/admin.conf"; if ( $@ ) { print $lex{Error}. " $@
\n"; die $lex{Error}. " $@\n"; } my $q = new CGI; print $q->header; my %arr = $q->Vars; my $dsn = "DBI:$dbtype:dbname=$dbase"; my $dbh = DBI->connect($dsn,$user,$password); # Print Page Header print "$doctype\n". $lex{'Edit Translation'}; print "\n"; print "$chartype\n\n"; print "[ ". $lex{Main}. " |\n"; print "". $lex{Eoy}. " ]\n"; print "

". $lex{'Edit Translation'}. "

\n"; if ( not $arr{page} ) { showStartPage(); } elsif ( $arr{page} == 1 ) { delete $arr{page}; showChoices(); } elsif ( $arr{page} == 2 ) { delete $arr{page}; updateRecords(); } #---------------- sub showChoices { #---------------- # foreach my $key (keys %arr ) { print "K:$key V:$arr{$key}
\n"; } # Passed: sposition, sphrase and sgroup # Create select based on group, as well look my ( $select, $phrase ); if ( $arr{sphrase} ) { if ( $arr{sposition} eq $lex{'Start of Phrase'} ) { $phrase = q{'}. $arr{sphrase}. q{%'}; } else { $phrase = q{'%}. $arr{sphrase}. q{%'}; } } my $table; if ( $arr{sgroup} eq 'phrase' or $arr{sgroup} eq 'both' ) { $table = 'xlat_phrase'; if ( $phrase ) { $select = "where phrase like $phrase"; } } else { $table = 'xlat_lang'; if ( $phrase ) { $select = "where translation like $phrase"; } } #print "
Table: $table
\n"; my $sth = $dbh->prepare("select distinct phrase from $table $select"); $sth->execute; if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } my %phrases = (); while ( my $phrase = $sth->fetchrow ) { $phrases{$phrase} = 1; } if ( $arr{sgroup} eq $lex{Both} ) { $table = 'xlat_lang'; my $sth = $dbh->prepare("select phrase from $table $select"); $sth->execute; if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } my %phrases = (); while ( my $phrase = $sth->fetchrow ) { $phrases{$phrase} = 1; } } # We should now have the %phrases hash fully populated. # print form header. print "
\n"; print "\n"; print "\n"; print "\n"; my $sth = $dbh->prepare("select id, translation from xlat_lang where phrase = ?"); foreach my $phrase ( sort keys %phrases ) { $sth->execute( $phrase ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } my ( $id, $translation ) = $sth->fetchrow; if ( not $id ) { # No record yet in trans table, use id of phrase table my $sth1 = $dbh->prepare("select id from xlat_phrase where phrase = ?"); $sth1->execute( $phrase ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } my $pid = $sth1->fetchrow; if ( not $pid ) { print "Phrase ID not found for: $phrase
\n"; die; } $id = "A-$pid"; } # filter tag identifiers $phrase =~ s//>/g; print "\n"; } # print form footer. print "\n"; print "
"; print "
$phrase"; print ""; print "
"; print "
\n"; exit; } #---------------- sub showStartPage { #---------------- print "
\n"; print "\n"; print "\n"; print "\n"; print "\n\n\n"; print "\n"; print "
". $lex{Search}; print " "; print "\n"; print $lex{'Blank=All'}. "
". $lex{'Original Text'}; print ""; print "
"; print $lex{'Translated Text'}. ""; print ""; print "
"; print $lex{Both}. ""; print "
"; print "
\n"; exit; } #--------------- sub updateRecords { #--------------- #foreach my $key ( sort keys %arr) { print "K:$key V:$arr{$key}
\n"; } print "
\n"; print '\n"; my $sth = $dbh->prepare("select translation from xlat_lang where id = ?"); my $sth1 = $dbh->prepare("update xlat_lang set translation = ? where id = ?"); foreach my $key ( sort keys %arr ) { # $key contains record id (or A-number if new) if ( $key =~ m/^\d+/ ) { # if a digit # Read stored translation $sth->execute( $key ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } my $oldtrans = $sth->fetchrow; if ( $oldtrans eq $arr{$key} ) { next; } # skip this one since not changed. # update record $sth1->execute( $arr{$key}, $key ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } print "\n"; } elsif ( $arr{$key} ) { # if we have a value to insert... my ( $dud, $phraseid ) = split /-/, $key; my $sth = $dbh->prepare("select phrase from xlat_phrase where id = ?"); $sth->execute( $phraseid ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } my $phrase = $sth->fetchrow; #print "Phrase: $phrase Value: $arr{$key}
\n"; my $sth2 = $dbh->prepare("insert into xlat_lang ( phrase, translation ) values( ?, ?)"); $sth2->execute( $phrase, $arr{$key} ); if ( $DBI::errstr ){ print $DBI::errstr; die $DBI::errstr; } print "\n"; } } print "
'. $lex{Translation}. "
". $arr{$key}. "
". $arr{$key}. "
\n"; print "

[ ". $lex{Eoy}. " ]\n"; print "

\n"; exit; } # End of writeRecords