4880b8abdb3b79b95c3a0b7c3355e9d923ddb26f
[koha.git] / svc / cataloguing / framework
1 #!/usr/bin/perl
2
3 use Modern::Perl '2009';
4
5 use CGI;
6 use C4::ClassSource;
7 use C4::Context;
8 use C4::Biblio;
9 use C4::Service;
10 use Koha::Database;
11 use Koha::Libraries;
12
13 my ( $query, $response ) = C4::Service->init( editcatalogue => 'edit_catalogue' );
14
15 my $frameworkcode = $query->param( 'frameworkcode' ) // '';
16
17 my $tagslib = GetMarcStructure( 1, $frameworkcode );
18
19 my @tags;
20
21 foreach my $tag ( sort keys %$tagslib ) {
22     my $taglib = $tagslib->{$tag};
23     my $taginfo = { map { $_, $taglib->{$_} } grep { length $_ > 1 } keys %$taglib };
24     $taginfo->{subfields} = [ map { [ $_, $taglib->{$_} ] } grep { length $_ == 1 } sort keys %$taglib ];
25
26     push @tags, [ $tag, $taginfo ];
27 }
28
29 my $schema = Koha::Database->new->schema;
30 my $authorised_values = {};
31
32 my $branches = { map { $_->branchcode => $_->branchname } Koha::Libraries->search_filtered };
33 $authorised_values->{branches} = [];
34 foreach my $thisbranch ( sort keys %$branches ) {
35     push @{ $authorised_values->{branches} }, { value => $thisbranch, lib => $branches->{$thisbranch} };
36 }
37
38 $authorised_values->{itemtypes} = [ $schema->resultset( "Itemtype" )->search( undef, {
39     columns => [ { value => 'itemtype' }, { lib => "description" } ],
40     order_by => "description",
41     result_class => 'DBIx::Class::ResultClass::HashRefInflator'
42 } ) ];
43
44 my $class_sources = GetClassSources();
45
46 my $default_source = C4::Context->preference("DefaultClassificationSource");
47
48 foreach my $class_source (sort keys %$class_sources) {
49     next unless $class_sources->{$class_source}->{'used'} or
50                 ($class_source eq $default_source);
51     push @{ $authorised_values->{cn_source} }, { value => $class_source, lib => $class_sources->{$class_source}->{'description'} };
52 }
53
54 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
55 my $results;
56 if( $branch_limit ) {
57     $results = $schema->resultset( "AuthorisedValue" )->search(
58     { "authorised_values_branches.branchcode" => { "=", [ $branch_limit, undef ] } },
59     { join => "authorised_values_branches", order_by => "lib" } );
60 } else {
61     $results = $schema->resultset( "AuthorisedValue" )->search(
62     undef,
63     { order_by => "lib" } );
64 }
65
66 foreach my $result ( $results->all ) {
67     $authorised_values->{$result->category} ||= [];
68     push @{ $authorised_values->{$result->category} }, { value => $result->authorised_value, lib => $result->lib };
69 }
70
71 $response->param( framework => \@tags, authorised_values => $authorised_values );
72
73 C4::Service->return_success( $response );