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

(-)a/Koha/REST/V1/Biblios.pm (-115 / +62 lines)
Lines 28-33 use List::MoreUtils qw( any ); Link Here
28
use MARC::Record::MiJ;
28
use MARC::Record::MiJ;
29
29
30
use Try::Tiny qw( catch try );
30
use Try::Tiny qw( catch try );
31
use JSON;
32
use Data::Dumper;
31
33
32
=head1 API
34
=head1 API
33
35
Lines 43-54 sub get { Link Here
43
    my $c = shift->openapi->valid_input or return;
45
    my $c = shift->openapi->valid_input or return;
44
46
45
    my $attributes;
47
    my $attributes;
46
    $attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed
48
    $attributes =
47
        unless $c->req->headers->accept =~ m/application\/json/;
49
      { prefetch => ['metadata'] }    # don't prefetch metadata if not needed
50
      unless $c->req->headers->accept =~ m/application\/json/;
48
51
49
    my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, $attributes );
52
    my $biblio = Koha::Biblios->find(
53
        { biblionumber => $c->validation->param('biblio_id') }, $attributes );
50
54
51
    unless ( $biblio ) {
55
    unless ($biblio) {
52
        return $c->render(
56
        return $c->render(
53
            status  => 404,
57
            status  => 404,
54
            openapi => {
58
            openapi => {
Lines 92-101 sub get { Link Here
92
                any => {
96
                any => {
93
                    status  => 406,
97
                    status  => 406,
94
                    openapi => [
98
                    openapi => [
95
                        "application/json",
99
                        "application/json",         "application/marcxml+xml",
96
                        "application/marcxml+xml",
100
                        "application/marc-in-json", "application/marc",
97
                        "application/marc-in-json",
98
                        "application/marc",
99
                        "text/plain"
101
                        "text/plain"
100
                    ]
102
                    ]
101
                }
103
                }
Lines 154-256 sub get_biblios_public { Link Here
154
156
155
    my $c = shift->openapi->valid_input or return;
157
    my $c = shift->openapi->valid_input or return;
156
158
157
    my $requested_content_type = $c->req->headers->header('Accept');
158
    my $searcher = Koha::SearchEngine::Search->new( { index => 'biblios' } );
159
    my $searcher = Koha::SearchEngine::Search->new( { index => 'biblios' } );
159
    my $query    = $c->validation->param('q_ccl');
160
    my $query    = $c->validation->param('q_ccl');
160
    my $record_processor = Koha::RecordProcessor->new(
161
        {
162
            filters => 'ViewPolicy',
163
            options => {
164
                interface => 'opac',
165
            }
166
        }
167
    );
168
161
169
    my ( $error, $results, $total_hits ) =
162
    warn Dumper($query);
170
      $searcher->simple_search_compat( $query, 0, undef );
163
164
    my ( $error, $results, $total_hits ) = $searcher->simple_search_compat( $query, 0, undef );
171
165
172
    if ( !$total_hits ) {
166
    if ( !$total_hits ) {
173
        return $c->render(
167
        return $c->render(
174
            status  => 404,
168
            status  => 404,
175
            openapi => {
169
            openapi => { error => 'Nothing found.' }
176
                error => 'Nothing found.'
177
            }
178
        );
170
        );
179
    }
171
    }
180
172
181
    sub format_record_by_content_type {
182
        my ($args) = @_;
183
184
        if ( $args->{'content_type'} eq 'application/marc-in-json' ) {
185
            for my $record ( @{ $args->{'records'} } ) {
186
                $record = $record->to_mij;
187
            }
188
            return ( q{[} . ( join q{,}, @{ $args->{'records'} } ) . q{]} );
189
        }
190
191
        # Insert additional content types here or above, you know .. alphabetically
192
    }
193
194
    sub process_record {
195
        my ($args) = @_;
196
197
        my $biblionumber =
198
          $args->{'searcher'}->extract_biblionumber( $args->{'record'} );
199
        my $biblio = Koha::Biblios->find( { biblionumber => $biblionumber } );
200
201
        if (
202
            !(
203
                   $args->{'patron'}
204
                && $args->{'patron'}->category->override_hidden_items
205
            )
206
          )
207
        {
208
            if ( $biblio->hidden_in_opac( { rules => $args->{'rules'} } ) ) {
209
                next;
210
            }
211
        }
212
213
        $args->{'processor'}->process( $args->{'record'} );
214
215
        return $args->{'record'};
216
    }
217
218
    return try {
173
    return try {
219
174
220
        my $marcflavour = C4::Context->preference('marcflavour');
175
        my $patron                = $c->stash('koha.user');
221
176
        my $is_public             = $c->stash('is_public');
222
        # Apply framework's filtering to MARC::Record object
177
        my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems');
223
178
        my @biblionumbers         = map { $_->field('999')->subfield('c') } $results->@*;
224
        my $patron = $c->stash('koha.user');
179
        my @biblios               = map { Koha::Biblios->find( { biblionumber => $_ } ) } @biblionumbers;
225
        my $opachiddenitems_rules =
180
        my @records               = map {
226
          C4::Context->yaml_preference('OpacHiddenItems');
181
            next if ( $is_public && !( $patron && $patron->category->override_hidden_items )
227
182
                && $_->hidden_in_opac( { rules => $opachiddenitems_rules } ) );
228
        my @records;
183
            $_->metadata->record;
229
        for my $result ( @{$results} ) {
184
        } @biblios;
230
231
            if ( ref($result) eq q{} ) { next; }
232
233
            push @records,
234
              process_record(
235
                {
236
                    searcher  => $searcher,
237
                    processor => $record_processor,
238
                    record    => $result,
239
                    patron    => $patron,
240
                    rules     => $opachiddenitems_rules
241
                }
242
              );
243
        }
244
245
        my $response =
246
          format_record_by_content_type(
247
            { content_type => $requested_content_type, records => \@records } );
248
185
249
        $c->respond_to(
186
        $c->respond_to(
250
            mij => {
187
            mij => {
251
                status => 200,
188
                status => 200,
252
                format => 'mij',
189
                format => 'mij',
253
                data   => $response
190
                data   => '[' . ( join ',', map { $_->to_mij } @records ) . ']',
254
            },
191
            },
255
            any => {
192
            any => {
256
                status  => 406,
193
                status  => 406,
Lines 263-268 sub get_biblios_public { Link Here
263
    };
200
    };
264
}
201
}
265
202
203
266
=head3 get_public
204
=head3 get_public
267
205
268
Controller function that handles retrieving a single biblio object
206
Controller function that handles retrieving a single biblio object
Lines 289-302 sub get_public { Link Here
289
227
290
        my $record = $biblio->metadata->record;
228
        my $record = $biblio->metadata->record;
291
229
292
        my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems');
230
        my $opachiddenitems_rules =
231
          C4::Context->yaml_preference('OpacHiddenItems');
293
        my $patron = $c->stash('koha.user');
232
        my $patron = $c->stash('koha.user');
294
233
295
        # Check if the biblio should be hidden for unprivileged access
234
        # Check if the biblio should be hidden for unprivileged access
296
        # unless there's a logged in user, and there's an exception for it's
235
        # unless there's a logged in user, and there's an exception for it's
297
        # category
236
        # category
298
        unless ( $patron and $patron->category->override_hidden_items ) {
237
        unless ( $patron and $patron->category->override_hidden_items ) {
299
            if ( $biblio->hidden_in_opac({ rules => $opachiddenitems_rules }) )
238
            if (
239
                $biblio->hidden_in_opac( { rules => $opachiddenitems_rules } ) )
300
            {
240
            {
301
                return $c->render(
241
                return $c->render(
302
                    status  => 404,
242
                    status  => 404,
Lines 309-321 sub get_public { Link Here
309
249
310
        my $marcflavour = C4::Context->preference("marcflavour");
250
        my $marcflavour = C4::Context->preference("marcflavour");
311
251
312
        my $record_processor = Koha::RecordProcessor->new({
252
        my $record_processor = Koha::RecordProcessor->new(
313
            filters => 'ViewPolicy',
253
            {
314
            options => {
254
                filters => 'ViewPolicy',
315
                interface => 'opac',
255
                options => {
316
                frameworkcode => $biblio->frameworkcode
256
                    interface     => 'opac',
257
                    frameworkcode => $biblio->frameworkcode
258
                }
317
            }
259
            }
318
        });
260
        );
261
319
        # Apply framework's filtering to MARC::Record object
262
        # Apply framework's filtering to MARC::Record object
320
        $record_processor->process($record);
263
        $record_processor->process($record);
321
264
Lines 343-352 sub get_public { Link Here
343
            any => {
286
            any => {
344
                status  => 406,
287
                status  => 406,
345
                openapi => [
288
                openapi => [
346
                    "application/marcxml+xml",
289
                    "application/marcxml+xml", "application/marc-in-json",
347
                    "application/marc-in-json",
290
                    "application/marc",        "text/plain"
348
                    "application/marc",
349
                    "text/plain"
350
                ]
291
                ]
351
            }
292
            }
352
        );
293
        );
Lines 365-373 Controller function that handles retrieving biblio's items Link Here
365
sub get_items {
306
sub get_items {
366
    my $c = shift->openapi->valid_input or return;
307
    my $c = shift->openapi->valid_input or return;
367
308
368
    my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, { prefetch => ['items'] } );
309
    my $biblio = Koha::Biblios->find(
310
        { biblionumber => $c->validation->param('biblio_id') },
311
        { prefetch     => ['items'] } );
369
312
370
    unless ( $biblio ) {
313
    unless ($biblio) {
371
        return $c->render(
314
        return $c->render(
372
            status  => 404,
315
            status  => 404,
373
            openapi => {
316
            openapi => {
Lines 379-385 sub get_items { Link Here
379
    return try {
322
    return try {
380
323
381
        my $items_rs = $biblio->items;
324
        my $items_rs = $biblio->items;
382
        my $items    = $c->objects->search( $items_rs );
325
        my $items    = $c->objects->search($items_rs);
383
        return $c->render(
326
        return $c->render(
384
            status  => 200,
327
            status  => 200,
385
            openapi => $items
328
            openapi => $items
Lines 437-443 sub pickup_locations { Link Here
437
    my $c = shift->openapi->valid_input or return;
380
    my $c = shift->openapi->valid_input or return;
438
381
439
    my $biblio_id = $c->validation->param('biblio_id');
382
    my $biblio_id = $c->validation->param('biblio_id');
440
    my $biblio = Koha::Biblios->find( $biblio_id );
383
    my $biblio    = Koha::Biblios->find($biblio_id);
441
384
442
    unless ($biblio) {
385
    unless ($biblio) {
443
        return $c->render(
386
        return $c->render(
Lines 447-453 sub pickup_locations { Link Here
447
    }
390
    }
448
391
449
    my $patron_id = delete $c->validation->output->{patron_id};
392
    my $patron_id = delete $c->validation->output->{patron_id};
450
    my $patron    = Koha::Patrons->find( $patron_id );
393
    my $patron    = Koha::Patrons->find($patron_id);
451
394
452
    unless ($patron) {
395
    unless ($patron) {
453
        return $c->render(
396
        return $c->render(
Lines 463-476 sub pickup_locations { Link Here
463
        my @response = ();
406
        my @response = ();
464
        if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
407
        if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
465
408
466
            my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
409
            my $libraries_rs =
467
            my $libraries    = $c->objects->search($libraries_rs);
410
              Koha::Libraries->search( { pickup_location => 1 } );
411
            my $libraries = $c->objects->search($libraries_rs);
468
412
469
            @response = map {
413
            @response = map {
470
                my $library = $_;
414
                my $library = $_;
471
                $library->{needs_override} = (
415
                $library->{needs_override} = (
472
                    any { $_->branchcode eq $library->{library_id} }
416
                    any { $_->branchcode eq $library->{library_id} }
473
                    @{$pl_set->as_list}
417
                    @{ $pl_set->as_list }
474
                  )
418
                  )
475
                  ? Mojo::JSON->false
419
                  ? Mojo::JSON->false
476
                  : Mojo::JSON->true;
420
                  : Mojo::JSON->true;
Lines 480-486 sub pickup_locations { Link Here
480
        else {
424
        else {
481
425
482
            my $pickup_locations = $c->objects->search($pl_set);
426
            my $pickup_locations = $c->objects->search($pl_set);
483
            @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
427
            @response = map { $_->{needs_override} = Mojo::JSON->false; $_; }
428
              @{$pickup_locations};
484
        }
429
        }
485
430
486
        return $c->render(
431
        return $c->render(
Lines 503-511 access. Link Here
503
sub get_items_public {
448
sub get_items_public {
504
    my $c = shift->openapi->valid_input or return;
449
    my $c = shift->openapi->valid_input or return;
505
450
506
    my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, { prefetch => ['items'] } );
451
    my $biblio = Koha::Biblios->find(
452
        { biblionumber => $c->validation->param('biblio_id') },
453
        { prefetch     => ['items'] } );
507
454
508
    unless ( $biblio ) {
455
    unless ($biblio) {
509
        return $c->render(
456
        return $c->render(
510
            status  => 404,
457
            status  => 404,
511
            openapi => {
458
            openapi => {
Lines 518-525 sub get_items_public { Link Here
518
465
519
        my $patron = $c->stash('koha.user');
466
        my $patron = $c->stash('koha.user');
520
467
521
        my $items_rs = $biblio->items->filter_by_visible_in_opac({ patron => $patron });
468
        my $items_rs =
522
        my $items    = $c->objects->search( $items_rs );
469
          $biblio->items->filter_by_visible_in_opac( { patron => $patron } );
470
        my $items = $c->objects->search($items_rs);
523
        return $c->render(
471
        return $c->render(
524
            status  => 200,
472
            status  => 200,
525
            openapi => $items
473
            openapi => $items
Lines 530-533 sub get_items_public { Link Here
530
    };
478
    };
531
}
479
}
532
480
533
1;
481
1;
534
- 

Return to bug 25870