|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Thinktomorrow\Chief\Settings\Application; |
|
5
|
|
|
|
|
6
|
|
|
use Thinktomorrow\Chief\Concerns\Morphable\Morphables; |
|
7
|
|
|
use Thinktomorrow\Chief\FlatReferences\FlatReferenceFactory; |
|
8
|
|
|
use Thinktomorrow\Chief\Settings\Setting; |
|
9
|
|
|
use Thinktomorrow\Chief\Urls\Application\RevertUrlSlug; |
|
10
|
|
|
use Thinktomorrow\Chief\Urls\Application\SaveUrlSlugs; |
|
11
|
|
|
use Thinktomorrow\Chief\Urls\UrlRecord; |
|
12
|
|
|
|
|
13
|
|
|
class ChangeHomepage |
|
14
|
|
|
{ |
|
15
|
4 |
|
public function onSettingChanged(array $existingValues) |
|
16
|
|
|
{ |
|
17
|
4 |
|
$setting = Setting::findByKey(Setting::HOMEPAGE); |
|
18
|
|
|
|
|
19
|
4 |
|
$flatReferences = is_array($setting->value) |
|
|
|
|
|
|
20
|
2 |
|
? $setting->value |
|
21
|
4 |
|
: array_fill_keys(config('translatable.locales'), $setting->value); |
|
22
|
|
|
|
|
23
|
|
|
// Filter out empty values |
|
24
|
|
|
// foreach($flatReferences as $locale => $flatReferenceString) |
|
25
|
|
|
// { |
|
26
|
|
|
// if(!$flatReferenceString) { |
|
27
|
|
|
// unset($flatReferences[$locale]); |
|
28
|
|
|
// } |
|
29
|
|
|
// } |
|
30
|
|
|
|
|
31
|
4 |
|
foreach($flatReferences as $locale => $flatReferenceString) { |
|
32
|
|
|
|
|
33
|
4 |
|
if(!$flatReferenceString) { |
|
34
|
|
|
|
|
35
|
|
|
// TODO: when empty we'll remove the entry, we'll also want to revert to last redirect. |
|
36
|
|
|
|
|
37
|
2 |
|
if(isset($existingValues[$locale])) { |
|
38
|
1 |
|
$flatReferenceInstance = FlatReferenceFactory::fromString(($existingValues[$locale])); |
|
39
|
1 |
|
(new RevertUrlSlug($flatReferenceInstance->instance()))->handle($locale); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
1 |
|
continue; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Find the previous value so can alter it... |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
$flatReferenceInstance = FlatReferenceFactory::fromString($flatReferenceString); |
|
48
|
4 |
|
(new SaveUrlSlugs($flatReferenceInstance->instance()))->strict(false)->handle([$locale => '/']); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
3 |
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function onUrlChanged(UrlRecord $urlRecord) |
|
53
|
|
|
{ |
|
54
|
2 |
|
$model = Morphables::instance($urlRecord->model_type)->find($urlRecord->model_id); |
|
55
|
|
|
|
|
56
|
2 |
|
if(!$homepage = Setting::findByKey(Setting::HOMEPAGE)){ |
|
57
|
2 |
|
$homepage = Setting::create(['key' => Setting::HOMEPAGE, 'value' => []]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
$homepage->value = array_merge($homepage->value, [$urlRecord->locale => $model->flatReference()->get()]); |
|
|
|
|
|
|
61
|
2 |
|
$homepage->save(); |
|
62
|
|
|
} |
|
63
|
|
|
} |