-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTrainingResource.php
More file actions
144 lines (118 loc) · 3.65 KB
/
TrainingResource.php
File metadata and controls
144 lines (118 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace App;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class TrainingResource extends Model
{
use HasFactory;
protected $fillable = [
'slug',
'card_title',
'card_author',
'card_image',
'page_title',
'hero_author',
'intro',
'highlight_box',
'video_url',
'body_image',
'body_image_alt',
'content',
'button_text',
'button_url',
'secondary_button_text',
'secondary_button_url',
'meta_title',
'meta_description',
'position',
'active',
];
protected $casts = [
'active' => 'boolean',
'position' => 'integer',
];
public function scopeActive($query)
{
return $query->where('active', true);
}
public function scopeOrdered($query)
{
return $query->orderBy('position')->orderBy('created_at', 'desc');
}
public function getRouteKeyName(): string
{
return 'slug';
}
protected static function booted(): void
{
static::saving(function (self $resource) {
if (blank($resource->slug)) {
$baseSlug = Str::slug($resource->card_title ?: $resource->page_title ?: 'training-resource');
$resource->slug = $resource->generateUniqueSlug($baseSlug ?: 'training-resource');
}
if (blank($resource->card_title)) {
$resource->card_title = $resource->page_title ?: Str::headline($resource->slug);
}
if (blank($resource->page_title)) {
$resource->page_title = $resource->card_title;
}
});
}
protected function generateUniqueSlug(string $baseSlug): string
{
$slug = $baseSlug;
$counter = 1;
while (self::query()
->where('slug', $slug)
->when($this->exists, fn ($query) => $query->where('id', '!=', $this->id))
->exists()) {
$slug = $baseSlug.'-'.$counter;
$counter++;
}
return $slug;
}
public function getResolvedCardImageAttribute(): string
{
$image = trim((string) $this->card_image);
if ($image === '') {
return '/img/learning/cody-color-kit.png';
}
// Allow absolute URLs (including Amazon S3/CloudFront), protocol-relative, or root-relative paths.
if (Str::startsWith($image, ['http://', 'https://', '//', '/'])) {
return $image;
}
// Backward-compatible shorthand: treat plain filenames as /img/learning/{filename}.
return '/img/learning/'.$image;
}
public function getResolvedBodyImageAttribute(): ?string
{
$image = trim((string) $this->body_image);
if ($image === '') {
return null;
}
if (Str::startsWith($image, ['http://', 'https://', '//', '/'])) {
return $image;
}
return '/img/learning/'.$image;
}
public function getYoutubeVideoIdAttribute(): ?string
{
$url = trim((string) $this->video_url);
if ($url === '') {
return null;
}
$patterns = [
'/youtu\.be\/([a-zA-Z0-9_-]{11})/i',
'/youtube\.com\/watch\?v=([a-zA-Z0-9_-]{11})/i',
'/youtube\.com\/embed\/([a-zA-Z0-9_-]{11})/i',
'/youtube\.com\/shorts\/([a-zA-Z0-9_-]{11})/i',
];
foreach ($patterns as $pattern) {
if (preg_match($pattern, $url, $matches) === 1) {
return $matches[1];
}
}
return null;
}
}