1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Xetaravel\Livewire\Forms; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Livewire\Attributes\Validate; |
10
|
|
|
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; |
11
|
|
|
use Livewire\Form; |
12
|
|
|
use Xetaio\Mentions\Parser\MentionParser; |
13
|
|
|
use Xetaravel\Models\BlogArticle; |
14
|
|
|
use Xetaravel\Models\BlogCategory; |
15
|
|
|
use Throwable; |
16
|
|
|
|
17
|
|
|
class BlogArticleForm extends Form |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The article to update. |
21
|
|
|
* |
22
|
|
|
* @var BlogArticle|null |
23
|
|
|
*/ |
24
|
|
|
public ?BlogArticle $blogArticle = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The title of the article. |
28
|
|
|
* |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
#[Validate('required|min:5')] |
32
|
|
|
public ?string $title = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The category of the article |
36
|
|
|
* |
37
|
|
|
* @var int|null |
38
|
|
|
*/ |
39
|
|
|
#[Validate('required|numeric|exists:blog_categories,id')] |
40
|
|
|
public ?int $blog_category_id = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The published date of the article. |
44
|
|
|
* |
45
|
|
|
* @var string|null |
46
|
|
|
*/ |
47
|
|
|
#[Validate('nullable|date_format:Y-m-d H:i')] |
48
|
|
|
public ?string $published_at = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The content of the post, only when creating. |
52
|
|
|
* |
53
|
|
|
* @var string|null |
54
|
|
|
*/ |
55
|
|
|
#[Validate('required|min:10')] |
56
|
|
|
public ?string $content = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The banner of the article. |
60
|
|
|
* |
61
|
|
|
* @var TemporaryUploadedFile|null |
62
|
|
|
*/ |
63
|
|
|
#[Validate('nullable|image|max:10240')] |
64
|
|
|
public ?TemporaryUploadedFile $banner = null; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The categories used in choice. |
68
|
|
|
* |
69
|
|
|
* @var Collection|array |
70
|
|
|
*/ |
71
|
|
|
public Collection|array $categoriesSearchable = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Function to store the model. |
75
|
|
|
* |
76
|
|
|
* @return BlogArticle |
77
|
|
|
* |
78
|
|
|
* @throws Throwable |
79
|
|
|
*/ |
80
|
|
|
public function create(): BlogArticle |
81
|
|
|
{ |
82
|
|
|
return DB::transaction(function () { |
83
|
|
|
$properties = [ |
84
|
|
|
'blog_category_id', |
85
|
|
|
'title', |
86
|
|
|
'content' |
87
|
|
|
]; |
88
|
|
|
if ($this->published_at) { |
89
|
|
|
$properties[] = 'published_at'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$blogArticle = BlogArticle::create($this->only($properties)); |
93
|
|
|
|
94
|
|
|
$parser = new MentionParser($blogArticle, [ |
|
|
|
|
95
|
|
|
'regex' => config('mentions.regex') |
96
|
|
|
]); |
97
|
|
|
$content = $parser->parse($blogArticle->content); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$blogArticle->content = $content; |
100
|
|
|
$blogArticle->save(); |
101
|
|
|
|
102
|
|
|
// Default banner for the article. |
103
|
|
|
$banner = public_path('images/articles/default_banner.jpg'); |
104
|
|
|
|
105
|
|
|
if (!is_null($this->banner)) { |
106
|
|
|
$banner = $this->banner; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$blogArticle->clearMediaCollection('article'); |
110
|
|
|
$blogArticle->addMedia($banner) |
111
|
|
|
->preservingOriginal() |
112
|
|
|
->setName(mb_substr(md5($blogArticle->title), 0, 10)) |
|
|
|
|
113
|
|
|
->setFileName(mb_substr(md5($blogArticle->title), 0, 10) . '.' . (is_string($banner) ? 'jpg' : $banner->getClientOriginalExtension())) |
114
|
|
|
->toMediaCollection('article'); |
115
|
|
|
|
116
|
|
|
return $blogArticle; |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Function to update the article. |
122
|
|
|
* |
123
|
|
|
* @return BlogArticle |
124
|
|
|
* |
125
|
|
|
* @throws Throwable |
126
|
|
|
*/ |
127
|
|
|
public function update(): BlogArticle |
128
|
|
|
{ |
129
|
|
|
return DB::transaction(function () { |
130
|
|
|
$this->blogArticle->title = $this->title; |
131
|
|
|
$this->blogArticle->blog_category_id = $this->blog_category_id; |
132
|
|
|
$this->blogArticle->published_at = $this->published_at; |
133
|
|
|
|
134
|
|
|
$parser = new MentionParser($this->blogArticle, [ |
|
|
|
|
135
|
|
|
'regex' => config('mentions.regex') |
136
|
|
|
]); |
137
|
|
|
$content = $parser->parse($this->content); |
138
|
|
|
|
139
|
|
|
$this->blogArticle->content = $content; |
140
|
|
|
$this->blogArticle->save(); |
|
|
|
|
141
|
|
|
|
142
|
|
|
if (!is_null($this->banner)) { |
143
|
|
|
$this->blogArticle->clearMediaCollection('article'); |
144
|
|
|
$this->blogArticle->addMedia($this->banner) |
145
|
|
|
->preservingOriginal() |
146
|
|
|
->setName(mb_substr(md5($this->blogArticle->title), 0, 10)) |
|
|
|
|
147
|
|
|
->setFileName(mb_substr(md5($this->blogArticle->title), 0, 10) . '.' . $this->banner->getClientOriginalExtension()) |
148
|
|
|
->toMediaCollection('article'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->blogArticle; |
152
|
|
|
}); |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Function to search categories. |
159
|
|
|
* |
160
|
|
|
* @param string $value |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function searchCategories(string $value = ''): void |
165
|
|
|
{ |
166
|
|
|
$selectedOption = BlogCategory::where('id', $this->blog_category_id)->get(); |
167
|
|
|
|
168
|
|
|
$categories = BlogCategory::query() |
169
|
|
|
->where('title', 'like', "%$value%"); |
170
|
|
|
|
171
|
|
|
$this->categoriesSearchable = $categories->take(10) |
172
|
|
|
->orderBy('title') |
|
|
|
|
173
|
|
|
->get() |
174
|
|
|
->merge($selectedOption); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|