|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\App\Http\Controllers\Back; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Thinktomorrow\Chief\Management\Managers; |
|
7
|
|
|
use Thinktomorrow\Chief\App\Http\Controllers\Controller; |
|
8
|
|
|
use Thinktomorrow\Chief\Management\Application\StoreManager; |
|
9
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\DeleteAborted; |
|
10
|
|
|
use Thinktomorrow\Chief\Management\Application\DeleteManager; |
|
11
|
|
|
use Thinktomorrow\Chief\Management\Application\UpdateManager; |
|
12
|
|
|
|
|
13
|
|
|
class ManagersController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Managers */ |
|
16
|
|
|
private $managers; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Managers $managers) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->managers = $managers; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function index(string $key) |
|
24
|
|
|
{ |
|
25
|
|
|
$manager = $this->managers->findByKey($key); |
|
26
|
|
|
|
|
27
|
|
|
$manager->guard('index'); |
|
28
|
|
|
|
|
29
|
|
|
$managers = $manager->findAllManaged(true); |
|
30
|
|
|
|
|
31
|
|
|
return view('chief::back.managers.index', [ |
|
32
|
|
|
'modelManager' => $manager, |
|
33
|
|
|
'managers' => $managers, |
|
34
|
|
|
]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function create(string $key) |
|
38
|
|
|
{ |
|
39
|
|
|
$manager = $this->managers->findByKey($key); |
|
40
|
|
|
|
|
41
|
|
|
$manager->guard('create'); |
|
42
|
|
|
|
|
43
|
|
|
return view('chief::back.managers.create', [ |
|
44
|
|
|
'manager' => $manager, |
|
45
|
|
|
]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function store(string $key, Request $request) |
|
49
|
|
|
{ |
|
50
|
|
|
$modelManager = $this->managers->findByKey($key); |
|
51
|
|
|
|
|
52
|
|
|
$manager = app(StoreManager::class)->handle($modelManager, $request); |
|
53
|
|
|
|
|
54
|
|
|
return redirect()->to($manager->route('edit')) |
|
55
|
|
|
->with('messages.success', '<i class="fa fa-fw fa-check-circle"></i> "' . $manager->details()->title . '" is toegevoegd'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function edit(string $key, $id) |
|
59
|
|
|
{ |
|
60
|
|
|
$manager = $this->managers->findByKey($key, $id); |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* If the manager does not contain a model, it means that this request tries |
|
64
|
|
|
* to retrieve a (soft) deleted model. In that case we kindly redirect |
|
65
|
|
|
* the admin to the managers index with a brief explanation. |
|
66
|
|
|
*/ |
|
67
|
|
|
if (!$manager->model()) { |
|
68
|
|
|
return redirect()->route('chief.back.dashboard')->with('messages.error', 'Oeps, de pagina die je probeerde te bewerken, is verwijderd of bestaat niet meer.'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$manager->guard('edit'); |
|
72
|
|
|
|
|
73
|
|
|
return view('chief::back.managers.edit', [ |
|
74
|
|
|
'manager' => $manager, |
|
75
|
|
|
]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function update(string $key, $id, Request $request) |
|
79
|
|
|
{ |
|
80
|
|
|
$manager = $this->managers->findByKey($key, $id); |
|
81
|
|
|
|
|
82
|
|
|
app(UpdateManager::class)->handle($manager, $request); |
|
83
|
|
|
|
|
84
|
|
|
return redirect()->to($manager->route('edit')) |
|
85
|
|
|
->with('messages.success', '<i class="fa fa-fw fa-check-circle"></i> "' . $manager->details()->title . '" werd aangepast'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function delete(string $key, $id, Request $request) |
|
89
|
|
|
{ |
|
90
|
|
|
$manager = $this->managers->findByKey($key, $id); |
|
91
|
|
|
|
|
92
|
|
|
try { |
|
93
|
|
|
app(DeleteManager::class)->handle($manager, $request); |
|
94
|
|
|
} catch (DeleteAborted $e) { |
|
95
|
|
|
return redirect()->back()->with('messages.warning', $manager->details()->singular . ' is niet verwijderd.'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return redirect()->to($manager->route('index')) |
|
99
|
|
|
->with('messages.success', '<i class="fa fa-fw fa-check-circle"></i> "' . $manager->details()->title . '" is verwijderd.'); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|