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

(-)a/C4/Koha.pm (-34 / +13 lines)
Lines 547-589 sub GetAuthorisedValues { Link Here
547
    my $result = $cache->get_from_cache($cache_key);
547
    my $result = $cache->get_from_cache($cache_key);
548
    return $result if $result;
548
    return $result if $result;
549
549
550
    # FIXME Need to deal with $opac
551
    my $avs = Koha::AuthorisedValues->search_with_localization(
552
        {
553
            ( $category ? ( category => $category ) : () ),
554
            ( $branch_limit ? ( branchcode => $branch_limit ) : () ),
555
        }
556
    );
550
    my @results;
557
    my @results;
551
    my $dbh      = C4::Context->dbh;
558
    while ( my $av = $avs->next ) {
552
    my $query = qq{
559
        my $av_unblessed = $av->unblessed;
553
        SELECT DISTINCT av.*
560
        $av_unblessed->{translated_description} = $av->translated_description;
554
        FROM authorised_values av
561
        $av_unblessed->{lib} = $av->translated_description;
555
    };
562
        $av_unblessed->{lib_opac} = $av->translated_description;
556
    $query .= qq{
563
        push @results, $av_unblessed;
557
          LEFT JOIN authorised_values_branches ON ( id = av_id )
558
    } if $branch_limit;
559
    my @where_strings;
560
    my @where_args;
561
    if($category) {
562
        push @where_strings, "category = ?";
563
        push @where_args, $category;
564
    }
565
    if($branch_limit) {
566
        push @where_strings, "( branchcode = ? OR branchcode IS NULL )";
567
        push @where_args, $branch_limit;
568
    }
569
    if(@where_strings > 0) {
570
        $query .= " WHERE " . join(" AND ", @where_strings);
571
    }
572
    $query .= ' ORDER BY category, ' . (
573
                $opac ? 'COALESCE(lib_opac, lib)'
574
                      : 'lib, lib_opac'
575
              );
576
577
    my $sth = $dbh->prepare($query);
578
564
579
    $sth->execute( @where_args );
580
    while (my $data=$sth->fetchrow_hashref) {
581
        if ($opac && $data->{lib_opac}) {
582
            $data->{lib} = $data->{lib_opac};
583
        }
584
        push @results, $data;
585
    }
565
    }
586
    $sth->finish;
587
566
588
    $cache->set_in_cache( $cache_key, \@results, { expiry => 5 } );
567
    $cache->set_in_cache( $cache_key, \@results, { expiry => 5 } );
589
    return \@results;
568
    return \@results;
(-)a/Koha/AuthorisedValue.pm (+56 lines)
Lines 21-27 use Modern::Perl; Link Here
21
21
22
use Carp;
22
use Carp;
23
23
24
use C4::Languages;
24
use Koha::Database;
25
use Koha::Database;
26
use Koha::Localizations;
25
27
26
use base qw(Koha::Object Koha::Object::Limit::Library);
28
use base qw(Koha::Object Koha::Object::Limit::Library);
27
29
Lines 47-54 sub opac_description { Link Here
47
    return $self->lib_opac() || $self->lib();
49
    return $self->lib_opac() || $self->lib();
48
}
50
}
49
51
52
50
=head2 Internal methods
53
=head2 Internal methods
51
54
55
=head3 translated_description
56
57
=cut
58
59
sub translated_description {
60
    my ( $self, $lang ) = @_;
61
    if ( my $translated_description = eval { $self->get_column('translated_description') } ) {
62
        # If the value has already been fetched (eg. from sarch_with_localization),
63
        # do not search for it again
64
        # Note: This is a bit hacky but should be fast
65
        return $translated_description
66
             ? $translated_description
67
             : $self->lib;
68
    }
69
    $lang ||= C4::Languages::getlanguage;
70
    my $translated_description = Koha::Localizations->search({
71
        code => $self->authorised_value,
72
        entity => 'avs',
73
        lang => $lang
74
    })->next;
75
    return $translated_description
76
         ? $translated_description->translation
77
         : $self->lib;
78
}
79
80
=head3 translated_descriptions
81
82
=cut
83
84
sub translated_descriptions {
85
    my ( $self ) = @_;
86
    my @translated_descriptions = Koha::Localizations->search(
87
        {   entity => 'avs',
88
            code   => $self->authorised_value,
89
        }
90
    );
91
    return [ map {
92
        {
93
            lang => $_->lang,
94
            translation => $_->translation,
95
        }
96
    } @translated_descriptions ];
97
}
98
99
=head3 image_location
100
101
=cut
102
103
sub image_location {
104
    my ( $self, $interface ) = @_;
105
    return C4::Koha::getitemtypeimagelocation( $interface, $self->SUPER::imageurl );
106
}
107
52
=head3 _type
108
=head3 _type
53
109
54
=cut
110
=cut
(-)a/Koha/AuthorisedValues.pm (-2 / +28 lines)
Lines 60-66 sub search { Link Here
60
        ]
