a34394440738d65fb411bf0909a286cce4e3bd40
[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;
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 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 sub get_table_settings {
63     my ( $module, $page, $tablename ) = @_;
64     my $list = get_yaml;
65
66     my $schema = Koha::Database->new->schema;
67
68     my $rs = $schema->resultset('TablesSetting')->search(
69         {
70             module    => $module,
71             page      => $page,
72             tablename => $tablename,
73         }
74     )->next;
75     return $rs ? $rs : $list->{modules}{$module}{$page}{$tablename};
76 }
77
78 sub get_modules {
79     my $list = get_yaml;
80
81     my $schema = Koha::Database->new->schema;
82     my $rs     = $schema->resultset('ColumnsSetting')->search;
83
84     while ( my $c = $rs->next ) {
85         my $column = first { $c->columnname eq $_->{columnname} }
86         @{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename }{columns} };
87         $column->{is_hidden}         = $c->is_hidden;
88         $column->{cannot_be_toggled} = $c->cannot_be_toggled;
89         $column->{cannot_be_modified} = 0
90             unless exists $column->{cannot_be_modified};
91     }
92
93     return $list->{modules};
94 }
95
96 sub update_columns {
97     my ($params) = @_;
98     my $columns = $params->{columns};
99
100     my $schema = Koha::Database->new->schema;
101
102     for my $c (@$columns) {
103         $c->{is_hidden}         //= 0;
104         $c->{cannot_be_toggled} //= 0;
105
106         $schema->resultset('ColumnsSetting')->update_or_create(
107             {
108                 module     => $c->{module},
109                 page       => $c->{page},
110                 tablename  => $c->{tablename},
111                 columnname => $c->{columnname},
112                 is_hidden         => $c->{is_hidden},
113                 cannot_be_toggled => $c->{cannot_be_toggled},
114             }
115         );
116     }
117 }
118
119 =head3 update_table_settings
120
121 C4::Utils::DataTables::TablesSettings::update_table_settings(
122     {
123         module                 => $module,
124         pag                    => $page,
125         tablename              => $tablename,
126         default_display_length => $default_display_length,
127         default_sort_order     => $default_sort_order
128     }
129 );
130
131 Will update the default_display_length and default_sort_order for the given table.
132
133 =cut
134
135 sub update_table_settings {
136     my ($params)               = @_;
137     my $module                 = $params->{module};
138     my $page                   = $params->{page};
139     my $tablename              = $params->{tablename};
140     my $default_display_length = $params->{default_display_length};
141     my $default_sort_order     = $params->{default_sort_order};
142
143     my $schema = Koha::Database->new->schema;
144
145     $schema->resultset('TablesSetting')->update_or_create(
146         {
147             module                 => $module,
148             page                   => $page,
149             tablename              => $tablename,
150             default_display_length => $default_display_length,
151             default_sort_order     => $default_sort_order,
152         }
153     );
154 }
155
156 1;