aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Contact.pm
blob: 980636fd0eae181b136f36beb25fdec65a5cff3a (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
package FixMyStreet::App::Controller::Contact;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Contact - Catalyst Controller

=head1 DESCRIPTION

Contact us page

=head1 METHODS

=cut

=head2 index

=cut

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

    return
      unless $c->forward('setup_request')
          && $c->forward('determine_contact_type');

#    my ($q, $errors, $field_errors) = @_;
#    my @errors = @$errors;
#    my %field_errors = %{$field_errors};
#    push @errors, _('There were problems with your report. Please see below.') if (scalar keys %field_errors);
#    my @vars = qw(name em subject message);
#    my %input = map { $_ => $q->param($_) || '' } @vars;
#    my %input_h = map { $_ => $q->param($_) ? ent($q->param($_)) : '' } @vars;
#    my $out = '';

    #    my $cobrand = Page::get_cobrand($q);
    #    my $form_action = Cobrand::url($cobrand, '/contact', $q);
    #
    #    my $intro = '';
    #    my $item_title = '';
    #    my $item_body = '';
    #    my $item_meta = '';
    #    my $hidden_vals = '';

#    my $cobrand_form_elements = Cobrand::form_elements(Page::get_cobrand($q), 'contactForm', $q);
#    my %vars = (
#      header => $header,
#      errors => $errors,
#      intro => $intro,
#      item_title => $item_title,
#      item_meta => $item_meta,
#      item_body => $item_body,
#      hidden_vals => $hidden_vals,
#      form_action => $form_action,
#      input_h => \%input_h,
#      field_errors => \%field_errors,
#      label_name => _('Your name:'),
#      label_email => _('Your email:'),
#      label_subject => _('Subject:'),
#      label_message => _('Message:'),
#      label_submit => _('Post'),
#      contact_details => contact_details($q),
#      cobrand_form_elements => $cobrand_form_elements
#    );
#    $out .= Page::template_include('contact', $q, Page::template_root($q), %vars);
#    return $out;
}

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

    return
      unless $c->forward('setup_request')
          && $c->forward('validate')
          && $c->forward('prepare_params_for_email');
}

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

    my $id        = $c->req->param('id');
    my $update_id = $c->req->param('update_id');
    $id        = undef unless $id        && $id        =~ /^[1-9]\d*$/;
    $update_id = undef unless $update_id && $update_id =~ /^[1-9]\d*$/;

    if ($id) {
        my $problem = $c->model('DB::Problem')->find(
            { id => $id },
            {
                'select' => [
                    'title', 'detail', 'name',
                    'anonymous',
                    'user_id',
                    {
                        extract => 'epoch from confirmed',
                        -as     => 'confirmed'
                    }
                ]
            }
        );

        if ($update_id) {

#             my $u = dbh()->selectrow_hashref(
#            'select comment.text, comment.name, problem.title, extract(epoch from comment.confirmed) as confirmed
#            from comment, problem where comment.id=?
#            and comment.problem_id = problem.id
#            and comment.problem_id=?', {}, $update_id ,$id);
        }
        elsif ($problem) {
            $c->stash->{problem} = $problem;
        }
    }

    return 1;
}

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

    my ( %field_errors, @errors );
    my %required = (
        name    => _('Please give your name'),
        em      => _('Please give your email'),
        subject => _('Please give a subject'),
        message => _('Please write a message')
    );

    foreach my $field ( keys %required ) {
        $field_errors{$field} = $required{$field}
          unless $c->req->param($field) =~ /\S/;
    }

    unless ( $field_errors{em} ) {
        $field_errors{em} = _('Please give a valid email address')
          if !mySociety::EmailUtil::is_valid_email( $c->req->param('em') );
    }

    push @errors, _('Illegal ID')
      if $c->req->param('id') && $c->req->param('id') !~ /^[1-9]\d*$/
          or $c->req->param('update_id')
          && $c->req->param('update_id') !~ /^[1-9]\d*$/;

    if ( @errors or scalar keys %field_errors ) {
        $c->stash->{errors}       = \@errors;
        $c->stash->{field_errors} = \%field_errors;
        $c->go('index');
    }
}

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

    $c->stash->{message} =~ s/\r\n/\n/g;
    $c->stash->{subject} =~ s/\r|\n/ /g;

    my $base_url       = $c->cobrand->base_url_for_emails;
    my $admin_base_url = $c->cobrand->admin_base_url
      || 'https://secure.mysociety.org/admin/bci/';

    if ( $c->stash->{problem} and $c->stash->{update} ) {

        # FIXME - correct url here
        my $problem_url = $base_url;
        my $admin_url   = $admin_base_url;
        $c->stash->{message} .= sprintf(
            " \n\n[ Complaint about update %d on report %d - %s - %s ]",
            $c->stash->{update}->id,
            $c->stash->{problem}->id,
            $problem_url, $admin_url
        );
    }
    elsif ( $c->stash->{problem} ) {

        # FIXME - correct url here
        my $problem_url = $base_url;
        my $admin_url   = $admin_base_url;
        $c->stash->{message} .= sprintf(
            " \n\n[ Complaint about report %d - %s - %s ]",
            $c->stash->{problem}->id,
            $problem_url, $admin_url
        );
    }

    return 1;
}

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

    $c->stash->{contact_email} = $c->cobrand->contact_email;
    $c->stash->{contact_email} =~ s/\@/@/;

    for my $param (qw/em subject message/) {
        $c->stash->{$param} = $c->req->param($param);
    }

    # name is already used in the stash for the app class name
    $c->stash->{form_name} = $c->req->param('name');

    return 1;
}

=head1 AUTHOR

Struan Donald

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;