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

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

Return to bug 31383