-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailEventController.php
117 lines (98 loc) · 3.32 KB
/
EmailEventController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace KodiCMS\Email\Http\Controllers;
use KodiCMS\Email\Repository\EmailEventRepository;
use KodiCMS\CMS\Http\Controllers\System\BackendController;
class EmailEventController extends BackendController
{
/**
* @param EmailEventRepository $repository
*/
public function getIndex(EmailEventRepository $repository)
{
$emailEvents = $repository->paginate();
$this->setContent('email.event.list', compact('emailEvents'));
}
/**
* @param EmailEventRepository $repository
*/
public function getCreate(EmailEventRepository $repository)
{
$this->setTitle(trans('email::core.title.events.create'));
$emailEvent = $repository->instance();
$action = 'backend.email.event.create.post';
$this->setContent('email.event.form', compact('emailEvent', 'action'));
}
/**
* @param EmailEventRepository $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postCreate(EmailEventRepository $repository)
{
$data = $this->request->all();
$this->formatFields($data);
$repository->validateOnCreate($data);
$emailEvent = $repository->create($data);
return $this->smartRedirect([$emailEvent])
->with('success', trans('email::core.messages.events.created', [
'title' => $emailEvent->name,
]));
}
/**
* @param EmailEventRepository $repository
* @param $id
*/
public function getEdit(EmailEventRepository $repository, $id)
{
$emailEvent = $repository->findOrFail($id);
$this->setTitle(trans('email::core.title.events.edit', [
'title' => $emailEvent->name,
]));
$action = 'backend.email.event.edit.post';
$this->setContent('email.event.form', compact('emailEvent', 'action'));
}
/**
* @param EmailEventRepository $repository
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postEdit(EmailEventRepository $repository, $id)
{
$data = $this->request->all();
$this->formatFields($data);
$repository->validateOnUpdate($data);
$emailEvent = $repository->update($id, $data);
return $this->smartRedirect([$emailEvent])
->with('success', trans('email::core.messages.events.updated', [
'title' => $emailEvent->name,
]));
}
/**
* @param EmailEventRepository $repository
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
public function postDelete(EmailEventRepository $repository, $id)
{
$emailEvent = $repository->delete($id);
return $this->smartRedirect()
->with('success', trans('email::core.messages.events.deleted', [
'title' => $emailEvent->name,
]));
}
/**
* @param array $data
*/
protected function formatFields(&$data)
{
$fields = array_get($data, 'fields', []);
$keys = array_get($fields, 'key', []);
$names = array_get($fields, 'value', []);
$value = array_combine($keys, $names);
$value = array_unique(array_filter($value));
array_set($data, 'fields', $value);
}
}