|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Adminetic\Website\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Support\Facades\Auth; |
|
7
|
|
|
|
|
8
|
|
|
trait PostTrait |
|
9
|
|
|
{ |
|
10
|
|
|
public function scopeTenent($query) |
|
11
|
|
|
{ |
|
12
|
|
|
if (Auth::user()->exists()) { |
|
|
|
|
|
|
13
|
|
|
if (!Auth::user()->hasRole('superadmin') || !Auth::user()->hasRole('admin')) { |
|
|
|
|
|
|
14
|
|
|
return $query->where('author_id', Auth::user()->id); |
|
|
|
|
|
|
15
|
|
|
} else { |
|
16
|
|
|
return $query->whereNotNull('id'); |
|
17
|
|
|
} |
|
18
|
|
|
} else { |
|
19
|
|
|
return $query->whereNotNull('id'); |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
// Drafted Posts |
|
23
|
|
|
public function scopeDraft($query) |
|
24
|
|
|
{ |
|
25
|
|
|
return $query->where('status', 1); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
// Pending Post |
|
29
|
|
|
public function scopePending($query) |
|
30
|
|
|
{ |
|
31
|
|
|
return $query->where('status', 2); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Published Posts |
|
35
|
|
|
public function scopePublished($query) |
|
36
|
|
|
{ |
|
37
|
|
|
return $query->where('status', 3); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// Featured Posts |
|
41
|
|
|
public function scopeFeatured($query) |
|
42
|
|
|
{ |
|
43
|
|
|
return $query->published()->where('featured', 1); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Normal Posts |
|
47
|
|
|
public function scopeNormalPost($query) |
|
48
|
|
|
{ |
|
49
|
|
|
return $query->published()->where('featured', '<>', 1); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
public function scopeRelatedTagPosts($query, $id, $tags, $limit = 5) |
|
54
|
|
|
{ |
|
55
|
|
|
return $query->withAnyTag($tags)->where('id', '<>', $id)->take($limit); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function scopeRelatedCategoryPosts($query, $id, $category_id, $limit = 5) |
|
59
|
|
|
{ |
|
60
|
|
|
return $query->where('category_id', $category_id)->where('id', '<>', $id)->take($limit); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function scopeRelatedPosts($query, $id, $category_id, $tags, $limit = 5) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
return $query->withAnyTag($tags) |
|
66
|
|
|
->orWhere('category_id', $category_id) |
|
67
|
|
|
->where('id', '<>', $id) |
|
68
|
|
|
->orderByViews(); |
|
69
|
|
|
} |
|
70
|
|
|
public function scopeGeneral($query) |
|
71
|
|
|
{ |
|
72
|
|
|
return $query->where('type', 1); |
|
73
|
|
|
} |
|
74
|
|
|
public function scopeVideo($query) |
|
75
|
|
|
{ |
|
76
|
|
|
return $query->where('type', 2)->whereNotNull('video'); |
|
77
|
|
|
} |
|
78
|
|
|
public function scopeAudio($query) |
|
79
|
|
|
{ |
|
80
|
|
|
return $query->where('type', 1)->whereNotNull('audio'); |
|
81
|
|
|
} |
|
82
|
|
|
public function scopeBreakingNews($query) |
|
83
|
|
|
{ |
|
84
|
|
|
return $query->where('breaking_news', 1); |
|
85
|
|
|
} |
|
86
|
|
|
public function scopeHotNews($query) |
|
87
|
|
|
{ |
|
88
|
|
|
return $query->where('hot_news', 1); |
|
89
|
|
|
} |
|
90
|
|
|
public function scopePriority($query, $desc = true) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($desc) { |
|
93
|
|
|
return $query->orderBy('priority', 'desc'); |
|
94
|
|
|
} else { |
|
95
|
|
|
return $query->orderBy('priority', 'asc'); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
public function scopeTrending($query, $limit = 12) |
|
99
|
|
|
{ |
|
100
|
|
|
return $query->with('author', 'tagged')->published()->get()->sortByDesc(function ($p) { |
|
101
|
|
|
return $p->weight; |
|
102
|
|
|
})->take($limit); |
|
103
|
|
|
} |
|
104
|
|
|
// Filter |
|
105
|
|
|
public function scopeToday($query) |
|
106
|
|
|
{ |
|
107
|
|
|
return $query->whereDate('updated_at', Carbon::now()); |
|
108
|
|
|
} |
|
109
|
|
|
public function scopeYesterday($query) |
|
110
|
|
|
{ |
|
111
|
|
|
return $query->whereDate('updated_at', Carbon::yesterday()); |
|
112
|
|
|
} |
|
113
|
|
|
public function scopeWeek($query) |
|
114
|
|
|
{ |
|
115
|
|
|
return $query->whereBetween('updated_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()]); |
|
116
|
|
|
} |
|
117
|
|
|
public function scopeMonth($query) |
|
118
|
|
|
{ |
|
119
|
|
|
$year = Carbon::now()->year; |
|
120
|
|
|
$month = Carbon::now()->month; |
|
121
|
|
|
return $query->whereYear('updated_at', $year)->whereMonth('updated_at', $month); |
|
122
|
|
|
} |
|
123
|
|
|
public function scopeYear($query) |
|
124
|
|
|
{ |
|
125
|
|
|
$year = Carbon::now()->year; |
|
126
|
|
|
return $query->whereYear('updated_at', $year); |
|
127
|
|
|
} |
|
128
|
|
|
// Custom |
|
129
|
|
|
public function scopeLatestLimitedPosts($query, $limit = 10) |
|
130
|
|
|
{ |
|
131
|
|
|
return $query->with('author', 'tagged')->published()->latest()->take($limit); |
|
132
|
|
|
} |
|
133
|
|
|
public function scopeFeaturedLimitedPosts($query, $limit = 10) |
|
134
|
|
|
{ |
|
135
|
|
|
return $query->published()->featured()->with('author', 'tagged')->latest()->take($limit); |
|
136
|
|
|
} |
|
137
|
|
|
public function scopeLimitedHotNews($query, $limit = 10) |
|
138
|
|
|
{ |
|
139
|
|
|
return $query->published()->hotNews()->with('author', 'tagged')->latest()->take($limit); |
|
140
|
|
|
} |
|
141
|
|
|
public function scopeLimitedBreakingNews($query, $limit = 5) |
|
142
|
|
|
{ |
|
143
|
|
|
return $query->published()->breakingNews()->with('author', 'tagged')->latest()->take($limit); |
|
144
|
|
|
} |
|
145
|
|
|
public function scopeLimitedPriorityPosts($query, $limit = 10) |
|
146
|
|
|
{ |
|
147
|
|
|
return $query->published()->priority()->with('author', 'tagged')->take($limit); |
|
148
|
|
|
} |
|
149
|
|
|
public function scopeYesterdayMostVisitedPosts($query, $limit = 5) |
|
150
|
|
|
{ |
|
151
|
|
|
return $query->published()->with('author', 'tagged')->yesterday()->orderByViews()->take($limit); |
|
152
|
|
|
} |
|
153
|
|
|
public function scopeWeekMostVisitedPosts($query, $limit = 5) |
|
154
|
|
|
{ |
|
155
|
|
|
return $query->published()->with('author', 'tagged')->week()->orderByViews()->take($limit); |
|
156
|
|
|
} |
|
157
|
|
|
public function scopeMonthMostVisitedPosts($query, $limit = 5) |
|
158
|
|
|
{ |
|
159
|
|
|
return $query->published()->with('author', 'tagged')->month()->orderByViews()->take($limit); |
|
160
|
|
|
} |
|
161
|
|
|
public function scopeMostVisitedPostsChunked($query, $limit = 12) |
|
162
|
|
|
{ |
|
163
|
|
|
return $query->published()->with('author', 'tagged')->orderByViews()->take($limit)->get()->chunk((int) ($limit / 2)); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|