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