summaryrefslogtreecommitdiff
path: root/src/Controller/Card.php
blob: d4ee3936147832dbc40df20bd48b335d0b598054 (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
<?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 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 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('/');

    return $response;
  }

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

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

    return $card->getMeta('name');
  }
}