1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
<?php
namespace Elements\Controller;
use Elements\DB;
use Elements\Model\Artwork;
use Elements\Model\Card as CardModel;
use Elements\Model\CardMeta;
use Elements\Model\VoteArtwork;
use Elements\Model\VoteCard;
use Elements\Template;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Card
{
public static function get(Request $request)
{
$route = $request->attributes->get('route');
$cardId = $route['id'];
$card = DB::$entityManager->getRepository(CardModel::class)->find($cardId);
return $card;
}
public static function getAll(): array
{
// TODO: is that correct? yes - how to call this getAll?
return DB::$entityManager->getRepository(CardModel::class)->findAll();
}
public static function listCards(): string
{
// get all cards
$cards = DB::$entityManager->getRepository(CardModel::class)->findAll();
return Template::render('card/card_list.twig', [
'cards' => $cards
]);
}
public static function addCard(): string
{
// TODO: how to access all available meta?
return Template::render('card/card_add.twig', [
'fields' => [
'name' => 'meta[name]',
'converted mana cost' => 'meta[cmc]',
'is uno reverse' => 'meta[is_uno_reverse]',
],
]);
}
public static function displayCard(Request $request): string
{
$route = $request->attributes->get('route');
$cardId = $route['id'];
$card = DB::$entityManager->getRepository(CardModel::class)->find($cardId);
return Template::render('card/card_display.twig', [
'card' => $card
]);
}
public static function editCard(Request $request)
{
$route = $request->attributes->get('route');
$cardId = $route['id'];
$card = DB::$entityManager->getRepository(CardModel::class)->find($cardId);
$metas = DB::$entityManager->getRepository(CardMeta::class)->findAll();
return Template::render('card/card_edit.twig', [
'card' => $card
]);
}
public static function add(Request $request)
{
$card = new CardModel();
foreach ($request->get('meta') as $key => $value) {
$meta = new CardMeta($key, $value);
$card->addMeta($meta);
DB::save($meta);
}
/**@var UploadedFile[] $files*/
if ($files = $request->files->get('images')) {
foreach ($files as $file) {
$image = Artwork::fromUploadedFile($file);
/*TEST*/
for ($i = 0; $i <= rand(0, 40); $i++) {
$vote = new VoteArtwork();
$vote->value = rand(-1, 1) >= 0 ? 1 : -1;
$image->addVote($vote);
DB::save($vote);
}
/*TEST*/
$card->addArtwork($image);
DB::save($image);
}
}
/*TEST*/
for ($i = 0; $i <= rand(0, 60); $i++) {
$vote = new VoteCard();
$vote->value = rand(-1, 1) > 0 ? 1 : -1;
$card->addVote($vote);
DB::save($vote);
}
/*TEST*/
DB::save($card);
$response = new RedirectResponse('/card/' . $card->id);
return $response;
}
public static function edit(Request $request)
{
$route = $request->attributes->get('route');
$cardId = $route['id'];
// TODO: like add but edit
$response = new RedirectResponse('/card/' . $cardId);
return $response;
}
}
|