/
Forum.php
148 lines (126 loc) · 3.19 KB
/
Forum.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
<?php
namespace App\Modules\Adaptbb\Models;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use App\Modules\Adaptbb\Models\ForumCategory;
use App\Modules\Adaptbb\Models\Reply;
use App\Modules\Adaptbb\Models\Topic;
class Forum extends Model implements Sortable
{
use Sluggable,
SortableTrait;
protected $table = 'plugin_adaptbb_forums';
protected $fillable = [
'name',
'notice',
'locked',
'ord',
'description',
'backgroundColor',
'icon',
'borderColor',
'borderWidth',
'category_id',
'meta_keywords',
'meta_description'
];
public $sortable = [
'order_column_name' => 'ord',
'sort_when_creating' => true,
];
/**
* Category
*
* @return ForumCategory
*/
public function category()
{
return $this->belongsTo('App\Modules\Adaptbb\Models\ForumCategory', 'category_id');
}
/**
* Topics
*
* @return Collection
*/
public function topics()
{
return $this->hasMany('App\Modules\Adaptbb\Models\Topic', 'forum_id');
}
/**
* Replies
*
* @return Collection
*/
public function replies()
{
return $this->hasMany('App\Modules\Adaptbb\Models\Reply', 'forum_id');
}
/**
* Get Index
*
* @return array
*/
public function getIndex()
{
$forums = [];
$categories = ForumCategory::orderBy('ord', 'ASC')->get();
foreach($categories as $category) {
$row = [
'category' => $category,
'forums' => []
];
foreach($category->forums as $forum) {
$row['forums'][] = $forum;
}
$forums[] = $row;
}
return $forums;
}
/**
* Get Latest Post
*
* @return null|Reply
*/
public function getLatestPost()
{
// get the last topic and reply
$last_topic = Topic::where('forum_id', '=', $this->id)->where('active', '=', 1)->orderBy('created_at', 'DESC')->first();
$last_reply = Reply::where('forum_id', '=', $this->id)->where('active', '=', 1)->orderBy('created_at', 'DESC')->first();
// if no posts, return empty array
if (empty($last_topic) && empty($last_reply)) {
return [];
}
// set a default date for topic and reply
if (empty($last_topic)) {
$last_topic_date = 0;
} else {
$last_topic_date = strtotime($last_topic->created_at);
}
if (empty($last_reply)) {
$last_reply_date = 0;
} else {
$last_reply_date = strtotime($last_reply->created_at);
}
// get the absolute newest
if ($last_topic_date > $last_reply_date) {
$post = $last_topic;
} else {
$post = $last_reply;
}
return $post;
}
/**
* Sluggable
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable()
{
return [
'slug' => [
'source' => 'name'
]
];
}
}