<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package FixMyStreet::App::Controller::Admin::Triage;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Admin::Triage - Catalyst Controller

=head1 DESCRIPTION

Admin pages for triaging reports.

This allows reports to be triaged before being sent to the council. It works
by having a set of categories with a send_method of Triage which sets the report
state to 'for_triage'. Any reports with the state are then show on '/admin/triage'
which is available to users with the 'triage' permission.

Clicking on reports on this list will then allow a user to change the category of
the report to one that has an alternative send method, which will trigger the report
to be resent.

In order for this to work additional work needs to be done to the cobrand to only
display triageable categories to the user.

=head1 METHODS

=cut

sub auto : Private {
    my ( $self, $c ) = @_;

    unless ( $c-&gt;user-&gt;has_body_permission_to('triage') ) {
        $c-&gt;detach('/page_error_403_access_denied', []);
    }
}

sub index : Path : Args(0) {
    my ( $self, $c ) = @_;

    # default sort to oldest
    unless ( $c-&gt;get_param('sort') ) {
        $c-&gt;set_param('sort', 'created-asc');
    }

    # Do not set stash-&gt;{body} to the $body as we want to show all a cobrand's
    # reports, not just ones for the body (normally this would be the same, but
    # e.g. Bucks include parishes)
    my $body = $c-&gt;forward('/reports/body_find', [ $c-&gt;cobrand-&gt;council_area ]);

    $c-&gt;forward( 'stash_report_filter_status' );
    $c-&gt;forward('/reports/stash_report_sort', [ $c-&gt;cobrand-&gt;reports_ordering ]);
    $c-&gt;forward( '/reports/load_and_group_problems' );
    $c-&gt;stash-&gt;{page} = 'reports'; # So the map knows to make clickable pins

    $c-&gt;stash-&gt;{body} = { id =&gt; 0 }; # So the problems are output by the template
    if ($c-&gt;get_param('ajax')) {
        my $ajax_template = $c-&gt;stash-&gt;{ajax_template} || 'reports/_problem-list.html';
        $c-&gt;detach('/reports/ajax', [ $ajax_template ]);
    }

    my @categories = $body-&gt;contacts-&gt;not_deleted-&gt;search( undef, {
        columns =&gt; [ 'id', 'category', 'extra' ],
        distinct =&gt; 1,
    } )-&gt;all_sorted;
    $c-&gt;stash-&gt;{filter_categories} = \@categories;
    $c-&gt;stash-&gt;{filter_category} = { map { $_ =&gt; 1 } $c-&gt;get_param_list('filter_category', 1) };
    my $pins = $c-&gt;stash-&gt;{pins} || [];

    my %map_params = (
        latitude  =&gt; @$pins ? $pins-&gt;[0]{latitude} : 0,
        longitude =&gt; @$pins ? $pins-&gt;[0]{longitude} : 0,
        area      =&gt; [ keys %{$body-&gt;areas} ],
        any_zoom  =&gt; 1,
    );
    FixMyStreet::Map::display_map(
        $c, %map_params, pins =&gt; $pins,
    );
}

sub stash_report_filter_status : Private {
    my ( $self, $c ) = @_;
    $c-&gt;stash-&gt;{filter_problem_states} = { 'for triage' =&gt; 1 };
    return 1;
}

sub setup_categories : Private {
    my ( $self, $c ) = @_;

    if ( $c-&gt;stash-&gt;{problem}-&gt;state eq 'for triage' ) {
        $c-&gt;stash-&gt;{end_options} = [ grep { !$_-&gt;send_method || $_-&gt;send_method ne 'Triage' } @{$c-&gt;stash-&gt;{contacts}} ];
        delete $c-&gt;stash-&gt;{categories_hash};
        my %category_groups = ();
        for my $category (@{$c-&gt;stash-&gt;{end_options}}) {
            my $groups = $category-&gt;groups;
            push( @{$category_groups{$_}}, $category ) for @$groups;
        }
        my @category_groups = ();
        for my $group ( grep { $_ ne _('Other') } sort keys %category_groups ) {
            push @category_groups, { name =&gt; $group, categories =&gt; $category_groups{$group} };
        }
        $c-&gt;stash-&gt;{end_groups} = \@category_groups;
    }

    return 1;
}

sub update : Private {
    my ($self, $c) = @_;

    my $problem = $c-&gt;stash-&gt;{problem};

    my $current_category = $problem-&gt;category;
    my $new_category_id = $c-&gt;get_param('category');

    if (!$new_category_id) {
        my $errors = $c-&gt;stash-&gt;{errors} || [];
        push @$errors, _"Please choose a category";
        $c-&gt;stash-&gt;{errors} = $errors;

        $c-&gt;detach;
    }

    my $contact = FixMyStreet::DB-&gt;resultset("Contact")-&gt;find($new_category_id);
    my $new_category = $contact-&gt;category;

    my $changed = $c-&gt;forward('/admin/reports/edit_category', [ $problem, 1, $contact ] );

    if ( $changed ) {
        $c-&gt;stash-&gt;{problem}-&gt;update( { state =&gt; 'confirmed' } );
        $c-&gt;forward( '/admin/log_edit', [ $problem-&gt;id, 'problem', 'triage' ] );

        my $extra;
        $extra-&gt;{triage_report} = 1;
        $extra-&gt;{holding_category} = $current_category;
        $extra-&gt;{new_category} = $new_category;

        my $comment = $problem-&gt;add_to_comments( {
            text =&gt; "Report triaged from $current_category to $new_category",
            user =&gt; $c-&gt;user-&gt;obj,
            problem_state =&gt; $problem-&gt;state,
            extra =&gt; $extra,
            whensent =&gt; \'current_timestamp',
            send_state =&gt; 'processed',
        } );

        # Stop any alerts being sent out about this closure.
        $problem-&gt;cancel_update_alert($comment-&gt;id);
    }
}

1;
</pre></body></html>