|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Thinktomorrow\Chief\Management\Assistants; |
|
6
|
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
use Thinktomorrow\Chief\Audit\Audit; |
|
10
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\NotAllowedManagerRoute; |
|
11
|
|
|
use Thinktomorrow\Chief\Management\Manager; |
|
12
|
|
|
use Thinktomorrow\Chief\Management\Managers; |
|
13
|
|
|
use Thinktomorrow\Chief\States\PageState; |
|
14
|
|
|
use Thinktomorrow\Chief\Urls\ProvidesUrl\ProvidesUrl; |
|
15
|
|
|
|
|
16
|
|
|
class PublishAssistant implements Assistant |
|
17
|
|
|
{ |
|
18
|
|
|
private $manager; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Managers */ |
|
21
|
|
|
private $managers; |
|
22
|
|
|
|
|
23
|
79 |
|
public function __construct(Managers $managers) |
|
24
|
|
|
{ |
|
25
|
79 |
|
$this->managers = $managers; |
|
26
|
79 |
|
} |
|
27
|
|
|
|
|
28
|
79 |
|
public function manager(Manager $manager) |
|
29
|
|
|
{ |
|
30
|
79 |
|
$this->manager = $manager; |
|
31
|
79 |
|
} |
|
32
|
|
|
|
|
33
|
97 |
|
public static function key(): string |
|
34
|
|
|
{ |
|
35
|
97 |
|
return 'publish'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
public function isPublished(): bool |
|
39
|
|
|
{ |
|
40
|
3 |
|
return $this->manager->existingModel()->isPublished(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
public function isDraft(): bool |
|
44
|
|
|
{ |
|
45
|
2 |
|
return $this->manager->existingModel()->isDraft(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function publishedAt(): Carbon |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->manager->existingModel()->published_at; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
public function publish() |
|
54
|
|
|
{ |
|
55
|
3 |
|
$this->guard('publish'); |
|
56
|
|
|
|
|
57
|
3 |
|
PageState::make($this->manager->existingModel())->apply('publish'); |
|
58
|
2 |
|
$this->manager->existingModel()->save(); |
|
59
|
|
|
|
|
60
|
2 |
|
Audit::activity() |
|
61
|
2 |
|
->performedOn($this->manager->existingModel()) |
|
62
|
2 |
|
->log('published'); |
|
63
|
|
|
|
|
64
|
2 |
|
return redirect()->back()->with('messages.success', $this->manager->details()->title . ' is online gezet.'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
public function unpublish() |
|
68
|
|
|
{ |
|
69
|
3 |
|
$this->guard('unpublish'); |
|
70
|
|
|
|
|
71
|
3 |
|
PageState::make($this->manager->existingModel())->apply('unpublish'); |
|
72
|
2 |
|
$this->manager->existingModel()->save(); |
|
73
|
|
|
|
|
74
|
2 |
|
Audit::activity() |
|
75
|
2 |
|
->performedOn($this->manager->existingModel()) |
|
76
|
2 |
|
->log('unpublished'); |
|
77
|
|
|
|
|
78
|
2 |
|
return redirect()->back()->with('messages.success', $this->manager->details()->title . ' is offline gehaald.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function findAll(): Collection |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->manager->existingModel()->published()->get()->map(function ($model) { |
|
84
|
|
|
return $this->managers->findByModel($model); |
|
85
|
|
|
}); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
9 |
|
public function route($verb): ?string |
|
89
|
|
|
{ |
|
90
|
|
|
$modelRoutes = [ |
|
91
|
9 |
|
'publish' => route('chief.back.assistants.update', [ |
|
92
|
9 |
|
$this->key(), |
|
93
|
9 |
|
'publish', |
|
94
|
9 |
|
$this->manager->details()->key, |
|
95
|
9 |
|
$this->manager->existingModel()->id, |
|
96
|
|
|
]), |
|
97
|
9 |
|
'unpublish' => route('chief.back.assistants.update', [ |
|
98
|
9 |
|
$this->key(), |
|
99
|
9 |
|
'unpublish', |
|
100
|
9 |
|
$this->manager->details()->key, |
|
101
|
9 |
|
$this->manager->existingModel()->id, |
|
102
|
|
|
]), |
|
103
|
|
|
]; |
|
104
|
|
|
|
|
105
|
9 |
|
return $modelRoutes[$verb] ?? null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
6 |
|
public function can($verb): bool |
|
109
|
|
|
{ |
|
110
|
6 |
|
return !is_null($this->route($verb)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
6 |
|
private function guard($verb): Assistant |
|
114
|
|
|
{ |
|
115
|
6 |
|
if (!$this->can($verb)) { |
|
116
|
|
|
NotAllowedManagerRoute::notAllowedVerb($verb, $this->manager); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
6 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
4 |
|
public function hasUrl(): bool |
|
123
|
|
|
{ |
|
124
|
4 |
|
return $this->manager->existingModel() instanceof ProvidesUrl; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
3 |
|
public function publicationStatusAsLabel($plain = false) |
|
128
|
|
|
{ |
|
129
|
3 |
|
$label = $this->publicationStatusAsPlainLabel(); |
|
130
|
3 |
|
$class = ''; |
|
131
|
|
|
|
|
132
|
3 |
|
if ($this->isPublished()) { |
|
133
|
1 |
|
$class = 'text-success'; |
|
134
|
2 |
|
} elseif ($this->isDraft()) { |
|
135
|
2 |
|
$class = 'text-error'; |
|
136
|
|
|
} elseif ($this->manager->isAssistedBy('archive') && $this->manager->assistant('archive')->isArchived()) { |
|
137
|
|
|
$class = 'text-warning'; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
3 |
|
$statusAsLabel = '<span class="' . $class . '">' . $label . '</span>'; |
|
141
|
|
|
|
|
142
|
3 |
|
if (!$plain && $this->hasUrl()) { |
|
143
|
2 |
|
$statusAsLabel = '<a href="' . $this->url() . '" target="_blank">' . $statusAsLabel . '</a>'; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
return $statusAsLabel; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
3 |
|
private function publicationStatusAsPlainLabel() |
|
150
|
|
|
{ |
|
151
|
3 |
|
if ($this->isPublished()) { |
|
152
|
1 |
|
return 'online'; |
|
153
|
2 |
|
} elseif ($this->isDraft()) { |
|
154
|
2 |
|
return 'offline'; |
|
155
|
|
|
} elseif ($this->manager->isAssistedBy('archive') && $this->manager->assistant('archive')->isArchived()) { |
|
156
|
|
|
return 'gearchiveerd'; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return '-'; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
4 |
|
public function url(): string |
|
163
|
|
|
{ |
|
164
|
4 |
|
return $this->manager->existingModel()->url(); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|