1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebDevEtc\BlogEtc\Repositories; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Database\Eloquent\Collection; |
9
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
10
|
|
|
use WebDevEtc\BlogEtc\Exceptions\PostNotFoundException; |
11
|
|
|
use WebDevEtc\BlogEtc\Models\Post; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class BlogEtcPostsRepository. |
15
|
|
|
*/ |
16
|
|
|
class PostsRepository |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Post |
20
|
|
|
*/ |
21
|
|
|
private $model; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* BlogEtcPostsRepository constructor. |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Post $model) |
27
|
|
|
{ |
28
|
|
|
$this->model = $model; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Return blog posts ordered by posted_at, paginated. |
33
|
|
|
* |
34
|
|
|
* @param int $categoryID |
35
|
|
|
*/ |
36
|
|
|
public function indexPaginated(int $perPage = 10, int $categoryID = null): LengthAwarePaginator |
37
|
|
|
{ |
38
|
|
|
$query = $this->query(true) |
39
|
|
|
->orderBy('posted_at', 'desc'); |
40
|
|
|
|
41
|
|
|
if ($categoryID > 0) { |
42
|
|
|
$query->whereHas('categories', static function (Builder $query) use ($categoryID) { |
43
|
|
|
$query->where('blog_etc_post_categories.blog_etc_category_id', $categoryID); |
44
|
|
|
})->get(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $query->paginate($perPage); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return new instance of the Query Builder for this model. |
52
|
|
|
*/ |
53
|
|
|
public function query(bool $eagerLoad = false): Builder |
54
|
|
|
{ |
55
|
|
|
$queryBuilder = $this->model->newQuery(); |
56
|
|
|
|
57
|
|
|
if (true === $eagerLoad) { |
58
|
|
|
$queryBuilder->with(['categories']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $queryBuilder; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return posts for RSS feed. |
66
|
|
|
* |
67
|
|
|
* @return Builder[]|Collection |
68
|
|
|
*/ |
69
|
|
|
public function rssItems(): Collection |
70
|
|
|
{ |
71
|
|
|
return $this->query(false) |
72
|
|
|
->orderBy('posted_at', 'desc') |
73
|
|
|
->limit(config('blogetc.rssfeed.posts_to_show_in_rss_feed')) |
74
|
|
|
->with('author') |
75
|
|
|
->get(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Find a blog etc post by slug |
80
|
|
|
* If cannot find, throw exception. |
81
|
|
|
*/ |
82
|
|
|
public function findBySlug(string $slug): Post |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
// the published_at + is_published are handled by BlogEtcPublishedScope, and don't take effect if the |
86
|
|
|
// logged in user can manage log posts |
87
|
|
|
return $this->query(true) |
88
|
|
|
->where('slug', $slug) |
89
|
|
|
->firstOrFail(); |
90
|
|
|
} catch (ModelNotFoundException $e) { |
91
|
|
|
throw new PostNotFoundException('Unable to find blog post with slug: '.$slug); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Find a blog etc post by ID |
97
|
|
|
* If cannot find, throw exception. |
98
|
|
|
*/ |
99
|
|
|
public function findById(int $id): Post |
100
|
|
|
{ |
101
|
|
|
try { |
102
|
|
|
// the published_at + is_published are handled by BlogEtcPublishedScope, and don't take effect if the |
103
|
|
|
// logged in user can manage log posts |
104
|
|
|
return $this->query(true) |
105
|
|
|
->where('id', $id) |
106
|
|
|
->firstOrFail(); |
107
|
|
|
} catch (ModelNotFoundException $e) { |
108
|
|
|
throw new PostNotFoundException('Unable to find blog post with id: '.$id); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Create a new BlogEtcPost post. |
114
|
|
|
*/ |
115
|
|
|
public function create(array $attributes): Post |
116
|
|
|
{ |
117
|
|
|
return $this->query()->create($attributes); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Delete a post. |
122
|
|
|
* |
123
|
|
|
* @throws Exception |
124
|
|
|
*/ |
125
|
|
|
public function delete(int $postID): bool |
126
|
|
|
{ |
127
|
|
|
$post = $this->find($postID); |
128
|
|
|
|
129
|
|
|
return (bool) $post->delete(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Find a blog etc post by ID |
134
|
|
|
* If cannot find, throw exception. |
135
|
|
|
*/ |
136
|
|
|
public function find(int $blogEtcPostID): Post |
137
|
|
|
{ |
138
|
|
|
try { |
139
|
|
|
return $this->query(true)->findOrFail($blogEtcPostID); |
140
|
|
|
} catch (ModelNotFoundException $e) { |
141
|
|
|
throw new PostNotFoundException('Unable to find blog post with ID: '.$blogEtcPostID); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Update image sizes (or in theory any attribute) on a blog etc post. |
147
|
|
|
* |
148
|
|
|
* TODO - currently untested. |
149
|
|
|
* |
150
|
|
|
* @param array $uploadedImages |
151
|
|
|
*/ |
152
|
|
|
public function updateImageSizes(Post $post, ?array $uploadedImages): Post |
153
|
|
|
{ |
154
|
|
|
if (!empty($uploadedImages)) { |
155
|
|
|
// does not use update() here as it would require fillable for each field - and in theory someone |
156
|
|
|
// might want to add more image sizes. |
157
|
|
|
foreach ($uploadedImages as $size => $imageName) { |
158
|
|
|
$post->$size = $imageName; |
159
|
|
|
} |
160
|
|
|
$post->save(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $post; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Search for posts. |
168
|
|
|
* |
169
|
|
|
* This is a rough implementation - proper full text search has been removed in current version. |
170
|
|
|
*/ |
171
|
|
|
public function search(string $search, int $max = 25): Collection |
172
|
|
|
{ |
173
|
|
|
$query = $this->query(true)->limit($max); |
174
|
|
|
|
175
|
|
|
trim($search) |
176
|
|
|
? $query->where('title', 'like', '%'.$search) |
177
|
|
|
: $query->where('title', ''); |
178
|
|
|
|
179
|
|
|
return $query->get(); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|