|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Core\ViewModel; |
|
6
|
|
|
use Core\Application; |
|
7
|
|
|
use App\Entity\Comment as CommentModel; |
|
8
|
|
|
use App\Entity\User as UserModel; |
|
9
|
|
|
use App\Auth\LoginManagerInterface; |
|
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
11
|
|
|
use \DateTime; |
|
12
|
|
|
|
|
13
|
|
|
class Comment |
|
14
|
|
|
{ |
|
15
|
|
|
public function showEditForm($id, EntityManagerInterface $entityManager, ViewModel $viewModel) |
|
16
|
|
|
{ |
|
17
|
|
|
$viewModel->set('comment', $entityManager->getRepository(CommentModel::class)->find($id)); |
|
18
|
|
|
|
|
19
|
|
|
return 'default/commentForm'; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function edit($id, $parsedBody, EntityManagerInterface $entityManager, LoginManagerInterface $loginManager, ViewModel $viewModel) |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
/** @var CommentModel */ |
|
25
|
|
|
$comment = $entityManager->getRepository(CommentModel::class)->find($id); |
|
26
|
|
|
$comment->setContent($parsedBody['content']); |
|
27
|
|
|
$comment->setUpdatedAt(new DateTime()); |
|
28
|
|
|
$this->fillUserInfomanionByLoginAccount($comment, $parsedBody, $loginManager, $entityManager); |
|
29
|
|
|
|
|
30
|
|
|
$entityManager->flush(); |
|
31
|
|
|
|
|
32
|
|
|
return 'redirect: ' . Application::getUrl('/post/' . $comment->getPost()->getId()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
private function fillUserInfomanionByLoginAccount($userContent, $parsedBody, LoginManagerInterface $loginManager, EntityManagerInterface $entityManager) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
if ($loginManager->getLoggedAccountId()) { |
|
38
|
|
|
/** @var UserModel */ |
|
39
|
|
|
$user = $entityManager->getRepository(UserModel::class) |
|
40
|
|
|
->findOneBy(['accountId' => $loginManager->getLoggedAccountId()]); |
|
41
|
|
|
|
|
42
|
|
|
$userContent->setUser($user); |
|
43
|
|
|
$userContent->setAuthor($user->getNickname()); |
|
44
|
|
|
$userContent->setPassword($user->getPassword()); |
|
45
|
|
|
} else { |
|
46
|
|
|
$userContent->setAuthor($parsedBody['author']); |
|
47
|
|
|
$userContent->setPassword($parsedBody['password']); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.