aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB/Result/Problem.pm
blob: ffc6aba93701ef4a3c5ead0f41020cfe6cd890ce (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
use utf8;
package FixMyStreet::DB::Result::Problem;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use strict;
use warnings;

use base 'DBIx::Class::Core';
__PACKAGE__->load_components("FilterColumn", "InflateColumn::DateTime", "EncodedColumn");
__PACKAGE__->table("problem");
__PACKAGE__->add_columns(
  "id",
  {
    data_type         => "integer",
    is_auto_increment => 1,
    is_nullable       => 0,
    sequence          => "problem_id_seq",
  },
  "postcode",
  { data_type => "text", is_nullable => 0 },
  "title",
  { data_type => "text", is_nullable => 0 },
  "detail",
  { data_type => "text", is_nullable => 0 },
  "photo",
  { data_type => "bytea", is_nullable => 1 },
  "name",
  { data_type => "text", is_nullable => 0 },
  "created",
  {
    data_type     => "timestamp",
    default_value => \"current_timestamp",
    is_nullable   => 0,
    original      => { default_value => \"now()" },
  },
  "state",
  { data_type => "text", is_nullable => 0 },
  "whensent",
  { data_type => "timestamp", is_nullable => 1 },
  "used_map",
  { data_type => "boolean", is_nullable => 0 },
  "bodies_str",
  { data_type => "text", is_nullable => 1 },
  "anonymous",
  { data_type => "boolean", is_nullable => 0 },
  "category",
  { data_type => "text", default_value => "Other", is_nullable => 0 },
  "confirmed",
  { data_type => "timestamp", is_nullable => 1 },
  "send_questionnaire",
  { data_type => "boolean", default_value => \"true", is_nullable => 0 },
  "lastupdate",
  {
    data_type     => "timestamp",
    default_value => \"current_timestamp",
    is_nullable   => 0,
    original      => { default_value => \"now()" },
  },
  "areas",
  { data_type => "text", is_nullable => 0 },
  "service",
  { data_type => "text", default_value => "", is_nullable => 0 },
  "lang",
  { data_type => "text", default_value => "en-gb", is_nullable => 0 },
  "cobrand",
  { data_type => "text", default_value => "", is_nullable => 0 },
  "cobrand_data",
  { data_type => "text", default_value => "", is_nullable => 0 },
  "latitude",
  { data_type => "double precision", is_nullable => 0 },
  "longitude",
  { data_type => "double precision", is_nullable => 0 },
  "external_id",
  { data_type => "text", is_nullable => 1 },
  "external_body",
  { data_type => "text", is_nullable => 1 },
  "external_team",
  { data_type => "text", is_nullable => 1 },
  "user_id",
  { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
  "flagged",
  { data_type => "boolean", default_value => \"false", is_nullable => 0 },
  "extra",
  { data_type => "text", is_nullable => 1 },
  "geocode",
  { data_type => "bytea", is_nullable => 1 },
  "send_fail_count",
  { data_type => "integer", default_value => 0, is_nullable => 0 },
  "send_fail_reason",
  { data_type => "text", is_nullable => 1 },
  "send_fail_timestamp",
  { data_type => "timestamp", is_nullable => 1 },
  "send_method_used",
  { data_type => "text", is_nullable => 1 },
  "non_public",
  { data_type => "boolean", default_value => \"false", is_nullable => 1 },
  "external_source",
  { data_type => "text", is_nullable => 1 },
  "external_source_id",
  { data_type => "text", is_nullable => 1 },
  "interest_count",
  { data_type => "integer", default_value => 0, is_nullable => 1 },
  "subcategory",
  { data_type => "text", is_nullable => 1 },
  "bodies_missing",
  { data_type => "text", is_nullable => 1 },
  "response_priority_id",
  { data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
  "defect_type_id",
  { data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->has_many(
  "comments",
  "FixMyStreet::DB::Result::Comment",
  { "foreign.problem_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);
__PACKAGE__->belongs_to(
  "defect_type",
  "FixMyStreet::DB::Result::DefectType",
  { id => "defect_type_id" },
  {
    is_deferrable => 0,
    join_type     => "LEFT",
    on_delete     => "NO ACTION",
    on_update     => "NO ACTION",
  },
);
__PACKAGE__->has_many(
  "moderation_original_datas",
  "FixMyStreet::DB::Result::ModerationOriginalData",
  { "foreign.problem_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);
__PACKAGE__->has_many(
  "questionnaires",
  "FixMyStreet::DB::Result::Questionnaire",
  { "foreign.problem_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);
__PACKAGE__->belongs_to(
  "response_priority",
  "FixMyStreet::DB::Result::ResponsePriority",
  { id => "response_priority_id" },
  {
    is_deferrable => 0,
    join_type     => "LEFT",
    on_delete     => "NO ACTION",
    on_update     => "NO ACTION",
  },
);
__PACKAGE__->belongs_to(
  "user",
  "FixMyStreet::DB::Result::User",
  { id => "user_id" },
  { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
);
__PACKAGE__->has_many(
  "user_planned_reports",
  "FixMyStreet::DB::Result::UserPlannedReport",
  { "foreign.report_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);


# Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-02-13 15:11:11
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:8zzWlJX7OQOdvrGxKuZUmg

# Add fake relationship to stored procedure table
__PACKAGE__->has_one(
  "nearby",
  "FixMyStreet::DB::Result::Nearby",
  { "foreign.problem_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

# This will return the oldest moderation_original_data, if any.
# The plural can be used to return all entries.
__PACKAGE__->might_have(
  "moderation_original_data",
  "FixMyStreet::DB::Result::ModerationOriginalData",
  { "foreign.problem_id" => "self.id" },
  { where => { 'comment_id' => undef },
    order_by => 'id',
    rows => 1,
    cascade_copy => 0, cascade_delete => 1 },
);

__PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn");
__PACKAGE__->rabx_column('extra');
__PACKAGE__->rabx_column('geocode');

use Moo;
use namespace::clean -except => [ 'meta' ];
use Utils;
use FixMyStreet::Map::FMS;
use LWP::Simple qw($ua);
use RABX;
use URI;
use URI::QueryParam;

my $IM = eval {
    require Image::Magick;
    Image::Magick->import;
    1;
};

with 'FixMyStreet::Roles::Abuser',
     'FixMyStreet::Roles::Extra',
     'FixMyStreet::Roles::Translatable',
     'FixMyStreet::Roles::PhotoSet';

=head2

    @states = FixMyStreet::DB::Problem::open_states();

Get a list or states that are regarded as open. If called in
array context then returns an array of names, otherwise returns a
HASHREF.

=cut

sub open_states {
    my @states = map { $_->label } @{FixMyStreet::DB->resultset("State")->open};
    return wantarray ? @states : { map { $_ => 1 } @states };
}

=head2

    @states = FixMyStreet::DB::Problem::fixed_states();

Get a list or states that should be regarded as fixed. If called in
array context then returns an array of names, otherwise returns a
HASHREF.

=cut

sub fixed_states {
    my @states = map { $_->label } @{FixMyStreet::DB->resultset("State")->fixed};
    push @states, 'fixed - user', 'fixed - council' if @states;
    return wantarray ? @states : { map { $_ => 1 } @states };
}

=head2

    @states = FixMyStreet::DB::Problem::closed_states();

Get a list or states that should be regarded as closed. If called in
array context then returns an array of names, otherwise returns a
HASHREF.

=cut

sub closed_states {
    my @states = map { $_->label } @{FixMyStreet::DB->resultset("State")->closed};
    return wantarray ? @states : { map { $_ => 1 } @states };
}

=head2

    @states = FixMyStreet::DB::Problem::all_states();

Get a list of all states that a problem can have. If called in
array context then returns an array of names, otherwise returns a
HASHREF.

=cut

sub all_states {
    my $states = {
        'hidden'                      => 1,
        'partial'                     => 1,
        'unconfirmed'                 => 1,
        'fixed - council'             => 1,
        'fixed - user'                => 1,
    };
    map { $states->{$_->label} = 1 } @{FixMyStreet::DB->resultset("State")->states};
    return wantarray ? keys %{$states} : $states;
}

=head2

    @visible_states = FixMyStreet::DB::Problem::visible_states();
    @hidden_states  = FixMyStreet::DB::Problem::hidden_states();

Get a list of states that should be visible (or hidden) on the site. If called
in array context then returns an array of names, otherwise returns a HASHREF.

=cut

my $hidden_states = {
    'hidden' => 1,
    'partial' => 1,
    'unconfirmed' => 1,
};

sub hidden_states {
    return wantarray ? keys %{$hidden_states} : $hidden_states;
}

sub visible_states {
    my %visible_states = map {
        $hidden_states->{$_} ? () : ($_ => 1)
    } all_states();
    return wantarray ? keys %visible_states : \%visible_states;
}

sub visible_states_add {
    my ($self, @states) = @_;
    for my $state (@states) {
        delete $hidden_states->{$state};
    }
}

sub visible_states_remove {
    my ($self, @states) = @_;
    for my $state (@states) {
        $hidden_states->{$state} = 1;
    }
}

my $stz = sub {
    my ( $orig, $self ) = ( shift, shift );
    my $s = $self->$orig(@_);
    return $s unless $s && UNIVERSAL::isa($s, "DateTime");
    FixMyStreet->set_time_zone($s);
    return $s;
};

around created => $stz;
around confirmed => $stz;
around whensent => $stz;
around lastupdate => $stz;

around service => sub {
    my ( $orig, $self ) = ( shift, shift );
    # service might be undef if e.g. unsaved code report
    my $s = $self->$orig(@_) || "";
    $s =~ s/_/ /g;
    return $s;
};

sub title_safe {
    my $self = shift;
    return _('Awaiting moderation') if $self->cobrand eq 'zurich' && $self->state eq 'submitted';
    return $self->title;
}

=head2 check_for_errors

    $error_hashref = $problem->check_for_errors();

Look at all the fields and return a hashref with all errors found, keyed on the
field name. This is intended to be passed back to the form to display the
errors.

TODO - ideally we'd pass back error codes which would be humanised in the
templates (eg: 'missing','email_not_valid', etc).

=cut

sub check_for_errors {
    my $self = shift;

    my %errors = ();

    $errors{title} = _('Please enter a subject')
      unless $self->title =~ m/\S/;

    $errors{detail} = _('Please enter some details')
      unless $self->detail =~ m/\S/;

    $errors{bodies} = _('No council selected')
      unless $self->bodies_str
          && $self->bodies_str =~ m/^(?:-1|[\d,]+)$/;

    if ( !$self->name || $self->name !~ m/\S/ ) {
        $errors{name} = _('Please enter your name');
    }

    if (   $self->category
        && $self->category eq _('-- Pick a category --') )
    {
        $errors{category} = _('Please choose a category');
        $self->category(undef);
    }

    return \%errors;
}

=head2 confirm

    $bool = $problem->confirm(  );
    $problem->update;


Set the state to 'confirmed' and put current time into 'confirmed' field. This
is a no-op if the report is already confirmed.

NOTE - does not update storage - call update or insert to do that.

=cut

sub confirm {
    my $self = shift;

    return if $self->state eq 'confirmed';

    $self->state('confirmed');
    $self->confirmed( \'current_timestamp' );
    return 1;
}

sub category_display {
    my $self = shift;
    $self->translate_column('category');
}

sub bodies_str_ids {
    my $self = shift;
    return [] unless $self->bodies_str;
    my @bodies = split( /,/, $self->bodies_str );
    return \@bodies;
}

=head2 bodies

Returns a hashref of bodies to which a report was sent.

=cut

has bodies => (
    is => 'ro',
    lazy => 1,
    default => sub {
        my $self = shift;
        return {} unless $self->bodies_str;
        my $cache = $self->result_source->schema->cache;
        return $cache->{bodies}{$self->bodies_str} if $cache->{bodies}{$self->bodies_str};

        my $bodies = $self->bodies_str_ids;
        my @bodies = $self->result_source->schema->resultset('Body')->search(
            { id => $bodies },
            { prefetch => 'body_areas' },
        )->all;
        $cache->{bodies}{$self->bodies_str} = { map { $_->id => $_ } @bodies };
        return $cache->{bodies}{$self->bodies_str};
    },
);

sub body_names($) {
    my $self = shift;
    my $bodies = $self->bodies;
    my @names = map { $_->name } values %$bodies;
    return \@names;
}

sub to_body_named($$) {
    my ($self, $re) = @_;
    my $names = join(',,', @{$self->body_names});
    $names =~ /$re/;
}

=head2 url

Returns a URL for this problem report.

=cut

sub url {
    my $self = shift;
    return "/report/" . $self->id;
}

sub admin_url {
    my ($self, $cobrand) = @_;
    return $cobrand->admin_base_url . '/report_edit/' . $self->id;
}

=head2 tokenised_url

Return a url for this problem report that logs a user in

=cut

sub tokenised_url {
    my ($self, $user, $params) = @_;

    my %params;
    if ($user->email_verified) {
        $params{email} = $user->email;
    } elsif ($user->phone_verified) {
        $params{phone} = $user->phone;
        # This is so the email token can look up/ log in a phone user
        $params{login_type} = 'phone';
    }

    my $token = FixMyStreet::App->model('DB::Token')->create(
        {
            scope => 'email_sign_in',
            data  => {
                %params,
                id    => $self->id,
                r     => $self->url,
                p     => $params,
            }
        }
    );

    return "/M/". $token->token;
}

=head2 is_hidden

Returns 1 if the problem is in an hidden state otherwise 0.

=cut

sub is_hidden {
    my $self = shift;

    return exists $self->hidden_states->{ $self->state } ? 1 : 0;
}

=head2 is_open

Returns 1 if the problem is in a open state otherwise 0.

=cut

sub is_open {
    my $self = shift;

    return exists $self->open_states->{ $self->state } ? 1 : 0;
}

=head2 is_in_progress

Sees if the problem is in an open, not 'confirmed' state.

=cut

sub is_in_progress {
    my $self = shift;
    return $self->is_open && $self->state ne 'confirmed' ? 1 : 0;
}

=head2 is_fixed

Returns 1 if the problem is in a fixed state otherwise 0.

=cut

sub is_fixed {
    my $self = shift;

    return exists $self->fixed_states->{ $self->state } ? 1 : 0;
}

=head2 is_closed

Returns 1 if the problem is in a closed state otherwise 0.

=cut

sub is_closed {
    my $self = shift;

    return exists $self->closed_states->{ $self->state } ? 1 : 0;
}

=head2 is_visible

Returns 1 if the problem should be displayed on the site otherwise 0.

=cut

sub is_visible {
    my $self = shift;

    return exists $self->visible_states->{ $self->state } ? 1 : 0;
}

=head2 meta_line

Returns a string to be used on a problem report page, describing some of the
meta data about the report.

=cut

sub meta_line {
    my ( $problem, $c ) = @_;

    my $date_time = Utils::prettify_dt( $problem->confirmed );
    my $meta = '';

    my $category = $problem->category_display;
    $category = $c->cobrand->call_hook(change_category_text => $category) || $category;

    if ( $problem->anonymous ) {
        if ( $problem->service and $category && $category ne _('Other') ) {
            $meta =
            sprintf( _('Reported via %s in the %s category anonymously at %s'),
                $problem->service, $category, $date_time );
        } elsif ( $problem->service ) {
            $meta = sprintf( _('Reported via %s anonymously at %s'),
                $problem->service, $date_time );
        } elsif ( $category and $category ne _('Other') ) {
            $meta = sprintf( _('Reported in the %s category anonymously at %s'),
                $category, $date_time );
        } else {
            $meta = sprintf( _('Reported anonymously at %s'), $date_time );
        }
    } else {
        my $problem_name = $problem->name;

        if ($c->user_exists and
            $c->user->has_permission_to('view_body_contribute_details', $problem->bodies_str_ids) and
            $problem->name ne $problem->user->name) {
            $problem_name = sprintf('%s (%s)', $problem->name, $problem->user->name );
        }

        if ( $problem->service and $category && $category ne _('Other') ) {
            $meta = sprintf(
                _('Reported via %s in the %s category by %s at %s'),
                $problem->service, $category,
                $problem_name,    $date_time
            );
        } elsif ( $problem->service ) {
            $meta = sprintf( _('Reported via %s by %s at %s'),
                $problem->service, $problem_name, $date_time );
        } elsif ( $category and $category ne _('Other') ) {
            $meta = sprintf( _('Reported in the %s category by %s at %s'),
                $category, $problem_name, $date_time );
        } else {
            $meta = sprintf( _('Reported by %s at %s'), $problem_name, $date_time );
        }
    }

    return $meta;
}

sub nearest_address {
    my $self = shift;

    return '' unless $self->geocode;

    my $address = $self->geocode->{resourceSets}[0]{resources}[0];
    return $address->{name};
}

sub body {
    my ( $problem, $c ) = @_;
    my $body;
    if ($problem->external_body) {
        if ($problem->cobrand eq 'zurich') {
            my $cache = $problem->result_source->schema->cache;
            return $cache->{bodies}{$problem->external_body} //= $c->model('DB::Body')->find({ id => $problem->external_body });
        } else {
            $body = $problem->external_body;
        }
    } else {
        my $bodies = $problem->bodies;
        my @body_names = sort map {
            my $name = $_->name;
            if ($c and FixMyStreet->config('AREA_LINKS_FROM_PROBLEMS')) {
                '<a href="' . $_->url . '">' . $name . '</a>';
            } else {
                $name;
            }
        } values %$bodies;
        if ( scalar @body_names > 2 ) {
            $body = join( ', ', splice @body_names, 0, -1);
            $body = join( ',' . _(' and '), ($body, $body_names[-1]));
        } else {
            $body = join( _(' and '), @body_names);
        }
    }
    return $body;
}


=head2 time_ago
  Returns how long ago a problem was reported in an appropriately
  prettified duration, depending on the duration.
=cut

sub time_ago {
    my ( $self, $date ) = @_;
    $date ||= 'confirmed';
    my $duration = time() - $self->$date->epoch;

    return Utils::prettify_duration( $duration );
}

=head2 days_ago

  Returns how many days ago a problem was reported.

=cut

sub days_ago {
    my ( $self, $date ) = @_;
    $date ||= 'confirmed';
    my $now = DateTime->now( time_zone => FixMyStreet->time_zone || FixMyStreet->local_time_zone );
    my $duration = $now->delta_days($self->$date);
    return $duration->delta_days;
}

=head2 response_templates

Returns all ResponseTemplates attached to this problem's bodies, in alphabetical
order of title.

=cut

sub response_templates {
    my $self = shift;
    return $self->result_source->schema->resultset('ResponseTemplate')->search(
        {
            'me.body_id' => $self->bodies_str_ids,
            'contact.category' => [ $self->category, undef ],
        },
        {
            order_by => 'title',
            join => { 'contact_response_templates' => 'contact' },
        }
    );
}

=head2 response_priorities

Returns all ResponsePriorities attached to this problem's category/contact, in
alphabetical order of name.

=cut

sub response_priorities {
    my $self = shift;
    return $self->result_source->schema->resultset('ResponsePriority')->for_bodies($self->bodies_str_ids, $self->category);
}

=head2 defect_types

Returns all DefectTypes attached to this problem's category/contact, in
alphabetical order of name.

=cut

sub defect_types {
    my $self = shift;
    return $self->result_source->schema->resultset('DefectType')->for_bodies($self->bodies_str_ids, $self->category);
}

# returns true if the external id is the council's ref, i.e., useful to publish it
# (by way of an example, the Oxfordshire send method returns a useful reference when
# it succeeds, so that is the ref we should show on the problem report page).
#     Future: this is installation-dependent so maybe should be using the contact
#             data to determine if the external id is public on a council-by-council basis.
#     Note:   this only makes sense when called on a problem that has been sent!
sub can_display_external_id {
    my $self = shift;
    if ($self->external_id && $self->send_method_used && $self->to_body_named('Oxfordshire|Lincolnshire')) {
        return 1;
    }
    return 0;
}

sub duration_string {
    my ( $problem, $c ) = @_;
    my $body = $c->cobrand->call_hook(link_to_council_cobrand => $problem) || $problem->body($c);
    my $handler = $c->cobrand->call_hook(get_body_handler_for_problem => $problem);
    if ( $handler && $handler->call_hook('is_council_with_case_management') ) {
        return sprintf(_('Received by %s moments later'), $body);
    }
    return unless $problem->whensent;
    return sprintf(_('Sent to %s %s later'), $body,
        Utils::prettify_duration($problem->whensent->epoch - $problem->confirmed->epoch, 'minute')
    );
}

sub local_coords {
    my $self = shift;
    my $cobrand = $self->get_cobrand_logged;
    if ($cobrand->moniker eq 'zurich') {
        my ($x, $y) = Geo::Coordinates::CH1903Plus::from_latlon($self->latitude, $self->longitude);
        return ( int($x+0.5), int($y+0.5) );
    } elsif ($cobrand->country eq 'GB') {
        my $coordsyst = 'G';
        $coordsyst = 'I' if FixMyStreet::Map::FMS::in_northern_ireland_box($self->latitude, $self->longitude);
        my ($x, $y) = Utils::convert_latlon_to_en( $self->latitude, $self->longitude, $coordsyst );
        return ($x, $y, $coordsyst);
    }
}

=head2 update_from_open311_service_request

    $p->update_from_open311_service_request( $request, $body, $system_user );

Updates the problem based on information in the passed in open311 request
(standard, not the extension that uses GetServiceRequestUpdates) . If the
request has an older update time than the problem's lastupdate time then
nothing happens.

Otherwise a comment will be created if there is status update text in the
open311 request. If the open311 request has a state of closed then the problem
will be marked as fixed.

NB: a comment will always be created if the problem is being marked as fixed.

Fixed problems will not be re-opened by this method.

=cut

sub update_from_open311_service_request {
    my ( $self, $request, $body, $system_user ) = @_;

    my ( $updated, $status_notes );

    if ( ! ref $request->{updated_datetime} ) {
        $updated = $request->{updated_datetime};
    }

    if ( ! ref $request->{status_notes} ) {
        $status_notes = $request->{status_notes};
    }

    my $update = $self->new_related(comments => {
        state => 'confirmed',
        created => $updated || \'current_timestamp',
        confirmed => \'current_timestamp',
        text => $status_notes,
        mark_open => 0,
        mark_fixed => 0,
        user => $system_user,
        anonymous => 0,
        name => $body->name,
    });

    my $w3c = DateTime::Format::W3CDTF->new;
    my $req_time = $w3c->parse_datetime( $request->{updated_datetime} );

    # set a timezone here as the $req_time will have one and if we don't
    # use a timezone then the date comparisons are invalid.
    # of course if local timezone is not the one that went into the data
    # base then we're also in trouble
    my $lastupdate = $self->lastupdate;
    $lastupdate->set_time_zone( FixMyStreet->local_time_zone );

    # update from open311 is older so skip
    if ( $req_time < $lastupdate ) {
        return 0;
    }

    if ( $request->{status} eq 'closed' ) {
        if ( $self->state ne 'fixed' ) {
            $self->state('fixed');
            $update->mark_fixed(1);

            if ( !$status_notes ) {
                # FIXME - better text here
                $status_notes = _('Closed by council');
            }
        }
    }

    if ( $status_notes ) {
        $update->text( $status_notes );
        $self->lastupdate( $req_time );
        $self->update;
        $update->insert;
    }

    return 1;
}

sub update_send_failed {
    my $self = shift;
    my $msg  = shift;

    $self->update( {
        send_fail_count => $self->send_fail_count + 1,
        send_fail_timestamp => \'current_timestamp',
        send_fail_reason => $msg
    } );
}

=head2 updates_sent_to_body

Returns 1 if updates left on this report will be sent to any of the receiving
bodies by some mechanism. Right now that mechanism is Open311.

=cut

sub updates_sent_to_body {
    my $self = shift;
    return unless $self->send_method_used && $self->send_method_used =~ /Open311/;

    # Some bodies only send updates *to* FMS, they don't receive updates.
    my $cobrand = $self->get_cobrand_logged;
    my $handler = $cobrand->call_hook(get_body_handler_for_problem => $self);
    return 0 if $handler && $handler->call_hook('open311_post_update_skip');

    my @bodies = values %{ $self->bodies };
    my @updates_sent = grep {
        $_->send_comments &&
        (
            $_->send_method eq 'Open311' ||
            $_->send_method eq 'Noop' # Sending might be temporarily disabled
        )
    } @bodies;
    return scalar @updates_sent;
}

sub add_send_method {
    my $self = shift;
    my $sender = shift;
    ($sender = ref $sender) =~ s/^.*:://;
    if (my $send_method = $self->send_method_used) {
        $self->send_method_used("$send_method,$sender");
    } else {
        $self->send_method_used($sender);
    }
}

sub resend {
    my $self = shift;
    $self->whensent(undef);
    $self->send_method_used(undef);
}

sub as_hashref {
    my ($self, $c, $cols) = @_;

    my $state_t = FixMyStreet::DB->resultset("State")->display($self->state);

    my $out = {
        id        => $self->id,
        title     => $self->title,
        category  => $self->category,
        detail    => $self->detail,
        latitude  => $self->latitude,
        longitude => $self->longitude,
        postcode  => $self->postcode,
        areas     => $self->areas,
        state     => $self->state,
        state_t   => $state_t,
        used_map  => $self->used_map,
        created   => $self->created,
    };
    $out->{is_fixed} = $self->fixed_states->{ $self->state } ? 1 : 0 if !$cols || $cols->{is_fixed};
    $out->{photos} = [ map { $_->{url} } @{$self->photos} ] if !$cols || $cols->{photos};
    $out->{meta} = $self->confirmed ? $self->meta_line( $c ) : '' if !$cols || $cols->{meta};
    $out->{created_pp} = $c->cobrand->prettify_dt( $self->created ) if !$cols || $cols->{created_pp};
    if ($self->confirmed) {
        $out->{confirmed} = $self->confirmed if !$cols || $cols->{confirmed};
        $out->{confirmed_pp} = $c->cobrand->prettify_dt( $self->confirmed ) if !$cols || $cols->{confirmed_pp};
    }
    return $out;
}

=head2 latest_moderation_log_entry

Return most recent ModerationLog object

=cut

sub latest_moderation_log_entry {
    my $self = shift;
    return $self->admin_log_entries->search({ action => 'moderation' }, { order_by => { -desc => 'id' } })->first;
}

__PACKAGE__->has_many(
  "admin_log_entries",
  "FixMyStreet::DB::Result::AdminLog",
  { "foreign.object_id" => "self.id" },
  {
      cascade_copy => 0, cascade_delete => 0,
      where => { 'object_type' => 'problem' },
  }
);

sub get_time_spent {
    my $self = shift;
    my $admin_logs = $self->admin_log_entries->search({},
        {
            group_by => 'object_id',
            columns => [
                { sum_time_spent => { sum => 'time_spent' } },
            ]
        })->single;
    return $admin_logs ? $admin_logs->get_column('sum_time_spent') : 0;
}

=head2 get_cobrand_logged

Get a cobrand object for the cobrand the problem was logged for.

e.g. if a problem was logged at www.fixmystreet.com, this will be a
FixMyStreet::Cobrand::FixMyStreet object.

=cut

has get_cobrand_logged => (
    is => 'ro',
    lazy => 1,
    default => sub {
        my $self = shift;
        my $cobrand_class = FixMyStreet::Cobrand->get_class_for_moniker( $self->cobrand );
        return $cobrand_class->new;
    },
);


sub pin_data {
    my ($self, $c, $page, %opts) = @_;
    my $colour = $c->cobrand->pin_colour($self, $page);
    my $title = $opts{private} ? $self->title : $self->title_safe;
    $title = $c->cobrand->call_hook(pin_hover_title => $self, $title) || $title;
    {
        latitude => $self->latitude,
        longitude => $self->longitude,
        colour => $colour,
        id => $self->id,
        title => $title,
        problem => $self,
        draggable => $opts{draggable},
        type => $opts{type},
    }
};

sub static_map {
    my ($self) = @_;

    return unless $IM;

    my $orig_map_class = FixMyStreet::Map::set_map_class('OSM')
        unless $FixMyStreet::Map::map_class->isa("FixMyStreet::Map::OSM");

    my $map_data = $FixMyStreet::Map::map_class->generate_map_data(
        { cobrand => $self->get_cobrand_logged },
        latitude  => $self->latitude,
        longitude => $self->longitude,
        pins      => $self->used_map
        ? [ {
            latitude  => $self->latitude,
            longitude => $self->longitude,
            colour    => $self->get_cobrand_logged->pin_colour($self, 'report'),
            type      => 'big',
          } ]
        : [],
    );

    $ua->agent("FixMyStreet/1.0");
    my $image;
    for (my $i=0; $i<4; $i++) {
        my $tile_url = $map_data->{tiles}->[$i];
        if ($tile_url =~ m{^//}) {
            $tile_url = "https:$tile_url";
        }
        my $tile;
        if (FixMyStreet->test_mode) {
            $tile = FixMyStreet->path_to('t/app/controller/sample.jpg')->slurp(iomode => '<:raw');
        } else {
            $tile = LWP::Simple::get($tile_url);
        }
        next unless $tile;
        my $im = Image::Magick->new;
        $im->BlobToImage($tile);
        my $gravity = ($i<2?'North':'South') . ($i%2?'East':'West');
        if (!$image) {
            $image = $im;
            $image->Extent(geometry => '512x512', gravity => $gravity);
        } else {
            $image->Composite(image => $im, gravity => $gravity);
        }
    }

    unless ($image) {
        FixMyStreet::Map::set_map_class($orig_map_class) if $orig_map_class;
        return;
    }

    # The only pin might be the report pin, with added x/y
    my $pin = $map_data->{pins}->[0];
    if ($pin) {
        my $im = Image::Magick->new;
        $im->read(FixMyStreet->path_to('web', 'i', 'pin-yellow.png'));
        $im->Scale( geometry => '48x64' );
        $image->Composite(image => $im, gravity => 'NorthWest',
            x => $pin->{px} - 24, y => $pin->{py} - 64);
    }

    # Bottom 128/ top 64 pixels will never have a pin
    $image->Extent( geometry => '512x384', gravity => 'NorthWest');
    $image->Extent( geometry => '512x320', gravity => 'SouthWest');

    $image->Scale( geometry => "310x200>" );

    my @blobs = $image->ImageToBlob(magick => 'jpeg');
    undef $image;

    FixMyStreet::Map::set_map_class($orig_map_class) if $orig_map_class;

    return {
        data => $blobs[0],
        content_type => 'image/jpeg',
    };
}

has shortlisted_user => (
    is => 'ro',
    lazy => 1,
    default => sub {
        my $self = shift;
        my $user = $self->user_planned_reports->active->first;
        return $user->user if $user;
    },
);

sub set_duplicate_of {
    my ($self, $other_id) = @_;
    $self->set_extra_metadata( duplicate_of => $other_id );
    my $dupe = $self->result_source->schema->resultset("Problem")->find($other_id);
    my $dupes_duplicates = $dupe->get_extra_metadata('duplicates') || [];
    push @$dupes_duplicates, $self->id;
    $dupe->set_extra_metadata( duplicates => $dupes_duplicates );
    $dupe->update;
}

has duplicate_of => (
    is => 'ro',
    lazy => 1,
    default => sub {
        my $self = shift;
        return unless $self->state eq 'duplicate';
        my $duplicate_of = int($self->get_extra_metadata("duplicate_of") || 0);
        return unless $duplicate_of;
        return $self->result_source->schema->resultset('Problem')->search({ id => $duplicate_of })->first;
    },
);

has duplicates => (
    is => 'ro',
    lazy => 1,
    default => sub {
        my $self = shift;
        my $duplicates = $self->get_extra_metadata("duplicates") || [];
        return [] unless $duplicates && @$duplicates;
        my @duplicates = $self->result_source->schema->resultset('Problem')->search({ id => $duplicates })->all;
        return \@duplicates;
    },
);

has traffic_management_options => (
    is => 'ro',
    lazy => 1,
    default => sub {
        my $self = shift;
        my $cobrand = $self->get_cobrand_logged;
        $cobrand = $cobrand->call_hook(get_body_handler_for_problem => $self) || $cobrand;
        return $cobrand->traffic_management_options;
    },
);

has inspection_log_entry => (
    is => 'ro',
    lazy => 1,
    default => sub {
        my $self = shift;
        return $self->admin_log_entries->search({ action => 'inspected' }, { order_by => { -desc => 'whenedited' } })->first;
    },
);

1;