diff options
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Open311.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Photo.pm | 2 | ||||
-rwxr-xr-x | perllib/FixMyStreet/App/Controller/Rss.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Zurich.pm | 105 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 20 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 32 | ||||
-rw-r--r-- | perllib/FixMyStreet/Roles/PhotoSet.pm | 34 |
8 files changed, 118 insertions, 87 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Open311.pm b/perllib/FixMyStreet/App/Controller/Open311.pm index 95b29d116..c7e4e5bee 100644 --- a/perllib/FixMyStreet/App/Controller/Open311.pm +++ b/perllib/FixMyStreet/App/Controller/Open311.pm @@ -284,7 +284,7 @@ sub output_requests : Private { my $display_photos = $c->cobrand->allow_photo_display($problem); if ($display_photos && $problem->photo) { my $url = $c->cobrand->base_url(); - my $imgurl = $url . $problem->photos->[0]->{url_full}; + my $imgurl = $url . $problem->photos->[$display_photos-1]->{url_full}; $request->{'media_url'} = $imgurl; } push(@problemlist, $request); diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm index 2302322bf..f41702dcf 100644 --- a/perllib/FixMyStreet/App/Controller/Photo.pm +++ b/perllib/FixMyStreet/App/Controller/Photo.pm @@ -63,7 +63,7 @@ sub index :LocalRegex('^(c/)?([1-9]\d*)(?:\.(\d+))?(?:\.(full|tn|fp))?\.(?:jpeg| $c->detach( 'no_photo' ) unless $item; - $c->detach( 'no_photo' ) unless $c->cobrand->allow_photo_display($item); # Should only be for reports, not updates + $c->detach( 'no_photo' ) unless $c->cobrand->allow_photo_display($item, $photo_number); # Should only be for reports, not updates my $photo; $photo = $item->get_photoset diff --git a/perllib/FixMyStreet/App/Controller/Rss.pm b/perllib/FixMyStreet/App/Controller/Rss.pm index 7cf4783c0..e1da4445d 100755 --- a/perllib/FixMyStreet/App/Controller/Rss.pm +++ b/perllib/FixMyStreet/App/Controller/Rss.pm @@ -282,14 +282,15 @@ sub add_row : Private { $item{pubDate} = $pubDate if $pubDate; $item{category} = encode_entities($row->{category}) if $row->{category}; - if ($c->cobrand->allow_photo_display($row) && $row->{photo}) { + if ((my $photo_to_show = $c->cobrand->allow_photo_display($row)) && $row->{photo}) { # Bit yucky as we don't have full objects here my $photoset = FixMyStreet::App::Model::PhotoSet->new({ db_data => $row->{photo} }); - my $first_fn = $photoset->get_id(0); + my $idx = $photo_to_show - 1; + my $first_fn = $photoset->get_id($idx); my ($hash, $format) = split /\./, $first_fn; my $cachebust = substr($hash, 0, 8); my $key = $alert_type->item_table eq 'comment' ? 'c/' : ''; - $item{description} .= encode_entities("\n<br><img src=\"". $base_url . "/photo/$key$row->{id}.0.$format?$cachebust\">"); + $item{description} .= encode_entities("\n<br><img src=\"". $base_url . "/photo/$key$row->{id}.$idx.$format?$cachebust\">"); } if ( $row->{used_map} ) { diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 4c521eaaa..7888f8ccf 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -465,11 +465,12 @@ sub allow_photo_upload { return 1; } =item allow_photo_display Return a boolean indicating whether the cobrand allows photo display +for the particular report and photo. =cut sub allow_photo_display { - my ( $self, $r ) = @_; + my ( $self, $r, $num ) = @_; return 1; } diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm index 4dc95b178..f0308d6d7 100644 --- a/perllib/FixMyStreet/Cobrand/Zurich.pm +++ b/perllib/FixMyStreet/Cobrand/Zurich.pm @@ -4,6 +4,7 @@ use base 'FixMyStreet::Cobrand::Default'; use DateTime; use POSIX qw(strcoll); use RABX; +use List::Util qw(min); use Scalar::Util 'blessed'; use DateTime::Format::Pg; @@ -223,19 +224,30 @@ sub updates_as_hashref { return $hashref; } +# If $num is undefined, we want to return the minimum photo number that can be +# shown (1-indexed), or false for no display. If $num is defined, return +# boolean whether that indexed photo can be shown. sub allow_photo_display { - my ( $self, $r ) = @_; + my ( $self, $r, $num ) = @_; + my $publish_photo; if (blessed $r) { - return $r->get_extra_metadata( 'publish_photo' ); + $publish_photo = $r->get_extra_metadata('publish_photo'); + } else { + # additional munging in case $r isn't an object, TODO see if we can remove this + my $extra = $r->{extra}; + utf8::encode($extra) if utf8::is_utf8($extra); + my $h = new IO::String($extra); + $extra = RABX::wire_rd($h); + return unless ref $extra eq 'HASH'; + $publish_photo = $extra->{publish_photo}; } + # Old style stored 1/0 integer, which can still be used if present. + return $publish_photo unless ref $publish_photo; + return $publish_photo->{$num} if defined $num; - # additional munging in case $r isn't an object, TODO see if we can remove this - my $extra = $r->{extra}; - utf8::encode($extra) if utf8::is_utf8($extra); - my $h = new IO::String($extra); - $extra = RABX::wire_rd($h); - return unless ref $extra eq 'HASH'; - return $extra->{publish_photo}; + # We return a 1-indexed number so that '0' can be taken as 'not allowed' + my $i = min grep { $publish_photo->{$_} } keys %$publish_photo; + return $i + 1; } sub show_unconfirmed_reports { @@ -250,12 +262,27 @@ sub get_body_sender { # Report overdue functions my %public_holidays = map { $_ => 1 } ( - '2013-01-01', '2013-01-02', '2013-03-29', '2013-04-01', - '2013-04-15', '2013-05-01', '2013-05-09', '2013-05-20', - '2013-08-01', '2013-09-09', '2013-12-25', '2013-12-26', - '2014-01-01', '2014-01-02', '2014-04-18', '2014-04-21', - '2014-04-28', '2014-05-01', '2014-05-29', '2014-06-09', - '2014-08-01', '2014-09-15', '2014-12-25', '2014-12-26', + # New Year's Day, Saint Berchtold, Good Friday, Easter Monday, + # Sechseläuten, Labour Day, Ascension Day, Whit Monday, + # Swiss National Holiday, Knabenschiessen, Christmas, St Stephen's Day + # Extra holidays + + '2018-01-01', '2018-01-02', '2018-03-30', '2018-04-02', + '2018-04-16', '2018-05-01', '2018-05-10', '2018-05-21', + '2018-08-01', '2018-09-10', '2018-12-25', '2018-12-26', + '2018-03-29', '2018-05-11', '2018-12-27', '2018-12-28', '2018-12-31', + + '2019-01-01', '2019-01-02', '2019-04-19', '2019-04-22', + '2019-04-08', '2019-05-01', '2019-05-30', '2019-06-10', + '2019-08-01', '2019-09-09', '2019-12-25', '2019-12-26', + + '2020-01-01', '2020-01-02', '2020-04-10', '2020-04-13', + '2020-04-20', '2020-05-01', '2020-05-21', '2020-06-01', + '2020-09-14', '2020-12-25', + + '2021-01-01', '2021-04-02', '2021-04-05', + '2021-04-19', '2021-05-13', '2021-05-24', + '2021-09-13', ); sub is_public_holiday { @@ -566,7 +593,19 @@ sub admin_report_edit { # Problem updates upon submission if ( ($type eq 'super' || $type eq 'dm') && $c->get_param('submit') ) { - $problem->set_extra_metadata('publish_photo' => $c->get_param('publish_photo') || 0 ); + + my @keys = grep { /^publish_photo/ } keys %{ $c->req->params }; + my %publish_photo; + foreach my $key (@keys) { + my ($index) = $key =~ /(\d+)$/; + $publish_photo{$index} = 1 if $c->get_param($key); + } + + if (%publish_photo) { + $problem->set_extra_metadata('publish_photo' => \%publish_photo); + } else { + $problem->unset_extra_metadata('publish_photo'); + } $problem->set_extra_metadata('third_personal' => $c->get_param('third_personal') || 0 ); # Make sure we have a copy of the original detail field @@ -1005,23 +1044,28 @@ sub _admin_send_email { sub munge_sendreport_params { my ($self, $row, $h, $params) = @_; - if ($row->state =~ /^(closed|investigating)$/ && $row->get_extra_metadata('publish_photo')) { + + if ($row->state =~ /^(closed|investigating)$/) { # we attach images to reports sent to external bodies my $photoset = $row->get_photoset(); my $num = $photoset->num_images or return; my $id = $row->id; my @attachments = map { - my $image = $photoset->get_raw_image($_); - { - body => $image->{data}, - attributes => { - filename => "$id.$_." . $image->{extension}, - content_type => $image->{content_type}, - encoding => 'base64', - # quoted-printable ends up with newlines corrupting binary data - name => "$id.$_." . $image->{extension}, - }, + if ($self->allow_photo_display($row, $_)) { + my $image = $photoset->get_raw_image($_); + { + body => $image->{data}, + attributes => { + filename => "$id.$_." . $image->{extension}, + content_type => $image->{content_type}, + encoding => 'base64', + # quoted-printable ends up with newlines corrupting binary data + name => "$id.$_." . $image->{extension}, + }, + }; + } else { + (); } } (0..$num-1); $params->{_attachments_} = \@attachments; @@ -1165,7 +1209,10 @@ sub admin_stats { $public_response =~ s{\r?\n}{ <br/> }g if $public_response; # Assemble photo URL, if report has a photo - my $media_url = ( @{$report->photos} && $c->cobrand->allow_photo_display($report) ) ? ($c->cobrand->base_url . $report->photos->[0]->{url}) : ''; + my $photo_to_display = $c->cobrand->allow_photo_display($report); + my $media_url = (@{$report->photos} && $photo_to_display) + ? $c->cobrand->base_url . $report->photos->[$photo_to_display-1]->{url} + : ''; my @columns = ( $report->id, @@ -1231,7 +1278,7 @@ sub admin_stats { # pictures taken my $pictures_taken = $c->model('DB::Problem')->search( { photo => { '!=', undef }, %params } )->count; # pictures published - my $pictures_published = $c->model('DB::Problem')->search( { extra => { like => '%publish_photo,I1:1%' }, %params } )->count; + my $pictures_published = $c->model('DB::Problem')->search( { extra => { like => '%publish_photo%' }, %params } )->count; # how many times was a telephone number provided # XXX => How many users have a telephone number stored # my $phone = $c->model('DB::User')->search( { phone => { '!=', undef } } )->count; diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 60fd31510..4e869ab24 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -156,26 +156,6 @@ sub url { return "/report/" . $self->problem_id . '#update_' . $self->id; } -sub photos { - my $self = shift; - my $photoset = $self->get_photoset; - my $i = 0; - my $id = $self->id; - my @photos = map { - my $cachebust = substr($_, 0, 8); - my ($hash, $format) = split /\./, $_; - { - id => $hash, - url_temp => "/photo/temp.$hash.$format", - url_temp_full => "/photo/fulltemp.$hash.$format", - url => "/photo/c/$id.$i.$format?$cachebust", - url_full => "/photo/c/$id.$i.full.$format?$cachebust", - idx => $i++, - } - } $photoset->all_ids; - return \@photos; -} - =head2 latest_moderation_log_entry Return most recent ModerationLog object diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 8625bf17a..a32c17ffb 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -919,38 +919,6 @@ sub latest_moderation_log_entry { return $self->admin_log_entries->search({ action => 'moderation' }, { order_by => { -desc => 'id' } })->first; } -sub photos { - my $self = shift; - my $photoset = $self->get_photoset; - my $i = 0; - my $id = $self->id; - my @photos = map { - my $cachebust = substr($_, 0, 8); - # Some Varnish configurations (e.g. on mySociety infra) strip cookies from - # images, which means image requests will be redirected to the login page - # if LOGIN_REQUIRED is set. To stop this happening, Varnish should be - # configured to not strip cookies if the cookie_passthrough param is - # present, which this line ensures will be if LOGIN_REQUIRED is set. - my $extra = ''; - if (FixMyStreet->config('LOGIN_REQUIRED')) { - $cachebust .= '&cookie_passthrough=1'; - $extra = '?cookie_passthrough=1'; - } - my ($hash, $format) = split /\./, $_; - { - id => $hash, - url_temp => "/photo/temp.$hash.$format$extra", - url_temp_full => "/photo/fulltemp.$hash.$format$extra", - url => "/photo/$id.$i.$format?$cachebust", - url_full => "/photo/$id.$i.full.$format?$cachebust", - url_tn => "/photo/$id.$i.tn.$format?$cachebust", - url_fp => "/photo/$id.$i.fp.$format?$cachebust", - idx => $i++, - } - } $photoset->all_ids; - return \@photos; -} - __PACKAGE__->has_many( "admin_log_entries", "FixMyStreet::DB::Result::AdminLog", diff --git a/perllib/FixMyStreet/Roles/PhotoSet.pm b/perllib/FixMyStreet/Roles/PhotoSet.pm index 9607b5049..2a6863cff 100644 --- a/perllib/FixMyStreet/Roles/PhotoSet.pm +++ b/perllib/FixMyStreet/Roles/PhotoSet.pm @@ -32,4 +32,38 @@ sub get_first_image_fp { return $self->get_photoset->get_image_data( num => 0, size => 'fp' ); } +sub photos { + my $self = shift; + my $photoset = $self->get_photoset; + my $i = 0; + my $id = $self->id; + my $typ = $self->result_source->name eq 'comment' ? 'c/' : ''; + + my @photos = map { + my $cachebust = substr($_, 0, 8); + # Some Varnish configurations (e.g. on mySociety infra) strip cookies from + # images, which means image requests will be redirected to the login page + # if LOGIN_REQUIRED is set. To stop this happening, Varnish should be + # configured to not strip cookies if the cookie_passthrough param is + # present, which this line ensures will be if LOGIN_REQUIRED is set. + my $extra = ''; + if (FixMyStreet->config('LOGIN_REQUIRED')) { + $cachebust .= '&cookie_passthrough=1'; + $extra = '?cookie_passthrough=1'; + } + my ($hash, $format) = split /\./, $_; + { + id => $hash, + url_temp => "/photo/temp.$hash.$format$extra", + url_temp_full => "/photo/fulltemp.$hash.$format$extra", + url => "/photo/$typ$id.$i.$format?$cachebust", + url_full => "/photo/$typ$id.$i.full.$format?$cachebust", + url_tn => "/photo/$typ$id.$i.tn.$format?$cachebust", + url_fp => "/photo/$typ$id.$i.fp.$format?$cachebust", + idx => $i++, + } + } $photoset->all_ids; + return \@photos; +} + 1; |