diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm | 99 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Location.pm | 20 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Offline.pm | 106 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Root.pm | 2 |
4 files changed, 192 insertions, 35 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm b/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm new file mode 100644 index 000000000..9e3bdc33e --- /dev/null +++ b/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm @@ -0,0 +1,99 @@ +package FixMyStreet::App::Controller::Admin::ManifestTheme; +use Moose; +use namespace::autoclean; + +BEGIN { extends 'Catalyst::Controller'; } + +use FixMyStreet::App::Form::ManifestTheme; + +sub auto :Private { + my ($self, $c) = @_; + + if ( $c->cobrand->moniker eq 'fixmystreet' ) { + $c->stash(rs => $c->model('DB::ManifestTheme')->search_rs({}), show_all => 1); + } else { + $c->stash(rs => $c->model('DB::ManifestTheme')->search_rs({ cobrand => $c->cobrand->moniker })); + } +} + +sub index :Path :Args(0) { + my ( $self, $c ) = @_; + + unless ( $c->stash->{show_all} ) { + if ( $c->stash->{rs}->count ) { + $c->res->redirect($c->uri_for($self->action_for('edit'), [ $c->stash->{rs}->first->cobrand ])); + } else { + $c->res->redirect($c->uri_for($self->action_for('create'))); + } + $c->detach; + } +} + +sub item :PathPart('admin/manifesttheme') :Chained :CaptureArgs(1) { + my ($self, $c, $cobrand) = @_; + + my $obj = $c->stash->{rs}->find({ cobrand => $cobrand }) + or $c->detach('/page_error_404_not_found', []); + $c->stash(obj => $obj); +} + +sub edit :PathPart('') :Chained('item') :Args(0) { + my ($self, $c) = @_; + + my $form = $self->form($c, $c->stash->{obj}); + + # We need to do this after form processing, in case a form POST has deleted + # an icon. + $c->stash->{editing_manifest_theme} = $c->forward('/offline/_find_manifest_theme', [ $c->stash->{obj}->cobrand, 1 ]); + + return $form; +} + + +sub create :Local :Args(0) { + my ($self, $c) = @_; + + unless ( $c->stash->{show_all} || $c->stash->{rs}->count == 0) { + $c->res->redirect($c->uri_for($self->action_for('edit'), [ $c->stash->{rs}->first->cobrand ])); + $c->detach; + } + + my $theme = $c->stash->{rs}->new_result({}); + return $self->form($c, $theme); +} + +sub form { + my ($self, $c, $theme) = @_; + + if ($c->get_param('delete_theme')) { + $c->forward('_delete_all_manifest_icons'); + $c->forward('/offline/_clear_manifest_theme_cache', [ $theme->cobrand ]); + $theme->delete; + $c->forward('/admin/log_edit', [ $theme->id, 'manifesttheme', 'delete' ]); + $c->response->redirect($c->uri_for($self->action_for('index'))); + $c->detach; + } + + my $action = $theme->in_storage ? 'edit' : 'add'; + my $form = FixMyStreet::App::Form::ManifestTheme->new( cobrand => $c->cobrand->moniker ); + $c->stash(template => 'admin/manifesttheme/form.html', form => $form); + my $params = $c->req->params; + $params->{icon} = $c->req->upload('icon') if $params->{icon}; + $form->process(item => $theme, params => $params); + return unless $form->validated; + + $c->forward('/admin/log_edit', [ $theme->id, 'manifesttheme', $action ]); + $c->forward('/offline/_clear_manifest_theme_cache', [ $theme->cobrand ]); + $c->response->redirect($c->uri_for($self->action_for('index'))); +} + +sub _delete_all_manifest_icons :Private { + my ($self, $c) = @_; + + my $theme = $c->forward('/offline/_find_manifest_theme', [ $c->stash->{obj}->cobrand, 1 ]); + foreach my $icon ( @{ $theme->{icons} } ) { + unlink FixMyStreet->path_to('web', $icon->{src}); + } +} + +1; diff --git a/perllib/FixMyStreet/App/Controller/Location.pm b/perllib/FixMyStreet/App/Controller/Location.pm index 8d5b0b147..81c2c33fc 100644 --- a/perllib/FixMyStreet/App/Controller/Location.pm +++ b/perllib/FixMyStreet/App/Controller/Location.pm @@ -6,6 +6,7 @@ BEGIN {extends 'Catalyst::Controller'; } use Encode; use FixMyStreet::Geocode; +use Try::Tiny; use Utils; =head1 NAME @@ -107,6 +108,25 @@ sub determine_location_from_pc : Private { # pass errors back to the template $c->stash->{location_error_pc_lookup} = 1; $c->stash->{location_error} = $error; + + # Log failure in a log db + try { + my $dbfile = FixMyStreet->path_to('../data/analytics.sqlite'); + my $db = DBI->connect("dbi:SQLite:dbname=$dbfile", undef, undef) or die "$DBI::errstr\n"; + my $sth = $db->prepare("INSERT INTO location_searches_with_no_results + (datetime, cobrand, geocoder, url, user_input) + VALUES (?, ?, ?, ?, ?)") or die $db->errstr . "\n"; + my $rv = $sth->execute( + POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime(time())), + $c->cobrand->moniker, + $c->cobrand->get_geocoder(), + $c->stash->{geocoder_url}, + $pc, + ); + } catch { + $c->log->debug("Unable to log to analytics.sqlite: $_"); + }; + return; } diff --git a/perllib/FixMyStreet/App/Controller/Offline.pm b/perllib/FixMyStreet/App/Controller/Offline.pm index 57cbe201c..adb3de14d 100644 --- a/perllib/FixMyStreet/App/Controller/Offline.pm +++ b/perllib/FixMyStreet/App/Controller/Offline.pm @@ -33,42 +33,12 @@ sub manifest: Path("/.well-known/manifest.webmanifest") { my ($self, $c) = @_; $c->res->content_type('application/manifest+json'); - my $theme = $c->model('DB::ManifestTheme')->find({ cobrand => $c->cobrand->moniker }); - unless ( $theme ) { - $theme = $c->model('DB::ManifestTheme')->new({ - name => $c->stash->{site_name}, - short_name => $c->stash->{site_name}, - background_colour => '#ffffff', - theme_colour => '#ffd000', - }); - } - - my @icons; - my $uri = '/theme/' . $c->cobrand->moniker; - my $theme_path = path(FixMyStreet->path_to('web' . $uri)); - $theme_path->visit( - sub { - my ($x, $y, $typ) = Image::Size::imgsize($_->stringify); - push @icons, { - src => join('/', $uri, $_->basename), - sizes => join('x', $x, $y), - type => $typ eq 'PNG' ? 'image/png' : $typ eq 'GIF' ? 'image/gif' : $typ eq 'JPG' ? 'image/jpeg' : '', - }; - } - ); - - unless (@icons) { - push @icons, - { src => "/cobrands/fixmystreet/images/192.png", sizes => "192x192", type => "image/png" }, - { src => "/cobrands/fixmystreet/images/512.png", sizes => "512x512", type => "image/png" }; - } - my $data = { - name => $theme->name, - short_name => $theme->short_name, - background_color => $theme->background_colour, - theme_color => $theme->theme_colour, - icons => \@icons, + name => $c->stash->{manifest_theme}->{name}, + short_name => $c->stash->{manifest_theme}->{short_name}, + background_color => $c->stash->{manifest_theme}->{background_colour}, + theme_color => $c->stash->{manifest_theme}->{theme_colour}, + icons => $c->stash->{manifest_theme}->{icons}, lang => $c->stash->{lang_code}, display => "minimal-ui", start_url => "/?pwa", @@ -82,6 +52,72 @@ sub manifest: Path("/.well-known/manifest.webmanifest") { $c->res->body($json); } +sub _stash_manifest_theme : Private { + my ($self, $c, $cobrand) = @_; + + $c->stash->{manifest_theme} = $c->forward('_find_manifest_theme', [ $cobrand ]); +} + +sub _find_manifest_theme : Private { + my ($self, $c, $cobrand, $ignore_cache_and_defaults) = @_; + + my $key = "manifest_theme:$cobrand"; + # ignore_cache_and_defaults is only used in the admin, so no harm bypassing cache + my $manifest_theme = $ignore_cache_and_defaults ? undef : Memcached::get($key); + + unless ( $manifest_theme ) { + my $theme = $c->model('DB::ManifestTheme')->find({ cobrand => $cobrand }); + unless ( $theme ) { + $theme = $c->model('DB::ManifestTheme')->new({ + name => $c->stash->{site_name}, + short_name => $c->stash->{site_name}, + background_colour => '#ffffff', + theme_colour => '#ffd000', + }); + } + + my @icons; + my $uri = '/theme/' . $cobrand; + my $theme_path = path(FixMyStreet->path_to('web' . $uri)); + $theme_path->visit( + sub { + my ($x, $y, $typ) = Image::Size::imgsize($_->stringify); + push @icons, { + src => join('/', $uri, $_->basename), + sizes => join('x', $x, $y), + type => $typ eq 'PNG' ? 'image/png' : $typ eq 'GIF' ? 'image/gif' : $typ eq 'JPG' ? 'image/jpeg' : '', + }; + } + ); + + unless (@icons || $ignore_cache_and_defaults) { + push @icons, + { src => "/cobrands/fixmystreet/images/192.png", sizes => "192x192", type => "image/png" }, + { src => "/cobrands/fixmystreet/images/512.png", sizes => "512x512", type => "image/png" }; + } + + $manifest_theme = { + icons => \@icons, + background_colour => $theme->background_colour, + theme_colour => $theme->theme_colour, + name => $theme->name, + short_name => $theme->short_name, + }; + + unless ($ignore_cache_and_defaults) { + Memcached::set($key, $manifest_theme); + } + } + + return $manifest_theme; +} + +sub _clear_manifest_theme_cache : Private { + my ($self, $c, $cobrand ) = @_; + + Memcached::delete("manifest_theme:$cobrand"); +} + __PACKAGE__->meta->make_immutable; 1; diff --git a/perllib/FixMyStreet/App/Controller/Root.pm b/perllib/FixMyStreet/App/Controller/Root.pm index caaa260ff..71dcf8e27 100644 --- a/perllib/FixMyStreet/App/Controller/Root.pm +++ b/perllib/FixMyStreet/App/Controller/Root.pm @@ -42,6 +42,8 @@ sub auto : Private { $c->forward('check_password_expiry'); $c->detach('/auth/redirect') if $c->cobrand->call_hook('check_login_disallowed'); + $c->forward('/offline/_stash_manifest_theme', [ $c->cobrand->moniker ]); + return 1; } |