aboutsummaryrefslogtreecommitdiffstats
path: root/bin/fixmystreet.com/add_emergency_message
blob: 3cfeae83e75f5df949e368e9fa2f6d75f92057f5 (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
#!/usr/bin/env perl
#
# One off script to add emergency messages to a council

use strict;
use warnings;
use v5.14;

BEGIN {
    use File::Basename qw(dirname);
    use File::Spec;
    my $d = dirname(File::Spec->rel2abs($0));
    require "$d/../../setenv.pl";
}

use Getopt::Long::Descriptive;
use Term::ANSIColor;
use FixMyStreet::DB;

my ($opts, $usage) = describe_options(
    '%c %o',
    ['commit', 'whether to commit changes to the database' ],
    ['body=s', 'name of body to attach question to', { required => 1 } ],
    ['mode' => 'hidden' => { one_of => [
        ['questions' => 'add an emergency question'],
        ['category' => 'add an emergency message'],
    ], required => 1 }],
    ['update' => 'only update existing messages'],
    ['code=s', 'code to use for question'],
    ['question=s', 'question to ask user first'],
    ['yes=s', 'yes answer to question'],
    ['no=s', 'no answer to question'],
    ['message=s', 'message to be shown if form disabled', { required => 1 } ],
    ['send_method=s', 'send method to restrict categories to' ],
    ['help|h', "print usage message and exit" ],
);
$usage->die if $opts->help;

if (!$opts->commit) {
    say colored("*** DRY RUN ***", 'cyan');
}

my $field;

if ( $opts->mode eq 'questions' ) {
    die "questions, code, yes and no required"
        unless $opts->questions && $opts->code && $opts->yes && $opts->no;

    $field = {
        order => 0,
        required => 'true',
        protected => 'true',
        code => $opts->code,
        description => $opts->question,
        datatype => 'singlevaluelist',
        variable => 'true',
        values => [
            {
                key => 'yes',
                name => $opts->yes,
                disable => 1,
                disable_message => $opts->message,
            },
            {
                key => 'no',
                name => $opts->no,
            }
        ],
    };
} else {
    $field = {
        order => 0,
        protected => 'true',
        disable_form => 'true',
        code => '_fms_disable_',
        description => $opts->message,
        variable => 'false',
    };
}

my $body = FixMyStreet::DB->resultset("Body")->find({ name => $opts->body });
unless ($body) {
    say STDERR "Could not find body " . $opts->body;
    exit 1;
}

my $contacts = $body->contacts->not_deleted;
$contacts = $contacts->search({ send_method => $opts->send_method }) if $opts->send_method;
foreach my $category ($contacts->all) {
    my $found = $category->get_extra_field(code => $field->{code});
    if ($found) {
        say colored("Updating ", 'red') . $field->{code} . " message disable form on " . $category->category . ", " . $opts->body;
        $category->update_extra_field($field);
    } elsif (!$opts->update) {
        say colored("Making ", 'green') . $field->{code} . " message disable form on " . $category->category . ", " . $opts->body;
        $category->update_extra_field($field);
    }

    if ($opts->commit) {
        $category->update({
            editor => $0,
            whenedited => \'current_timestamp',
            note => $opts->code . ' extra field updated by script',
        });
    }
}