|
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
|
65 |
|
public function manager(Manager $manager) |
|
25
|
|
|
{ |
|
26
|
65 |
|
$this->manager = $manager; |
|
27
|
|
|
|
|
28
|
65 |
|
if (! $manager->model() instanceof ProvidesUrl) { |
|
29
|
|
|
throw new \Exception('UrlAssistant requires the model interfaced by ' . ProvidesUrl::class . '.'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
65 |
|
$this->model = $manager->model(); |
|
33
|
|
|
|
|
34
|
65 |
|
$this->urlRecords = MemoizedUrlRecord::getByModel($this->model); |
|
35
|
65 |
|
} |
|
36
|
|
|
|
|
37
|
53 |
|
public static function key(): string |
|
38
|
|
|
{ |
|
39
|
53 |
|
return 'url'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function route($verb): ?string |
|
43
|
|
|
{ |
|
44
|
|
|
$routes = [ |
|
45
|
1 |
|
'check' => route('chief.back.assistants.url.check', [$this->manager->details()->key, $this->manager->model()->id]), |
|
|
|
|
|
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
1 |
|
return $routes[$verb] ?? null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
65 |
|
public function fields(): Fields |
|
52
|
|
|
{ |
|
53
|
65 |
|
return new Fields([ |
|
54
|
65 |
|
InputField::make('url-slugs') |
|
55
|
65 |
|
->validation( |
|
56
|
|
|
[ |
|
57
|
65 |
|
'url-slugs' => ['array', 'min:1', new UniqueUrlSlugRule(($this->model && $this->model->exists) ? $this->model : null),], |
|
|
|
|
|
|
58
|
|
|
], |
|
59
|
65 |
|
[], |
|
60
|
|
|
[ |
|
61
|
65 |
|
'url-slugs.*' => 'taalspecifieke link', |
|
62
|
|
|
]) |
|
63
|
65 |
|
->view('chief::back._fields.url-slugs') |
|
64
|
65 |
|
->viewData(['fields' => UrlSlugFields::fromModel($this->model) ]), |
|
65
|
|
|
]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
59 |
|
public function saveUrlSlugsField(Field $field, Request $request) |
|
69
|
|
|
{ |
|
70
|
59 |
|
(new SaveUrlSlugs($this->model))->handle($request->get('url-slugs', [])); |
|
71
|
59 |
|
} |
|
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.