|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Marky |
|
5
|
|
|
* Date: 16/02/2018 |
|
6
|
|
|
* Time: 16:00. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Classes\Repositories; |
|
10
|
|
|
|
|
11
|
|
|
use App\Model\Menu; |
|
12
|
|
|
use App\Model\Article; |
|
13
|
|
|
use Illuminate\Support\Collection; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
15
|
|
|
use Illuminate\Support\Facades\DB; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ArticleRepository. |
|
19
|
|
|
*/ |
|
20
|
|
|
class ArticleRepository extends BaseRepository |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Article|Builder|Collection |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $model; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* PageRepository constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Article|Menu $model |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Article $model) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->model = $model; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Load all the articles that will be displayed dont he front page based on the criteria of being an public post. |
|
39
|
|
|
* |
|
40
|
|
|
* @ver 5.2.0 |
|
41
|
|
|
* @date 14/05/2018 |
|
42
|
|
|
* @return Builder|Collection |
|
43
|
|
|
*/ |
|
44
|
|
|
private function publicArticles() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->model->where([ |
|
|
|
|
|
|
47
|
|
|
['status', Article::STATUS_PUBLIC], |
|
48
|
|
|
['publish_date', '<=', DB::raw('NOW()')], |
|
49
|
|
|
['unpublish_date', '>=', DB::raw('NOW()')] |
|
50
|
|
|
])->orWhere([ |
|
51
|
|
|
['status', Article::STATUS_PUBLIC], |
|
52
|
|
|
['publish_date', '<=', DB::raw('NOW()')], |
|
53
|
|
|
['unpublish_date', '=', null]] |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function whereSitemappable() |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
return $this->model->where(['sitemap' => true, 'status' => true])->get(); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @ver 5.0.2 |
|
64
|
|
|
* @date 08/03/2018 |
|
65
|
|
|
* @param int $count |
|
66
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|Collection|static[] |
|
67
|
|
|
*/ |
|
68
|
|
|
public function latest($count = 7) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->publicArticles()->take($count)->orderBy('created_at', 'desc')->get(); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @ver 5.0.2 |
|
75
|
|
|
* @date 08/03/2018 |
|
76
|
|
|
* @param int $count |
|
77
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|Collection|static[] |
|
78
|
|
|
*/ |
|
79
|
|
|
public function mostViewed($count = 7) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->publicArticles()->orderBy('views', 'desc')->take($count)->get(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @ver 5.1.13 |
|
86
|
|
|
* @date 19/03/2018 |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function uniqueCreators() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->model->all()->unique('creator_id'); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @ver 5.1.14 |
|
96
|
|
|
* @date 19/03/2018 |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
|
|
public function viewable() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->model->where('status', Article::STATUS_PUBLIC)->get(); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param int $count |
|
106
|
|
|
* @return \Illuminate\Contracts\Pagination\Paginator |
|
107
|
|
|
*/ |
|
108
|
|
|
public function paginateLatest(int $count) |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->publicArticles()->orderBy('created_at', 'desc')->simplePaginate($count); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @ver 5.1.13 |
|
115
|
|
|
* @date 19/03/2018 |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
public function activeCategories() |
|
119
|
|
|
{ |
|
120
|
|
|
return app(ArticleCategoryRepository::class)->whereStatusActive(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @ver 5.1.15 |
|
125
|
|
|
* @date 19/03/2018 |
|
126
|
|
|
* @param int $creator_id |
|
127
|
|
|
* @return int |
|
128
|
|
|
*/ |
|
129
|
|
|
public function publishedArticlesCount(int $creator_id) |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
return $this->publicArticles()->where('creator_id', $creator_id)->count(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|mixed|static[] |
|
136
|
|
|
*/ |
|
137
|
|
|
public function whereStatusActive() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->model->where('status', true)->get(); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function collectArticle(string $slug) |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
return $this->publicArticles()->where('slug', $slug)->orderBy('created_at', 'desc')->first(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $text |
|
149
|
|
|
* @param int $paginate |
|
150
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
|
151
|
|
|
*/ |
|
152
|
|
|
public function searchThenPaginate(string $text, int $paginate = 7) |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->publicArticles()->search($text)->orderBy('created_at', 'desc')->paginate($paginate); |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function whereCreatorId(int $creator_id, int $paginate = 5) |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
return $this->publicArticles() |
|
160
|
|
|
->orderBy('created_at', 'desc') |
|
161
|
|
|
->where('creator_id', $creator_id) |
|
162
|
|
|
->simplePaginate($paginate); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function whereCategoryId(int $id, int $paginate = 5) |
|
|
|
|
|
|
166
|
|
|
{ |
|
167
|
|
|
return $this->publicArticles() |
|
168
|
|
|
->where('category_id', $id) |
|
169
|
|
|
->orderBy('created_at', 'desc') |
|
170
|
|
|
->simplePaginate($paginate); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|