1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Topic |
4
|
|
|
* |
5
|
|
|
* @package TheyWorkForYou |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace MySociety\TheyWorkForYou; |
9
|
|
|
|
10
|
|
|
class Topic { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* DB handle |
14
|
|
|
*/ |
15
|
|
|
private $db; |
16
|
|
|
|
17
|
|
|
private $raw; |
|
|
|
|
18
|
|
|
private $id; |
19
|
|
|
private $title; |
20
|
|
|
private $slug; |
21
|
|
|
private $description; |
22
|
|
|
private $image; |
23
|
|
|
private $search_string; |
24
|
|
|
private $front_page; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
public function __construct($data = NULL) |
32
|
|
|
{ |
33
|
|
|
$this->db = new \ParlDB; |
34
|
|
|
|
35
|
|
|
if (is_null($data)) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->id = $data['id']; |
40
|
|
|
$this->title = $data['title']; |
41
|
|
|
$this->slug = $data['slug']; |
42
|
|
|
$this->description = $data['description']; |
43
|
|
|
$this->search_string = $data['search_string']; |
44
|
|
|
$this->front_page = $data['front_page']; |
45
|
|
|
$this->image = $data['image']; |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
function title() { |
|
|
|
|
51
|
|
|
return $this->title; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function sctitle() { |
|
|
|
|
55
|
|
|
$title = $this->title; |
56
|
|
|
if (strpos($title, 'The ') === 0 ) { |
57
|
|
|
$title = lcfirst($title); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $title; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
function set_title($title) { |
|
|
|
|
64
|
|
|
$this->title = $title; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
function slug() { |
|
|
|
|
68
|
|
|
return $this->slug; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
function set_slug($slug) { |
|
|
|
|
72
|
|
|
$this->slug = $slug; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
function url() { |
|
|
|
|
76
|
|
|
$url = new \URL('topic'); |
77
|
|
|
return $url->generate() . $this->slug; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
function image() { |
|
|
|
|
81
|
|
|
return $this->image; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
function image_url() { |
|
|
|
|
85
|
|
|
return "/topic/image.php?id=" . $this->slug(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
function image_path() { |
|
|
|
|
89
|
|
|
if ($this->image) { |
90
|
|
|
return sprintf('%s%s%s', TOPICIMAGEPATH, DIRECTORY_SEPARATOR, $this->image); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
function set_image($image) { |
|
|
|
|
97
|
|
|
$this->image = $image; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
function description() { |
|
|
|
|
102
|
|
|
return $this->description; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
function set_description($description) { |
|
|
|
|
106
|
|
|
$this->description = $description; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
function search_string() { |
|
|
|
|
110
|
|
|
return $this->search_string; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
function set_search_string($search_string) { |
|
|
|
|
114
|
|
|
$this->search_string = $search_string; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
function set_front_page($on) { |
|
|
|
|
118
|
|
|
$this->front_page = $on; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
function onFrontPage() { |
|
|
|
|
122
|
|
|
return $this->front_page == 1; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function _getContentIDs() { |
126
|
|
|
$q = $this->db->query( |
127
|
|
|
"SELECT body, gid, ep.epobject_id FROM epobject ep |
128
|
|
|
JOIN hansard h on ep.epobject_id = h.epobject_id |
129
|
|
|
JOIN topic_epobjects te on te.epobject_id = ep.epobject_id |
130
|
|
|
WHERE topic_key = :topic_key", |
131
|
|
|
array( |
132
|
|
|
':topic_key' => $this->id |
133
|
|
|
) |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
return $q; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
function getContent() { |
|
|
|
|
140
|
|
|
$q = $this->_getContentIDs(); |
141
|
|
|
|
142
|
|
|
$content = array(); |
143
|
|
|
$rows = $q->rows; |
144
|
|
|
for ($i = 0; $i < $rows; $i++) { |
145
|
|
|
$content[] = array( |
146
|
|
|
'title' => $q->field($i, 'body'), |
147
|
|
|
'href' => Utility\Hansard::gid_to_url($q->field($i, 'gid')), |
148
|
|
|
'id' => $q->field($i, 'epobject_id'), |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $content; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
function getFullContent() { |
|
|
|
|
156
|
|
|
$q = $this->_getContentIDs(); |
157
|
|
|
|
158
|
|
|
$content = array(); |
159
|
|
|
$rows = $q->rows; |
160
|
|
|
for ($i = 0; $i < $rows; $i++) { |
161
|
|
|
$gid = $q->field($i, 'gid'); |
162
|
|
View Code Duplication |
if (strpos($gid, 'lords') !== false) { |
|
|
|
|
163
|
|
|
$debatelist = new \LORDSDEBATELIST; |
164
|
|
|
} elseif (strpos($gid, 'westminhall') !== false) { |
165
|
|
|
$debatelist = new \WHALLLIST; |
166
|
|
|
} else { |
167
|
|
|
$debatelist = new \DEBATELIST; |
168
|
|
|
} |
169
|
|
|
$data = $debatelist->display('featured_gid', array('gid' => $gid), 'none'); |
170
|
|
|
|
171
|
|
|
$item = $data['data']; |
172
|
|
|
if (isset($item['parent']) && $item['body'] == $item['parent']['body']) { |
173
|
|
|
unset($item['parent']); |
174
|
|
|
} |
175
|
|
|
$content[] = $item; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $content; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
function addContent($gid) { |
|
|
|
|
182
|
|
|
$q = $this->db->query( |
183
|
|
|
"SELECT epobject_id FROM hansard WHERE gid = :gid", |
184
|
|
|
array( |
185
|
|
|
":gid" => $gid |
186
|
|
|
) |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
if (!$q->success() || $q->rows == 0) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$epobject_id = $q->field(0, 'epobject_id'); |
194
|
|
|
|
195
|
|
|
$q = $this->db->query( |
196
|
|
|
"INSERT INTO topic_epobjects (topic_key, epobject_id) VALUES (:topic, :ep_id)", |
197
|
|
|
array( |
198
|
|
|
":topic" => $this->id, |
199
|
|
|
":ep_id" => $epobject_id |
200
|
|
|
) |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
return $q->success(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
function deleteContent($id) { |
|
|
|
|
207
|
|
|
$q = $this->db->query( |
208
|
|
|
"DELETE FROM topic_epobjects WHERE topic_key = :topic AND epobject_id = :ep_id", |
209
|
|
|
array( |
210
|
|
|
":topic" => $this->id, |
211
|
|
|
":ep_id" => $id |
212
|
|
|
) |
213
|
|
|
); |
214
|
|
|
|
215
|
|
|
return $q->success(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
View Code Duplication |
function getPolicySets() { |
|
|
|
|
219
|
|
|
$q = $this->db->query( |
220
|
|
|
"SELECT policyset FROM topic_policysets WHERE topic_key = :key", |
221
|
|
|
array( |
222
|
|
|
':key' => $this->id |
223
|
|
|
) |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
$sets = array(); |
227
|
|
|
$count = $q->rows; |
228
|
|
|
for ($i = 0; $i < $count; $i++) { |
229
|
|
|
$sets[] = $q->field($i, 'policyset'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $sets; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
View Code Duplication |
function addPolicySets($sets) { |
|
|
|
|
236
|
|
|
if ($sets === '' or count($sets) == 0) { |
237
|
|
|
$q = $this->db->query( |
238
|
|
|
"DELETE FROM topic_policysets WHERE topic_key = :topic_key", |
239
|
|
|
array( |
240
|
|
|
":topic_key" => $this->id |
241
|
|
|
) |
242
|
|
|
); |
243
|
|
|
} else { |
244
|
|
|
foreach ($sets as $set) { |
245
|
|
|
if ($set == '' ) { |
246
|
|
|
continue; |
247
|
|
|
} |
248
|
|
|
$q = $this->db->query( |
249
|
|
|
"REPLACE INTO topic_policysets (policyset, topic_key) VALUES (:policyset, :topic_key)", |
250
|
|
|
array( |
251
|
|
|
':topic_key' => $this->id, |
252
|
|
|
':policyset' => $set |
253
|
|
|
) |
254
|
|
|
); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $q->success(); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
View Code Duplication |
function getPolicies() { |
|
|
|
|
262
|
|
|
$q = $this->db->query( |
263
|
|
|
'SELECT policy_id FROM topic_policies WHERE topic_key = :key', |
264
|
|
|
array( |
265
|
|
|
':key' => $this->id |
266
|
|
|
) |
267
|
|
|
); |
268
|
|
|
|
269
|
|
|
$policies = array(); |
270
|
|
|
$count = $q->rows; |
271
|
|
|
for ($i = 0; $i < $count; $i++) { |
272
|
|
|
$policies[] = $q->field($i, 'policy_id'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $policies; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
View Code Duplication |
function addPolicies($policies) { |
|
|
|
|
279
|
|
|
if ($policies === '' or count($policies) == 0) { |
280
|
|
|
$q = $this->db->query( |
281
|
|
|
"DELETE FROM topic_policies WHERE topic_key = :topic_key", |
282
|
|
|
array( |
283
|
|
|
":topic_key" => $this->id |
284
|
|
|
) |
285
|
|
|
); |
286
|
|
|
} else { |
287
|
|
|
foreach ($policies as $policy) { |
288
|
|
|
if ($policy == '' ) { |
289
|
|
|
continue; |
290
|
|
|
} |
291
|
|
|
$q = $this->db->query( |
292
|
|
|
"REPLACE INTO topic_policies (policy_id, topic_key) VALUES (:policy, :topic_key)", |
293
|
|
|
array( |
294
|
|
|
':topic_key' => $this->id, |
295
|
|
|
':policy' => $policy |
296
|
|
|
) |
297
|
|
|
); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return $q->success(); |
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
function getAllPolicies() { |
|
|
|
|
305
|
|
|
$policy_sets = $this->getPolicySets(); |
306
|
|
|
$all_policies = array(); |
307
|
|
|
$policies = new Policies(); |
308
|
|
|
foreach ($policy_sets as $set) { |
309
|
|
|
$all_policies = array_merge($all_policies, array_keys($policies->limitToSet($set)->getPolicies())); |
310
|
|
|
} |
311
|
|
|
$topic_policies = $this->getPolicies(); |
312
|
|
|
$all_policies = array_merge($all_policies, $topic_policies); |
313
|
|
|
|
314
|
|
|
return array_unique($all_policies); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
function save() { |
|
|
|
|
318
|
|
|
$q = $this->db->query( |
319
|
|
|
"REPLACE INTO topics |
320
|
|
|
(id, title, slug, description, search_string, front_page, image) |
321
|
|
|
VALUES |
322
|
|
|
(:id, :title, :slug, :description, :search_string, :front_page, :image)", |
323
|
|
|
array( |
324
|
|
|
':id' => $this->id, |
325
|
|
|
':slug' => $this->slug(), |
326
|
|
|
':title' => $this->title(), |
327
|
|
|
':description' => $this->description(), |
328
|
|
|
':search_string' => $this->search_string(), |
329
|
|
|
':front_page' => $this->onFrontPage(), |
330
|
|
|
':image' => $this->image() |
331
|
|
|
) |
332
|
|
|
); |
333
|
|
|
|
334
|
|
|
return $q->success(); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.