X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=sql%2FPg-upgrade2%2Fcp_greeting_migration.pl;h=176516572fbe8064e3d1e3068434a74778eda1e0;hb=8974129421f2b7c98495f8036e5f4d2d0ac7b738;hp=1ee6abd61b70a4fdc62de8046d7287044311d7a3;hpb=946324538fb1706eb9f3ff7f98ecad8e27710c95;p=kivitendo-erp.git
diff --git a/sql/Pg-upgrade2/cp_greeting_migration.pl b/sql/Pg-upgrade2/cp_greeting_migration.pl
index 1ee6abd61..176516572 100644
--- a/sql/Pg-upgrade2/cp_greeting_migration.pl
+++ b/sql/Pg-upgrade2/cp_greeting_migration.pl
@@ -2,13 +2,13 @@
# @description: Migration of cp_greeting to cp_gender
# @depends: generic_translations
-die("This script cannot be run from the command line.") unless ($main::form);
+use strict;
+die("This script cannot be run from the command line.") unless ($main::form);
sub mydberror {
my ($msg) = @_;
- die($dbup_locale->text("Database update error:") .
- " $msg " . $DBI::errstr);
+ die($dbup_locale->text("Database update error:") . " $msg " . $DBI::errstr);
}
sub do_query {
@@ -26,61 +26,58 @@ sub query_result {
# list of all entries where cp_greeting is empty, meaning can't determine gender from parsing Herr/Frau/...
# this assumes cp_greeting still exists, i.e. gender.sql was not run yet
- my $gender_table;
+ my ($gender_table, $mchecked, $fchecked);
my $sql2 = "select cp_id,cp_givenname,cp_name,cp_title,cp_greeting from contacts where not (cp_greeting ILIKE '%frau%' OR cp_greeting ILIKE '%herr%' or cp_greeting ILIKE '%mrs.%' or cp_greeting ILIKE '%miss%') ";
my $sth2 = $dbh->prepare($sql2) or die $dbh->errstr();
- $sth2->execute() or die $dbh->errstr();
+ $sth2->execute() or die $dbh->errstr();
- my $i = 1;
+ my $i = 1;
$gender_table .= '
";
$main::form->{gender_table} = $gender_table;
my $title_table;
-
+
my $sql3 = "select cp_id,cp_givenname,cp_name,cp_title,cp_greeting from contacts where not ( (cp_greeting ILIKE '%frau%' OR cp_greeting ILIKE '%herr%' or cp_greeting ILIKE '%mrs.%' or cp_greeting ILIKE '%miss%')) and not (cp_greeting like ''); ";
my $sth3 = $dbh->prepare($sql3) or die $dbh->errstr();
- $sth3->execute() or die $dbh->errstr();
+ $sth3->execute() or die $dbh->errstr();
$title_table = '
cp_givenname
cp_name
cp_title
cp_greeting
cp_title new
';
my $j = 1;
while (my $row = $sth3->fetchrow_hashref()) {
# Vorschlagsfeld fuer neuen Titel mit Werten von cp_greeting und cp_title vorbelegen
- my $value = "$row->{cp_greeting}";
- $value .= " " if $row->{cp_greeting};
- $value .= "$row->{cp_title}";
+ my $value = "$row->{cp_greeting}";
+ $value .= " " if $row->{cp_greeting};
+ $value .= "$row->{cp_title}";
- $title_table .= "
";
$main::form->{title_table} = $title_table;
-
-};
-
-
+}
sub print_question {
query_result();
@@ -88,9 +85,26 @@ sub print_question {
print $main::form->parse_html_template("dbupgrade/cp_greeting_update_form");
}
+sub alter_schema_only {
+ my $sqlcode = <do($sqlcode);
+}
+
sub do_update {
# main function
+ # Do not ask the user anything if there are no entries in the
+ # contacts table.
+ my ($data_exists) = $dbh->selectrow_array("SELECT * FROM contacts LIMIT 1");
+ if (!$data_exists) {
+ alter_schema_only();
+ return 1;
+ }
+
# first of all check if gender.sql was already run and thus cp_gender exists
# if it exists there is no need for this update anymore, so return
# without doing anything
@@ -112,9 +126,9 @@ sub do_update {
} else {
# case 2: submit button was pressed, hidden field do_migrate was set
migrate_data();
- };
+ }
-return 1;
+ return 1;
}
@@ -132,15 +146,15 @@ EOF
for (my $i = 1; $i <= $main::form->{number_of_gender_entries}; $i++ ) {
next unless $main::form->{"cp_id_$i"};
- if ( $main::form->{"gender_$i"} eq "f" ) {
- $sqlcode .= "UPDATE contacts SET cp_gender = \'f\' WHERE cp_id = $main::form->{\"cp_id_$i\"};\n";
- };
- };
+ if ( $main::form->{"gender_$i"} eq "f" ) {
+ $sqlcode .= "UPDATE contacts SET cp_gender = \'f\' WHERE cp_id = $main::form->{\"cp_id_$i\"};\n";
+ }
+ }
for (my $i = 1; $i <= $main::form->{number_of_title_entries}; $i++ ) {
next unless $main::form->{"cp_id_title_$i"} and $main::form->{"cp_id_$i"};
- $sqlcode .= "UPDATE contacts SET cp_title = \'$main::form->{\"cp_name_$i\"}\' WHERE cp_id = $main::form->{\"cp_id_$i\"};\n";
- };
+ $sqlcode .= "UPDATE contacts SET cp_title = \'$main::form->{\"cp_name_$i\"}\' WHERE cp_id = $main::form->{\"cp_id_$i\"};\n";
+ }
$sqlcode .= "ALTER TABLE contacts DROP COLUMN cp_greeting;";
# insert chosen default values
@@ -149,22 +163,18 @@ EOF
my $query = $sqlcode;
do_query($query);
-};
-
-
+}
sub set_default_greetings {
# add html input boxes to template so user can specify default greetings
- my $default_male = "Herr";
- my $default_female = "Frau";
-
- my $default_greeting_text_male = " ";
- my $default_greeting_text_female = " ";
- $main::form->{default_greeting_text_male} = $default_greeting_text_male;
+ my $default_male = "Herr";
+ my $default_female = "Frau";
+ my $default_greeting_text_male = " ";
+ my $default_greeting_text_female = " ";
+ $main::form->{default_greeting_text_male} = $default_greeting_text_male;
$main::form->{default_greeting_text_female} = $default_greeting_text_female;
-};
-
+}
return do_update();