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