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