|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use App\Form\ContactForm; |
|
8
|
|
|
use App\Service\Mailer; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Yiisoft\Http\Header; |
|
12
|
|
|
use Yiisoft\Http\Method; |
|
13
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
|
14
|
|
|
use Yiisoft\Yii\Web\Flash; |
|
15
|
|
|
use Yiisoft\Yii\Web\Middleware\Csrf; |
|
16
|
|
|
|
|
17
|
|
|
class SiteController extends AbstractController |
|
18
|
|
|
{ |
|
19
|
3 |
|
public function index(ServerRequestInterface $request): ResponseInterface |
|
20
|
|
|
{ |
|
21
|
3 |
|
return $this->render( |
|
22
|
3 |
|
'site/index', |
|
23
|
|
|
[ |
|
24
|
3 |
|
'csrf' => $request->getAttribute(Csrf::REQUEST_NAME) |
|
25
|
|
|
] |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
public function about(ServerRequestInterface $request): ResponseInterface |
|
30
|
|
|
{ |
|
31
|
1 |
|
return $this->render( |
|
32
|
1 |
|
'site/about', |
|
33
|
|
|
[ |
|
34
|
1 |
|
'csrf' => $request->getAttribute(Csrf::REQUEST_NAME) |
|
35
|
|
|
] |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
6 |
|
public function contact( |
|
40
|
|
|
ContactForm $form, |
|
41
|
|
|
Flash $flash, |
|
42
|
|
|
Mailer $mailer, |
|
43
|
|
|
UrlGeneratorInterface $url, |
|
44
|
|
|
ServerRequestInterface $request |
|
45
|
|
|
): ResponseInterface { |
|
46
|
6 |
|
$body = $request->getParsedBody(); |
|
47
|
6 |
|
$method = $request->getMethod(); |
|
48
|
|
|
|
|
49
|
6 |
|
if (($method === Method::POST) && $form->load($body) && $form->validate()) { |
|
|
|
|
|
|
50
|
2 |
|
$message = $mailer->message($form); |
|
51
|
|
|
|
|
52
|
2 |
|
$attachFiles = $request->getUploadedFiles(); |
|
53
|
|
|
|
|
54
|
2 |
|
foreach ($attachFiles as $attachFile) { |
|
55
|
|
|
foreach ($attachFile as $file) { |
|
56
|
|
|
if ($file->getError() === UPLOAD_ERR_OK) { |
|
57
|
|
|
$message->addFile($file); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
$mailer->send($message); |
|
63
|
|
|
|
|
64
|
2 |
|
$flash->add( |
|
65
|
2 |
|
'is-success', |
|
66
|
|
|
[ |
|
67
|
2 |
|
'header' => 'System mailer notification.', |
|
68
|
|
|
'body' => 'Thanks to contact us, we\'ll get in touch with you as soon as possible.' |
|
69
|
|
|
], |
|
70
|
2 |
|
true |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
2 |
|
return $this->responseFactory |
|
74
|
2 |
|
->createResponse(302) |
|
75
|
2 |
|
->withHeader( |
|
76
|
2 |
|
Header::LOCATION, |
|
77
|
2 |
|
$url->generate('site/index') |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
6 |
|
return $this->render( |
|
82
|
6 |
|
'site/contact', |
|
83
|
|
|
[ |
|
84
|
6 |
|
'csrf' => $request->getAttribute(Csrf::REQUEST_NAME), |
|
85
|
6 |
|
'form' => $form |
|
86
|
|
|
] |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
8 |
|
public function getViewPath(): string |
|
91
|
|
|
{ |
|
92
|
8 |
|
return $this->aliases->get('@views'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|