Bug 24156: move ColumnsSettings to TablesSettings
[koha.git] / C4 / Utils / DataTables / TablesSettings.pm
1 package C4::Utils::DataTables::TablesSettings;
2
3 use Modern::Perl;
4 use List::Util qw( first );
5 use YAML;
6 use C4::Context;
7 use Koha::Database;
8 use Koha::Caches;
9
10 sub get_yaml {
11     my $yml_path = C4::Context->config('intranetdir') . '/admin/columns_settings.yml';
12     my $cache = Koha::Caches->get_instance();
13     my $yaml  = $cache->get_from_cache('TablesSettingsYaml');
14
15     unless ($yaml) {
16         $yaml = eval { YAML::LoadFile($yml_path) };
17         warn "ERROR: the yaml file for DT::TablesSettings is not correctly formated: $@"
18           if $@;
19         $cache->set_in_cache( 'TablesSettingsYaml', $yaml, { expiry => 3600 } );
20     }
21
22     return $yaml;
23 }
24
25 sub get_columns {
26     my ( $module, $page, $tablename ) = @_;
27
28     my $list = get_yaml;
29
30     my $schema = Koha::Database->new->schema;
31
32     my $rs = $schema->resultset('ColumnsSetting')->search(
33         {
34             module    => $module,
35             page      => $page,
36             tablename => $tablename,
37         }
38     );
39
40     while ( my $c = $rs->next ) {
41         my $column = first { $c->columnname eq $_->{columnname} }
42         @{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename } };
43         $column->{is_hidden}         = $c->is_hidden;
44         $column->{cannot_be_toggled} = $c->cannot_be_toggled;
45     }
46
47     my $columns = $list->{modules}{$module}{$page}{$tablename} || [];
48
49     # Assign default value if does not exist
50     $columns = [ map {
51         {
52             cannot_be_toggled => exists $_->{cannot_be_toggled} ? $_->{cannot_be_toggled} : 0,
53             cannot_be_modified => exists $_->{cannot_be_modified} ? $_->{cannot_be_modified} : 0,
54             is_hidden => exists $_->{is_hidden} ? $_->{is_hidden} : 0,
55             columnname => $_->{columnname},
56         }
57     } @$columns ];
58
59     return $columns;
60 }
61
62 sub get_modules {
63     my $list = get_yaml;
64
65     my $schema = Koha::Database->new->schema;
66     my $rs     = $schema->resultset('ColumnsSetting')->search;
67
68     while ( my $c = $rs->next ) {
69         my $column = first { $c->columnname eq $_->{columnname} }
70         @{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename } };
71         $column->{is_hidden}         = $c->is_hidden;
72         $column->{cannot_be_toggled} = $c->cannot_be_toggled;
73         $column->{cannot_be_modified} = 0
74             unless exists $column->{cannot_be_modified};
75     }
76
77     return $list->{modules};
78 }
79
80 sub update_columns {
81     my ($params) = @_;
82     my $columns = $params->{columns};
83
84     my $schema = Koha::Database->new->schema;
85
86     for my $c (@$columns) {
87         $c->{is_hidden}         //= 0;
88         $c->{cannot_be_toggled} //= 0;
89
90         $schema->resultset('ColumnsSetting')->update_or_create(
91             {
92                 module     => $c->{module},
93                 page       => $c->{page},
94                 tablename  => $c->{tablename},
95                 columnname => $c->{columnname},
96                 is_hidden         => $c->{is_hidden},
97                 cannot_be_toggled => $c->{cannot_be_toggled},
98             }
99         );
100     }
101 }
102
103 1;