View | Details | Raw Unified | Return to bug 35386
Collapse All | Expand All

(-)a/Koha/Item.pm (-13 / +29 lines)
Lines 1195-1224 Returns the branchcode to be recorded in statistics renewal of the item Link Here
1195
1195
1196
sub renewal_branchcode {
1196
sub renewal_branchcode {
1197
1197
1198
    my ($self, $params ) = @_;
1198
    my ( $self, $params ) = @_;
1199
1199
1200
    my $interface = C4::Context->interface;
1200
    my $interface = C4::Context->interface;
1201
    my $branchcode;
1201
    my $branchcode;
1202
    if ( $interface eq 'opac' ){
1202
    my $renewal_branchcode;
1203
        my $renewal_branchcode = C4::Context->preference('OpacRenewalBranch');
1203
1204
        if( !defined $renewal_branchcode || $renewal_branchcode eq 'opacrenew' ){
1204
    if ( $interface eq 'opac' ) {
1205
        $renewal_branchcode = C4::Context->preference('OpacRenewalBranch');
1206
        if ( !defined $renewal_branchcode || $renewal_branchcode eq 'opacrenew' ) {
1205
            $branchcode = 'OPACRenew';
1207
            $branchcode = 'OPACRenew';
1206
        }
1208
        }
1207
        elsif ( $renewal_branchcode eq 'itemhomebranch' ) {
1209
    } elsif ( $interface eq 'api' ) {
1208
            $branchcode = $self->homebranch;
1210
        $renewal_branchcode = C4::Context->preference('RESTAPIRenewalBranch');
1211
        if ( !defined $renewal_branchcode || $renewal_branchcode eq 'apirenew' ) {
1212
            $branchcode = 'APIRenew';
1209
        }
1213
        }
1210
        elsif ( $renewal_branchcode eq 'patronhomebranch' ) {
1214
    }
1215
1216
    return $branchcode if $branchcode;
1217
1218
    if ($renewal_branchcode) {
1219
        if ( $renewal_branchcode eq 'itemhomebranch' ) {
1220
            $branchcode = $self->homebranch;
1221
        } elsif ( $renewal_branchcode eq 'patronhomebranch' ) {
1211
            $branchcode = $self->checkout->patron->branchcode;
1222
            $branchcode = $self->checkout->patron->branchcode;
1212
        }
1223
        } elsif ( $renewal_branchcode eq 'checkoutbranch' ) {
1213
        elsif ( $renewal_branchcode eq 'checkoutbranch' ) {
1214
            $branchcode = $self->checkout->branchcode;
1224
            $branchcode = $self->checkout->branchcode;
1215
        }
1225
        } elsif ( $renewal_branchcode eq 'apiuserbranch' ) {
1216
        else {
1226
            $branchcode =
1227
                ( C4::Context->userenv && defined C4::Context->userenv->{branch} )
1228
                ? C4::Context->userenv->{branch}
1229
                : $params->{branch};
1230
        } else {
1217
            $branchcode = "";
1231
            $branchcode = "";
1218
        }
1232
        }
1219
    } else {
1233
    } else {
1220
        $branchcode = ( C4::Context->userenv && defined C4::Context->userenv->{branch} )
1234
        $branchcode =
1221
            ? C4::Context->userenv->{branch} : $params->{branch};
1235
            ( C4::Context->userenv && defined C4::Context->userenv->{branch} )
1236
            ? C4::Context->userenv->{branch}
1237
            : $params->{branch};
1222
    }
1238
    }
1223
    return $branchcode;
1239
    return $branchcode;
1224
}
1240
}
(-)a/installer/data/mysql/atomicupdate/bug_35386.pl (+19 lines)
Line 0 Link Here
1
use Modern::Perl;
2
3
return {
4
    bug_number  => "35386",
5
    description => "Add RESTAPIRenewalBranch preference",
6
    up          => sub {
7
        my ($args) = @_;
8
        my ( $dbh, $out ) = @$args{qw(dbh out)};
9
10
        $dbh->do(
11
            q{
12
            INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES
13
            ('RESTAPIRenewalBranch','apiuserbranch','itemhomebranch|patronhomebranch|checkoutbranch|apiuserbranch|none','Choose how the branch for an API renewal is recorded in statistics','Choice')
14
        }
15
        );
16
17
        say $out "Added new system preference 'RESTAPIRenewalBranch'";
18
    },
19
};
(-)a/installer/data/mysql/mandatory/sysprefs.sql (+1 lines)
Lines 638-643 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
638
('RESTOAuth2ClientCredentials','0',NULL,'If enabled, the OAuth2 client credentials flow is enabled for the REST API.','YesNo'),
638
('RESTOAuth2ClientCredentials','0',NULL,'If enabled, the OAuth2 client credentials flow is enabled for the REST API.','YesNo'),
639
('RESTPublicAnonymousRequests','1',NULL,'If enabled, the API will allow anonymous access to public routes that don\'t require authenticated access.','YesNo'),
639
('RESTPublicAnonymousRequests','1',NULL,'If enabled, the API will allow anonymous access to public routes that don\'t require authenticated access.','YesNo'),
640
('RESTPublicAPI','1',NULL,'If enabled, the REST API will expose the /public endpoints.','YesNo'),
640
('RESTPublicAPI','1',NULL,'If enabled, the REST API will expose the /public endpoints.','YesNo'),
641
('RESTAPIRenewalBranch','apiuserbranch','itemhomebranch|patronhomebranch|checkoutbranch|apiuserbranch|none','Choose how the branch for an API renewal is recorded in statistics','Choice'),
641
('RestrictedPageLocalIPs','',NULL,'Beginning of IP addresses considered as local (comma separated ex: "127.0.0,127.0.2")','Free'),
642
('RestrictedPageLocalIPs','',NULL,'Beginning of IP addresses considered as local (comma separated ex: "127.0.0,127.0.2")','Free'),
642
('RestrictedPageContent','',NULL,'HTML content of the restricted page','TextArea'),
643
('RestrictedPageContent','',NULL,'HTML content of the restricted page','TextArea'),
643
('RestrictedPageTitle','',NULL,'Title of the restricted page (breadcrumb and header)','Free'),
644
('RestrictedPageTitle','',NULL,'Title of the restricted page (breadcrumb and header)','Free'),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/web_services.pref (+11 lines)
Lines 35-40 Web services: Link Here
35
                  1: Enable
