diff options
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r-- | perllib/FixMyStreet/App.pm | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 187 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Around.pm | 10 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/My.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 13 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Reports.pm | 15 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Model/DB.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Zurich.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB.pm | 17 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Body.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Contact.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 14 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Translation.pm | 44 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Schema.pm | 28 | ||||
-rw-r--r-- | perllib/FixMyStreet/Roles/Translatable.pm | 97 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/Alerts.pm | 4 | ||||
-rwxr-xr-x | perllib/FixMyStreet/Script/UpdateAllReports.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Test.pm | 2 |
19 files changed, 387 insertions, 76 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index c1628d010..2365118ea 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -168,6 +168,9 @@ template paths, maps, languages etc, etc. sub setup_request { my $c = shift; + # Set the Catalyst model schema to the same as the DB schema + $c->model("DB")->schema( FixMyStreet::DB->schema ); + $c->setup_dev_overrides(); my $cobrand = $c->cobrand; diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index d1ac5e76b..0fa58eb9d 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -244,6 +244,9 @@ sub bodies : Path('bodies') : Args(0) { $c->stash->{edit_activity} = $edit_activity; + $c->forward( 'fetch_languages' ); + $c->forward( 'fetch_translations' ); + my $posted = $c->get_param('posted') || ''; if ( $posted eq 'body' ) { $c->forward('check_for_super_user'); @@ -257,6 +260,9 @@ sub bodies : Path('bodies') : Args(0) { $c->model('DB::BodyArea')->create( { body => $body, area_id => $_ } ); } + $c->stash->{object} = $body; + $c->stash->{translation_col} = 'name'; + $c->forward('update_translations'); $c->stash->{updated} = _('New body added'); } } @@ -300,30 +306,6 @@ sub body_form_dropdowns : Private { $c->stash->{send_methods} = \@methods; } -sub body : Path('body') : Args(1) { - my ( $self, $c, $body_id ) = @_; - - $c->stash->{body_id} = $body_id; - - unless ($c->user->has_permission_to('category_edit', $body_id)) { - $c->forward('check_for_super_user'); - } - - $c->forward( '/auth/get_csrf_token' ); - $c->forward( 'lookup_body' ); - $c->forward( 'fetch_all_bodies' ); - $c->forward( 'body_form_dropdowns' ); - - if ( $c->get_param('posted') ) { - $c->log->debug( 'posted' ); - $c->forward('update_contacts'); - } - - $c->forward('fetch_contacts'); - - return 1; -} - sub check_for_super_user : Private { my ( $self, $c ) = @_; @@ -407,6 +389,12 @@ sub update_contacts : Private { $contact->insert; } + unless ( %errors ) { + $c->stash->{translation_col} = 'category'; + $c->stash->{object} = $contact; + $c->forward('update_translations'); + } + } elsif ( $posted eq 'update' ) { $c->forward('/auth/check_csrf_token'); @@ -446,11 +434,45 @@ sub update_contacts : Private { # Remove any others $c->stash->{body}->body_areas->search( { area_id => [ keys %current ] } )->delete; + $c->stash->{translation_col} = 'name'; + $c->stash->{object} = $c->stash->{body}; + $c->forward('update_translations'); + $c->stash->{updated} = _('Values updated'); } } } +sub update_translations : Private { + my ( $self, $c ) = @_; + + foreach my $lang (keys(%{$c->stash->{languages}})) { + my $id = $c->get_param('translation_id_' . $lang); + my $text = $c->get_param('translation_' . $lang); + if ($id) { + my $translation = $c->model('DB::Translation')->find( + { + id => $id, + } + ); + + if ($text) { + $translation->msgstr($text); + $translation->update; + } else { + $translation->delete; + } + } elsif ($text) { + my $col = $c->stash->{translation_col}; + $c->stash->{object}->add_translation_for( + $col, $lang, $text + ); + } + } + + $c->stash->{updated} = _('Translations updated'); +} + sub body_params : Private { my ( $self, $c ) = @_; @@ -497,6 +519,44 @@ sub fetch_contacts : Private { return 1; } +sub fetch_languages : Private { + my ( $self, $c ) = @_; + + my $lang_map = {}; + foreach my $lang (sort @{$c->cobrand->languages}) { + my ($id, $name, $code) = split(',', $lang); + $lang_map->{$id} = { name => $name, code => $code }; + } + + $c->stash->{languages} = $lang_map; + + return 1; +} + +sub fetch_translations : Private { + my ( $self, $c ) = @_; + + my $translations = {}; + if ($c->get_param('posted')) { + foreach my $lang (keys %{$c->stash->{languages}}) { + if (my $msgstr = $c->get_param('translation_' . $lang)) { + $translations->{$lang} = { msgstr => $msgstr }; + } + if (my $id = $c->get_param('translation_id_' . $lang)) { + $translations->{$lang}->{id} = $id; + } + } + } elsif ($c->stash->{object}) { + my @translations = $c->stash->{object}->translation_for($c->stash->{translation_col})->all; + + foreach my $tx (@translations) { + $translations->{$tx->lang} = { id => $tx->id, msgstr => $tx->msgstr }; + } + } + + $c->stash->{translations} = $translations; +} + sub lookup_body : Private { my ( $self, $c ) = @_; @@ -516,35 +576,94 @@ sub lookup_body : Private { return 1; } +sub body_base : Chained('/') : PathPart('admin/body') : CaptureArgs(0) { } + # This is for if the category name contains a '/' -sub category_edit_all : Path('body') { +sub category_edit_all : Chained('body_base') : PathPart('') { my ( $self, $c, $body_id, @category ) = @_; my $category = join( '/', @category ); - $c->go( 'category_edit', [ $body_id, $category ] ); -} -sub category_edit : Path('body') : Args(2) { - my ( $self, $c, $body_id, $category ) = @_; + $c->stash->{body_id} = $body_id; + $c->forward( 'lookup_body' ); + my $contact = $c->stash->{body}->contacts->search( { category => $category } )->first; + $c->stash->{contact} = $contact; + + $c->stash->{template} = 'admin/category_edit.html'; + $c->forward( 'category_edit' ); +} + +sub body : Chained('body_base') : PathPart('') : CaptureArgs(1) { + my ( $self, $c, $body_id ) = @_; $c->stash->{body_id} = $body_id; +} + +sub edit_body : Chained('body') : PathPart('') : Args(0) { + my ( $self, $c ) = @_; + + unless ($c->user->has_permission_to('category_edit', $c->stash->{body_id})) { + $c->forward('check_for_super_user'); + } + + $c->forward( '/auth/get_csrf_token' ); + $c->forward( 'lookup_body' ); + $c->forward( 'fetch_all_bodies' ); + $c->forward( 'body_form_dropdowns' ); + $c->forward('fetch_languages'); + + if ( $c->get_param('posted') ) { + $c->forward('update_contacts'); + } + + $c->stash->{object} = $c->stash->{body}; + $c->stash->{translation_col} = 'name'; + + # if there's a contact then it's because we're displaying error + # messages about adding a contact so grabbing translations will + # fetch the contact submitted translations. So grab them, stash + # them and then clear posted so we can fetch the body translations + if ($c->stash->{contact}) { + $c->forward('fetch_translations'); + $c->stash->{contact_translations} = $c->stash->{translations}; + } + $c->set_param('posted', ''); + + $c->forward('fetch_translations'); + $c->forward('fetch_contacts'); + + $c->stash->{template} = 'admin/body.html'; + return 1; +} + +sub category : Chained('body') : PathPart('') : CaptureArgs(1) { + my ( $self, $c, $category ) = @_; $c->forward( '/auth/get_csrf_token' ); $c->forward( 'lookup_body' ); my $contact = $c->stash->{body}->contacts->search( { category => $category } )->first; $c->stash->{contact} = $contact; +} + +sub category_edit : Chained('category') : PathPart('') : Args(0) { + my ( $self, $c ) = @_; + + $c->stash->{translation_col} = 'category'; + $c->stash->{object} = $c->stash->{contact}; + + $c->forward('fetch_languages'); + $c->forward('fetch_translations'); my $history = $c->model('DB::ContactsHistory')->search( { - body_id => $body_id, - category => $category + body_id => $c->stash->{body_id}, + category => $c->stash->{contact}->category }, { order_by => ['contacts_history_id'] }, ); $c->stash->{history} = $history; - my @methods = map { $_ =~ s/FixMyStreet::SendReport:://; $_ } keys %{ FixMyStreet::SendReport->get_senders }; $c->stash->{send_methods} = \@methods; @@ -925,8 +1044,8 @@ sub categories_for_point : Private { # Remove the "Pick a category" option shift @{$c->stash->{category_options}} if @{$c->stash->{category_options}}; - $c->stash->{categories} = $c->stash->{category_options}; - $c->stash->{categories_hash} = { map { $_ => 1 } @{$c->stash->{category_options}} }; + $c->stash->{category_options_copy} = $c->stash->{category_options}; + $c->stash->{categories_hash} = { map { $_->{name} => 1 } @{$c->stash->{category_options}} }; } sub templates : Path('templates') : Args(0) { diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm index bd9e80dc7..a8782eba2 100644 --- a/perllib/FixMyStreet/App/Controller/Around.pm +++ b/perllib/FixMyStreet/App/Controller/Around.pm @@ -196,8 +196,8 @@ sub display_location : Private { my @pins; unless ($c->get_param('no_pins')) { @pins = map { - # Here we might have a DB::Problem or a DB::Nearby, we always want the problem. - my $p = (ref $_ eq 'FixMyStreet::App::Model::DB::Nearby') ? $_->problem : $_; + # Here we might have a DB::Problem or a DB::Result::Nearby, we always want the problem. + my $p = (ref $_ eq 'FixMyStreet::DB::Result::Nearby') ? $_->problem : $_; $p->pin_data($c, 'around'); } @$on_map_all, @$nearby; } @@ -259,7 +259,7 @@ sub check_and_stash_category : Private { distinct => 1 } )->all; - my @categories = map { $_->category } @contacts; + my @categories = map { { name => $_->category, value => $_->category_display } } @contacts; $c->stash->{filter_categories} = \@categories; my %categories_mapped = map { $_ => 1 } @categories; @@ -311,8 +311,8 @@ sub ajax : Path('/ajax') { # create a list of all the pins my @pins = map { - # Here we might have a DB::Problem or a DB::Nearby, we always want the problem. - my $p = (ref $_ eq 'FixMyStreet::App::Model::DB::Nearby') ? $_->problem : $_; + # Here we might have a DB::Problem or a DB::Result::Nearby, we always want the problem. + my $p = (ref $_ eq 'FixMyStreet::DB::Result::Nearby') ? $_->problem : $_; my $colour = $c->cobrand->pin_colour( $p, 'around' ); my $title = $c->cobrand->call_hook(pin_hover_title => $p, $p->title_safe) || $p->title_safe; [ $p->latitude, $p->longitude, diff --git a/perllib/FixMyStreet/App/Controller/My.pm b/perllib/FixMyStreet/App/Controller/My.pm index 6fee25ec5..ea7aee016 100644 --- a/perllib/FixMyStreet/App/Controller/My.pm +++ b/perllib/FixMyStreet/App/Controller/My.pm @@ -162,7 +162,7 @@ sub setup_page_data : Private { distinct => 1, order_by => [ 'category' ], } )->all; - @categories = map { $_->category } @categories; + @categories = map { { name => $_->category, value => $_->category_display } } @categories; $c->stash->{filter_categories} = \@categories; $c->stash->{page} = 'my'; diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index 3acb385bd..ab4c616fb 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -201,7 +201,7 @@ sub report_form_ajax : Path('ajax') : Args(0) { if ($c->user_exists) { my @bodies = keys %{$c->stash->{bodies}}; my $ca_another_user = $c->user->has_permission_to('contribute_as_another_user', \@bodies); - my $ca_body = $c->user->has_permission_to('contribute_as_body', \@bodies); + my $ca_body = $c->user->from_body && $c->user->has_permission_to('contribute_as_body', \@bodies); $contribute_as->{another_user} = $ca_another_user if $ca_another_user; $contribute_as->{body} = $ca_body if $ca_body; } @@ -213,7 +213,6 @@ sub report_form_ajax : Path('ajax') : Args(0) { category => $category, extra_name_info => $extra_name_info, titles_list => $extra_titles_list, - categories => $c->stash->{category_options}, %$contribute_as ? (contribute_as => $contribute_as) : (), $top_message ? (top_message => $top_message) : (), } @@ -645,7 +644,7 @@ sub setup_categories_and_bodies : Private { $bodies_to_list{ $contact->body_id } = $contact->body; unless ( $seen{$contact->category} ) { - push @category_options, $contact->category; + push @category_options, { name => $contact->category, value => $contact->category_display }; my $metas = $contact->get_metadata_for_input; $category_extras{$contact->category} = $metas if @$metas; @@ -657,13 +656,15 @@ sub setup_categories_and_bodies : Private { $non_public_categories{ $contact->category } = 1 if $contact->non_public; } - $seen{$contact->category} = 1; + $seen{$contact->category} = $contact->category_display; } if (@category_options) { # If there's an Other category present, put it at the bottom - @category_options = ( _('-- Pick a category --'), grep { $_ ne _('Other') } @category_options ); - push @category_options, _('Other') if $seen{_('Other')}; + @category_options = ( + { name => _('-- Pick a category --'), value => _('-- Pick a category --') }, + grep { $_->{name} ne _('Other') } @category_options ); + push @category_options, { name => _('Other'), value => $seen{_('Other')} } if $seen{_('Other')}; } $c->cobrand->call_hook(munge_category_list => \@category_options, \@contacts, \%category_extras); diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm index 8f068f0ec..33a1ac5b9 100644 --- a/perllib/FixMyStreet/App/Controller/Reports.pm +++ b/perllib/FixMyStreet/App/Controller/Reports.pm @@ -150,7 +150,7 @@ sub ward : Path : Args(2) { distinct => 1, order_by => [ 'category' ], } )->all; - @categories = map { $_->category } @categories; + @categories = map { { name => $_->category, value => $_->category_display } } @categories; $c->stash->{filter_categories} = \@categories; $c->stash->{filter_category} = { map { $_ => 1 } $c->get_param_list('filter_category', 1) }; @@ -318,6 +318,19 @@ sub body_check : Private { } } + my @translations = $c->model('DB::Translation')->search( { + tbl => 'body', + col => 'name', + msgstr => $q_body + } )->all; + + if (@translations == 1) { + if ( my $body = $c->model('DB::Body')->find( { id => $translations[0]->object_id } ) ) { + $c->stash->{body} = $body; + return; + } + } + # No result, bad body name. $c->detach( 'redirect_index' ); } diff --git a/perllib/FixMyStreet/App/Model/DB.pm b/perllib/FixMyStreet/App/Model/DB.pm index ac1f98dc9..9d09186b8 100644 --- a/perllib/FixMyStreet/App/Model/DB.pm +++ b/perllib/FixMyStreet/App/Model/DB.pm @@ -7,8 +7,8 @@ use warnings; use FixMyStreet; __PACKAGE__->config( - schema_class => 'FixMyStreet::DB', - connect_info => sub { FixMyStreet::DB->storage->dbh }, + schema_class => 'FixMyStreet::DB::Schema', + connect_info => sub { FixMyStreet::DB->schema->storage->dbh }, ); =head1 NAME diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 4a886204c..1a0bbb0c8 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -269,6 +269,8 @@ sub set_lang_and_domain { DateTime->DefaultLocale( 'en_US' ); } + FixMyStreet::DB->schema->lang($set_lang); + return $set_lang; } sub languages { FixMyStreet->config('LANGUAGES') || [] } diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm index 776350b25..c56f595ca 100644 --- a/perllib/FixMyStreet/Cobrand/Zurich.pm +++ b/perllib/FixMyStreet/Cobrand/Zurich.pm @@ -519,7 +519,7 @@ sub admin_report_edit { # Can change category to any other my @categories = $c->model('DB::Contact')->not_deleted->all; - $c->stash->{categories} = [ map { $_->category } @categories ]; + $c->stash->{category_options} = [ map { { name => $_->category, value => $_->category } } @categories ]; } elsif ($type eq 'dm') { @@ -534,7 +534,7 @@ sub admin_report_edit { # Can change category to any other my @categories = $c->model('DB::Contact')->not_deleted->all; - $c->stash->{categories} = [ map { $_->category } @categories ]; + $c->stash->{category_options} = [ map { { name => $_->category, value => $_->category } } @categories ]; } diff --git a/perllib/FixMyStreet/DB.pm b/perllib/FixMyStreet/DB.pm index d920c809f..cee66b434 100644 --- a/perllib/FixMyStreet/DB.pm +++ b/perllib/FixMyStreet/DB.pm @@ -1,22 +1,13 @@ -use utf8; package FixMyStreet::DB; -# Created by DBIx::Class::Schema::Loader -# DO NOT MODIFY THE FIRST PART OF THIS FILE - use strict; use warnings; +use FixMyStreet::DB::Schema; -use base 'DBIx::Class::Schema'; - -__PACKAGE__->load_namespaces; - - -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-08 17:19:55 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:CjFpUvon7KggFM7OF7VK/w +my $schema; -use FixMyStreet; +sub schema { $schema ||= FixMyStreet::DB::Schema->clone } -__PACKAGE__->connection(FixMyStreet->dbic_connect_info); +sub resultset { shift->schema->resultset(@_) } 1; diff --git a/perllib/FixMyStreet/DB/Result/Body.pm b/perllib/FixMyStreet/DB/Result/Body.pm index 9a64d1608..db7777053 100644 --- a/perllib/FixMyStreet/DB/Result/Body.pm +++ b/perllib/FixMyStreet/DB/Result/Body.pm @@ -121,12 +121,19 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-02-13 15:11:11 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:BOJANVwg3kR/1VjDq0LykA +use Moo; +use namespace::clean; + +with 'FixMyStreet::Roles::Translatable'; + sub url { my ( $self, $c, $args ) = @_; # XXX $areas_info was used here for Norway parent - needs body parents, I guess return $c->uri_for( '/reports/' . $c->cobrand->short_name( $self ), $args || {} ); } +around name => \&translate_around; + sub areas { my $self = shift; my %ids = map { $_->area_id => 1 } $self->body_areas->all; diff --git a/perllib/FixMyStreet/DB/Result/Contact.pm b/perllib/FixMyStreet/DB/Result/Contact.pm index 3454c5806..fb731b9a3 100644 --- a/perllib/FixMyStreet/DB/Result/Contact.pm +++ b/perllib/FixMyStreet/DB/Result/Contact.pm @@ -82,12 +82,18 @@ __PACKAGE__->rabx_column('extra'); use Moo; use namespace::clean -except => [ 'meta' ]; -with 'FixMyStreet::Roles::Extra'; +with 'FixMyStreet::Roles::Extra', + 'FixMyStreet::Roles::Translatable'; __PACKAGE__->many_to_many( response_templates => 'contact_response_templates', 'response_template' ); __PACKAGE__->many_to_many( response_priorities => 'contact_response_priorities', 'response_priority' ); __PACKAGE__->many_to_many( defect_types => 'contact_defect_types', 'defect_type' ); +sub category_display { + my $self = shift; + $self->translate('category'); +} + sub get_metadata_for_input { my $self = shift; my $id_field = $self->id_field; diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index f353c02ee..afa117e4c 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -206,6 +206,7 @@ my $IM = eval { with 'FixMyStreet::Roles::Abuser', 'FixMyStreet::Roles::Extra', + 'FixMyStreet::Roles::Translatable', 'FixMyStreet::Roles::PhotoSet'; =head2 @@ -456,12 +457,6 @@ sub check_for_errors { $errors{category} = _('Please choose a category'); $self->category(undef); } - elsif ($self->category - && $self->category eq _('-- Pick a property type --') ) - { - $errors{category} = _('Please choose a property type'); - $self->category(undef); - } return \%errors; } @@ -489,6 +484,11 @@ sub confirm { return 1; } +sub category_display { + my $self = shift; + $self->translate('category'); +} + sub bodies_str_ids { my $self = shift; return [] unless $self->bodies_str; @@ -635,7 +635,7 @@ sub meta_line { my $date_time = Utils::prettify_dt( $problem->confirmed ); my $meta = ''; - my $category = $problem->category; + my $category = $problem->category_display; $category = $c->cobrand->call_hook(change_category_text => $category) || $category; if ( $problem->anonymous ) { diff --git a/perllib/FixMyStreet/DB/Result/Translation.pm b/perllib/FixMyStreet/DB/Result/Translation.pm new file mode 100644 index 000000000..fafc7ccf1 --- /dev/null +++ b/perllib/FixMyStreet/DB/Result/Translation.pm @@ -0,0 +1,44 @@ +use utf8; +package FixMyStreet::DB::Result::Translation; + +# Created by DBIx::Class::Schema::Loader +# DO NOT MODIFY THE FIRST PART OF THIS FILE + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; +__PACKAGE__->load_components("FilterColumn", "InflateColumn::DateTime", "EncodedColumn"); +__PACKAGE__->table("translation"); +__PACKAGE__->add_columns( + "id", + { + data_type => "integer", + is_auto_increment => 1, + is_nullable => 0, + sequence => "translation_id_seq", + }, + "tbl", + { data_type => "text", is_nullable => 0 }, + "object_id", + { data_type => "integer", is_nullable => 0 }, + "col", + { data_type => "text", is_nullable => 0 }, + "lang", + { data_type => "text", is_nullable => 0 }, + "msgstr", + { data_type => "text", is_nullable => 0 }, +); +__PACKAGE__->set_primary_key("id"); +__PACKAGE__->add_unique_constraint( + "translation_tbl_object_id_col_lang_key", + ["tbl", "object_id", "col", "lang"], +); + + +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-07-14 23:24:32 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:///VNqg4BOuO29xKhnY8vw + + +# You can replace this text with custom code or comments, and it will be preserved on regeneration +1; diff --git a/perllib/FixMyStreet/DB/Schema.pm b/perllib/FixMyStreet/DB/Schema.pm new file mode 100644 index 000000000..45d731c33 --- /dev/null +++ b/perllib/FixMyStreet/DB/Schema.pm @@ -0,0 +1,28 @@ +use utf8; +package FixMyStreet::DB::Schema; + +# Created by DBIx::Class::Schema::Loader +# DO NOT MODIFY THE FIRST PART OF THIS FILE + +use strict; +use warnings; + +use base 'DBIx::Class::Schema'; + +__PACKAGE__->load_namespaces( + result_namespace => "+FixMyStreet::DB::Result", + resultset_namespace => "+FixMyStreet::DB::ResultSet", +); + + +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-07-13 14:15:09 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:UpH30RXb6SbCqRv2FPmpkg + +use Moo; +use FixMyStreet; + +__PACKAGE__->connection(FixMyStreet->dbic_connect_info); + +has lang => ( is => 'rw' ); + +1; diff --git a/perllib/FixMyStreet/Roles/Translatable.pm b/perllib/FixMyStreet/Roles/Translatable.pm new file mode 100644 index 000000000..49f10d51a --- /dev/null +++ b/perllib/FixMyStreet/Roles/Translatable.pm @@ -0,0 +1,97 @@ +package FixMyStreet::Roles::Translatable; + +use Moo::Role; + +sub translate_around { + my ($orig, $self) = (shift, shift); + my $fallback = $self->$orig(@_); + (my $col = (caller(2))[3]) =~ s/.*:://; + $self->_translate($col, $fallback); +} + +sub translate { + my ($self, $col) = (shift, shift); + my $fallback = $self->$col(@_); + $self->_translate($col, $fallback); +} + +sub _translate { + my ($self, $col, $fallback) = @_; + + my $schema = $self->result_source->schema; + my $table = lc $self->result_source->source_name; + my $id = $self->id; + + # Deal with the fact problem table has denormalized copy of category string + if ($table eq 'problem' && $col eq 'category') { + my $body_id = $self->bodies_str_ids->[0]; + return $fallback unless $body_id && $body_id =~ /^[0-9]+$/; + my $contact = $schema->resultset("Contact")->find( { + body_id => $body_id, + category => $fallback, + } ); + return $fallback unless $contact; # Shouldn't happen, but some tests + $table = 'contact'; + $id = $contact->id; + } + + if (ref $schema) { + my $translation = $schema->resultset('Translation')->find({ + lang => $schema->lang, + tbl => $table, + object_id => $id, + col => $col + }); + return $translation->msgstr if $translation; + } else { + warn "Can't use translation on this call to $table.$col"; + } + return $fallback; +}; + +# These next two functions (translation_for and and_translation_for) are +# convenience methods for use in the translation interface in the admin. +# They shouldn't be used else where as they don't take account of things +# like denormalised strings (e.g report category) +sub translation_for { + my ($self, $col, $lang) = @_; + + my $schema = $self->result_source->schema; + + my $props = { + tbl => lc $self->result_source->source_name, + object_id => $self->id, + col => $col + }; + + if ($lang) { + $props->{lang} = $lang; + } + + my $translations = $schema->resultset('Translation')->search($props); + + return $lang ? $translations->first : $translations; +} + +sub add_translation_for { + my ($self, $col, $lang, $msgstr) = @_; + + my $schema = $self->result_source->schema; + + my $props = { + tbl => lc $self->result_source->source_name, + object_id => $self->id, + col => $col, + lang => $lang, + msgstr => $msgstr, + }; + + my $translation = $schema->resultset('Translation')->update_or_create( + $props, + { key => 'translation_tbl_object_id_col_lang_key' } + ); + + return $translation; +} + +1; diff --git a/perllib/FixMyStreet/Script/Alerts.pm b/perllib/FixMyStreet/Script/Alerts.pm index ef1bdb08b..aefe13318 100644 --- a/perllib/FixMyStreet/Script/Alerts.pm +++ b/perllib/FixMyStreet/Script/Alerts.pm @@ -62,7 +62,7 @@ sub send() { $query =~ s/\?/alert.parameter/ if ($query =~ /\?/); $query =~ s/\?/alert.parameter2/ if ($query =~ /\?/); - $query = FixMyStreet::DB->storage->dbh->prepare($query); + $query = FixMyStreet::DB->schema->storage->dbh->prepare($query); $query->execute(); my $last_alert_id; my %data = ( template => $alert_type->template, data => [], schema => $schema ); @@ -225,7 +225,7 @@ sub send() { and (select whenqueued from alert_sent where alert_sent.alert_id = ? and alert_sent.parameter::integer = problem.id) is null and users.email <> ? order by confirmed desc"; - $q = FixMyStreet::DB->storage->dbh->prepare($q); + $q = FixMyStreet::DB->schema->storage->dbh->prepare($q); $q->execute($latitude, $longitude, $d, $alert->whensubscribed, $alert->id, $alert->user->email); while (my $row = $q->fetchrow_hashref) { $schema->resultset('AlertSent')->create( { diff --git a/perllib/FixMyStreet/Script/UpdateAllReports.pm b/perllib/FixMyStreet/Script/UpdateAllReports.pm index 51cb7b856..1bd069ee8 100755 --- a/perllib/FixMyStreet/Script/UpdateAllReports.pm +++ b/perllib/FixMyStreet/Script/UpdateAllReports.pm @@ -158,7 +158,7 @@ sub generate_dashboard { ); $data{last_seven_days} = \%last_seven_days; - my $dtf = FixMyStreet::DB->storage->datetime_parser; + my $dtf = FixMyStreet::DB->schema->storage->datetime_parser; my $eight_ago = $dtf->format_datetime(DateTime->now->subtract(days => 8)); %problems_reported_by_period = stuff_by_day_or_year('day', 'Problem', diff --git a/perllib/FixMyStreet/Test.pm b/perllib/FixMyStreet/Test.pm index add67dfd9..6b6bc02bc 100644 --- a/perllib/FixMyStreet/Test.pm +++ b/perllib/FixMyStreet/Test.pm @@ -8,7 +8,7 @@ use utf8; use Test::More; use FixMyStreet::DB; -my $db = FixMyStreet::DB->storage; +my $db = FixMyStreet::DB->schema->storage; sub import { strict->import; |