Passed
Pull Request — master (#1932)
by Struan
04:51
created

AddTermView::add_example_urls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
nc 2
nop 1
dl 0
loc 24
rs 9.7666
c 1
b 0
f 0
1
<?php
2
3
# For adding glossary terms
4
5
namespace MySociety\TheyWorkForYou\GlossaryView;
6
7
class AddTermView extends BaseView {
8
    public $glossary;
9
10
    public function display(): array {
11
        global $THEUSER;
12
        if (!$this->has_edit_access()) {
13
            return ['error' => _("You don't have permission to manage the glossary")];
14
        }
15
16
        $data = [];
17
        $term = filter_user_input(get_http_var('g'), 'strict');
18
        // glossary matches should always be quoted.
19
        // Just to be sure, we'll strip away any quotes and add them again.
20
        if (preg_match("/^(\"|\')/", $term)) {
21
            $term = preg_replace("/\"|\'/", "", $term);
22
        }
23
        $data['term'] = $term;
24
        $data['title'] = $term;
25
26
        $this->glossary = new \GLOSSARY(['s' => $term]);
27
        $data['glossary'] = $this->glossary;
28
29
        if (get_http_var('submitterm') != '') {
30
            $data = $this->add_glossary_entry($data);
31
        } elseif ((get_http_var('g') != '') && (get_http_var('previewterm') == '')) {
32
            $data = $this->check_glossary_entry($data);
33
        } elseif (get_http_var('previewterm') != '') {
34
            $data['contributing_user'] = $THEUSER->firstname . " " . $THEUSER->lastname;
35
            $data['definition_raw'] = get_http_var('definition');
36
            $data['definition'] = $this->format_body($data['definition_raw']);
37
            $data['preview'] = 1;
38
        } else {
39
            $data = $this->add_example_urls($data);
40
        }
41
42
        $URL = new \MySociety\TheyWorkForYou\Url('glossary_addterm');
43
        $data['form_url'] = $URL->generate();
44
45
        return $data;
46
    }
47
48
    protected function has_stop_words(): bool {
49
        if (in_array($this->glossary->query, $this->glossary->stopwords)) {
50
            return true;
51
        }
52
        return false;
53
    }
54
55
    protected function check_term_is_useful(array $data): array {
56
        if ($this->has_stop_words()) {
57
            $data['error'] = 'Sorry, that phrase appears too many times to be a useful as a link within the parliamentary record.';
58
        } elseif (isset($data['appearances']) && !$data['appearances']) {
59
            $data['error'] = "Unfortunately <strong>" . $data['term'] . "</strong>, doesn't seem to appear in hansard at all...</p>";
60
        } elseif ($this->glossary->num_search_matches > 0) {
61
            $data['show_matches'] = 1;
62
            $data['error'] = 'Existing matches';
63
            $data['count'] = $this->glossary->num_search_matches;
64
        }
65
66
        return $data;
67
    }
68
69
    protected function get_appearance_count(string $term): int {
70
        global $SEARCHENGINE;
71
        $SEARCHENGINE = new \SEARCHENGINE($term);
72
        $count = $SEARCHENGINE->run_count(0, 10000);
73
        return $count;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $count could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
74
    }
75
76
    protected function check_glossary_entry(array $data): array {
77
        $data['appearances'] = $this->get_appearance_count($data['term']);
78
        $data['definition_raw'] = '';
79
        $data = $this->check_term_is_useful($data);
80
81
        if (!isset($data['error'])) {
82
            $data['definition_raw'] = '';
83
            $list = new \HANSARDLIST();
84
            $examples = $list->display('search', [
85
                'num' => 5,
86
                's' => $data['term'],
87
                'view_override' => 'glossary_search',
88
            ], 'none');
89
            $data['examples'] = $examples['rows'];
90
        }
91
92
        return $data;
93
    }
94
95
    protected function add_glossary_entry(array $data): array {
96
        $data['submitted'] = 1;
97
        $data['body'] = get_http_var('definition');
98
        $data = $this->check_term_is_useful($data);
99
100
        $success = false;
101
        if (!isset($data['error'])) {
102
            $entry = [
0 ignored issues
show
Unused Code introduced by
The assignment to $entry is dead and can be removed.
Loading history...
103
                'title' => $data['term'],
104
                'body'  => $data['body'],
105
            ];
106
            $success = $this->glossary->create($data);
107
        }
108
109
        if (is_int($success)) {
110
            $data['success'] = 1;
111
        } elseif (is_array($success)) {
112
            $data = array_merge($data, $success);
113
        } else {
114
            if (!isset($data['error'])) {
115
                $data['error'] = "Sorry, there was an error and we were unable to add your Glossary item.";
116
            }
117
        }
118
119
        return $data;
120
    }
121
122
    protected function add_example_urls(array $data): array {
123
        $URL = new \MySociety\TheyWorkForYou\Url('glossary');
124
125
        $examples = [
126
            'technical' => [
127
                'name' => 'Early Day Motion', 'id' => 90,
128
            ],
129
            'organisation' => [
130
                'name' => 'Devon County Council', 'id' => 12,
131
            ],
132
            'document' => [
133
                'name' => 'Hutton Report', 'id' => 7,
134
            ],
135
        ];
136
137
        $example_urls = [];
138
        foreach ($examples as $name => $example) {
139
            $URL->insert(["gl" => $example['id']]);
140
            $example['url'] = $URL->generate();
141
            $example_urls[$name] = $example;
142
        }
143
144
        $data['example_urls'] = $example_urls;
145
        return $data;
146
    }
147
148
}
149