1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
# For adding glossary terms |
4
|
|
|
|
5
|
|
|
namespace MySociety\TheyWorkForYou\GlossaryView; |
6
|
|
|
|
7
|
|
|
class AddTermView { |
8
|
|
|
public $glossary; |
9
|
|
|
|
10
|
|
|
public function display(): array { |
11
|
|
|
global $THEUSER; |
12
|
|
|
if (!$this->has_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
|
|
|
$Parsedown = new \Parsedown(); |
37
|
|
|
$Parsedown->setSafeMode(true); |
38
|
|
|
$data['definition'] = $Parsedown->text($data['definition_raw']); |
39
|
|
|
$data['preview'] = 1; |
40
|
|
|
} else { |
41
|
|
|
$data = $this->add_example_urls($data); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$URL = new \MySociety\TheyWorkForYou\Url('glossary_addterm'); |
45
|
|
|
$data['form_url'] = $URL->generate(); |
46
|
|
|
|
47
|
|
|
return $data; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function has_access(): bool { |
51
|
|
|
global $THEUSER; |
52
|
|
|
|
53
|
|
|
if (!$THEUSER->is_able_to('addterm')) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function has_stop_words(): bool { |
61
|
|
|
if (in_array($this->glossary->query, $this->glossary->stopwords)) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function check_term_is_useful(array $data): array { |
68
|
|
|
if ($this->has_stop_words()) { |
69
|
|
|
$data['error'] = 'Sorry, that phrase appears too many times to be a useful as a link within the parliamentary record.'; |
70
|
|
|
} elseif (isset($data['appearances']) && !$data['appearances']) { |
71
|
|
|
$data['error'] = "Unfortunately <strong>" . $data['term'] . "</strong>, doesn't seem to appear in hansard at all...</p>"; |
72
|
|
|
} elseif ($this->glossary->num_search_matches > 0) { |
73
|
|
|
$data['show_matches'] = 1; |
74
|
|
|
$data['error'] = 'Existing matches'; |
75
|
|
|
$data['count'] = $this->glossary->num_search_matches; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $data; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function get_appearance_count(string $term): int { |
82
|
|
|
global $SEARCHENGINE; |
83
|
|
|
$SEARCHENGINE = new \SEARCHENGINE($term); |
84
|
|
|
$count = $SEARCHENGINE->run_count(0, 10000); |
85
|
|
|
return $count; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function check_glossary_entry(array $data): array { |
89
|
|
|
$data['appearances'] = $this->get_appearance_count($data['term']); |
90
|
|
|
$data['definition_raw'] = ''; |
91
|
|
|
$data = $this->check_term_is_useful($data); |
92
|
|
|
|
93
|
|
|
if (!isset($data['error'])) { |
94
|
|
|
$data['definition_raw'] = ''; |
95
|
|
|
$list = new \HANSARDLIST(); |
96
|
|
|
$examples = $list->display('search', [ |
97
|
|
|
'num' => 5, |
98
|
|
|
's' => $data['term'], |
99
|
|
|
'view_override' => 'glossary_search', |
100
|
|
|
], 'none'); |
101
|
|
|
$data['examples'] = $examples['rows']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $data; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function add_glossary_entry(array $data): array { |
108
|
|
|
$data['submitted'] = 1; |
109
|
|
|
$data['body'] = get_http_var('definition'); |
110
|
|
|
$data = $this->check_term_is_useful($data); |
111
|
|
|
|
112
|
|
|
$success = false; |
113
|
|
|
if (!isset($data['error'])) { |
114
|
|
|
$entry = [ |
|
|
|
|
115
|
|
|
'title' => $data['term'], |
116
|
|
|
'body' => $data['body'], |
117
|
|
|
]; |
118
|
|
|
$success = $this->glossary->create($data); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (is_int($success)) { |
122
|
|
|
$data['success'] = 1; |
123
|
|
|
} elseif (is_array($success)) { |
124
|
|
|
$data = array_merge($data, $success); |
125
|
|
|
} else { |
126
|
|
|
if (!isset($data['error'])) { |
127
|
|
|
$data['error'] = "Sorry, there was an error and we were unable to add your Glossary item."; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $data; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function add_example_urls(array $data): array { |
135
|
|
|
$URL = new \MySociety\TheyWorkForYou\Url('glossary'); |
136
|
|
|
|
137
|
|
|
$examples = [ |
138
|
|
|
'technical' => [ |
139
|
|
|
'name' => 'Early Day Motion', 'id' => 90, |
140
|
|
|
], |
141
|
|
|
'organisation' => [ |
142
|
|
|
'name' => 'Devon County Council', 'id' => 12, |
143
|
|
|
], |
144
|
|
|
'document' => [ |
145
|
|
|
'name' => 'Hutton Report', 'id' => 7, |
146
|
|
|
], |
147
|
|
|
]; |
148
|
|
|
|
149
|
|
|
$example_urls = []; |
150
|
|
|
foreach ($examples as $name => $example) { |
151
|
|
|
$URL->insert(["gl" => $example['id']]); |
152
|
|
|
$example['url'] = $URL->generate(); |
153
|
|
|
$example_urls[$name] = $example; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$data['example_urls'] = $example_urls; |
157
|
|
|
return $data; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|