summaryrefslogtreecommitdiff
path: root/src/Controller/Card.php
blob: 3d8d8e037f7d8fcb529829dbd7525472fb5a110e (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?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
  {
    return DB::$entityManager->getRepository(CardModel::class)->findAll();
  }

  public static function listCards(): string
  {
    // get all cards
    $cards = self::getAll();

    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' => 'name',
        'converted mana cost' => 'cmc',
        'is uno reverse' => 'is_uno_reverse',
      ],
    ]);
  }

  public static function viewCard(Request $request): string
  {
    $route = $request->attributes->get('route');
    $cardId = $route['id'];

    $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId);

    return Template::render('card/card_view.twig', [
      'card' => $card
    ]);
  }

  public static function viewArtwork(Request $request): string
  {
    $route = $request->attributes->get('route');
    $cardId = $route['id'];
    $artworkId = $route['artworkId'];

    $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId);
    $artwork = DB::$entityManager->getRepository(Artwork::class)->find($artworkId);

    return Template::render('artwork/artwork_view.twig', [
      'card' => $card,
      'artwork' => $artwork
    ]);
  }

  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'];
    $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId);

    foreach ($request->get('meta') as $key => $value) {
      $meta = $card->getMeta($key);
      if ($meta == null) {
        $meta = new CardMeta($key, $value);
        $card->addMeta($meta);
      } else {
        $meta->value = strval($value);
      }

      DB::save($meta);
    }

    DB::save($card);

    $response = new RedirectResponse('/card/' . $cardId);
    return $response;
  }

  public static function vote(Request $request)
  {
    var_dump('VOTE');
    $route = $request->attributes->get('route');
    $cardId = $route['id'];
    $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId);

    $vote = new VoteCard();
    $vote->value = intval($request->get('value'));
    $card->addVote($vote);
    DB::save($vote);
    DB::save($card);

    // TODO: redirect to last visited
    $response = new RedirectResponse('/card');
    return $response;
  }
}