@@ 18-158 (lines=141) @@ | ||
15 | use Zend\Http\PhpEnvironment\Request; |
|
16 | use Zend\Session\SessionManager; |
|
17 | ||
18 | class PostController extends AbstractController |
|
19 | { |
|
20 | /** |
|
21 | * @var Template |
|
22 | */ |
|
23 | private $template; |
|
24 | ||
25 | /** |
|
26 | * @var PostService |
|
27 | */ |
|
28 | private $postService; |
|
29 | ||
30 | /** |
|
31 | * @var SessionManager |
|
32 | */ |
|
33 | private $session; |
|
34 | ||
35 | /** |
|
36 | * @var Router |
|
37 | */ |
|
38 | private $router; |
|
39 | ||
40 | /** |
|
41 | * @var CategoryService |
|
42 | */ |
|
43 | private $categoryService; |
|
44 | ||
45 | /** |
|
46 | * PostController constructor. |
|
47 | * |
|
48 | * @param Template $template |
|
49 | * @param Router $router |
|
50 | * @param PostService $postService |
|
51 | * @param SessionManager $session |
|
52 | * @param CategoryService $categoryService |
|
53 | */ |
|
54 | public function __construct( |
|
55 | Template $template, |
|
56 | Router $router, |
|
57 | PostService $postService, |
|
58 | SessionManager $session, |
|
59 | CategoryService $categoryService |
|
60 | ) { |
|
61 | $this->template = $template; |
|
62 | $this->postService = $postService; |
|
63 | $this->session = $session; |
|
64 | $this->router = $router; |
|
65 | $this->categoryService = $categoryService; |
|
66 | } |
|
67 | ||
68 | public function index(): HtmlResponse |
|
69 | { |
|
70 | $params = $this->request->getQueryParams(); |
|
71 | $page = isset($params['page']) ? $params['page'] : 1; |
|
72 | $limit = isset($params['limit']) ? $params['limit'] : 15; |
|
73 | $posts = $this->postService->fetchAllArticles($page, $limit); |
|
74 | ||
75 | return new HtmlResponse($this->template->render( |
|
76 | 'article::post/index', |
|
77 | ['list' => $posts, 'layout' => 'layout/admin']) |
|
78 | ); |
|
79 | } |
|
80 | ||
81 | /** |
|
82 | * Add/Edit show form. |
|
83 | * |
|
84 | * @return \Psr\Http\Message\ResponseInterface |
|
85 | */ |
|
86 | public function edit($errors = []): \Psr\Http\Message\ResponseInterface |
|
87 | { |
|
88 | $id = $this->request->getAttribute('id'); |
|
89 | $post = $this->postService->fetchSingleArticle($id); |
|
90 | $categories = $this->categoryService->getAll(ArticleType::POST); |
|
91 | ||
92 | if ($this->request->getParsedBody()) { |
|
93 | $post = (object) ($this->request->getParsedBody() + (array) $post); |
|
94 | $post->article_id = $id; |
|
95 | } |
|
96 | ||
97 | return new HtmlResponse( |
|
98 | $this->template->render( |
|
99 | 'article::post/edit', [ |
|
100 | 'post' => $post, |
|
101 | 'categories' => $categories, |
|
102 | 'errors' => $errors, |
|
103 | 'layout' => 'layout/admin', |
|
104 | ] |
|
105 | ) |
|
106 | ); |
|
107 | } |
|
108 | ||
109 | /** |
|
110 | * Add/Edit article action. |
|
111 | * |
|
112 | * @throws FilterException if filter fails |
|
113 | * @throws \Exception |
|
114 | * |
|
115 | * @return \Psr\Http\Message\ResponseInterface |
|
116 | */ |
|
117 | public function save(): \Psr\Http\Message\ResponseInterface |
|
118 | { |
|
119 | try { |
|
120 | $id = $this->request->getAttribute('id'); |
|
121 | $user = $this->session->getStorage()->user; |
|
122 | $data = $this->request->getParsedBody(); |
|
123 | $data += (new Request())->getFiles()->toArray(); |
|
124 | ||
125 | if ($id) { |
|
126 | $this->postService->updateArticle($data, $id); |
|
127 | } else { |
|
128 | $this->postService->createArticle($user, $data); |
|
129 | } |
|
130 | } catch (FilterException $fe) { |
|
131 | return $this->edit($fe->getArrayMessages()); |
|
132 | } catch (\Exception $e) { |
|
133 | throw $e; |
|
134 | } |
|
135 | ||
136 | return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.posts')); |
|
137 | } |
|
138 | ||
139 | /** |
|
140 | * Delete post by id. |
|
141 | * |
|
142 | * @throws \Exception |
|
143 | * |
|
144 | * @return \Psr\Http\Message\ResponseInterface |
|
145 | */ |
|
146 | public function delete(): \Psr\Http\Message\ResponseInterface |
|
147 | { |
|
148 | try { |
|
149 | $this->postService->deleteArticle($this->request->getAttribute('id')); |
|
150 | } catch (\Exception $e) { |
|
151 | throw $e; |
|
152 | } |
|
153 | ||
154 | return $this->response->withStatus(302)->withHeader( |
|
155 | 'Location', $this->router->generateUri('admin.posts', ['action' => 'index']) |
|
156 | ); |
|
157 | } |
|
158 | } |
|
159 |
@@ 18-149 (lines=132) @@ | ||
15 | use Zend\Http\PhpEnvironment\Request; |
|
16 | use Zend\Session\SessionManager; |
|
17 | ||
18 | class VideoController extends AbstractController |
|
19 | { |
|
20 | /** @var Template |
|
21 | */ |
|
22 | private $template; |
|
23 | ||
24 | /** @var VideoService */ |
|
25 | private $videoService; |
|
26 | ||
27 | /** @var SessionManager */ |
|
28 | private $session; |
|
29 | ||
30 | /** @var Router */ |
|
31 | private $router; |
|
32 | ||
33 | /** @var CategoryService */ |
|
34 | private $categoryService; |
|
35 | ||
36 | /** |
|
37 | * VideoController constructor. |
|
38 | * |
|
39 | * @param Template $template |
|
40 | * @param Router $router |
|
41 | * @param VideoService $videoService |
|
42 | * @param SessionManager $session |
|
43 | * @param CategoryService $categoryService |
|
44 | */ |
|
45 | public function __construct( |
|
46 | Template $template, |
|
47 | Router $router, |
|
48 | VideoService $videoService, |
|
49 | SessionManager $session, |
|
50 | CategoryService $categoryService |
|
51 | ) { |
|
52 | $this->template = $template; |
|
53 | $this->videoService = $videoService; |
|
54 | $this->session = $session; |
|
55 | $this->router = $router; |
|
56 | $this->categoryService = $categoryService; |
|
57 | } |
|
58 | ||
59 | public function index(): HtmlResponse |
|
60 | { |
|
61 | $params = $this->request->getQueryParams(); |
|
62 | $page = isset($params['page']) ? $params['page'] : 1; |
|
63 | $limit = isset($params['limit']) ? $params['limit'] : 15; |
|
64 | $videos = $this->videoService->fetchAllArticles($page, $limit); |
|
65 | ||
66 | return new HtmlResponse($this->template->render('article::video/index', |
|
67 | ['list' => $videos, 'layout' => 'layout/admin']) |
|
68 | ); |
|
69 | } |
|
70 | ||
71 | /** |
|
72 | * Add/Edit show form. |
|
73 | * |
|
74 | * @return \Psr\Http\Message\ResponseInterface |
|
75 | */ |
|
76 | public function edit($errors = []): \Psr\Http\Message\ResponseInterface |
|
77 | { |
|
78 | $id = $this->request->getAttribute('id'); |
|
79 | $video = $this->videoService->fetchSingleArticle($id); |
|
80 | $categories = $this->categoryService->getAll(ArticleType::VIDEO); |
|
81 | ||
82 | if ($this->request->getParsedBody()) { |
|
83 | $video = (object) ($this->request->getParsedBody() + (array) $video); |
|
84 | $video->article_id = $id; |
|
85 | } |
|
86 | ||
87 | return new HtmlResponse( |
|
88 | $this->template->render( |
|
89 | 'article::video/edit', [ |
|
90 | 'video' => $video, |
|
91 | 'categories' => $categories, |
|
92 | 'errors' => $errors, |
|
93 | 'layout' => 'layout/admin', |
|
94 | ] |
|
95 | ) |
|
96 | ); |
|
97 | } |
|
98 | ||
99 | /** |
|
100 | * Add/Edit article action. |
|
101 | * |
|
102 | * @throws FilterException if filter fails |
|
103 | * @throws \Exception |
|
104 | * |
|
105 | * @return \Psr\Http\Message\ResponseInterface |
|
106 | */ |
|
107 | public function save(): \Psr\Http\Message\ResponseInterface |
|
108 | { |
|
109 | try { |
|
110 | $id = $this->request->getAttribute('id'); |
|
111 | $user = $this->session->getStorage()->user; |
|
112 | $data = $this->request->getParsedBody(); |
|
113 | $data += (new Request())->getFiles()->toArray(); |
|
114 | ||
115 | if ($id) { |
|
116 | $this->videoService->updateArticle($data, $id); |
|
117 | } else { |
|
118 | $this->videoService->createArticle($user, $data); |
|
119 | } |
|
120 | } catch (FilterException $fe) { |
|
121 | return $this->edit($fe->getArrayMessages()); |
|
122 | } catch (\Exception $e) { |
|
123 | throw $e; |
|
124 | } |
|
125 | ||
126 | return $this->response->withStatus(302)->withHeader('Location', |
|
127 | $this->router->generateUri('admin.videos')); |
|
128 | } |
|
129 | ||
130 | /** |
|
131 | * Delete video by id. |
|
132 | * |
|
133 | * @throws \Exception |
|
134 | * |
|
135 | * @return \Psr\Http\Message\ResponseInterface |
|
136 | */ |
|
137 | public function delete(): \Psr\Http\Message\ResponseInterface |
|
138 | { |
|
139 | try { |
|
140 | $this->videoService->deleteArticle($this->request->getAttribute('id')); |
|
141 | } catch (\Exception $e) { |
|
142 | throw $e; |
|
143 | } |
|
144 | ||
145 | return $this->response->withStatus(302)->withHeader('Location', |
|
146 | $this->router->generateUri('admin.videos', ['action' => 'index']) |
|
147 | ); |
|
148 | } |
|
149 | } |
|
150 |