From 0f63f89f66e40cc01ef02da3654fcfb404c9001d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 30 Apr 2015 17:30:47 +0200 Subject: [PATCH] Bug 14100: Generic solution for language overlay - Item types Test plan: 1/ update the Schema (misc/devel/update_dbix_class_files.pl) 2/ Translate templates for some languages (es-DE, de-DE for instance) 3/ Enable them in the pref (search for 'lang') for the staff interface 4/ Go on the item type admin page (admin/itemtypes.pl) 5/ Edit one 6/ Click on the 'translate for other languages' link 7/ You are now on the interface to translate the item type's description in the languages you want. So translate some :) 8/ Go back on the item type list view (admin/itemtypes.pl) 9/ You should see the original description (non translated) 10/ Switch the language 11/ You should see the translated description in the correct language. If the description is non translated, the original description is displayed. 12/ On the different page where the item type is displayed, confirm that the translated description appears. Think further / Todo: 1/ Update all occurrences of the item type's description (DONE) 2/ Implement for authorised values 3/ Implement for syspref value (at least textarea) 4/ Implement for branch names 5/ Centralize all the translation on a single page in the admin area ... N/ Implement a webservice to centralize all the translations and give the ability to sync the item types/authorised values description with the rest of the world (push and pull). Signed-off-by: Josef Moravec Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- C4/Items.pm | 9 +- C4/Koha.pm | 49 +++- C4/Search.pm | 12 +- Koha/Localization.pm | 28 +++ Koha/Localizations.pm | 34 +++ Koha/Template/Plugin/ItemTypes.pm | 7 +- admin/itemtypes.pl | 50 +++-- admin/localization.pl | 64 ++++++ authorities/authorities.pl | 16 +- catalogue/search.pl | 2 +- cataloguing/addbiblio.pl | 17 +- cataloguing/additem.pl | 11 +- .../Bug_14100-add_table_localization.sql | 8 + installer/data/mysql/kohastructure.sql | 14 ++ .../prog/en/modules/admin/itemtypes.tt | 24 +- .../prog/en/modules/admin/localization.tt | 212 ++++++++++++++++++ svc/checkouts | 7 +- svc/localization | 92 ++++++++ tools/batchMod.pl | 15 +- 19 files changed, 581 insertions(+), 90 deletions(-) create mode 100644 Koha/Localization.pm create mode 100644 Koha/Localizations.pm create mode 100755 admin/localization.pl create mode 100644 installer/data/mysql/atomicupdate/Bug_14100-add_table_localization.sql create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt create mode 100755 svc/localization diff --git a/C4/Items.pm b/C4/Items.pm index b1b9339dbf..213ca4e9b5 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -3027,13 +3027,12 @@ sub PrepareItemrecordDisplay { #----- itemtypes } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { - my $sth = $dbh->prepare( "SELECT itemtype,description FROM itemtypes ORDER BY description" ); - $sth->execute; + my $itemtypes = GetItemTypes( style => 'array' ); push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} ); - while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { - push @authorised_values, $itemtype; - $authorised_lib{$itemtype} = $description; + for my $itemtype ( @$itemtypes ) { + push @authorised_values, $itemtype->{itemtype}; + $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description}; } #---- class_sources } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) { diff --git a/C4/Koha.pm b/C4/Koha.pm index d0042e5299..d06b8b2761 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -254,15 +254,30 @@ sub GetItemTypes { my ( %params ) = @_; my $style = defined( $params{'style'} ) ? $params{'style'} : 'hash'; + my $language = C4::Languages::getlanguage(); # returns a reference to a hash of references to itemtypes... my %itemtypes; my $dbh = C4::Context->dbh; - my $query = qq| - SELECT * + my $query = q| + SELECT + itemtypes.itemtype, + itemtypes.description, + itemtypes.rentalcharge, + itemtypes.notforloan, + itemtypes.imageurl, + itemtypes.summary, + itemtypes.checkinmsg, + itemtypes.checkinmsgtype, + itemtypes.sip_media_type, + COALESCE( localization.translation, itemtypes.description ) AS translated_description FROM itemtypes + LEFT JOIN localization ON itemtypes.itemtype = localization.code + AND localization.entity = 'itemtypes' + AND localization.lang = ? + ORDER BY itemtype |; my $sth = $dbh->prepare($query); - $sth->execute; + $sth->execute( $language ); if ( $style eq 'hash' ) { while ( my $IT = $sth->fetchrow_hashref ) { @@ -540,14 +555,30 @@ Defaults to intranet. sub getitemtypeinfo { my ($itemtype, $interface) = @_; - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("select * from itemtypes where itemtype=?"); - $sth->execute($itemtype); - my $res = $sth->fetchrow_hashref; + my $dbh = C4::Context->dbh; + my $language = C4::Languages::getlanguage(); + my $it = $dbh->selectrow_hashref(q| + SELECT + itemtypes.itemtype, + itemtypes.description, + itemtypes.rentalcharge, + itemtypes.notforloan, + itemtypes.imageurl, + itemtypes.summary, + itemtypes.checkinmsg, + itemtypes.checkinmsgtype, + itemtypes.sip_media_type, + COALESCE( localization.translation, itemtypes.description ) AS translated_description + FROM itemtypes + LEFT JOIN localization ON itemtypes.itemtype = localization.code + AND localization.entity = 'itemtypes' + AND localization.lang = ? + WHERE itemtypes.itemtype = ? + |, undef, $language, $itemtype ); - $res->{imageurl} = getitemtypeimagelocation( ( ( defined $interface && $interface eq 'opac' ) ? 'opac' : 'intranet' ), $res->{imageurl} ); + $it->{imageurl} = getitemtypeimagelocation( ( ( defined $interface && $interface eq 'opac' ) ? 'opac' : 'intranet' ), $it->{imageurl} ); - return $res; + return $it; } =head2 getitemtypeimagedir diff --git a/C4/Search.pm b/C4/Search.pm index c9d2529dd3..31e24849e1 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -1873,15 +1873,9 @@ sub searchResults { #Build itemtype hash #find itemtype & itemtype image my %itemtypes; - $bsth = - $dbh->prepare( - "SELECT itemtype,description,imageurl,summary,notforloan FROM itemtypes" - ); - $bsth->execute(); - while ( my $bdata = $bsth->fetchrow_hashref ) { - foreach (qw(description imageurl summary notforloan)) { - $itemtypes{ $bdata->{'itemtype'} }->{$_} = $bdata->{$_}; - } + my $itemtypes = GetItemTypes( style => 'array' ); + for my $itemtype ( @$itemtypes ) { + $itemtypes{ $itemtype->{itemtype} } = $itemtype; } #search item field code diff --git a/Koha/Localization.pm b/Koha/Localization.pm new file mode 100644 index 0000000000..013db3a427 --- /dev/null +++ b/Koha/Localization.pm @@ -0,0 +1,28 @@ +package Koha::Localization; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Koha::Database; + +use base qw(Koha::Object); + +sub type { + return 'Localization'; +} + +1; diff --git a/Koha/Localizations.pm b/Koha/Localizations.pm new file mode 100644 index 0000000000..bcbd8c225c --- /dev/null +++ b/Koha/Localizations.pm @@ -0,0 +1,34 @@ +package Koha::Localizations; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Koha::Database; + +use Koha::Localization; + +use base qw(Koha::Objects); + +sub type { + return 'Localization'; +} + +sub object_class { + return 'Koha::Localization'; +} + +1; diff --git a/Koha/Template/Plugin/ItemTypes.pm b/Koha/Template/Plugin/ItemTypes.pm index 91e8c4e169..9c8326db6e 100644 --- a/Koha/Template/Plugin/ItemTypes.pm +++ b/Koha/Template/Plugin/ItemTypes.pm @@ -27,11 +27,8 @@ use C4::Koha; sub GetDescription { my ( $self, $itemtype ) = @_; - my $query = "SELECT description FROM itemtypes WHERE itemtype = ?"; - my $sth = C4::Context->dbh->prepare($query); - $sth->execute($itemtype); - my $d = $sth->fetchrow_hashref(); - return $d->{description} + my $itemtype = C4::Koha::getitemtypeinfo( $itemtype ); + return $itemtype->{translated_description}; } diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl index 61e59fe6cd..2af82a3ec5 100755 --- a/admin/itemtypes.pl +++ b/admin/itemtypes.pl @@ -52,18 +52,7 @@ use C4::Context; use C4::Auth; use C4::Output; -sub StringSearch { - my ( $searchstring, $type ) = @_; - my $dbh = C4::Context->dbh; - $searchstring =~ s/\'/\\\'/g; - my @data = split( ' ', $searchstring ); - my $sth = $dbh->prepare( - "SELECT * FROM itemtypes WHERE (description LIKE ?) ORDER BY itemtype" - ); - $sth->execute("$data[0]%"); - return $sth->fetchall_arrayref({}); # return ref-to-array of ref-to-hashes - # like [ fetchrow_hashref(), fetchrow_hashref() ... ] -} +use Koha::Localizations; my $input = new CGI; my $searchfield = $input->param('description'); @@ -99,8 +88,26 @@ if ( $op eq 'add_form' ) { #---- if primkey exists, it's a modify action, so read values to modify... my $data; if ($itemtype) { - my $sth = $dbh->prepare("select * from itemtypes where itemtype=?"); - $sth->execute($itemtype); + my $sth = $dbh->prepare(q| + SELECT + itemtypes.itemtype, + itemtypes.description, + itemtypes.rentalcharge, + itemtypes.notforloan, + itemtypes.imageurl, + itemtypes.summary, + itemtypes.checkinmsg, + itemtypes.checkinmsgtype, + itemtypes.sip_media_type, + COALESCE( localization.translation, itemtypes.description ) AS translated_description + FROM itemtypes + LEFT JOIN localization ON itemtypes.itemtype = localization.code + AND localization.entity='itemtypes' + AND localization.lang = ? + WHERE itemtype = ? + |); + my $language = C4::Languages::getlanguage(); + $sth->execute($language, $itemtype); $data = $sth->fetchrow_hashref; } @@ -259,11 +266,24 @@ elsif ( $op eq 'delete_confirmed' ) { } if ( $op eq 'list' ) { - my ($results) = StringSearch( $searchfield, 'web' ); + my $results = C4::Koha::GetItemTypes( style => 'array' ); my @loop; foreach my $itemtype ( @{$results} ) { $itemtype->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtype->{imageurl} ); $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} ); + + my @translated_descriptions = Koha::Localizations->search( + { entity => 'itemtypes', + code => $itemtype->{itemtype}, + } + ); + $itemtype->{translated_descriptions} = [ map { + { + lang => $_->lang, + translation => $_->translation, + } + } @translated_descriptions ]; + push( @loop, $itemtype ); } diff --git a/admin/localization.pl b/admin/localization.pl new file mode 100755 index 0000000000..061b9792c8 --- /dev/null +++ b/admin/localization.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + + +use Modern::Perl; + +use C4::Auth; +use C4::Output; + +use Koha::Localization; +use Koha::Localizations; + +use CGI qw( -utf8 ); + +my $query = new CGI; + +my ( $template, $borrowernumber, $cookie ) = get_template_and_user( + { template_name => "admin/localization.tt", + authnotrequired => 0, + flagsrequired => { parameters => 'parameters_remaining_permissions' }, + query => $query, + type => "intranet", + debug => 1, + } +); + +my $entity = $query->param('entity'); +my $code = $query->param('code'); +my $rs = Koha::Localizations->search( { entity => $entity, code => $code } ); +my @translations; +while ( my $s = $rs->next ) { + push @translations, + { id => $s->localization_id, + entity => $s->entity, + code => $s->code, + lang => $s->lang, + translation => $s->translation, + }; +} + +my $translated_languages = C4::Languages::getTranslatedLanguages( 'intranet', C4::Context->preference('template') ); + +$template->param( + translations => \@translations, + languages => $translated_languages, + entity => $entity, + code => $code, +); + +output_html_with_http_headers $query, $cookie, $template->output; diff --git a/authorities/authorities.pl b/authorities/authorities.pl index 16910c8e2d..1753de6dc4 100755 --- a/authorities/authorities.pl +++ b/authorities/authorities.pl @@ -83,23 +83,17 @@ sub build_authorized_values_list { push @authorised_values, $branchcode; $authorised_lib{$branchcode} = $branchname; } - - #----- itemtypes } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { - my $sth = - $dbh->prepare( - "select itemtype,description from itemtypes order by description"); - $sth->execute; push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) ); - + my $itemtype; - - while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { - push @authorised_values, $itemtype; - $authorised_lib{$itemtype} = $description; + my $itemtypes = GetItemTypes( style => 'array' ); + for my $itemtype ( @$itemtypes ) { + push @authorised_values, $itemtype->{itemtype}; + $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description}; } $value = $itemtype unless ($value); diff --git a/catalogue/search.pl b/catalogue/search.pl index 43d7f29b76..0bd2cc6c97 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -266,7 +266,7 @@ foreach my $advanced_srch_type (@advanced_search_types) { my %row =( number=>$cnt++, ccl => "$itype_or_itemtype,phr", code => $thisitemtype, - description => $itemtypes->{$thisitemtype}->{'description'}, + description => $itemtypes->{$thisitemtype}->{translated_description}, imageurl=> getitemtypeimagelocation( 'intranet', $itemtypes->{$thisitemtype}->{'imageurl'} ), ); push @itypesloop, \%row; diff --git a/cataloguing/addbiblio.pl b/cataloguing/addbiblio.pl index 8cf3b6ab4e..b4784edc2f 100755 --- a/cataloguing/addbiblio.pl +++ b/cataloguing/addbiblio.pl @@ -184,26 +184,19 @@ sub build_authorized_values_list { $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'}; } - #----- itemtypes } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { - my $sth = - $dbh->prepare( - "select itemtype,description from itemtypes order by description"); - $sth->execute; push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} && ( $value || $tagslib->{$tag}->{$subfield}->{defaultvalue} ) ); - + my $itemtype; - - while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { - push @authorised_values, $itemtype; - $authorised_lib{$itemtype} = $description; + my $itemtypes = GetItemTypes( style => 'array' ); + for my $itemtype ( @$itemtypes ) { + push @authorised_values, $itemtype->{itemtype}; + $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description}; } $value = $itemtype unless ($value); - - #---- class_sources } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) { push @authorised_values, "" diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl index 19279b35c2..b74605a7ec 100755 --- a/cataloguing/additem.pl +++ b/cataloguing/additem.pl @@ -177,13 +177,12 @@ sub generate_subfield_form { } elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) { push @authorised_values, "" unless ( $subfieldlib->{mandatory} ); - my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description"); - $sth->execute; - while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { - push @authorised_values, $itemtype; - $authorised_lib{$itemtype} = $description; + my $itemtypes = GetItemTypes( style => 'array' ); + for my $itemtype ( @$itemtypes ) { + push @authorised_values, $itemtype->{itemtype}; + $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description}; } - + unless ( $value ) { my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?"); $itype_sth->execute( $biblionumber ); diff --git a/installer/data/mysql/atomicupdate/Bug_14100-add_table_localization.sql b/installer/data/mysql/atomicupdate/Bug_14100-add_table_localization.sql new file mode 100644 index 0000000000..83bdb9df73 --- /dev/null +++ b/installer/data/mysql/atomicupdate/Bug_14100-add_table_localization.sql @@ -0,0 +1,8 @@ +CREATE TABLE `localization` ( + localization_id int(11) NOT NULL AUTO_INCREMENT, + entity varchar(16) COLLATE utf8_unicode_ci NOT NULL, + code varchar(64) COLLATE utf8_unicode_ci NOT NULL, + lang varchar(25) COLLATE utf8_unicode_ci NOT NULL, + translation text COLLATE utf8_unicode_ci, + PRIMARY KEY (localization_id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 82a079eada..8ad2205e14 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -3563,6 +3563,20 @@ CREATE TABLE `additional_field_values` ( CONSTRAINT `afv_fk` FOREIGN KEY (`field_id`) REFERENCES `additional_fields` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +-- +-- Table structure for table 'localization' +-- + +DROP TABLE IF EXISTS localization; +CREATE TABLE `localization` ( + localization_id int(11) NOT NULL AUTO_INCREMENT, + entity varchar(16) COLLATE utf8_unicode_ci NOT NULL, + code varchar(64) COLLATE utf8_unicode_ci NOT NULL, + lang varchar(25) COLLATE utf8_unicode_ci NOT NULL, --could be a foreign key + translation text COLLATE utf8_unicode_ci, + PRIMARY KEY (localization_id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt index 721da70db2..11d5cfff81 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt @@ -20,6 +20,7 @@ Data deleted [% INCLUDE 'doc-head-close.inc' %] +[% INCLUDE 'greybox.inc' %] [% INCLUDE 'datatables.inc' %] + + +
+ + + + + + + + + + + + + [% FOR t IN translations %] + + + + + + + + [% END %] + +
IdEntityCodeLangTranslation
[% t.id %][% t.entity %][% t.code %][% t.lang %][% t.translation %] +
+
+ + + Lang: + Translation: + +
+
+
+ + diff --git a/svc/checkouts b/svc/checkouts index cc703aeaeb..6ecfd09ed4 100755 --- a/svc/checkouts +++ b/svc/checkouts @@ -83,9 +83,7 @@ my $sql = ' branchname, items.itype, - itemtype_item.description AS itype_description, biblioitems.itemtype, - itemtype_bib.description AS itemtype_description, borrowernumber, surname, @@ -103,8 +101,6 @@ my $sql = ' LEFT JOIN biblioitems USING ( biblionumber ) LEFT JOIN borrowers USING ( borrowernumber ) LEFT JOIN branches ON ( issues.branchcode = branches.branchcode ) - LEFT JOIN itemtypes itemtype_bib ON ( biblioitems.itemtype = itemtype_bib.itemtype ) - LEFT JOIN itemtypes itemtype_item ON ( items.itype = itemtype_item.itemtype ) WHERE borrowernumber '; @@ -145,13 +141,14 @@ while ( my $c = $sth->fetchrow_hashref() ) { my ( $renewals_count, $renewals_allowed, $renewals_remaining ) = GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} ); + my $itemtype = C4::Koha::getitemtypeinfo( $item_level_itypes ? $c->{itype} : $c->{itemtype} ); my $checkout = { DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber}, title => $c->{title}, author => $c->{author}, barcode => $c->{barcode}, itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype}, - itemtype_description => $item_level_itypes ? $c->{itype_description} : $c->{itemtype_description}, + itemtype_description => $itemtype->{translated_description}, location => $c->{location} ? GetAuthorisedValueByCode( 'LOC', $c->{location} ) : q{}, itemnotes => $c->{itemnotes}, branchcode => $c->{branchcode}, diff --git a/svc/localization b/svc/localization new file mode 100755 index 0000000000..ebec0e29d1 --- /dev/null +++ b/svc/localization @@ -0,0 +1,92 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use C4::Service; +use Koha::Localizations; + +our ( $query, $response ) = C4::Service->init( parameters => 'parameters_remaining_permissions' ); + +sub get_translations { + my $rs = Koha::Localizations->search({ type => $query->param('type'), code => $query->param('code') }); + my @translations; + while ( my $s = $rs->next ) { + push @translations, { + id => $s->localization_id, + type => $s->type, + code => $s->code, + lang => $s->lang, + translation => $s->translation, + } + } + $response->param( translations => \@translations ); + C4::Service->return_success( $response ); +} + +sub update_translation { + my $id = $query->param('id'); + my $translation = $query->param('translation'); + my $lang = $query->param('lang'); + my $localization = Koha::Localizations->find( $id ); + + if ( defined $lang and $localization->lang ne $lang ) { + $localization->lang( $lang ) + } + if ( defined $translation and $localization->translation ne $translation ) { + $localization->translation( $translation ) + } + my $is_changed; + if ( $localization->is_changed ) { + $is_changed = 1; + $localization->store; + } + $response->param( + id => $localization->localization_id, + entity => $localization->entity, + code => $localization->code, + lang => $localization->lang, + translation => $localization->translation, + is_changed => $is_changed, + ); + C4::Service->return_success( $response ); +} + +sub add_translation { + my $entity = $query->param('entity'); + my $code = $query->param('code'); + my $lang = $query->param('lang'); + my $translation = $query->param('translation'); + my $localization = Koha::Localization->new( + { + entity => $entity, + code => $code, + lang => $lang, + translation => $translation, + } + ); + $localization->store; + $localization->localization_id; + $response->param( + id => $localization->localization_id, + entity => $localization->entity, + code => $localization->code, + lang => $localization->lang, + translation => $localization->translation, + ); + + C4::Service->return_success( $response ); +} + +sub delete_translation { + my $id = $query->param('id'); + Koha::Localizations->find($id)->delete; + $response->param( id => $id ); + C4::Service->return_success( $response ); +} + +C4::Service->dispatch( + [ 'GET /', [ 'id' ], \&get_translations ], + [ 'PUT /', [ 'id' ], \&update_translation ], + [ 'POST /', [ 'entity', 'code', 'lang', 'translation' ], \&add_translation ], + [ 'DELETE /', ['id'], \&delete_translation ], +); diff --git a/tools/batchMod.pl b/tools/batchMod.pl index 5804936536..56747177a9 100755 --- a/tools/batchMod.pl +++ b/tools/batchMod.pl @@ -355,14 +355,13 @@ foreach my $tag (sort keys %{$tagslib}) { } $value = ""; } - elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { - push @authorised_values, ""; - my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description"); - $sth->execute; - while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { - push @authorised_values, $itemtype; - $authorised_lib{$itemtype} = $description; - } + elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { + push @authorised_values, ""; + my $itemtypes = GetItemTypes( style => 'array' ); + for my $itemtype ( @$itemtypes ) { + push @authorised_values, $itemtype->{itemtype}; + $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description}; + } $value = ""; #---- class_sources -- 2.39.5