60
        ]
61
      }
61
      }
62
      : {};
62
      : {};
63
    my $join = $branchcode ? { join => 'authorised_values_branches' } : {};
63
64
    my @join = $attributes->{join} || ();
65
    push @join, 'authorised_values_branches';
66
    my $join = { join => \@join };
64
    $attributes //= {};
67
    $attributes //= {};
65
    $attributes = { %$attributes, %$join };
68
    $attributes = { %$attributes, %$join };
66
    return $self->SUPER::search( { %$params, %$or, }, $attributes );
69
    return $self->SUPER::search( { %$params, %$or, }, $attributes );
Lines 108-114 sub find_by_koha_field { Link Here
108
    my $kohafield        = $params->{kohafield};
111
    my $kohafield        = $params->{kohafield};
109
    my $authorised_value = $params->{authorised_value};
112
    my $authorised_value = $params->{authorised_value};
110
113
111
    my $av = $self->SUPER::search(
114
    my $av = $self->search(
112
        {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
115
        {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
113
            'marc_subfield_structures.kohafield'     => $kohafield,
116
            'marc_subfield_structures.kohafield'     => $kohafield,
114
            'me.authorised_value'                    => $authorised_value,
117
            'me.authorised_value'                    => $authorised_value,
Lines 175-180 sub categories { Link Here
175
    return map $_->get_column('category'), $rs->all;
178
    return map $_->get_column('category'), $rs->all;
176
}
179
}
177
180
181
=head3 search_with_localization
182
183
my $avs = Koha::AuthorisedValues->search_with_localization
184
185
=cut
186
187
sub search_with_localization {
188
    my ( $self, $params, $attributes ) = @_;
189
190
    my $language = C4::Languages::getlanguage();
191
    $Koha::Schema::Result::AuthorisedValue::LANGUAGE = $language;
192
    $attributes->{order_by} = 'translated_description' unless exists $attributes->{order_by};
193
    $attributes->{join} = 'localization';
194
    $attributes->{'+select'} = [
195
        {
196
            coalesce => [qw( localization.translation me.lib )],
197
            -as      => 'translated_description'
198
        }
199
    ];
200
    $self->search( $params, $attributes );
201
}
202
203
178
=head3 type
204
=head3 type
179
205
180
=cut
206
=cut
(-)a/Koha/Schema/Result/AuthorisedValue.pm (-1 / +16 lines)
Lines 148-153 __PACKAGE__->has_many( Link Here
148
# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-02-22 14:32:48
148
# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-02-22 14:32:48
149
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hDlebhEn+f+thqwBo/LOqQ
149
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hDlebhEn+f+thqwBo/LOqQ
150
150
151
# Use the ItemtypeLocalization view to create the join on localization
152
our $LANGUAGE;
153
__PACKAGE__->has_many(
154
  "localization" => "Koha::Schema::Result::AuthorisedValueLocalization",
155
    sub {
156
        my $args = shift;
157
158
        die "no lang specified!" unless $LANGUAGE;
159
160
        return ({
161
            "$args->{self_alias}.authorised_value" => { -ident => "$args->{foreign_alias}.code" },
162
            "$args->{foreign_alias}.lang" => $LANGUAGE,
163
        });
164
165
    }
166
);
151
167
152
# You can replace this text with custom code or comments, and it will be preserved on regeneration
153
1;
168
1;
(-)a/Koha/Schema/Result/AuthorisedValueLocalization.pm (+32 lines)
Line 0 Link Here
1
package Koha::Schema::Result::AuthorisedValueLocalization;
2
3
use base 'DBIx::Class::Core';
4
5
use Modern::Perl;
6
7
__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
8
9
__PACKAGE__->table('authorised_value_localizations');
10
__PACKAGE__->result_source_instance->is_virtual(1);
11
__PACKAGE__->result_source_instance->view_definition(
12
    "SELECT localization_id, code, lang, translation FROM localization WHERE entity='avs'"
13
);
14
15
__PACKAGE__->add_columns(
16
  "localization_id",
17
  { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
18
  "code",
19
  { data_type => "varchar", is_nullable => 0, size => 64 },
20
  "lang",
21
  { data_type => "varchar", is_nullable => 0, size => 25 },
22
  "translation",
23
  { data_type => "text", is_nullable => 1 },
24
);
25
26
__PACKAGE__->belongs_to(
27
    "authorised_value",
28
    "Koha::Schema::Result::AuthorisedValue",
29
    { code => 'authorised_value' }
30
);
31
32
1;
(-)a/admin/authorised_values.pl (-15 / +6 lines)
Lines 26-31 use C4::Auth; Link Here
26
use C4::Context;
26
use C4::Context;
27
use C4::Koha;
27
use C4::Koha;
28
use C4::Output;
28
use C4::Output;
29
use Carp::Always;
29
30
30
use Koha::AuthorisedValues;
31
use Koha::AuthorisedValues;
31
use Koha::AuthorisedValueCategories;
32
use Koha::AuthorisedValueCategories;
Lines 92-99 if ($op eq 'add_form') { Link Here
92
            imagesets => C4::Koha::getImageSets(),
93
            imagesets => C4::Koha::getImageSets(),
93
        );
94
        );
94
    }
95
    }
96
97
    my $translated_languages = C4::Languages::getTranslatedLanguages( undef , C4::Context->preference('template') );
95
    $template->param(
98
    $template->param(
96
        branches_loop    => \@branches_loop,
99
        branches_loop    => \@branches_loop,
100
        can_be_translated => ( scalar(@$translated_languages) > 1 ? 1 : 0 ),
97
    );
101
    );
98
102
99
} elsif ($op eq 'add') {
103
} elsif ($op eq 'add') {
Lines 221-243 if ( $op eq 'list' ) { Link Here
221
225
222
    $searchfield ||= $category_list[0];
226
    $searchfield ||= $category_list[0];
223
227
224
    my @avs_by_category = Koha::AuthorisedValues->new->search( { category => $searchfield } );
228
    my $avs_by_category = Koha::AuthorisedValues->search_with_localization( { category => $searchfield } );
225
    my @loop_data = ();
226
    # builds value list
227
    for my $av ( @avs_by_category ) {
228
        my %row_data;  # get a fresh hash for the row data
229
        $row_data{category}              = $av->category;
230
        $row_data{authorised_value}      = $av->authorised_value;
231
        $row_data{lib}                   = $av->lib;
232
        $row_data{lib_opac}              = $av->lib_opac;
233
        $row_data{imageurl}              = getitemtypeimagelocation( 'intranet', $av->imageurl );
234
        $row_data{branches}              = $av->library_limits ? $av->library_limits->as_list : [];
235
        $row_data{id}                    = $av->id;
236
        push(@loop_data, \%row_data);
237
    }
238
229
239
    $template->param(
230
    $template->param(
240
        loop     => \@loop_data,
231
        authorised_values => $avs_by_category,
241
        category => $searchfield,
232
        category => $searchfield,
242
        categories => \@category_list,
233
        categories => \@category_list,
243
    );
234
    );
(-)a/admin/localization.pl (-12 / +4 lines)
Lines 40-64 my ( $template, $borrowernumber, $cookie ) = get_template_and_user( Link Here
40
40
41
my $entity = $query->param('entity');
41
my $entity = $query->param('entity');
42
my $code   = $query->param('code');
42
my $code   = $query->param('code');
43
my $rs     = Koha::Localizations->search( { entity => $entity, code => $code } );
43
my $interface = $query->param('interface');
44
my @translations;
44
my $translations = Koha::Localizations->search( { entity => $entity, code => $code, interface => $interface } );
45
while ( my $s = $rs->next ) {
46
    push @translations,
47
      { id          => $s->localization_id,
48
        entity      => $s->entity,
49
        code        => $s->code,
50
        lang        => $s->lang,
51
        translation => $s->translation,
52
      };
53
}
54
45
55
my $translated_languages = C4::Languages::getTranslatedLanguages( 'intranet', C4::Context->preference('template') );
46
my $translated_languages = C4::Languages::getTranslatedLanguages( 'intranet', C4::Context->preference('template') );
56
47
57
$template->param(
48
$template->param(
58
    translations => \@translations,
49
    translations => $translations,
59
    languages    => $translated_languages,
50
    languages    => $translated_languages,
60
    entity       => $entity,
51
    entity       => $entity,
61
    code         => $code,
52
    code         => $code,
53
    interface_side => $interface, # "interface" conflicts with existing variable's name
62
);
54
);
63
55
64
output_html_with_http_headers $query, $cookie, $template->output;
56
output_html_with_http_headers $query, $cookie, $template->output;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt (-14 / +33 lines)
Lines 74-84 Link Here
74
        </li>
74
        </li>
75
        <li>
75
        <li>
76
            <label for="lib">Description: </label>
76
            <label for="lib">Description: </label>
77
            <input type="text" name="lib" id="lib" value="[% lib | html %]" maxlength="200" />
77
            [% IF can_be_translated %]
78
                <a href="/cgi-bin/koha/admin/localization.pl?entity=avs&code=[% authorised_value | uri %]&interface=intranet"" title="Translate description" rel="gb_page_center[600,500]"><i class="fa fa-pencil"></i> Translate into other languages</a>
79
            [% END %]
78
        </li>
80
        </li>
79
	<li>
81
	<li>
80
            <label for="lib_opac">Description (OPAC): </label>
82
            <label for="lib_opac">Description (OPAC): </label>
81
            <input type="text" name="lib_opac" id="lib_opac" value="[% lib_opac | html %]" maxlength="200" />
83
            <input type="text" name="lib_opac" id="lib_opac" value="[% lib_opac | html %]" maxlength="200" />
84
            [% IF can_be_translated %]
85
                <a href="/cgi-bin/koha/admin/localization.pl?entity=avs&code=[% authorised_value | uri %]&interface=opac" title="Translate OPAC description" rel="gb_page_center[600,500]"><i class="fa fa-pencil"></i> Translate into other languages</a>
86
            [% END %]
82
        </li>
87
        </li>
83
        <li><label for="branches">Library limitations: </label>
88
        <li><label for="branches">Library limitations: </label>
84
            <select id="branches" name="branches" multiple size="10">
89
            <select id="branches" name="branches" multiple size="10">
Lines 207-213 Link Here
207
        </p>
212
        </p>
208
    </form>
213
    </form>
209
214
210
    [% IF loop %]
215
    [% IF authorised_values.count %]
211
216
212
    <table id="categoriest" class="authorized_values_table">
217
    <table id="categoriest" class="authorized_values_table">
213
    <thead><tr>
218
    <thead><tr>
Lines 219-234 Link Here
219
        <th class="noExport">Actions</th>
224
        <th class="noExport">Actions</th>
220
        </tr>
225
        </tr>
221
    </thead><tbody>
226
    </thead><tbody>
222
    [% FOREACH loo IN loop %]
227
    [% FOREACH av IN authorised_values %]
223
    <tr>
228
    <tr>
224
        <td>[% loo.authorised_value | html %]</td>
229
        <td>[% av.authorised_value | html %]</td>
225
        <td>[% loo.lib | html %]</td>
230
        [% IF av.translated_descriptions.size %]
226
        <td>[% loo.lib_opac | html %]</td>
231
            [% av.lib | html %] (default)<br/>
227
        <td>[% IF ( loo.imageurl ) %]<img src="[% loo.imageurl | url %]" alt=""/>[% ELSE %]&nbsp;[% END %]</td>
232
            [% FOR description IN av.translated_descriptions %]
233
                [% IF description.translation == av.translated_description %]
234
                    <b>[% description.translation | html %]</b>
235
                [% ELSE %]
236
                    [% description.translation | html %] ([% description.lang | html %])
237
                [% END %]
238
                <br/>
239
            [% END %]
240
        [% ELSE %]
241
            [% av.lib |html %]
242
        [% END %]
243
244
        <td>[% av.lib_opac | html %]</td>
245
        <td>[% IF ( av.imageurl ) %]<img src="[% av.imageurl | url %]" alt=""/>[% ELSE %]&nbsp;[% END %]</td>
228
        <td>
246
        <td>
229
            [% IF loo.branches.size > 0 %]
247
            [% IF av.branch_limitations.size > 0 %]
230
                [% branches_str = "" %]
248
                [% branches_str = "" %]
231
                [% FOREACH branch IN loo.branches %]
249
                [% FOREACH library IN av.library_limits %]
232
                    [%- IF loop.first -%]
250
                    [%- IF loop.first -%]
233
                    [% branches_str = branch.branchname _ " (" _ branch.branchcode _ ")" %]
251
                    [% branches_str = branch.branchname _ " (" _ branch.branchcode _ ")" %]
234
                    [% ELSE %]
252
                    [% ELSE %]
Lines 236-252 Link Here
236
                    [% END %]
254
                    [% END %]
237
                [% END %]
255
                [% END %]
238
                <span class="library_limitation" title="[% branches_str | html %]">
256
                <span class="library_limitation" title="[% branches_str | html %]">
239
                    [% IF loo.branches.size > 1 %]
257
                    [% IF av.library_limits.size > 1 %]
240
                        [% loo.branches.size | html %] library limitations
258
                        [% av.library_limits.size | html %] library limitations
241
                    [% ELSE %]
259
                    [% ELSE %]
242
                        [% loo.branches.size | html %] library limitation
260
                        [% av.library_limits.size | html %] library limitation
243
                    [% END %]
261
                    [% END %]
244
            [% ELSE %]
262
            [% ELSE %]
245
                No limitation
263
                No limitation
246
            [% END %]
264
            [% END %]
247
        </td>
265
        </td>
248
        <td class="actions"><a href="/cgi-bin/koha/admin/authorised_values.pl?op=add_form&amp;id=[% loo.id | uri %]" class="btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a>
266
        <td class="actions"><a href="/cgi-bin/koha/admin/authorised_values.pl?op=add_form&amp;id=[% av.id | uri %]" class="btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a>
249
        <a class="delete btn btn-default btn-xs" href="/cgi-bin/koha/admin/authorised_values.pl?op=delete&amp;searchfield=[% searchfield | uri %]&amp;id=[% loo.id | uri %]"><i class="fa fa-trash"></i> Delete</a></td>
267
        <a class="delete btn btn-default btn-xs" href="/cgi-bin/koha/admin/authorised_values.pl?op=delete&amp;searchfield=[% searchfield | uri %]&amp;id=[% av.id | uri %]"><i class="fa fa-trash"></i> Delete</a></td>
250
    </tr>
268
    </tr>
251
    [% END # /FOREACH loop %]
269
    [% END # /FOREACH loop %]
252
    </tbody></table>
270
    </tbody></table>
Lines 301-306 Link Here
301
319
302
[% MACRO jsinclude BLOCK %]
320
[% MACRO jsinclude BLOCK %]
303
    [% Asset.js("js/admin-menu.js") | $raw %]
321
    [% Asset.js("js/admin-menu.js") | $raw %]
322
    [% INCLUDE 'greybox.inc' %]
304
    [% INCLUDE 'datatables.inc' %]
323
    [% INCLUDE 'datatables.inc' %]
305
    [% INCLUDE 'columns_settings.inc' %]
324
    [% INCLUDE 'columns_settings.inc' %]
306
    <script>
325
    <script>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt (-1 / +2 lines)
Lines 249-257 Link Here
249
                e.preventDefault();
249
                e.preventDefault();
250
                var entity = $(this).find('input[name="entity"]').val();
250
                var entity = $(this).find('input[name="entity"]').val();
251
                var code = $(this).find('input[name="code"]').val();
251
                var code = $(this).find('input[name="code"]').val();
252
                var interface = $(this).find('input[name="interface"]').val();
252
                var lang = $(this).find('select[name="lang"] option:selected').val();
253
                var lang = $(this).find('select[name="lang"] option:selected').val();
253
                var translation = $(this).find('input[name="translation"]').val();
254
                var translation = $(this).find('input[name="translation"]').val();
254
                var data = "entity=" + encodeURIComponent(entity) + "&code=" + encodeURIComponent(code) + "&lang=" + encodeURIComponent(lang) + "&translation=" + encodeURIComponent(translation);
255
                var data = "entity=" + encodeURIComponent(entity) + "&code=" + encodeURIComponent(code) + "&interface=" + encodeURIComponent(interface) + "&lang=" + encodeURIComponent(lang) + "&translation=" + encodeURIComponent(translation);
255
                $.ajax({
256
                $.ajax({
256
                    data: data,
257
                    data: data,
257
                    type: 'POST',
258
                    type: 'POST',
(-)a/svc/localization (-16 / +14 lines)
Lines 9-26 use Koha::Localizations; Link Here
9
our ( $query, $response ) = C4::Service->init( parameters => 'manage_itemtypes' );
9
our ( $query, $response ) = C4::Service->init( parameters => 'manage_itemtypes' );
10
10
11
sub get_translations {
11
sub get_translations {
12
    my $rs = Koha::Localizations->search({ entity => $query->param('entity'), code => $query->param('code') });
12
    my $translations = Koha::Localizations->search(
13
    my @translations;
13
        {
14
    while ( my $s = $rs->next ) {
14
            entity    => $query->param('entity'),
15
        push @translations, {
15
            code      => $query->param('code'),
16
              id          => $s->localization_id,
16
            interface => $query->param('interface')
17
              entity      => $s->entity,
18
              code        => $s->code,
19
              lang        => $s->lang,
20
              translation => $s->translation,
21
        }
17
        }
22
    }
18
    );
23
    $response->param( translations => \@translations );
19
    $response->param( translations => $translations );
24
    C4::Service->return_success( $response );
20
    C4::Service->return_success( $response );
25
}
21
}
26
22
Lines 64-76 sub add_translation { Link Here
64
    my $code = $query->param('code');
60
    my $code = $query->param('code');
65
    my $lang = $query->param('lang');
61
    my $lang = $query->param('lang');
66
    my $translation = $query->param('translation');
62
    my $translation = $query->param('translation');
63
    my $interface = $query->param('interface') || undef;
67
64
68
    unless ( Koha::Localizations->search({entity => $entity, code => $code, lang => $lang, })->count ) {
65
    unless ( Koha::Localizations->search({entity => $entity, code => $code, interface => $interface, lang => $lang })->count ) {
69
        my $localization = Koha::Localization->new(
66
        my $localization = Koha::Localization->new(
70
            {
67
            {
71
                entity => $entity,
68
                entity      => $entity,
72
                code => $code,
69
                interface   => $interface,
73
                lang => $lang,
70
                code        => $code,
71
                lang        => $lang,
74
                translation => $translation,
72
                translation => $translation,
75
            }
73
            }
76
        );
74
        );
Lines 78-83 sub add_translation { Link Here
78
        $response->param(
76
        $response->param(
79
            id          => $localization->localization_id,
77
            id          => $localization->localization_id,
80
            entity      => $localization->entity,
78
            entity      => $localization->entity,
79
            interface   => $localization->interface,
81
            code        => $localization->code,
80
            code        => $localization->code,
82
            lang        => $localization->lang,
81
            lang        => $localization->lang,
83
            translation => $localization->translation,
82
            translation => $localization->translation,
84
- 

Return to bug 20307