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
|
|
|
'template_name' => 'addterm_start', |
18
|
|
|
]; |
19
|
|
|
$term = filter_user_input(get_http_var('g'), 'strict'); |
20
|
|
|
// glossary matches should always be quoted. |
21
|
|
|
// Just to be sure, we'll strip away any quotes and add them again. |
22
|
|
|
if (preg_match("/^(\"|\')/", $term)) { |
23
|
|
|
$term = preg_replace("/\"|\'/", "", $term); |
24
|
|
|
} |
25
|
|
|
$data['title'] = $term; |
26
|
|
|
|
27
|
|
|
$this->glossary = new \GLOSSARY(['s' => $term]); |
28
|
|
|
$data['glossary'] = $this->glossary; |
29
|
|
|
|
30
|
|
|
if (get_http_var('submitterm') != '') { |
31
|
|
|
$data = $this->add_glossary_entry($data); |
32
|
|
|
} elseif (get_http_var('previewterm') != '') { |
33
|
|
|
$data['contributing_user'] = $THEUSER->firstname . " " . $THEUSER->lastname; |
34
|
|
|
$data['definition_raw'] = get_http_var('definition'); |
35
|
|
|
$data['definition'] = $this->format_body($data['definition_raw']); |
36
|
|
|
$data['template_name'] = 'addterm_preview'; |
37
|
|
|
} elseif (get_http_var('g') != '') { |
38
|
|
|
$data = $this->check_glossary_entry($data); |
39
|
|
|
} else { |
40
|
|
|
$data = $this->add_example_urls($data); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$URL = new \MySociety\TheyWorkForYou\Url('glossary_addterm'); |
44
|
|
|
$data['form_url'] = $URL->generate(); |
45
|
|
|
|
46
|
|
|
return $data; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function has_stop_words(): bool { |
50
|
|
|
if (in_array($this->glossary->query, $this->glossary->stopwords)) { |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function check_term_is_useful(array $data): array { |
57
|
|
|
if ($this->has_stop_words()) { |
58
|
|
|
$data['error'] = 'Sorry, that phrase appears too many times to be a useful as a link within the parliamentary record.'; |
59
|
|
|
} elseif (isset($data['appearances']) && !$data['appearances']) { |
60
|
|
|
$data['error'] = "Unfortunately <strong>" . $data['title'] . "</strong>, doesn't seem to appear in hansard at all...</p>"; |
61
|
|
|
} elseif ($this->glossary->num_search_matches > 0) { |
62
|
|
|
$data['template_name'] = 'addterm_matches'; |
63
|
|
|
$data['error'] = 'Existing matches'; |
64
|
|
|
$data['count'] = $this->glossary->num_search_matches; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function get_appearance_count(string $term): int { |
71
|
|
|
global $SEARCHENGINE; |
72
|
|
|
$SEARCHENGINE = new \SEARCHENGINE($term); |
73
|
|
|
$count = $SEARCHENGINE->run_count(0, 10000); |
74
|
|
|
return $count; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function check_glossary_entry(array $data): array { |
78
|
|
|
$data['template_name'] = 'addterm_appearances'; |
79
|
|
|
$data['appearances'] = $this->get_appearance_count($data['title']); |
80
|
|
|
$data['definition_raw'] = ''; |
81
|
|
|
$data = $this->check_term_is_useful($data); |
82
|
|
|
|
83
|
|
|
if (!isset($data['error'])) { |
84
|
|
|
$data['definition_raw'] = ''; |
85
|
|
|
$list = new \HANSARDLIST(); |
86
|
|
|
$examples = $list->display('search', [ |
87
|
|
|
'num' => 5, |
88
|
|
|
's' => $data['title'], |
89
|
|
|
'view_override' => 'glossary_search', |
90
|
|
|
], 'none'); |
91
|
|
|
$data['examples'] = $examples['rows']; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $data; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function add_glossary_entry(array $data): array { |
98
|
|
|
$data['submitted'] = 1; |
99
|
|
|
$data['template_name'] = 'addterm_examples'; |
100
|
|
|
$data['body'] = get_http_var('definition'); |
101
|
|
|
$data = $this->check_term_is_useful($data); |
102
|
|
|
|
103
|
|
|
$success = false; |
104
|
|
|
if (!isset($data['error'])) { |
105
|
|
|
$entry = [ |
106
|
|
|
'title' => $data['title'], |
107
|
|
|
'body' => $data['body'], |
108
|
|
|
]; |
109
|
|
|
$success = $this->glossary->create($entry); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (is_int($success)) { |
113
|
|
|
$data['template_name'] = 'addterm_success'; |
114
|
|
|
} elseif (is_array($success)) { |
115
|
|
|
$data = array_merge($data, $success); |
116
|
|
|
} else { |
117
|
|
|
if (!isset($data['error'])) { |
118
|
|
|
$data['error'] = "Sorry, there was an error and we were unable to add your Glossary item."; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $data; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function add_example_urls(array $data): array { |
126
|
|
|
$URL = new \MySociety\TheyWorkForYou\Url('glossary'); |
127
|
|
|
|
128
|
|
|
$examples = [ |
129
|
|
|
'technical' => [ |
130
|
|
|
'name' => 'Early Day Motion', 'id' => 90, |
131
|
|
|
], |
132
|
|
|
'organisation' => [ |
133
|
|
|
'name' => 'Devon County Council', 'id' => 12, |
134
|
|
|
], |
135
|
|
|
'document' => [ |
136
|
|
|
'name' => 'Hutton Report', 'id' => 7, |
137
|
|
|
], |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
$example_urls = []; |
141
|
|
|
foreach ($examples as $name => $example) { |
142
|
|
|
$URL->insert(["gl" => $example['id']]); |
143
|
|
|
$example['url'] = $URL->generate(); |
144
|
|
|
$example_urls[$name] = $example; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$data['example_urls'] = $example_urls; |
148
|
|
|
return $data; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|