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

(-)a/Koha/ILL/Request.pm (+36 lines)
Lines 2220-2225 sub get_staff_table_actions { Link Here
2220
    return $ill_table_actions;
2220
    return $ill_table_actions;
2221
}
2221
}
2222
2222
2223
=head3 AUTOLOAD
2224
2225
=cut
2226
2227
our $AUTOLOAD;
2228
2229
sub AUTOLOAD {
2230
    my ($self) = @_;
2231
2232
    my $name = $AUTOLOAD;
2233
    $name =~ s/.*:://;    # Remove package name
2234
2235
    if ( $name =~ /^extended_attributes_(\w+)$/ ) {
2236
        my $type = $1;
2237
2238
        # Define the method dynamically
2239
        no strict 'refs';
2240
        *{$AUTOLOAD} = sub {
2241
            my ($self)   = @_;
2242
            my $relation = 'extended_attributes_' . $type;
2243
            my $rs       = $self->_result->$relation;
2244
            return Koha::ILL::Request::Attributes->_new_from_dbic($rs)->search;
2245
        };
2246
2247
        # Call the newly defined method
2248
        return $self->$name();
2249
    }
2250
    my $wt = 'SUPER::'.$name;
2251
    return $self->$wt;
2252
}
2253
2254
sub get_column(){
2255
    my ($self, $column_name) = @_;
2256
    return $self->_result->get_column($column_name);
2257
}
2258
2223
=head3 _type
2259
=head3 _type
2224
2260
2225
=cut
2261
=cut
(-)a/Koha/ILL/Requests.pm (+40 lines)
Lines 108-113 sub search_incomplete { Link Here
108
108
109
=head2 Internal methods
109
=head2 Internal methods
110
110
111
112
=head3 _build_extended_attributes_relations
113
114
Method to dynamically add has_many relations for each ILLRequestAttribute type stored in the ILLRequestAttributes table.
115
116
Used in the API to allow for advanced joins.
117
118
Returns a list of relation accessor names.
119
120
=cut
121
122
sub _build_extended_attributes_relations {
123
    my ( $self, $types ) = @_;
124
125
    my $result_source = $self->_resultset->result_source;
126
    for my $type ( @{$types} ) {
127
        $result_source->add_relationship(
128
            "extended_attributes_$type",
129
            "Koha::Schema::Result::Illrequestattribute",
130
            sub {
131
                my $args = shift;
132
133
                return {
134
                    "$args->{foreign_alias}.illrequest_id" => { -ident => "$args->{self_alias}.illrequest_id" },
135
                    "$args->{foreign_alias}.type"          => { '=', $type },
136
                };
137
            },
138
            {
139
                accessor       => 'multi',
140
                join_type      => 'LEFT',
141
                cascade_copy   => 0,
142
                cascade_delete => 0,
143
                is_depends_on  => 0
144
            },
145
        );
146
147
    }
148
    return map { 'extended_attributes_' . $_ } @{$types};
149
}
150
111
=head3 _type
151
=head3 _type
112
152
113
=cut
153
=cut
(-)a/Koha/REST/Plugin/Objects.pm (-1 / +2 lines)
Lines 291-297 controller, and thus shouldn't be called twice in it. Link Here
291
            $c->dbic_extended_attributes_join(
291
            $c->dbic_extended_attributes_join(
292
                {
292
                {
293
                    attributes      => $attributes,
293
                    attributes      => $attributes,
294
                    filtered_params => $filtered_params
294
                    filtered_params => $filtered_params,
295
                    result_set      => $result_set
295
                }
296
                }
296
            );
297
            );
297
298
(-)a/Koha/REST/Plugin/Query.pm (-29 / +28 lines)
Lines 173-188 Generates the DBIC join attribute based on extended_attributes query entries, an Link Here
173
173
174
            my $attributes      = $args->{attributes};
174
            my $attributes      = $args->{attributes};
175
            my $filtered_params = $args->{filtered_params};
175
            my $filtered_params = $args->{filtered_params};
176
            my $result_set      = $args->{result_set};
176
177
177
            if (   reftype( $attributes->{prefetch} )
178
            if (   reftype( $attributes->{prefetch} )
178
                && reftype( $attributes->{prefetch} ) eq 'ARRAY'
179
                && reftype( $attributes->{prefetch} ) eq 'ARRAY'
179
                && grep ( /extended_attributes/, @{ $attributes->{prefetch} } ) )
180
                && grep ( /extended_attributes/, @{ $attributes->{prefetch} } ) )
180
            {
181
            {
181
                my $ea_entries = $self->_get_extended_attributes_entries( $filtered_params, 0 );
182
                my @array = $self->_get_extended_attributes_entries( $filtered_params, 0 );
182
                while ( $ea_entries > 0 ) {
183
183
                    push( @{ $attributes->{join} }, 'extended_attributes' );
184
                # Calling our private method to build the extended attributes relations
184
                    $ea_entries--;
185
                my @joins = $result_set->_build_extended_attributes_relations(\@array);
185
                }
186
                push @{ $attributes->{join} }, @joins;
187
186
            }
188
            }
187
        }
