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

(-)a/Koha/AdditionalContents.pm (-37 / +17 lines)
Lines 71-114 location is one of this: Link Here
71
71
72
sub search_for_display {
72
sub search_for_display {
73
    my ( $self, $params ) = @_;
73
    my ( $self, $params ) = @_;
74
    my $lang = $params->{lang};
74
75
75
    my $search_params;
76
    # If lang is not default, we will search for entries matching $lang but fallback to default if $lang is not found
76
    $search_params->{location}     = $params->{location};
77
    # Then we need a subquery count in where clause; DBIx::Class/SQL::Abstract does not support it, fallback to literal SQL
77
    $search_params->{branchcode}   = $params->{library_id} ? [ $params->{library_id}, undef ] : undef;
78
    my $subquery =
78
    $search_params->{published_on} = { '<=' => \'CAST(NOW() AS DATE)' };
79
        qq|(SELECT COUNT(*) FROM additional_contents_localizations WHERE lang='$lang' AND additional_content_id=me.additional_content_id)=0|;
79
    $search_params->{-or}          = [
80
        expirationdate => { '>=' => \'CAST(NOW() AS DATE)' },
81
        expirationdate => undef
82
    ];
83
    $search_params->{category} = $params->{category} if $params->{category};
84
85
    my $contents = $self->SUPER::search( $search_params, { order_by => 'number' } );
86
    my @all_content_id = $contents->get_column('id');
87
88
    my ( $translated_contents, @translated_content_id );
89
    if ( $params->{lang} && $params->{lang} ne 'default' ) {
90
        $translated_contents = Koha::AdditionalContentsLocalizations->search(
91
            {
92
                additional_content_id => [$contents->get_column('id')],
93
                lang => $params->{lang},
94
            }
95
        );
96
        @translated_content_id = $translated_contents->get_column('additional_content_id');
97
    }
98
99
    my $default_contents    = Koha::AdditionalContentsLocalizations->search(
100
        {
101
            additional_content_id => [array_minus(@all_content_id, @translated_content_id)],
102
            lang                  => 'default',
103
        }
104
    );
105
106
    return Koha::AdditionalContentsLocalizations->search(
107
        {
108
            id => [ $translated_contents ? $translated_contents->get_column('id') : (), $default_contents->get_column('id') ]
109
        },
110
    );
111
80
81
    my $search_params;
82
    $search_params->{location}       = $params->{location};
83
    $search_params->{branchcode}     = $params->{library_id} ? [ $params->{library_id}, undef ] : undef;
84
    $search_params->{published_on}   = { '<=' => \'CAST(NOW() AS DATE)' };
85
    $search_params->{expirationdate} = [ '-or', { '>=' => \'CAST(NOW() AS DATE)' }, undef ];
86
    $search_params->{category}       = $params->{category} if $params->{category};
87
    $search_params->{lang}           = 'default' if !$lang || $lang eq 'default';
88
    $search_params->{-or}            = [ { 'lang' => $lang }, '-and' => [ 'lang', 'default', \$subquery ] ]
89
        if !$search_params->{lang};
90
91
    my $attribs = { prefetch => 'additional_content', order_by => 'additional_content.number' };
92
    return Koha::AdditionalContentsLocalizations->search( $search_params, $attribs );
112
}
93
}
113
94
114
=head3 find_best_match
95
=head3 find_best_match
115
- 

Return to bug 31383