|
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
|
|
|
public function manager(Manager $manager) |
|
25
|
|
|
{ |
|
26
|
82 |
|
$this->manager = $manager; |
|
27
|
|
|
} |
|
28
|
82 |
|
|
|
29
|
|
|
public static function key(): string |
|
30
|
82 |
|
{ |
|
31
|
|
|
return 'url'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
82 |
|
public function route($verb): ?string |
|
35
|
|
|
{ |
|
36
|
82 |
|
$routes = [ |
|
37
|
82 |
|
'check' => route('chief.back.assistants.url.check', [$this->manager->details()->key, $this->manager->existingModel()->id]), |
|
38
|
|
|
]; |
|
39
|
17 |
|
|
|
40
|
|
|
return $routes[$verb] ?? null; |
|
41
|
17 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function fields(): Fields |
|
44
|
1 |
|
{ |
|
45
|
|
|
return new Fields([ |
|
46
|
|
|
InputField::make('url-slugs') |
|
47
|
1 |
|
->validation( |
|
48
|
|
|
[ |
|
49
|
|
|
'url-slugs' => ['array', 'min:1', new UniqueUrlSlugRule($this->manager->modelInstance(), $this->manager->hasExistingModel() ? $this->manager->existingModel() : null)], |
|
50
|
1 |
|
], |
|
51
|
|
|
[], |
|
52
|
|
|
[ |
|
53
|
82 |
|
'url-slugs.*' => 'taalspecifieke link', |
|
54
|
|
|
]) |
|
55
|
82 |
|
->view('chief::back._fields.url-slugs') |
|
56
|
82 |
|
->viewData(['fields' => UrlSlugFields::fromModel($this->manager->modelInstance()) ]), |
|
57
|
82 |
|
]); |
|
58
|
|
|
} |
|
59
|
82 |
|
|
|
60
|
|
|
public function saveUrlSlugsField(Field $field, Request $request) |
|
61
|
82 |
|
{ |
|
62
|
|
|
(new SaveUrlSlugs($this->manager->existingModel()))->handle($request->get('url-slugs', [])); |
|
63
|
82 |
|
|
|
64
|
|
|
// Push update to homepage setting value |
|
65
|
82 |
|
// TODO: we should just fetch the homepages and push that instead... |
|
66
|
82 |
|
UrlRecord::getByModel($this->manager->existingModel())->reject(function ($record) { |
|
67
|
|
|
return ($record->isRedirect() || !$record->isHomepage()); |
|
68
|
|
|
})->each(function ($record) { |
|
69
|
|
|
app(ChangeHomepage::class)->onUrlChanged($record); |
|
70
|
76 |
|
}); |
|
71
|
|
|
} |
|
72
|
76 |
|
|
|
73
|
|
|
public function can($verb): bool |
|
74
|
|
|
{ |
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
75 |
|
|
|
78
|
|
|
private function validateModel() |
|
79
|
3 |
|
{ |
|
80
|
76 |
|
if (! $this->manager->existingModel() instanceof ProvidesUrl) { |
|
81
|
76 |
|
throw new \Exception('UrlAssistant requires the model interfaced by ' . ProvidesUrl::class . '.'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|