|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Requests\PostStoreRequest; |
|
6
|
|
|
use App\Models\Post; |
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use Illuminate\Support\Facades\Auth; |
|
9
|
|
|
use Illuminate\Support\Facades\Config; |
|
10
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
|
11
|
|
|
|
|
12
|
|
|
class PostRepository implements PostRepositoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Get all Posts. |
|
17
|
|
|
* |
|
18
|
|
|
* @param int|null $recordsPerPage |
|
19
|
|
|
* @param array|null $searchParameters |
|
20
|
|
|
* |
|
21
|
|
|
* @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function getAll(int $recordsPerPage = null, array $searchParameters = null) |
|
24
|
|
|
{ |
|
25
|
1 |
|
$query = Post::orderBy('title', 'asc'); |
|
26
|
|
|
|
|
27
|
1 |
|
if (!is_null($searchParameters)) { |
|
28
|
|
|
if (!empty($searchParameters['title'])) { |
|
29
|
|
|
$query->where( |
|
30
|
|
|
'title', |
|
31
|
|
|
'like', |
|
32
|
|
|
'%' . $searchParameters['title'] . '%' |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
if (!empty($searchParameters['categoryId'])) { |
|
36
|
|
|
$query->where('category_id', $searchParameters['categoryId']); |
|
37
|
|
|
} |
|
38
|
|
|
if (!empty($searchParameters['startDate'])) { |
|
39
|
|
|
$startDate = Carbon::createFromFormat( |
|
40
|
|
|
'd/m/Y', |
|
41
|
|
|
$searchParameters['startDate'] |
|
42
|
|
|
); |
|
43
|
|
|
$query->where('created_at', '>=', $startDate); |
|
44
|
|
|
} |
|
45
|
|
|
if (!empty($searchParameters['endDate'])) { |
|
46
|
|
|
$endDate = Carbon::createFromFormat( |
|
47
|
|
|
'd/m/Y', |
|
48
|
|
|
$searchParameters['endDate'] |
|
49
|
|
|
); |
|
50
|
|
|
$query->where('created_at', '<=', $endDate); |
|
51
|
|
|
} |
|
52
|
|
|
if (!empty($searchParameters['status'])) { |
|
53
|
|
|
$query->currentStatus($searchParameters['status']); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
// Avoid retrieving the post used for the static image gallery |
|
58
|
1 |
|
$query->where('title->en', '!=', 'Static pages images'); |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
if ($recordsPerPage) { |
|
|
|
|
|
|
62
|
|
|
$results = $query->paginate($recordsPerPage)->withQueryString(); |
|
63
|
1 |
|
} else { |
|
64
|
|
|
$results = $query->get(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $results; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get Post by id |
|
72
|
2 |
|
* |
|
73
|
|
|
* @param int $postId |
|
74
|
2 |
|
* @return Post |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getById(int $postId): Post |
|
77
|
|
|
{ |
|
78
|
|
|
return Post::findOrFail($postId); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get Post by slug |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $postSlug |
|
85
|
1 |
|
* |
|
86
|
|
|
* @return Post |
|
87
|
1 |
|
*/ |
|
88
|
1 |
|
public function getBySlug(string $postSlug): Post |
|
89
|
|
|
{ |
|
90
|
|
|
return Post::where('slug', $postSlug)->first(); |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
/** |
|
94
|
|
|
* Store Post |
|
95
|
1 |
|
* |
|
96
|
1 |
|
* @param array $data |
|
97
|
|
|
* |
|
98
|
1 |
|
* @return Post |
|
99
|
|
|
* @throws \Spatie\ModelStatus\Exceptions\InvalidStatus |
|
100
|
|
|
*/ |
|
101
|
|
|
public function store(array $data): Post |
|
102
|
|
|
{ |
|
103
|
|
|
$post = new Post(); |
|
104
|
|
|
$post = self::assignDataAttributes($post, $data); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
// Creator - Logged user id or 1 for factories |
|
107
|
|
|
$post->user_id = !is_null(Auth::id()) ? Auth::id() : 1; |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
$post->save(); |
|
110
|
1 |
|
|
|
111
|
|
|
$post->tags()->sync($data['tag_ids'] ?? null); |
|
112
|
1 |
|
$post->setStatus('published'); |
|
113
|
1 |
|
|
|
114
|
|
|
return $post->fresh(); |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
/** |
|
118
|
|
|
* Update Post |
|
119
|
|
|
* |
|
120
|
1 |
|
* @param array $data |
|
121
|
1 |
|
* @param int $id |
|
122
|
1 |
|
* |
|
123
|
|
|
* @return Post |
|
124
|
|
|
* @throws \Spatie\ModelStatus\Exceptions\InvalidStatus |
|
125
|
1 |
|
*/ |
|
126
|
|
|
public function update(array $data, int $id): Post |
|
127
|
|
|
{ |
|
128
|
|
|
$post = $this->getById($id); |
|
129
|
|
|
$post = self::assignDataAttributes($post, $data); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
$post->update(); |
|
132
|
|
|
|
|
133
|
|
|
$post->tags()->sync($data['tag_ids'] ?? null); |
|
134
|
1 |
|
|
|
135
|
|
|
//$status = ($data['status'] == 'on') ? 'published' : 'unpublished'; |
|
136
|
1 |
|
$status = (isset($data['status'])) ? 'published' : 'unpublished'; |
|
137
|
1 |
|
if ($post->publishingStatus() != $status) { |
|
138
|
|
|
$post->setStatus($status); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $post; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Delete Post |
|
146
|
|
|
* |
|
147
|
2 |
|
* @param int $id |
|
148
|
|
|
* @return void |
|
149
|
2 |
|
*/ |
|
150
|
2 |
|
public function delete(int $id): void |
|
151
|
2 |
|
{ |
|
152
|
|
|
Post::destroy($id); |
|
153
|
2 |
|
} |
|
154
|
2 |
|
|
|
155
|
2 |
|
/** |
|
156
|
|
|
* Assign the attributes of the data array to the object |
|
157
|
2 |
|
* |
|
158
|
2 |
|
* @param \App\Models\Post $post |
|
159
|
2 |
|
* @param array $data |
|
160
|
|
|
* |
|
161
|
2 |
|
* @return \App\Models\Post |
|
162
|
|
|
*/ |
|
163
|
|
|
public function assignDataAttributes(Post $post, array $data): Post |
|
164
|
|
|
{ |
|
165
|
|
|
$post->title = $data['title'] ?? null; |
|
166
|
2 |
|
$post->category_id = $data['category_id'] ?? null; |
|
|
|
|
|
|
167
|
2 |
|
$post->intro_text = $data['intro_text'] ?? null; |
|
168
|
2 |
|
|
|
169
|
2 |
|
$post->body = $data['body'] ?? null; |
|
170
|
|
|
$post->before_content = $data['before_content'] ?? null; |
|
171
|
|
|
$post->after_content = $data['after_content'] ?? null; |
|
172
|
|
|
|
|
173
|
2 |
|
$post->featured = $data['featured'] ?? 0; |
|
174
|
|
|
$post->publish_at = $data['publish_at'] ?? null; |
|
175
|
|
|
$post->publish_until = $data['publish_until'] ?? null; |
|
176
|
|
|
|
|
177
|
|
|
if (isset($data['created_at'])) { |
|
178
|
|
|
$post->created_at = Carbon::createFromFormat('d/m/Y', $data['created_at']); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
// Translations |
|
182
|
|
|
foreach (LaravelLocalization::getSupportedLocales() as $countryCode => $countryAvTrans) { |
|
183
|
|
|
if ($countryCode != Config::get('app.fallback_locale')) { |
|
184
|
|
|
$post->setTranslation('title', $countryCode, $data['title_' . $countryCode] ?? null); |
|
185
|
|
|
$post->setTranslation('body', $countryCode, $data['body_' . $countryCode] ?? null); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $post; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: