|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Core\ViewModel; |
|
6
|
|
|
use Core\Application; |
|
7
|
|
|
use App\Entity\Post as PostModel; |
|
8
|
|
|
use App\Entity\User as UserModel; |
|
9
|
|
|
use App\Entity\Board as BoardModel; |
|
10
|
|
|
use App\Auth\LoginManagerInterface; |
|
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
12
|
|
|
|
|
13
|
|
|
class Board |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var EntityManagerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $entityManager; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var BoardModel |
|
22
|
|
|
*/ |
|
23
|
|
|
private $board; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct($name, EntityManagerInterface $entityManager) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->entityManager = $entityManager; |
|
28
|
|
|
$this->board = $entityManager->getRepository(BoardModel::class)->findOneBy(['name' => $name]); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function index($name, ViewModel $viewModel) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$posts = $this->getPosts(); |
|
34
|
|
|
|
|
35
|
|
|
$viewModel->set('board', $this->board); |
|
36
|
|
|
$viewModel->set('posts', $posts); |
|
37
|
|
|
|
|
38
|
|
|
return 'default/postList'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function showPostingForm($name, ViewModel $viewModel) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$viewModel->set('board', $this->board); |
|
44
|
|
|
|
|
45
|
|
|
return 'default/postingForm'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function write($name, $parsedBody, LoginManagerInterface $loginManager) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$post = new PostModel(); |
|
51
|
|
|
$post->setBoard($this->board); |
|
52
|
|
|
$post->setSubject($parsedBody['subject']); |
|
53
|
|
|
$post->setContent($parsedBody['content']); |
|
54
|
|
|
|
|
55
|
|
|
if ($loginManager->getLoggedAccountId()) { |
|
56
|
|
|
/** @var UserModel */ |
|
57
|
|
|
$user = $this->entityManager->getRepository(UserModel::class) |
|
58
|
|
|
->findOneBy(['accountId' => $loginManager->getLoggedAccountId()]); |
|
59
|
|
|
|
|
60
|
|
|
$post->setUser($user); |
|
61
|
|
|
$post->setAuthor($user->getNickname()); |
|
62
|
|
|
$post->setPassword($user->getPassword()); |
|
63
|
|
|
} else { |
|
64
|
|
|
$post->setAuthor($parsedBody['author']); |
|
65
|
|
|
$post->setPassword($parsedBody['password']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->entityManager->persist($post); |
|
69
|
|
|
$this->entityManager->flush(); |
|
70
|
|
|
|
|
71
|
|
|
return 'redirect: ' . Application::getUrl('/post/' . $post->getId()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
private function getPosts() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->entityManager->getRepository(PostModel::class) |
|
80
|
|
|
->findBy(['board' => $this->board->getId()], ['id' => 'DESC']); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.