diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller')
-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 |
5 files changed, 180 insertions, 47 deletions
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' ); } |