|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Menu\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Menu\Service\MenuService; |
|
8
|
|
|
use Std\AbstractController; |
|
9
|
|
|
use Std\FilterException; |
|
10
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
11
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
12
|
|
|
use Zend\Expressive\Router\RouterInterface as Router; |
|
13
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface as Template; |
|
14
|
|
|
|
|
15
|
|
|
class IndexController extends AbstractController |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Template |
|
19
|
|
|
*/ |
|
20
|
|
|
private $template; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Router |
|
24
|
|
|
*/ |
|
25
|
|
|
private $router; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var MenuService |
|
29
|
|
|
*/ |
|
30
|
|
|
private $menuService; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(Template $template, Router $router, MenuService $menuService) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->template = $template; |
|
35
|
|
|
$this->router = $router; |
|
36
|
|
|
$this->menuService = $menuService; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function index() |
|
40
|
|
|
{ |
|
41
|
|
|
return new HtmlResponse( |
|
42
|
|
|
$this->template->render( |
|
43
|
|
|
'menu::index/index', [ |
|
44
|
|
|
'menuNestedItems' => $this->menuService->getNestedAll(), |
|
45
|
|
|
'layout' => 'layout/admin', |
|
46
|
|
|
] |
|
47
|
|
|
) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function edit($errors = []) |
|
52
|
|
|
{ |
|
53
|
|
|
$id = $this->request->getAttribute('id'); |
|
54
|
|
|
$item = $this->menuService->get($id); |
|
55
|
|
|
|
|
56
|
|
|
if ($this->request->getParsedBody()) { |
|
57
|
|
|
$item = (object) ($this->request->getParsedBody() + (array) $item); |
|
58
|
|
|
$item->menu_id = $id; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return new HtmlResponse( |
|
62
|
|
|
$this->template->render( |
|
63
|
|
|
'menu::index/edit', [ |
|
64
|
|
|
'id' => $id, |
|
65
|
|
|
'item' => $item, |
|
66
|
|
|
'menuItems' => $this->menuService->getForSelect(), |
|
67
|
|
|
'errors' => $errors, |
|
68
|
|
|
'layout' => 'layout/admin', |
|
69
|
|
|
] |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
View Code Duplication |
public function save() |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
try { |
|
77
|
|
|
$id = $this->request->getAttribute('id'); |
|
78
|
|
|
$data = $this->request->getParsedBody(); |
|
79
|
|
|
|
|
80
|
|
|
if ($id) { |
|
81
|
|
|
$this->menuService->updateMenuItem($data, $id); |
|
82
|
|
|
} else { |
|
83
|
|
|
$this->menuService->addMenuItem($data); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.menu')); |
|
87
|
|
|
} catch (FilterException $fe) { |
|
88
|
|
|
return $this->edit($fe->getArrayMessages()); |
|
89
|
|
|
} catch (\Exception $e) { |
|
90
|
|
|
throw $e; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function delete() |
|
95
|
|
|
{ |
|
96
|
|
|
try { |
|
97
|
|
|
$id = $this->request->getAttribute('id'); |
|
98
|
|
|
$this->menuService->delete($id); |
|
99
|
|
|
|
|
100
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.menu')); |
|
101
|
|
|
} catch (\Exception $e) { |
|
102
|
|
|
throw $e; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function updateOrder() |
|
107
|
|
|
{ |
|
108
|
|
|
$data = $this->request->getParsedBody(); |
|
109
|
|
|
$menuOrder = isset($data['order']) ? json_decode($data['order']) : []; |
|
110
|
|
|
$status = $this->menuService->updateMenuOrder($menuOrder); |
|
111
|
|
|
|
|
112
|
|
|
return new JsonResponse(['status' => $status]); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.