1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Management\Assistants; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Thinktomorrow\Chief\Urls\MemoizedUrlRecord; |
7
|
|
|
use Thinktomorrow\Chief\Urls\SaveUrlSlugs; |
8
|
|
|
use Thinktomorrow\Chief\Urls\UrlSlugFields; |
9
|
|
|
use Thinktomorrow\Chief\Urls\ValidationRules\UniqueUrlSlugRule; |
10
|
|
|
use Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl; |
11
|
|
|
use Thinktomorrow\Chief\Fields\Fields; |
12
|
|
|
use Thinktomorrow\Chief\Fields\Types\Field; |
13
|
|
|
use Thinktomorrow\Chief\Fields\Types\InputField; |
14
|
|
|
use Thinktomorrow\Chief\Management\Manager; |
15
|
|
|
|
16
|
|
|
class UrlAssistant implements Assistant |
17
|
|
|
{ |
18
|
|
|
private $manager; |
19
|
|
|
|
20
|
|
|
private $model; |
21
|
|
|
|
22
|
|
|
private $urlRecords; |
23
|
|
|
|
24
|
64 |
|
public function manager(Manager $manager) |
25
|
|
|
{ |
26
|
64 |
|
$this->manager = $manager; |
27
|
|
|
|
28
|
64 |
|
if (! $manager->model() instanceof ProvidesUrl) { |
29
|
|
|
throw new \Exception('UrlAssistant requires the model interfaced by ' . ProvidesUrl::class . '.'); |
30
|
|
|
} |
31
|
|
|
|
32
|
64 |
|
$this->model = $manager->model(); |
33
|
|
|
|
34
|
64 |
|
$this->urlRecords = MemoizedUrlRecord::getByModel($this->model); |
35
|
64 |
|
} |
36
|
|
|
|
37
|
53 |
|
public static function key(): string |
38
|
|
|
{ |
39
|
53 |
|
return 'url'; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function route($verb): ?string |
43
|
|
|
{ |
44
|
|
|
$routes = [ |
45
|
2 |
|
'check' => route('chief.back.assistants.url.check', [$this->manager->details()->key, $this->manager->model()->id]), |
|
|
|
|
46
|
|
|
]; |
47
|
|
|
|
48
|
2 |
|
return $routes[$verb] ?? null; |
49
|
|
|
} |
50
|
|
|
|
51
|
64 |
|
public function fields(): Fields |
52
|
|
|
{ |
53
|
64 |
|
return new Fields([ |
54
|
64 |
|
InputField::make('url-slugs') |
55
|
64 |
|
->validation( |
56
|
|
|
[ |
57
|
64 |
|
'url-slugs' => ['array', 'min:1', new UniqueUrlSlugRule(($this->model && $this->model->exists) ? $this->model : null),], |
|
|
|
|
58
|
|
|
], |
59
|
64 |
|
[], |
60
|
|
|
[ |
61
|
64 |
|
'url-slugs.*' => 'taalspecifieke link', |
62
|
|
|
]) |
63
|
64 |
|
->view('chief::back._fields.url-slugs') |
64
|
64 |
|
->viewData(['fields' => UrlSlugFields::fromModel($this->model) ]), |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
58 |
|
public function saveUrlSlugsField(Field $field, Request $request) |
69
|
|
|
{ |
70
|
58 |
|
(new SaveUrlSlugs($this->model))->handle($request->get('url-slugs', [])); |
71
|
58 |
|
} |
72
|
|
|
|
73
|
|
|
public function can($verb): bool |
74
|
|
|
{ |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function guard($verb): Assistant |
79
|
|
|
{ |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.