Bug 27673: Replace YAML with YAML::XS
[koha-ffzg.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::XS;
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::XS::LoadFile($yml_path) };
17         warn "ERROR: the yaml file for DT::TablesSettings is not correctly formatted: $@"
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 }{ columns } };
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}{columns} || [];
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 =head3 get_table_settings
63
64 my $settings = C4::Utils::DataTables::TablesSettings::get_table_settings(
65     {
66         module                 => $module,
67         pag                    => $page,
68         tablename              => $tablename,
69     }
70 );
71
72 Returns the settings for a given table.
73
74 The settings are default_display_length and default_sort_order.
75
76 =cut
77
78 sub get_table_settings {
79     my ( $module, $page, $tablename ) = @_;
80     my $list = get_yaml;
81
82     my $schema = Koha::Database->new->schema;
83
84     my $rs = $schema->resultset('TablesSetting')->search(
85         {
86             module    => $module,
87             page      => $page,
88             tablename => $tablename,
89         }
90     )->next;
91     return $rs ? $rs : $list->{modules}{$module}{$page}{$tablename};
92 }
93
94 sub get_modules {
95     my $list = get_yaml;
96
97     my $schema = Koha::Database->new->schema;
98     my $rs     = $schema->resultset('ColumnsSetting')->search;
99
100     while ( my $c = $rs->next ) {
101         my $column = first { $c->columnname eq $_->{columnname} }
102         @{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename }{columns} };
103         $column->{is_hidden}         = $c->is_hidden;
104         $column->{cannot_be_toggled} = $c->cannot_be_toggled;
105         $column->{cannot_be_modified} = 0
106             unless exists $column->{cannot_be_modified};
107     }
108
109     return $list->{modules};
110 }
111
112 sub update_columns {
113     my ($params) = @_;
114     my $columns = $params->{columns};
115
116     my $schema = Koha::Database->new->schema;
117
118     for my $c (@$columns) {
119         $c->{is_hidden}         //= 0;
120         $c->{cannot_be_toggled} //= 0;
121
122         $schema->resultset('ColumnsSetting')->update_or_create(
123             {
124                 module     => $c->{module},
125                 page       => $c->{page},
126                 tablename  => $c->{tablename},
127                 columnname => $c->{columnname},
128                 is_hidden         => $c->{is_hidden},
129                 cannot_be_toggled => $c->{cannot_be_toggled},
130             }
131         );
132     }
133 }
134
135 =head3 update_table_settings
136
137 C4::Utils::DataTables::TablesSettings::update_table_settings(
138     {
139         module                 => $module,
140         pag                    => $page,
141         tablename              => $tablename,
142         default_display_length => $default_display_length,
143         default_sort_order     => $default_sort_order
144     }
145 );
146
147 Will update the default_display_length and default_sort_order for the given table.
148
149 =cut
150
151 sub update_table_settings {
152     my ($params)               = @_;
153     my $module                 = $params->{module};
154     my $page                   = $params->{page};
155     my $tablename              = $params->{tablename};
156     my $default_display_length = $params->{default_display_length};
157     my $default_sort_order     = $params->{default_sort_order};
158
159     my $schema = Koha::Database->new->schema;
160
161     $schema->resultset('TablesSetting')->update_or_create(
162         {
163             module                 => $module,
164             page                   => $page,
165             tablename              => $tablename,
166             default_display_length => $default_display_length,
167             default_sort_order     => $default_sort_order,
168         }
169     );
170 }
171
172 1;