aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Develop.pm
blob: ae7122fa1537b94706f3a9443ceef20d436a0e8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package FixMyStreet::App::Controller::Develop;
use Moose;
use namespace::autoclean;

use File::Basename;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Develop - Catalyst Controller

=head1 DESCRIPTION

Developer-helping Catalyst Controller.

=head1 METHODS

=over 4

=item auto

Makes sure this controller is only available when run in development.

=cut

sub auto : Private {
    my ($self, $c) = @_;
    $c->detach( '/page_error_404_not_found' ) unless $c->user_exists && $c->user->is_superuser;
    return 1;
}

=item index

Shows a list of links to preview HTML emails.

=cut

sub index : Path('/_dev') : Args(0) {
    my ( $self, $c ) = @_;
    $c->stash->{problem} = $c->model('DB::Problem')->search(undef, { rows => 1 } )->first;
}

=item email_list

Shows a list of links to preview HTML emails.

=cut

sub email_list : Path('/_dev/email') : Args(0) {
    my ( $self, $c ) = @_;

    my @include_path = @{ $c->cobrand->path_to_email_templates($c->stash->{lang_code}) };
    push @include_path, $c->view('Email')->config->{INCLUDE_PATH}->[0];
    my %templates;
    foreach (@include_path) {
        $templates{$_} = 1 for grep { /^[^_]/ } map { s/\.html$//; basename $_ } glob "$_/*.html";
    }

    my %with_update = ('update-confirm' => 1, 'other-updated' => 1);
    my %with_problem = ('alert-update' => 1, 'other-reported' => 1,
        'problem-confirm' => 1, 'problem-confirm-not-sending' => 1,
        'confirm_report_sent' => 1,
        'problem-moderated' => 1, 'questionnaire' => 1, 'submit' => 1);

    my $update = $c->model('DB::Comment')->search(undef, { rows => 1 } )->first;
    my $problem = $c->model('DB::Problem')->search(undef, { rows => 1 } )->first;

    $c->stash->{templates} = [];
    foreach (sort keys %templates) {
        my $url = $c->uri_for('/_dev/email', $_);
        $url .= "?problem=" . $problem->id if $problem && $with_problem{$_};
        $url .= "?update=" . $update->id if $update && $with_update{$_};
        push @{$c->stash->{templates}}, { name => $_, url => $url };
    }
}

=item email_previewer

Previews an HTML email template. A problem or update ID can be provided as a
query parameter, and other data is taken from the database.

=back

=cut

sub email_previewer : Path('/_dev/email') : Args(1) {
    my ( $self, $c, $template ) = @_;

    my $vars = {};
    if (my $id = $c->get_param('update')) {
        $vars->{update} = $c->model('DB::Comment')->find($id);
        $vars->{problem} = $vars->{report} = $vars->{update}->problem;
    } elsif ($id = $c->get_param('problem')) {
        $vars->{problem} = $vars->{report} = $c->model('DB::Problem')->find($id);
    }

    # Special case needed variables
    if ($template =~ /^alert-problem/) {
        $vars->{area_name} = 'Area Name';
        $vars->{ward_name} = 'Ward Name';
        $vars->{data} = [ $c->model('DB::Problem')->search({}, { rows => 5 })->all ];
    } elsif ($template eq 'alert-update') {
        $vars->{data} = [];
        my $q = $c->model('DB::Comment')->search({}, { rows => 5 });
        while (my $u = $q->next) {
            my $fn = sub {
                return FixMyStreet::App::Model::PhotoSet->new({
                    db_data => $u->photo,
                })->get_image_data( num => 0, size => 'fp' );
            };
            push @{$vars->{data}}, {
                item_photo => $u->photo, get_first_image_fp => $fn, item_text => $u->text,
                item_name => $u->name, item_anonymous => $u->anonymous, confirmed => $u->confirmed };
        }
    } elsif ($template eq 'questionnaire') {
        $vars->{created} = 'N weeks';
    }

    my $email = $c->construct_email("$template.txt", $vars);

    # Look through the Email::MIME email for the text/html part, and any inline
    # images. Turn the images into data: URIs.
    my $html = '';
    my %images;
    $email->walk_parts(sub {
        my ($part) = @_;
        return if $part->subparts;
        if ($part->content_type =~ m[^image/]i) {
            (my $cid = $part->header('Content-ID')) =~ s/[<>]//g;
            (my $ct = $part->content_type) =~ s/;.*//;
            $images{$cid} = "$ct;base64," . $part->body_raw;
        } elsif ($part->content_type =~ m[text/html]i) {
            $html = $part->body_str;
        }
    });

    foreach (keys %images) {
        $html =~ s/cid:([^"]*)/data:$images{$1}/g;
    }

    $c->response->body($html);
}

=item problem_confirm_previewer

Displays the confirmation page for a given problem.

=back

=cut

sub problem_confirm_previewer : Path('/_dev/confirm_problem') : Args(1) {
    my ( $self, $c, $id ) = @_;

    $c->log->info('Previewing confirmation page for problem ' . $id);

    my $problem = $c->model('DB::Problem')->find( { id => $id } )
      || $c->detach( '/page_error_404_not_found', [ _('Unknown problem ID') ] );
    $c->stash->{report} = $problem;

    $c->log->info('Problem ' . $id . ' found: ' . $problem->title);
    $c->stash->{template} = 'tokens/confirm_problem.html';
}

=item update_confirm_previewer

Displays the confirmation page for an update on the given problem.

=back

=cut

sub update_confirm_previewer : Path('/_dev/confirm_update') : Args(1) {
    my ( $self, $c, $id ) = @_;

    my $problem = $c->model('DB::Problem')->find( { id => $id } )
      || $c->detach( '/page_error_404_not_found', [ _('Unknown problem ID') ] );
    $c->stash->{problem} = $problem;

    $c->stash->{template} = 'tokens/confirm_update.html';
}

=item alert_confirm_previewer

Displays the confirmation page for an alert, with the supplied
confirmation type (ie: subscribed, or unsubscribed).

=back

=cut

sub alert_confirm_previewer : Path('/_dev/confirm_alert') : Args(1) {
    my ( $self, $c, $confirm_type ) = @_;
    $c->stash->{confirm_type} = $confirm_type;
    $c->stash->{template} = 'tokens/confirm_alert.html';
}

=item contact_submit_previewer

Displays the contact submission page, with success based on the
truthyness of the supplied argument.

=back

=cut

sub contact_submit_previewer : Path('/_dev/contact_submit') : Args(1) {
    my ( $self, $c, $success ) = @_;
    $c->stash->{success} = $success;
    $c->stash->{template} = 'contact/submit.html';
}

=item questionnaire_completed_previewer

Displays the questionnaire completed page, with content based on
the supplied ?new_state and ?been_fixed query params.

=back

=cut

sub questionnaire_completed_previewer : Path('/_dev/questionnaire_completed') : Args(0) {
    my ( $self, $c ) = @_;
    $c->stash->{been_fixed} = $c->get_param('been_fixed');
    $c->stash->{new_state} = $c->get_param('new_state');
    $c->stash->{template} = 'questionnaire/completed.html';
}

=item questionnaire_creator_fixed_previewer

Displays the page a user sees after they mark their own report as fixed.

=back

=cut

sub questionnaire_creator_fixed_previewer : Path('/_dev/questionnaire_creator_fixed') : Args(0) {
    my ( $self, $c ) = @_;
    $c->stash->{template} = 'questionnaire/creator_fixed.html';
}

sub auth_preview : Path('/_dev/auth') : Args(0) {
    my ( $self, $c ) = @_;
    $c->stash->{template} = 'auth/token.html';
}

sub report_new_preview : Path('/_dev/report_new') : Args(0) {
    my ( $self, $c ) = @_;
    $c->stash->{template}   = 'email_sent.html';
    $c->stash->{email_type} = $c->get_param('email_type');
}

__PACKAGE__->meta->make_immutable;

1;