-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topic.php
240 lines (210 loc) · 4.89 KB
/
Topic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* @file Topic.php
*
* description
*
* copyright (c) 2017 Frank Hellenkamp [jonas@depage.net]
*
* @author Frank Hellenkamp [jonas@depage.net]
*/
namespace Depage\Discuss;
/**
* @brief Topic
* Class Topics
*/
class Topic extends \Depage\Entity\Entity
{
// {{{ variables
/**
* @brief fields
**/
static protected $fields = array(
"id" => null,
"subject" => "",
"description" => "",
"pos" => 0,
"visible" => 1,
);
/**
* @brief primary
**/
static protected $primary = ["id"];
/**
* @brief pdo object for database access
**/
protected $pdo = null;
// }}}
// {{{ __construct()
/**
* @brief __construct
*
* @param mixed $
* @return void
**/
public function __construct($pdo)
{
parent::__construct($pdo);
$this->pdo = $pdo;
}
// }}}
// {{{ loadAll()
/**
* @brief loadAll
*
* @param mixed $
* @return void
**/
public static function loadAll($pdo)
{
$fields = "t." . implode(", t.", self::getFields());
$params = [];
$query = $pdo->prepare(
"SELECT $fields
FROM
{$pdo->prefix}_discuss_topics AS t
ORDER BY t.pos"
);
$query->execute($params);
// pass pdo-instance to constructor
$query->setFetchMode(\PDO::FETCH_CLASS, get_called_class(), array($pdo));
$n = $query->fetchAll();
return $n;
}
// }}}
// {{{ loadById()
/**
* @brief loadById
*
* @param mixed $
* @return void
**/
public static function loadById($pdo, $id)
{
$fields = "t." . implode(", t.", self::getFields());
$params = [
"id" => $id,
];
$query = $pdo->prepare(
"SELECT $fields
FROM
{$pdo->prefix}_discuss_topics AS t
WHERE t.id = :id
"
);
$query->execute($params);
// pass pdo-instance to constructor
$query->setFetchMode(\PDO::FETCH_CLASS, get_called_class(), array($pdo));
$n = $query->fetch();
return $n;
}
// }}}
// {{{ loadAllThreads()
/**
* @brief loadAllThreads
*
* @param mixed
* @return void
**/
public function loadAllThreads()
{
$threads = Thread::loadByTopic($this->pdo, $this->id);
return $threads;
}
// }}}
// {{{ loadThreadById()
/**
* @brief loadThreadById
*
* @param mixed $param
* @return void
**/
public function loadThreadById($id)
{
$thread = Thread::loadById($this->pdo, $id);
return $thread;
}
// }}}
// {{{ getNumThreads()
/**
* @brief getNumThreads
*
* @param mixed
* @return void
**/
public function getNumThreads()
{
return Thread::countByTopic($this->pdo, $this->id);
}
// }}}
// {{{ getNumPosts()
/**
* @brief getNumPosts
*
* @param mixed
* @return void
**/
public function getNumPosts()
{
return Post::countByTopic($this->pdo, $this->id);
}
// }}}
// {{{ addThread()
/**
* @brief addThread
*
* @param mixed
* @return void
**/
public function addThread($subject, $post, $uid)
{
$thread = new Thread($this->pdo);
$thread->setData([
'subject' => $subject,
'topicId' => $this->id,
'post' => $post,
'uid' => $uid,
])
->save();
return $thread;
}
// }}}
// {{{ save()
/**
* save a notification object
*
* @public
*/
public function save() {
$fields = array();
$primary = self::$primary[0];
$isNew = $this->data[$primary] === null;
$dirty = array_keys($this->dirty, true);
if (count($dirty) > 0) {
if ($isNew) {
$query = "INSERT INTO {$this->pdo->prefix}_discuss_topics";
} else {
$query = "UPDATE {$this->pdo->prefix}_discuss_topics";
}
foreach ($dirty as $key) {
$fields[] = "$key=:$key";
}
$query .= " SET " . implode(",", $fields);
if (!$isNew) {
$query .= " WHERE $primary=:$primary";
$dirty[] = $primary;
}
$params = array_intersect_key($this->data, array_flip($dirty));
$cmd = $this->pdo->prepare($query);
$success = $cmd->execute($params);
if ($isNew) {
$this->data[$primary] = $this->pdo->lastInsertId();
}
if ($success) {
$this->dirty = array_fill_keys(array_keys(static::$fields), false);
}
}
}
// }}}
}
// vim:set ft=php sw=4 sts=4 fdm=marker et :