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

(-)a/Koha/REST/Plugin/Objects.pm (+8 lines)
Lines 287-292 controller, and thus shouldn't be called twice in it. Link Here
287
            $c->stash('koha.pagination.base_total'   => $result_set->count);
287
            $c->stash('koha.pagination.base_total'   => $result_set->count);
288
            $c->stash('koha.pagination.query_params' => $args);
288
            $c->stash('koha.pagination.query_params' => $args);
289
289
290
            # Check and handle related metadata table joins
291
            $c->dbic_extended_attributes_join(
292
                {
293
                    attributes => $attributes,
294
                    filtered_params => $filtered_params
295
                }
296
            );
297
290
            # Generate the resultset
298
            # Generate the resultset
291
            my $objects_rs = $result_set->search( $filtered_params, $attributes );
299
            my $objects_rs = $result_set->search( $filtered_params, $attributes );
292
            # Stash the page total if requires, total otherwise
300
            # Stash the page total if requires, total otherwise
(-)a/Koha/REST/Plugin/Query.pm (-1 / +152 lines)
Lines 159-164 Generates the DBIC prefetch attribute based on embedded relations, and merges in Link Here
159
        }
159
        }
160
    );
160
    );
161
161
162
=head3 dbic_extended_attributes_join
163
164
    $attributes = $c->dbic_extended_attributes_join({ attributes => $attributes, result_set => $result_set });
165
166
Generates the DBIC join attribute based on extended_attributes query entires, and merges into I<$attributes>.
167
168
=cut
169
170
$app->helper(
171
    'dbic_extended_attributes_join' => sub {
172
        my ( $c, $args ) = @_;
173
174
        my $attributes = $args->{attributes};
175
        my $filtered_params = $args->{filtered_params};
176
       
177
        if (   reftype( $attributes->{prefetch} )
178
            && reftype( $attributes->{prefetch} ) eq 'ARRAY'
179
            && grep ( /extended_attributes/, @{ $attributes->{prefetch} } ) )
180
        {
181
            my $ea_entries = $self->_get_extended_attributes_entries( $filtered_params, 0 );
182
            while ( $ea_entries > 0 ) {
183
                push( @{ $attributes->{join} }, 'extended_attributes' );
184
                $ea_entries--;
185
            }
186
        }
187
    }
188
);
189
162
=head3 _build_query_params_from_api
190
=head3 _build_query_params_from_api
163
191
164
    my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
192
    my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
Lines 486-492 sub _parse_dbic_query { Link Here
486
    } else {
514
    } else {
487
        return $q_params;
515
        return $q_params;
488
    }
516
    }
517
}
518
519
=head3 _get_extended_attributes_entries
520
521
    $self->_get_extended_attributes_entries( $filtered_params, 0 )
522
523
Recursive function that returns the number of extended_attributes entries present in a query.
524
Example: Returns 2 if given a $filtered_params containing the below:
525
526
{
527
    "-and":[
528
        [
529
            {
530
                "extended_attributes.value":{
531
                    "like":"abc%"
532
                },
533
                "extended_attributes.code":[
534
                    [
535
                        "arbitrary_attr_code",
536
                        "another_attr_code"
537
                    ]
538
                ]
539
            }
540
        ],
541
        [
542
            {
543
                "extended_attributes.value":{
544
                    "like":"123%"
545
                },
546
                "extended_attributes.code":[
547
                    [
548
                        "arbitrary_attr_code",
549
                        "another_attr_code"
550
                    ]
551
                ]
552
            }
553
        ]
554
    ]
555
}
556
557
=cut
558
559
sub _get_extended_attributes_entries {
560
    my ( $self, $params, $extended_attributes_entries ) = @_;
561
562
    if ( reftype($params) && reftype($params) eq 'HASH' ) {
563
564
        # rewrite additional_field_values table query params
565
        $extended_attributes_entries =
566
            _rewrite_related_metadata_query( $params, $extended_attributes_entries, 'field_id', 'value' )
567
            if $params->{'extended_attributes.field_id'};
568
569
        # rewrite borrower_attributes table query params
570
        $extended_attributes_entries =
571
            _rewrite_related_metadata_query( $params, $extended_attributes_entries, 'code', 'attribute' )
572
            if $params->{'extended_attributes.code'};
573
574
        # rewrite illrequestattributes table query params
575
        $extended_attributes_entries =
576
            _rewrite_related_metadata_query( $params, $extended_attributes_entries, 'type', 'value' )
577
            if $params->{'extended_attributes.type'};
578
579
        foreach my $key ( keys %{$params} ) {
580
            return $self->_get_extended_attributes_entries( $params->{$key}, $extended_attributes_entries );
581
        }
582
    } elsif ( reftype($params) && reftype($params) eq 'ARRAY' ) {
583
        foreach my $ea_instance (@$params) {
584
            $extended_attributes_entries =
585
                +$self->_get_extended_attributes_entries( $ea_instance, $extended_attributes_entries );
586
        }
587
        return $extended_attributes_entries;
588
    } else {
589
        return $extended_attributes_entries;
590
    }
591
}
592
593
=head3 _rewrite_related_metadata_query
594
595
        $extended_attributes_entries =
596
            _rewrite_related_metadata_query( $params, $extended_attributes_entries, 'field_id', 'value' )
597
598
Helper function that rewrites all subsequent extended_attributes queries to match the alias generated by the dbic self left join
599
Take the below example (patrons), assuming it is the third instance of an extended_attributes query:
600
{
601
    "extended_attributes.value":{
602
        "like":"123%"
603
    },
604
    "extended_attributes.code":[
605
        [
606
            "arbitrary_attr_code",
607
            "another_attr_code"
608
        ]
609
    ]
610
}
611
612
It'll be rewritten as:
613
{
614
    "extended_attributes_3.value":{
615
        "like":"123%"
616
    },
617
    "extended_attributes_3.code":[
618
        [
619
            "arbitrary_attr_code",
620
            "another_attr_code"
621
        ]
622
    ]
623
}
624
625
=cut
626
627
sub _rewrite_related_metadata_query {
628
    my ( $params, $extended_attributes_entries, $key, $value ) = @_;
629
630
    $extended_attributes_entries++;
631
    if ( $extended_attributes_entries > 1 ) {
632
        my $old_key_value = delete $params->{ 'extended_attributes.' . $key };
633
        my $new_key_value = "extended_attributes_$extended_attributes_entries" . "." . $key;
634
        $params->{$new_key_value} = $old_key_value;
635
636
        my $old_value_value = delete $params->{ 'extended_attributes.' . $value };
637
        my $new_value_value = "extended_attributes_$extended_attributes_entries" . "." . $value;
638
        $params->{$new_value_value} = $old_value_value;
639
    }
489
640
641
    return $extended_attributes_entries;
490
}
642
}
491
643
492
1;
644
1;
493
- 

Return to bug 30645