|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Topics |
|
4
|
|
|
* |
|
5
|
|
|
* @package TheyWorkForYou |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace MySociety\TheyWorkForYou; |
|
9
|
|
|
|
|
10
|
|
|
class Topics { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* DB handle |
|
14
|
|
|
*/ |
|
15
|
|
|
private $db; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->db = new \ParlDB; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
View Code Duplication |
public function getTopics() { |
|
28
|
|
|
$q = $this->db->query("SELECT id, slug, title, description, search_string, front_page, image FROM topics"); |
|
29
|
|
|
|
|
30
|
|
|
$topics = array(); |
|
31
|
|
|
$count = $q->rows(); |
|
32
|
|
|
|
|
33
|
|
|
for ($i = 0; $i < $count; $i++ ) { |
|
34
|
|
|
$topic = $q->row($i); |
|
35
|
|
|
$topics[$topic['slug']] = new Topic($topic); |
|
36
|
|
|
} |
|
37
|
|
|
return $topics; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getTopic($topic_name) { |
|
41
|
|
|
$q = $this->db->query( |
|
42
|
|
|
"SELECT id, slug, title, description, search_string, front_page, image FROM topics WHERE slug = :slug", |
|
43
|
|
|
array(':slug' => $topic_name) |
|
44
|
|
|
); |
|
45
|
|
|
if ($q->rows) { |
|
46
|
|
|
return new Topic($q->row(0)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return NULL; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
public function getFrontPageTopics() { |
|
53
|
|
|
$q = $this->db->query( |
|
54
|
|
|
"SELECT id, slug, title, description, search_string, front_page, image FROM topics WHERE front_page = TRUE" |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$topics = array(); |
|
58
|
|
|
$count = $q->rows(); |
|
59
|
|
|
|
|
60
|
|
|
for ($i = 0; $i < $count; $i++ ) { |
|
61
|
|
|
$topic = $q->row($i); |
|
62
|
|
|
$topics[$topic['slug']] = new Topic($topic); |
|
63
|
|
|
} |
|
64
|
|
|
return $topics; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function updateFrontPageTopics($topics) { |
|
68
|
|
|
// PDO doesn't cope with arrays so we have to do this by hand :| |
|
69
|
|
|
$quoted = array(); |
|
70
|
|
|
foreach ($topics as $topic) { |
|
71
|
|
|
$quoted[] = $this->db->quote($topic); |
|
72
|
|
|
} |
|
73
|
|
|
$topics_str = implode(',', $quoted); |
|
74
|
|
|
|
|
75
|
|
|
$q = $this->db->query("UPDATE topics SET front_page = TRUE WHERE slug IN ($topics_str)"); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$q = $this->db->query("UPDATE topics SET front_page = FALSE WHERE slug NOT IN ($topics_str)"); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.