Koha/C4/Utils/DataTables/ColumnsSettings.pm
Jonathan Druart fc66eedcc4 Bug 16177: Fix tests for ColumnsSsettings.t
This failure has been introduced by
  commit 243b797dd1
    Bug 15285: Update common files

This is because the cannot_be_modified key is not always created by
C4::Utils::DataTables::ColumnsSettings subroutines

Test plan:
  prove t/db_dependent/ColumnsSettings.t
should return green
And you can also test a table where the feature is set and a column
cannot be modified from the admin page.

NOTE: Works as described. I was pondering this potential
      solution when I found it in bugzilla.

Signed-off-by: Mark Tompsett <mtompset@hotmail.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Brendan A Gallagher <brendan@bywatersolutions.com>
2016-04-01 19:08:23 +00:00

103 lines
3 KiB
Perl

package C4::Utils::DataTables::ColumnsSettings;
use Modern::Perl;
use List::Util qw( first );
use YAML;
use C4::Context;
use Koha::Database;
use Koha::Cache;
sub get_yaml {
my $yml_path = C4::Context->config('intranetdir') . '/admin/columns_settings.yml';
my $cache = Koha::Cache->get_instance();
my $yaml = $cache->get_from_cache('ColumnsSettingsYaml');
unless ($yaml) {
$yaml = eval { YAML::LoadFile($yml_path) };
warn "ERROR: the yaml file for DT::ColumnsSettings is not correctly formated: $@"
if $@;
$cache->set_in_cache( 'ColumnsSettingsYaml', $yaml, { expiry => 3600 } );
}
return $yaml;
}
sub get_columns {
my ( $module, $page, $tablename ) = @_;
my $list = get_yaml;
my $schema = Koha::Database->new->schema;
my $rs = $schema->resultset('ColumnsSetting')->search(
{
module => $module,
page => $page,
tablename => $tablename,
}
);
while ( my $c = $rs->next ) {
my $column = first { $c->columnname eq $_->{columnname} }
@{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename } };
$column->{is_hidden} = $c->is_hidden;
$column->{cannot_be_toggled} = $c->cannot_be_toggled;
}
my $columns = $list->{modules}{$module}{$page}{$tablename} || [];
# Assign default value if does not exist
$columns = [ map {
{
cannot_be_toggled => exists $_->{cannot_be_toggled} ? $_->{cannot_be_toggled} : 0,
cannot_be_modified => exists $_->{cannot_be_modified} ? $_->{cannot_be_modified} : 0,
is_hidden => exists $_->{is_hidden} ? $_->{is_hidden} : 0,
columnname => $_->{columnname},
}
} @$columns ];
return $columns;
}
sub get_modules {
my $list = get_yaml;
my $schema = Koha::Database->new->schema;
my $rs = $schema->resultset('ColumnsSetting')->search;
while ( my $c = $rs->next ) {
my $column = first { $c->columnname eq $_->{columnname} }
@{ $list->{modules}{ $c->module }{ $c->page }{ $c->tablename } };
$column->{is_hidden} = $c->is_hidden;
$column->{cannot_be_toggled} = $c->cannot_be_toggled;
$column->{cannot_be_modified} = 0
unless exists $column->{cannot_be_modified};
}
return $list->{modules};
}
sub update_columns {
my ($params) = @_;
my $columns = $params->{columns};
my $schema = Koha::Database->new->schema;
for my $c (@$columns) {
$c->{is_hidden} //= 0;
$c->{cannot_be_toggled} //= 0;
$schema->resultset('ColumnsSetting')->update_or_create(
{
module => $c->{module},
page => $c->{page},
tablename => $c->{tablename},
columnname => $c->{columnname},
is_hidden => $c->{is_hidden},
cannot_be_toggled => $c->{cannot_be_toggled},
}
);
}
}
1;