35
                  1: Enable
36
                  0: "Disable"
36
                  0: "Disable"
37
            - "the /public namespace of the API."
37
            - "the /public namespace of the API."
38
        -
39
            - Use
40
            - pref: RESTAPIRenewalBranch
41
              choices:
42
                  apiuserbranch: "the branch of the api user"
43
                  itemhomebranch: "the item's home library"
44
                  patronhomebranch: "the patron's home library"
45
                  checkoutbranch: "the library the item was checked out from"
46
                  none: "NULL"
47
                  apirenew: "'APIRenew'"
48
            - as branchcode to store in the statistics table.
38
    OAI-PMH:
49
    OAI-PMH:
39
        -
50
        -
40
            - pref: OAI-PMH
51
            - pref: OAI-PMH
(-)a/t/db_dependent/Koha/Item.t (-31 / +121 lines)
Lines 1019-1067 subtest 'deletion' => sub { Link Here
1019
};
1019
};
1020
1020
1021
subtest 'renewal_branchcode' => sub {
1021
subtest 'renewal_branchcode' => sub {
1022
    plan tests => 13;
1022
    plan tests => 25;
1023
1023
1024
    $schema->storage->txn_begin;
1024
    $schema->storage->txn_begin;
1025
1025
1026
    my $item = $builder->build_sample_item();
1026
    my $item     = $builder->build_sample_item();
1027
    my $branch = $builder->build_object({ class => 'Koha::Libraries' });
1027
    my $branch   = $builder->build_object( { class => 'Koha::Libraries' } );
1028
    my $checkout = $builder->build_object({
1028
    my $checkout = $builder->build_object(
1029
        class => 'Koha::Checkouts',
1029
        {
1030
        value => {
1030
            class => 'Koha::Checkouts',
1031
            itemnumber => $item->itemnumber,
1031
            value => {
1032
                itemnumber => $item->itemnumber,
1033
            }
1032
        }
1034
        }
1033
    });
1035
    );
1034
1036
1037
    C4::Context->interface('intranet');
1038
    t::lib::Mocks::mock_userenv( { branchcode => $branch->branchcode } );
1035
1039
1036
    C4::Context->interface( 'intranet' );
1040
    is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context" );
1037
    t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
1041
    is(
1042
        $item->renewal_branchcode( { branch => "PANDA" } ), $branch->branchcode,
1043
        "If interface not opac, we get the branch from context even if we pass one in"
1044
    );
1045
    C4::Context->set_userenv( 51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef )
1046
        ;    #mock userenv doesn't let us set null branch
1047
    is(
1048
        $item->renewal_branchcode( { branch => "PANDA" } ), "PANDA",
1049
        "If interface not opac, we get the branch we pass one in if context not set"
1050
    );
1038
1051
1039
    is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
1052
    C4::Context->interface('opac');
1040
    is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
1041
    C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
1042
    is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
1043
1053
1044
    C4::Context->interface( 'opac' );
1054
    t::lib::Mocks::mock_preference( 'OpacRenewalBranch', undef );
1055
    is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew" );
1056
    is(
1057
        $item->renewal_branchcode( { branch => 'COW' } ), 'OPACRenew',
1058
        "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed"
1059
    );
1045
1060
1046
    t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
1061
    t::lib::Mocks::mock_preference( 'OpacRenewalBranch', 'none' );
1047
    is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
1062
    is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string" );
1048
    is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
1063
    is(
1064
        $item->renewal_branchcode( { branch => 'COW' } ), '',
1065
        "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed"
1066
    );
1049
1067
1050
    t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
1068
    t::lib::Mocks::mock_preference( 'OpacRenewalBranch', 'checkoutbranch' );
1051
    is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
1069
    is(
1052
    is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
1070
        $item->renewal_branchcode, $checkout->branchcode,
1071
        "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout"
1072
    );
1073
    is(
1074
        $item->renewal_branchcode( { branch => 'MONKEY' } ), $checkout->branchcode,
1075
        "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout even if branch passed"
1076
    );
1053
1077
1054
    t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
1078
    t::lib::Mocks::mock_preference( 'OpacRenewalBranch', 'patronhomebranch' );
1055
    is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
1079
    is(
1056
    is( $item->renewal_branchcode({branch=>'MONKEY'}), $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout even if branch passed");
1080
        $item->renewal_branchcode, $checkout->patron->branchcode,
1081
        "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron"
1082
    );
1083
    is(
1084
        $item->renewal_branchcode( { branch => 'TURKEY' } ), $checkout->patron->branchcode,
1085
        "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron even if branch passed"
1086
    );
1057
1087
1058
    t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
1088
    t::lib::Mocks::mock_preference( 'OpacRenewalBranch', 'itemhomebranch' );
1059
    is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
1089
    is(
1060
    is( $item->renewal_branchcode({branch=>'TURKEY'}), $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron even if branch passed");
1090
        $item->renewal_branchcode, $item->homebranch,
1091
        "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item"
1092
    );
1093
    is(
1094
        $item->renewal_branchcode( { branch => 'MANATEE' } ), $item->homebranch,
1095
        "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item even if branch passed"
1096
    );
1097
1098
    C4::Context->interface('api');
1099
1100
    t::lib::Mocks::mock_preference( 'RESTAPIRenewalBranch', undef );
1101
    is( $item->renewal_branchcode, 'APIRenew', "If interface api and RESTAPIRenewalBranch undef, we get APIRenew" );
1102
    is(
1103
        $item->renewal_branchcode( { branch => 'COW' } ), 'APIRenew',
1104
        "If interface api and RESTAPIRenewalBranch undef, we get APIRenew even if branch passed"
1105
    );
1106
1107
    t::lib::Mocks::mock_preference( 'RESTAPIRenewalBranch', 'none' );
1108
    is( $item->renewal_branchcode, '', "If interface api and RESTAPIRenewalBranch is none, we get blank string" );
1109
    is(
1110
        $item->renewal_branchcode( { branch => 'COW' } ), '',
1111
        "If interface api and RESTAPIRenewalBranch is none, we get blank string even if branch passed"
1112
    );
1061
1113
1062
    t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
1114
    t::lib::Mocks::mock_preference( 'RESTAPIRenewalBranch', 'checkoutbranch' );
1063
    is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
1115
    is(
1064
    is( $item->renewal_branchcode({branch=>'MANATEE'}), $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item even if branch passed");
1116
        $item->renewal_branchcode, $checkout->branchcode,
1117
        "If interface api and RESTAPIRenewalBranch set to checkoutbranch, we get branch of checkout"
1118
    );
1119
    is(
1120
        $item->renewal_branchcode( { branch => 'MONKEY' } ), $checkout->branchcode,
1121
        "If interface api and RESTAPIRenewalBranch set to checkoutbranch, we get branch of checkout even if branch passed"
1122
    );
1123
1124
    t::lib::Mocks::mock_preference( 'RESTAPIRenewalBranch', 'patronhomebranch' );
1125
    is(
1126
        $item->renewal_branchcode, $checkout->patron->branchcode,
1127
        "If interface api and RESTAPIRenewalBranch set to patronbranch, we get branch of patron"
1128
    );
1129
    is(
1130
        $item->renewal_branchcode( { branch => 'TURKEY' } ), $checkout->patron->branchcode,
1131
        "If interface api and RESTAPIRenewalBranch set to patronbranch, we get branch of patron even if branch passed"
1132
    );
1133
1134
    t::lib::Mocks::mock_preference( 'RESTAPIRenewalBranch', 'itemhomebranch' );
1135
    is(
1136
        $item->renewal_branchcode, $item->homebranch,
1137
        "If interface api and RESTAPIRenewalBranch set to itemhomebranch, we get homebranch of item"
1138
    );
1139
    is(
1140
        $item->renewal_branchcode( { branch => 'MANATEE' } ), $item->homebranch,
1141
        "If interface api and RESTAPIRenewalBranch set to itemhomebranch, we get homebranch of item even if branch passed"
1142
    );
1143
1144
    t::lib::Mocks::mock_userenv( { branchcode => $branch->branchcode } );
1145
    t::lib::Mocks::mock_preference( 'RESTAPIRenewalBranch', 'apiuserbranch' );
1146
    is(
1147
        $item->renewal_branchcode, $branch->branchcode,
1148
        "If interface api and RESTAPIRenewalBranch set to apiuserbranch, we get branch from userenv"
1149
    );
1150
    is(
1151
        $item->renewal_branchcode( { branch => 'MANATEE' } ), $branch->branchcode,
1152
        "If interface api and RESTAPIRenewalBranch set to apiuserbranch, we get branch from userenv even if branch passed"
1153
    );
1154
    C4::Context->set_userenv( 51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef )
1155
        ;    #mock userenv doesn't let us set null branch
1065
1156
1066
    $schema->storage->txn_rollback;
1157
    $schema->storage->txn_rollback;
1067
};
1158
};
1068
- 

Return to bug 35386