189
        }
188
    );
190
    );
Lines 557-592 Example: Returns 2 if given a $filtered_params containing the below: Link Here
557
=cut
559
=cut
558
560
559
sub _get_extended_attributes_entries {
561
sub _get_extended_attributes_entries {
560
    my ( $self, $params, $extended_attributes_entries ) = @_;
562
    my ( $self, $params, $extended_attributes_entries, @array ) = @_;
561
563
562
    if ( reftype($params) && reftype($params) eq 'HASH' ) {
564
    if ( reftype($params) && reftype($params) eq 'HASH' ) {
563
565
564
        # rewrite additional_field_values table query params
566
        # rewrite additional_field_values table query params
565
        $extended_attributes_entries =
567
        @array =
566
            _rewrite_related_metadata_query( $params, $extended_attributes_entries, 'field_id', 'value' )
568
            _rewrite_related_metadata_query( $params, 'field_id', 'value', @array )
567
            if $params->{'extended_attributes.field_id'};
569
            if $params->{'extended_attributes.field_id'};
568
570
569
        # rewrite borrower_attributes table query params
571
        # rewrite borrower_attributes table query params
570
        $extended_attributes_entries =
572
        @array =
571
            _rewrite_related_metadata_query( $params, $extended_attributes_entries, 'code', 'attribute' )
573
            _rewrite_related_metadata_query( $params, 'code', 'attribute', @array )
572
            if $params->{'extended_attributes.code'};
574
            if $params->{'extended_attributes.code'};
573
575
574
        # rewrite illrequestattributes table query params
576
        # rewrite illrequestattributes table query params
575
        $extended_attributes_entries =
577
        @array =
576
            _rewrite_related_metadata_query( $params, $extended_attributes_entries, 'type', 'value' )
578
            _rewrite_related_metadata_query( $params, 'type', 'value', @array )
577
            if $params->{'extended_attributes.type'};
579
            if $params->{'extended_attributes.type'};
578
580
579
        foreach my $key ( keys %{$params} ) {
581
        foreach my $key ( keys %{$params} ) {
580
            return $self->_get_extended_attributes_entries( $params->{$key}, $extended_attributes_entries );
582
            return $self->_get_extended_attributes_entries( $params->{$key}, $extended_attributes_entries, @array );
581
        }
583
        }
582
    } elsif ( reftype($params) && reftype($params) eq 'ARRAY' ) {
584
    } elsif ( reftype($params) && reftype($params) eq 'ARRAY' ) {
583
        foreach my $ea_instance (@$params) {
585
        foreach my $ea_instance (@$params) {
584
            $extended_attributes_entries =
586
             @array = $self->_get_extended_attributes_entries( $ea_instance, $extended_attributes_entries, @array );
585
                +$self->_get_extended_attributes_entries( $ea_instance, $extended_attributes_entries );
586
        }
587
        }
587
        return $extended_attributes_entries;
588
        return @array;
588
    } else {
589
    } else {
589
        return $extended_attributes_entries;
590
        return @array;
590
    }
591
    }
591
}
592
}
592
593
Lines 625-644 It'll be rewritten as: Link Here
625
=cut
626
=cut
626
627
627
sub _rewrite_related_metadata_query {
628
sub _rewrite_related_metadata_query {
628
    my ( $params, $extended_attributes_entries, $key, $value ) = @_;
629
    my ( $params, $key, $value, @array ) = @_;
629
630
630
    $extended_attributes_entries++;
631
    my $old_key_value = delete $params->{ 'extended_attributes.' . $key };
631
    if ( $extended_attributes_entries > 1 ) {
632
    my $new_key_value = "extended_attributes_$old_key_value" . "." . $key;
632
        my $old_key_value = delete $params->{ 'extended_attributes.' . $key };
633
    $params->{$new_key_value} = $old_key_value;
633
        my $new_key_value = "extended_attributes_$extended_attributes_entries" . "." . $key;
634
        $params->{$new_key_value} = $old_key_value;
635
634
636
        my $old_value_value = delete $params->{ 'extended_attributes.' . $value };
635
    my $old_value_value = delete $params->{ 'extended_attributes.' . $value };
637
        my $new_value_value = "extended_attributes_$extended_attributes_entries" . "." . $value;
636
    my $new_value_value = "extended_attributes_$old_key_value" . "." . $value;
638
        $params->{$new_value_value} = $old_value_value;
637
    $params->{$new_value_value} = $old_value_value;
639
    }
638
639
    push @array, $old_key_value;
640
640
641
    return $extended_attributes_entries;
641
    return @array;
642
}
642
}
643
643
644
1;
644
1;
645
- 

Return to bug 37389