-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdiscussion.php
More file actions
494 lines (406 loc) · 11.8 KB
/
Copy pathdiscussion.php
File metadata and controls
494 lines (406 loc) · 11.8 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
487
488
489
490
491
492
493
494
<?php namespace Feather\Core;
use DB;
use Str;
use Cache;
use DateTime;
use Feather\Paginator;
use FeatherModelException;
class Discussion extends Base {
/**
* The table name.
*
* @var string
*/
public static $table = 'discussions';
/**
* Timestamps are enabled.
*
* @var bool
*/
public static $timestamps = true;
/**
* A discussion has one author.
*
* @return object
*/
public function author()
{
return $this->belongs_to('Feather\\Core\\User', 'user_id');
}
/**
* A discussion might have a recent poster, otherwise it has nothing.
*
* @return object
*/
public function recent()
{
return $this->belongs_to('Feather\\Core\\User', 'last_reply_user_id');
}
/**
* A discussion has a place.
*
* @return object
*/
public function place()
{
return $this->belongs_to('Feather\\Core\\Place', 'place_id');
}
/**
* A discussion can have many participants.
*
* @return object
*/
public function participants()
{
return $this->has_many('Feather\\Core\\Discussion\\Participant', 'discussion_id');
}
/**
* A discussion can have many replies.
*
* @return object
*/
public function replies()
{
return $this->has_many('Feather\\Core\\Reply', 'discussion_id');
}
/**
* Getter for a discussions slug.
*
* @return string
*/
public function get_slug()
{
return Str::slug($this->get_attribute('title'));
}
/**
* Getter for the replies. Turns it into a readable format.
*
* @return string
*/
public function get_short_replies()
{
return $this->shortner($this->get_attribute('replies'));
}
/**
* Getter for the views. Turns it into a readable format.
*
* @return string
*/
public function get_short_views()
{
return $this->shortner($this->get_attribute('views'));
}
/**
* Shorten the replies and views numbers
*
* @param int $number
* @return string
*/
protected function shortner($number)
{
// Oh wow, under a 1,000... Not bad.
if($number < 1000)
{
$number = number_format($number);
}
// Hold up, getting up in the thousands!
elseif($number < 1000000)
{
$number = number_format($number / 1000, 1) . 'K';
}
// Ah-hoy! A million!
elseif($number < 1000000000)
{
$number = number_format($number / 1000000, 1) . 'M';
}
// CRIKEY! A biollion!?
else
{
$number = number_format($number / 1000000000, 1) . 'B';
}
return str_replace('.0', '', $number);
}
/**
* Start a new discussion.
*
* @param array $input
* @return object
*/
public static function start($input)
{
return static::edit(new static, $input);
}
/**
* Edit an existing discussion.
*
* @param object $discussion
* @param array $input
* @return object
*/
public static function edit($discussion, $input)
{
$increment = false;
// If the discussion exists then we are only going to increment the total
// discussions for the place if the discussion is being saved from a draft
// to public viewing.
if($discussion->exists)
{
$increment = $discussion->draft ? (isset($input['draft']) ? false : true) : false;
// We'll also update the created_at and updated_at timestamps here if it's being
// started from a draft. Otherwise an old draft wouldn't be shown at the top of the
// discussions list.
if(isset($input['start']))
{
$discussion->fill(array(
'created_at' => new DateTime,
'updated_at' => new DateTime
));
}
}
// If the discussion doesn't exist we only increment the total discussions
// if the discussion is being started, not drafted.
else
{
$increment = isset($input['start']);
}
$place = Place::find($input['place']);
$discussion->fill(array(
'place_id' => $input['place'],
'user_id' => $input['user'],
'title' => isset($input['draft']) ? (empty($input['title']) ? 'Untitled Discussion' : $input['title']) : (isset($input['title']) ? $input['title'] : $discussion->title),
'body' => $input['body'],
'private' => empty($input['participants']) ? 0 : 1,
'draft' => isset($input['draft']) ? 1 : 0
));
if(!$discussion->save())
{
throw new FeatherModelException;
}
// If starting a discussion or saving a drafted discussion for public viewing we
// increment the places total discussions by one.
if($increment)
{
$place->total_discussions += 1;
$place->save();
// We can now increment the users discussion count by one if the place allows
// for a user post count increase.
if($place->user_post_increment)
{
$user = User::find($input['user']);
$user->total_discussions += 1;
$user->save();
}
}
$participants = array();
foreach(explode(',', $input['participants']) as $participant)
{
$participants[] = trim($participant);
}
$participants = array_filter($participants);
// Grab all the participants info from the database, make sure they're all valid then
// use their IDs.
if($participants and $users = User::where_in('username', $participants)->get())
{
$participants = array();
foreach($users as $key => $user)
{
$participants[$user->id] = array(
'user_id' => $user->id,
'discussion_id' => $discussion->id
);
}
}
// If this discussion is private we need to add the participants to the participants
// table. Sync the table, remove any that were removed and add any new ones.
if($discussion->participants)
{
// First thing is to delete any participants where the IDs are not in the
// submitted participants for this discussion.
if($participants)
{
Discussion\Participant::where_not_in('user_id', array_keys($participants))->where_discussion_id($discussion->id)->delete();
}
else
{
Discussion\Participant::where_discussion_id($discussion->id)->delete();
}
// Spin through the discussions existing participants and remove them from the
// array if they are already participating.
foreach($discussion->participants as $participant)
{
if(isset($participants[$participant->id]))
{
unset($participants[$participant->id]);
}
}
}
if($participants)
{
Discussion\Participant::insert($participants);
}
// Clear the cache for this discussion, as well as the place it belongs, all places,
// and the current user.
Cache::forget("discussion_{$discussion->id}");
Cache::forget("place_{$discussion->place_id}");
Cache::forget('places');
Cache::forget("user_{$discussion->user_id}");
return $discussion;
}
/**
* Returns an enriched array of discussions with relationships loaded.
*
* @param array $discussions
* @return array
*/
public static function enrichment($discussions)
{
if(!is_array($discussions))
{
$discussions = array($discussions);
}
$ids = array(
'participants' => array(),
'author' => array(),
'recent' => array()
);
foreach($discussions as $discussion)
{
$ids['author'][] = $discussion->user_id;
$ids['participants'][] = $discussion->id;
if($discussion->last_reply_user_id)
{
$ids['recent'][] = $discussion->last_reply_user_id;
}
}
$ids['author'] = array_unique($ids['author']);
$ids['recent'] = array_unique($ids['recent']);
// Here we will run three queries, one to fetch all the authors, another to fetch all the participants
// and the last will fetch the last poster details, if any.
$authors_raw = $ids['author'] ? User::where_in('id', $ids['author'])->get() : array();
$participants_raw = $ids['participants'] ? Discussion\Participant::with('details')->where_in('discussion_id', $ids['participants'])->get() : array();
$recently_raw = $ids['recent'] ? User::where_in('id', $ids['recent'])->get() : array();
// We now need to sort the arrays better, because at the moment accessing objects within the arrays
// is very hard. The keys for each array need to be the corrosponding foreign keys for the relationships.
$authors = array();
foreach($authors_raw as $author) $authors[$author->id] = $author;
$participants = array();
foreach($participants_raw as $participant) $participants[$participant->discussion_id][$participant->id] = $participant;
$recently = array();
if($recently_raw)
{
foreach($recently_raw as $recent) $recently[$recent->id] = $recent;
}
// Now that the arrays are sorted all lovely like we can spin through our discsussions again and
// assign the relationships to each discussion.
foreach($discussions as $discussion)
{
$discussion->relationships['author'] = $authors[$discussion->user_id];
if(isset($participants[$discussion->id]))
{
$discussion->relationships['participants'] = $participants[$discussion->id];
}
else
{
$discussion->relationships['participants'] = null;
}
if($discussion->last_reply_user_id)
{
$discussion->relationships['recent'] = $recently[$discussion->last_reply_user_id];
}
else
{
$discussion->relationships['recent'] = null;
}
}
return $discussions;
}
/**
* Builds a discussion bringing in it's replies and all related data. This essentially
* rewrites most of the object since the discussion itself is included as a reply.
*
* @param int $replies_per_page
* @return array
*/
public function build($replies_per_page = 15)
{
// Determine what discussions to select. Remembering to include the discussion itself as a reply.
// Otherwise our pages would be all messed up.
$page = Paginator::page($total_results = $this->replies()->count(), $replies_per_page);
$skip = ($page - 1) * $replies_per_page;
// If we are on the first page we need to include the actual discussion, so when selecting the replies
// only take the replies per page minus one.
$take = ($page == 1) ? ($replies_per_page - 1) : $replies_per_page;
// Spin through the replies and build an array of IDs, we need to get some extra details for each of the replies,
// such as author, editor, and deleter.
$replies = $users = array();
$ids = array($this->user_id);
foreach((array) $this->replies()->skip($skip)->take($take)->get() as $reply)
{
$replies[$reply->id] = $reply;
$ids[] = $reply->user_id;
if($reply->edited_id)
{
$ids[] = $reply->edited_id;
}
if($reply->deleted_id)
{
$ids[] = $reply->deleted_id;
}
}
$ids = array_unique($ids);
foreach(($ids ? User::where_in('id', $ids)->get() : array()) as $user)
{
$users[$user->id] = $user;
}
// Add the related user accounts to each reply.
foreach($replies as $reply)
{
$reply->relationships['author'] = $users[$reply->user_id];
$reply->relationships['deleter'] = $reply->deleted ? $users[$reply->deleted_id] : array();
$reply->relationships['editor'] = $reply->edited ? $users[$reply->edited_id] : array();
}
if($page == 1)
{
// We'll turn the discussion into a reply and merge it into the replies, then we'll perform our pagination
// of the discussion.
$discussion = new Reply(array(
'user_id' => $this->user_id,
'discussion_id' => $this->id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'body' => $this->body,
'deleted' => 0,
'edited' => 0
), true);
$discussion->relationships['author'] = $users[$this->user_id];
$discussion->relationships['deleter'] = array();
$discussion->relationships['editor'] = array();
$replies = array($discussion) + $replies;
}
$pagination = Paginator::make($replies, $total_results, $replies_per_page);
$this->relationships['replies'] = $pagination->results;
$this->pagination = $pagination->links();
return $this;
}
/**
* Converts participant IDs into a comma separated string.
*
* @return string
*/
public function participants_to_string()
{
$ids = $participants = array();
foreach($this->participants as $participant)
{
$ids[] = $participant->user_id;
}
if($ids and $users = User::where_in('id', $ids)->get())
{
foreach($users as $user)
{
$participants[] = $user->username;
}
}
return implode(', ', $participants);
}
}