forked from phpbb/ideas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidea.php
More file actions
486 lines (419 loc) · 11.9 KB
/
idea.php
File metadata and controls
486 lines (419 loc) · 11.9 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<?php
/**
*
* Ideas extension for the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace phpbb\ideas\factory;
use phpbb\ideas\ext;
/**
* Class for handling a single idea
*/
class idea extends base
{
/**
* Returns the specified idea.
*
* @param int $id The ID of the idea to return.
*
* @return array|false The idea row set, or false if not found.
*/
public function get_idea($id)
{
$sql = 'SELECT *
FROM ' . $this->table_ideas . '
WHERE idea_id = ' . (int) $id;
$result = $this->db->sql_query_limit($sql, 1);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
return $row;
}
/**
* Returns an idea specified by its topic ID.
*
* @param int $id The ID of the idea to return.
*
* @return array|false The idea row set, or false if not found.
*/
public function get_idea_by_topic_id($id)
{
$sql = 'SELECT idea_id
FROM ' . $this->table_ideas . '
WHERE topic_id = ' . (int) $id;
$result = $this->db->sql_query_limit($sql, 1);
$idea_id = (int) $this->db->sql_fetchfield('idea_id');
$this->db->sql_freeresult($result);
return $this->get_idea($idea_id);
}
/**
* Updates the status of an idea.
*
* @param int $idea_id The ID of the idea.
* @param int $status The ID of the status.
*
* @return void
*/
public function set_status($idea_id, $status)
{
$sql_ary = array(
'idea_status' => (int) $status,
);
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
// Send a notification
$idea = $this->get_idea($idea_id);
$notifications = $this->notification_manager->get_notified_users(ext::NOTIFICATION_TYPE_STATUS, ['item_id' => (int) $idea_id]);
$this->notification_manager->{empty($notifications) ? 'add_notifications' : 'update_notifications'}(
ext::NOTIFICATION_TYPE_STATUS,
[
'idea_id' => (int) $idea_id,
'status' => (int) $status,
'user_id' => (int) $this->user->data['user_id'],
'idea_author' => (int) $idea['idea_author'],
'idea_title' => $idea['idea_title'],
]
);
}
/**
* Sets the ID of the duplicate for an idea.
*
* @param int $idea_id ID of the idea to be updated.
* @param string $duplicate Idea ID of duplicate.
*
* @return bool True if set, false if invalid.
*/
public function set_duplicate($idea_id, $duplicate)
{
if ($duplicate && !is_numeric($duplicate))
{
return false;
}
$sql_ary = array(
'duplicate_id' => (int) $duplicate,
);
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
return true;
}
/**
* Sets the RFC link of an idea.
*
* @param int $idea_id ID of the idea to be updated.
* @param string $rfc Link to the RFC.
*
* @return bool True if set, false if invalid.
*/
public function set_rfc($idea_id, $rfc)
{
$match = '/^https?:\/\/area51\.phpbb\.com\/phpBB\/viewtopic\.php/';
if ($rfc && !preg_match($match, $rfc))
{
return false;
}
$sql_ary = array(
'rfc_link' => $rfc, // string is escaped by build_array()
);
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
return true;
}
/**
* Sets the ticket ID of an idea.
*
* @param int $idea_id ID of the idea to be updated.
* @param string $ticket Ticket ID.
*
* @return bool True if set, false if invalid.
*/
public function set_ticket($idea_id, $ticket)
{
if ($ticket && !is_numeric($ticket))
{
return false;
}
$sql_ary = array(
'ticket_id' => (int) $ticket,
);
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
return true;
}
/**
* Sets the implemented version of an idea.
*
* @param int $idea_id ID of the idea to be updated.
* @param string $version Version of phpBB the idea was implemented in.
*
* @return bool True if set, false if invalid.
*/
public function set_implemented($idea_id, $version)
{
$match = '/^\d\.\d\.\d+(-\w+)?$/';
if ($version && !preg_match($match, $version))
{
return false;
}
$sql_ary = array(
'implemented_version' => $version, // string is escaped by build_array()
);
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
return true;
}
/**
* Sets the title of an idea.
*
* @param int $idea_id ID of the idea to be updated.
* @param string $title New title.
*
* @return boolean True if updated, false if invalid length.
*/
public function set_title($idea_id, $title)
{
if (utf8_clean_string($title) === '')
{
return false;
}
$sql_ary = array(
'idea_title' => truncate_string($title, ext::SUBJECT_LENGTH),
);
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
return true;
}
/**
* Get the title of an idea.
*
* @param int $id ID of an idea
*
* @return string The idea's title, empty string if not found
*/
public function get_title($id)
{
$sql = 'SELECT idea_title
FROM ' . $this->table_ideas . '
WHERE idea_id = ' . (int) $id;
$result = $this->db->sql_query_limit($sql, 1);
$idea_title = $this->db->sql_fetchfield('idea_title');
$this->db->sql_freeresult($result);
return $idea_title ?: '';
}
/**
* Set the author of an idea
*
* @param int $idea_id
* @param int $user_id
* @return bool True if set, false if invalid.
*/
public function set_author($idea_id, $user_id)
{
if (!$user_id || !is_numeric($user_id))
{
return false;
}
$sql_ary = array(
'idea_author' => (int) $user_id,
);
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
return true;
}
/**
* Submit new idea data to the ideas table
*
* @param array $data An array of post data from a newly posted idea
*
* @return int The ID of the new idea.
*/
public function submit($data)
{
$sql_ary = [
'idea_title' => $data['topic_title'],
'idea_author' => $data['poster_id'],
'idea_date' => $data['post_time'],
'topic_id' => $data['topic_id'],
];
$idea_id = $this->insert_idea_data($sql_ary, $this->table_ideas);
// Initial vote
if (($idea = $this->get_idea($idea_id)) !== false)
{
$this->vote($idea, $data['poster_id'], 1);
}
return $idea_id;
}
/**
* Deletes an idea and the topic to go with it.
*
* @param int $id The ID of the idea to be deleted.
* @param int $topic_id The ID of the idea topic. Optional, but preferred.
*
* @return boolean Whether the idea was deleted or not.
*/
public function delete($id, $topic_id = 0)
{
if (!$topic_id)
{
$idea = $this->get_idea($id);
$topic_id = $idea ? $idea['topic_id'] : 0;
}
// Delete topic
delete_posts('topic_id', $topic_id);
// Delete idea
$deleted = $this->delete_idea_data($id, $this->table_ideas);
if ($deleted)
{
// Delete votes
$this->delete_idea_data($id, $this->table_votes);
// Delete notifications
$this->notification_manager->delete_notifications(ext::NOTIFICATION_TYPE_STATUS, $id);
}
return $deleted;
}
/**
* Submits a vote on an idea.
*
* @param array $idea The idea returned by get_idea().
* @param int $user_id The ID of the user voting.
* @param int $value Up (1) or down (0)?
*
* @return array|string Array of information or string on error.
*/
public function vote(&$idea, $user_id, $value)
{
// Validate $vote - must be 0 or 1
if ($value !== 0 && $value !== 1)
{
return 'INVALID_VOTE';
}
// Check whether user has already voted - update if they have
if ($row = $this->get_users_vote($idea['idea_id'], $user_id))
{
if ($row['vote_value'] != $value)
{
$sql = 'UPDATE ' . $this->table_votes . '
SET vote_value = ' . $value . '
WHERE user_id = ' . (int) $user_id . '
AND idea_id = ' . (int) $idea['idea_id'];
$this->db->sql_query($sql);
if ($value == 1)
{
// Change to upvote
$idea['idea_votes_up']++;
$idea['idea_votes_down']--;
}
else
{
// Change to downvote
$idea['idea_votes_up']--;
$idea['idea_votes_down']++;
}
$sql_ary = array(
'idea_votes_up' => $idea['idea_votes_up'],
'idea_votes_down' => $idea['idea_votes_down'],
);
$this->update_idea_data($sql_ary, $idea['idea_id'], $this->table_ideas);
}
return array(
'message' => $this->language->lang('UPDATED_VOTE'),
'votes_up' => $idea['idea_votes_up'],
'votes_down' => $idea['idea_votes_down'],
'points' => $this->language->lang('TOTAL_POINTS', $idea['idea_votes_up'] - $idea['idea_votes_down']),
'voters' => $this->get_voters($idea['idea_id']),
);
}
// Insert vote into votes table.
$sql_ary = array(
'idea_id' => (int) $idea['idea_id'],
'user_id' => (int) $user_id,
'vote_value' => (int) $value,
);
$this->insert_idea_data($sql_ary, $this->table_votes);
// Update number of votes in ideas table
$idea['idea_votes_' . ($value ? 'up' : 'down')]++;
$sql_ary = array(
'idea_votes_up' => $idea['idea_votes_up'],
'idea_votes_down' => $idea['idea_votes_down'],
);
$this->update_idea_data($sql_ary, $idea['idea_id'], $this->table_ideas);
return array(
'message' => $this->language->lang('VOTE_SUCCESS'),
'votes_up' => $idea['idea_votes_up'],
'votes_down' => $idea['idea_votes_down'],
'points' => $this->language->lang('TOTAL_POINTS', $idea['idea_votes_up'] - $idea['idea_votes_down']),
'voters' => $this->get_voters($idea['idea_id']),
);
}
/**
* Remove a user's vote from an idea
*
* @param array $idea The idea returned by get_idea().
* @param int $user_id The ID of the user voting.
*
* @return array Array of information.
*/
public function remove_vote(&$idea, $user_id)
{
// Only change something if user has already voted
if ($row = $this->get_users_vote($idea['idea_id'], $user_id))
{
$sql = 'DELETE FROM ' . $this->table_votes . '
WHERE idea_id = ' . (int) $idea['idea_id'] . '
AND user_id = ' . (int) $user_id;
$this->db->sql_query($sql);
$idea['idea_votes_' . ($row['vote_value'] == 1 ? 'up' : 'down')]--;
$sql_ary = array(
'idea_votes_up' => $idea['idea_votes_up'],
'idea_votes_down' => $idea['idea_votes_down'],
);
$this->update_idea_data($sql_ary, $idea['idea_id'], $this->table_ideas);
}
return array(
'message' => $this->language->lang('UPDATED_VOTE'),
'votes_up' => $idea['idea_votes_up'],
'votes_down' => $idea['idea_votes_down'],
'points' => $this->language->lang('TOTAL_POINTS', $idea['idea_votes_up'] - $idea['idea_votes_down']),
'voters' => $this->get_voters($idea['idea_id']),
);
}
/**
* Returns voter info on an idea.
*
* @param int $id ID of the idea.
*
* @return array Array of row data
*/
public function get_voters($id)
{
$sql = 'SELECT iv.user_id, iv.vote_value, u.username, u.user_colour
FROM ' . $this->table_votes . ' as iv,
' . USERS_TABLE . ' as u
WHERE iv.idea_id = ' . (int) $id . '
AND iv.user_id = u.user_id
ORDER BY u.username ASC';
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
// Process the username for the template now, so it is
// ready to use in AJAX responses and DOM injections.
$profile_url = append_sid(generate_board_url() . "/memberlist.$this->php_ext", array('mode' => 'viewprofile'));
foreach ($rows as &$row)
{
$row['user'] = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $profile_url);
}
return $rows;
}
/**
* Get a user's stored vote value for a given idea
*
* @param int $idea_id The idea id
* @param int $user_id The user id
* @return mixed Array with the row data, false if the row does not exist
*/
protected function get_users_vote($idea_id, $user_id)
{
$sql = 'SELECT idea_id, vote_value
FROM ' . $this->table_votes . '
WHERE idea_id = ' . (int) $idea_id . '
AND user_id = ' . (int) $user_id;
$result = $this->db->sql_query_limit($sql, 1);
$row = $this->db->sql_fetchrow();
$this->db->sql_freeresult($result);
return $row;
}
}