Merge branch 'test' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / sql / Pg-upgrade2 / custom_variables_convert_width_height_to_pixels.pl
1 # @tag: custom_variables_convert_width_height_to_pixels
2 # @description: Benutzerdefinierte Variablen: Optionen »WIDTH« & »HEIGHT« nach Pixel konvertieren
3 # @depends: release_3_5_8
4 package SL::DBUpgrade2::custom_variables_convert_width_height_to_pixels;
5
6 use strict;
7 use utf8;
8
9 use parent qw(SL::DBUpgrade2::Base);
10
11 use SL::DBUtils;
12
13 sub find_configs {
14   my ($self) = @_;
15
16   my $sql = <<SQL;
17     SELECT id, options
18     FROM custom_variable_configs
19     WHERE (COALESCE(options, '') ~ 'WIDTH=|HEIGHT=')
20       AND (type = 'textfield')
21 SQL
22
23   return selectall_hashref_query($::form, $self->dbh, $sql);
24 }
25
26 sub fix_configs {
27   my ($self, $configs) = @_;
28
29   my $sql = <<SQL;
30     UPDATE custom_variable_configs
31     SET options = ?
32     WHERE id = ?
33 SQL
34
35   my $update_h = prepare_query($::form, $self->dbh, $sql);
36
37   # Old defaults: 30 columns, 5 rows
38   # New defaults: 225px width, 90px height
39
40   foreach my $config (@{ $configs }) {
41     $config->{options} =~ s{WIDTH=(\d+)}{  int($1 * (225 / 30.0)) }eg;
42     $config->{options} =~ s{HEIGHT=(\d+)}{ int($1 * ( 90 /  5.0)) }eg;
43
44     $update_h->execute(@{$config}{qw(options id)}) || $self->db_error($sql);
45   }
46
47   $update_h->finish;
48 }
49
50 sub run {
51   my ($self) = @_;
52
53   my $configs = $self->find_configs;
54   $self->fix_configs($configs) if @{ $configs };
55
56   return 1;
57 }
58
59 1;