|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Taskforcedev\LaravelForum\Models; |
|
4
|
|
|
|
|
5
|
|
|
use \DB; |
|
6
|
|
|
use \Validator; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Forum |
|
10
|
|
|
* |
|
11
|
|
|
* @property integer $id Forum ID. |
|
12
|
|
|
* @property string $name Forum Name. |
|
13
|
|
|
* @property string $description Forum Description. |
|
14
|
|
|
* @property integer $category_id Category ID. |
|
15
|
|
|
* @property integer $public Whether the forum is publicly viewable. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Taskforcedev\LaravelForum\Models |
|
18
|
|
|
*/ |
|
19
|
|
|
class Forum extends AbstractModel |
|
20
|
|
|
{ |
|
21
|
|
|
public $table = 'forums'; |
|
22
|
|
|
|
|
23
|
|
|
public $fillable = ['name', 'description', 'category_id', 'public']; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Eloquent Relation. |
|
27
|
|
|
* @return mixed |
|
28
|
|
|
*/ |
|
29
|
|
|
public function category() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->belongsTo('Taskforcedev\LaravelForum\ForumCategory'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Eloquent Relation. |
|
36
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
37
|
|
|
*/ |
|
38
|
|
|
public function posts() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->hasMany('Taskforcedev\LaravelForum\ForumPost'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the number of threads inside the current forum. |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getThreadCount() |
|
48
|
|
|
{ |
|
49
|
|
|
try { |
|
50
|
|
|
$result = DB::select('SELECT COUNT(*) as count FROM `forum_posts` where forum_id = ?', [$this->id]); |
|
51
|
|
|
return $result[0]->count; |
|
52
|
|
|
} catch (\Exception $e) { |
|
53
|
|
|
return 0; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Is model data valid. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array|object $data The data to validate. |
|
61
|
|
|
* |
|
62
|
|
|
* @return boolean |
|
63
|
|
|
*/ |
|
64
|
|
View Code Duplication |
public static function valid($data) |
|
65
|
|
|
{ |
|
66
|
|
|
$rules = [ |
|
67
|
|
|
'name' => 'required|min:3', |
|
68
|
|
|
'description' => 'min:3', |
|
69
|
|
|
'category_id' => 'required|min:0|integer|exists:forum_categories,id', |
|
70
|
|
|
]; |
|
71
|
|
|
$validator = Validator::make($data, $rules); |
|
72
|
|
|
|
|
73
|
|
|
if ($validator->passes()) { |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|