1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Management\Assistants; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Thinktomorrow\Chief\Settings\Application\ChangeHomepage; |
7
|
|
|
use Thinktomorrow\Chief\Urls\MemoizedUrlRecord; |
8
|
|
|
use Thinktomorrow\Chief\Urls\Application\SaveUrlSlugs; |
9
|
|
|
use Thinktomorrow\Chief\Urls\UrlRecord; |
10
|
|
|
use Thinktomorrow\Chief\Urls\UrlSlugFields; |
11
|
|
|
use Thinktomorrow\Chief\Urls\ValidationRules\UniqueUrlSlugRule; |
12
|
|
|
use Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl; |
13
|
|
|
use Thinktomorrow\Chief\Fields\Fields; |
14
|
|
|
use Thinktomorrow\Chief\Fields\Types\Field; |
15
|
|
|
use Thinktomorrow\Chief\Fields\Types\InputField; |
16
|
|
|
use Thinktomorrow\Chief\Management\Manager; |
17
|
|
|
|
18
|
|
|
class UrlAssistant implements Assistant |
19
|
|
|
{ |
20
|
|
|
private $manager; |
21
|
|
|
|
22
|
|
|
private $model; |
23
|
|
|
|
24
|
|
|
private $urlRecords; |
25
|
|
|
|
26
|
73 |
|
public function manager(Manager $manager) |
27
|
|
|
{ |
28
|
73 |
|
$this->manager = $manager; |
29
|
|
|
|
30
|
73 |
|
if (! $manager->model() instanceof ProvidesUrl) { |
31
|
|
|
throw new \Exception('UrlAssistant requires the model interfaced by ' . ProvidesUrl::class . '.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
73 |
|
$this->model = $manager->model(); |
35
|
|
|
|
36
|
73 |
|
$this->urlRecords = MemoizedUrlRecord::getByModel($this->model); |
37
|
73 |
|
} |
38
|
|
|
|
39
|
61 |
|
public static function key(): string |
40
|
|
|
{ |
41
|
61 |
|
return 'url'; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function route($verb): ?string |
45
|
|
|
{ |
46
|
|
|
$routes = [ |
47
|
1 |
|
'check' => route('chief.back.assistants.url.check', [$this->manager->details()->key, $this->manager->model()->id]), |
48
|
|
|
]; |
49
|
|
|
|
50
|
1 |
|
return $routes[$verb] ?? null; |
51
|
|
|
} |
52
|
|
|
|
53
|
73 |
|
public function fields(): Fields |
54
|
|
|
{ |
55
|
73 |
|
return new Fields([ |
56
|
73 |
|
InputField::make('url-slugs') |
57
|
73 |
|
->validation( |
58
|
|
|
[ |
59
|
73 |
|
'url-slugs' => ['array', 'min:1', new UniqueUrlSlugRule(($this->model && $this->model->exists) ? $this->model : null),], |
60
|
|
|
], |
61
|
73 |
|
[], |
62
|
|
|
[ |
63
|
73 |
|
'url-slugs.*' => 'taalspecifieke link', |
64
|
|
|
]) |
65
|
73 |
|
->view('chief::back._fields.url-slugs') |
66
|
73 |
|
->viewData(['fields' => UrlSlugFields::fromModel($this->model) ]), |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
67 |
|
public function saveUrlSlugsField(Field $field, Request $request) |
71
|
|
|
{ |
72
|
67 |
|
(new SaveUrlSlugs($this->model))->handle($request->get('url-slugs', [])); |
73
|
|
|
|
74
|
|
|
// Push update to homepage setting value |
75
|
|
|
// TODO: we should just fetch the homepages and push that instead... |
76
|
|
|
UrlRecord::getByModel($this->model)->reject(function ($record) { |
77
|
66 |
|
return ($record->isRedirect() || !$record->isHomepage()); |
78
|
|
|
})->each(function ($record) { |
79
|
2 |
|
app(ChangeHomepage::class)->onUrlChanged($record); |
80
|
67 |
|
}); |
81
|
67 |
|
} |
82
|
|
|
|
83
|
|
|
public function can($verb): bool |
84
|
|
|
{ |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function guard($verb): Assistant |
89
|
|
|
{ |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|