diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | public/css/style.css | 36 | ||||
-rw-r--r-- | src/Controller/Card.php | 101 | ||||
-rw-r--r-- | src/Controller/Home.php | 11 | ||||
-rw-r--r-- | src/Model/Card.php | 61 | ||||
-rw-r--r-- | src/Router.php | 13 | ||||
-rw-r--r-- | templates/base.twig | 26 | ||||
-rw-r--r-- | templates/card/card.twig | 16 | ||||
-rw-r--r-- | templates/card/card_add.twig | 29 | ||||
-rw-r--r-- | templates/card/card_display.twig | 25 | ||||
-rw-r--r-- | templates/card/card_edit.twig | 26 | ||||
-rw-r--r-- | templates/card/card_list.twig | 38 | ||||
-rw-r--r-- | templates/dashboard.twig | 12 | ||||
-rw-r--r-- | templates/root.twig | 14 |
14 files changed, 378 insertions, 32 deletions
@@ -3,3 +3,5 @@ db.sqlite /public/artworks +# Jetbrains +.idea diff --git a/public/css/style.css b/public/css/style.css new file mode 100644 index 0000000..8cb9dca --- /dev/null +++ b/public/css/style.css @@ -0,0 +1,36 @@ +:root { + --bg: #fff; + --accent-bg: #f5f7ff; + --bg-header: #212121; + --text-header: #dcdcdc; + --text: #212121; + --text-light: #585858; + --border: #d8dae1; + --accent: #244D5D; + --code: #932227; + --preformatted: #444; + --marked: #1B8E39; + --disabled: #efefef; +} + +header { + padding: 0 0.5rem 0rem 0.5rem; +} +main { + padding-top: 1rem; +} + +main h3 { + margin-top: 1rem; + margin-bottom: 1rem +} + +main h4 { + margin-top: 2.5rem; + margin-bottom: 0rem +} + +main p { + margin-top: 0.25rem; + margin-bottom: 0.25rem; +}
\ No newline at end of file diff --git a/src/Controller/Card.php b/src/Controller/Card.php index d4ee393..e7fa793 100644 --- a/src/Controller/Card.php +++ b/src/Controller/Card.php @@ -8,6 +8,7 @@ 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; @@ -15,6 +16,69 @@ 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(); @@ -28,19 +92,19 @@ class Card /**@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); + $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); } } @@ -55,19 +119,18 @@ class Card DB::save($card); - $response = new RedirectResponse('/'); - + $response = new RedirectResponse('/card/' . $card->id); return $response; } - public static function get(Request $request) + public static function edit(Request $request) { $route = $request->attributes->get('route'); $cardId = $route['id']; - $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId); + // TODO: like add but edit - return $card->getMeta('name'); + $response = new RedirectResponse('/card/' . $cardId); + return $response; } } - diff --git a/src/Controller/Home.php b/src/Controller/Home.php index 1c1500c..7eda5ee 100644 --- a/src/Controller/Home.php +++ b/src/Controller/Home.php @@ -21,11 +21,16 @@ class Home return Template::render('home.twig', [ 'fields' => [ - 'name' => 'meta[name]', - 'converted mana cost' => 'meta[cmc]', - 'is uno reverse' => 'meta[is_uno_reverse]', + 'name' => 'meta[name]', + 'converted mana cost' => 'meta[cmc]', + 'is uno reverse' => 'meta[is_uno_reverse]', ], ]); } + + public static function dashboard() + { + return Template::render('dashboard.twig', []); + } } diff --git a/src/Model/Card.php b/src/Model/Card.php index abaa06e..59d2639 100644 --- a/src/Model/Card.php +++ b/src/Model/Card.php @@ -104,24 +104,71 @@ class Card // if meta is already hydrated if ($this->meta->isInitialized()) { $meta = $this->meta->unwrap() - #->findFirst(fn (CardMeta $item) => $item->key === $key); - ->filter(fn ($item) => $item->key === $key)->first(); - + #->findFirst(fn (CardMeta $item) => $item->key === $key); + ->filter(fn ($item) => $item->key === $key)->first(); return $meta->value ?? null; } // get directly from db otherwise $result = DB::$entityManager ->createQuery( - 'SELECT cm.value - FROM Elements\Model\CardMeta cm - WHERE cm.key = :key AND cm.card = :card' + 'SELECT cm.value + FROM Elements\Model\CardMeta cm + WHERE cm.key = :key AND cm.card = :card' ) ->setParameter('key', $key) ->setParameter('card', $this) ->getOneOrNullResult(); - return $result['value'] ?? null; + return $result['value'] ?? null; + } + + /** + * @return CardMeta[] + */ + public function getAllMeta() + { + // TODO: do we need this? + // if meta is already hydrated + if ($this->meta->isInitialized()) { + return $this->meta; + } + + // get directly from db otherwise + $result = DB::$entityManager + ->createQuery( + 'SELECT cm + FROM Elements\Model\CardMeta cm + WHERE cm.card = :card' + ) + ->setParameter('card', $this) + ->getArrayResult(); + + return $result; + } + + /** + * @return Artwork[] + */ + public function getAllArtworks() + { + // TODO: do we need this? + // if artworks are already hydrated + if ($this->artworks->isInitialized()) { + return $this->artworks; + } + + // get directly from db otherwise + $result = DB::$entityManager + ->createQuery( + 'SELECT aw + FROM Elements\Model\Artwork aw + WHERE aw.card = :card' + ) + ->setParameter('card', $this) + ->getArrayResult(); + + return $result; } /** diff --git a/src/Router.php b/src/Router.php index 5635d5c..aad6122 100644 --- a/src/Router.php +++ b/src/Router.php @@ -27,9 +27,16 @@ class Router $response = new Response(); $this->routes = new RouteCollection(); - $this->addRoute('GET', '/', [Home::class, 'index']); - $this->addRoute('GET', '/card/{id}', [Card::class, 'get']); - $this->addRoute('POST', '/card/add', [Card::class, 'add']); + $this->addRoute('GET', '/', [Home::class, 'index']); + $this->addRoute('GET', '/dashboard', [Home::class, 'dashboard']); + + // card + $this->addRoute('GET', '/card/list', [Card::class, 'listCards']); + $this->addRoute('GET', '/card/add', [Card::class, 'addCard']); + $this->addRoute('GET', '/card/{id}', [Card::class, 'displayCard']); + $this->addRoute('GET', '/card/{id}/edit', [Card::class, 'editCard']); + $this->addRoute('POST', '/card/add', [Card::class, 'add']); + $this->addRoute('POST', '/card/{id}/edit', [Card::class, 'edit']); $context = new RequestContext(); $context->fromRequest($request); diff --git a/templates/base.twig b/templates/base.twig new file mode 100644 index 0000000..a27b5d7 --- /dev/null +++ b/templates/base.twig @@ -0,0 +1,26 @@ +{% extends 'root.twig' %} + +{% block css %} + {{ parent() }} + <link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css"> + + <link rel="stylesheet" href="/css/style.css"/> +{% endblock %} + +{% block body %} + <header> + <nav> + <a href="/dashboard">Dashboard</a> + <a href="/card/list">Cards</a> + <a href="/card/add">New Card</a> + </nav> + <h1>Elements</h1> + <p>{% block subtitle %}{% endblock %}</p> + </header> + <main> + {% block main %}{% endblock %} + </main> + <footer> + visit <a href="https://gitlab.com/DRogueRonin/elements">Gitlab</a> + </footer> +{% endblock %} diff --git a/templates/card/card.twig b/templates/card/card.twig new file mode 100644 index 0000000..9a91bfa --- /dev/null +++ b/templates/card/card.twig @@ -0,0 +1,16 @@ +{% extends 'base.twig' %} + +{% block subtitle %} + Cards +{% endblock %} +{% block main %} + <div style="align-content: center; text-align: center"> + <button onclick="location.href='/card/list'">List</button> + <button onclick="location.href='/card/add'">Add</button> + <h3>{% block pagetitle %}{% endblock %}</h3> + </div> + + <div style="align-content: center;"> + {% block pagecontent %}{% endblock %} + </div> +{% endblock %} diff --git a/templates/card/card_add.twig b/templates/card/card_add.twig new file mode 100644 index 0000000..ef01632 --- /dev/null +++ b/templates/card/card_add.twig @@ -0,0 +1,29 @@ +{% extends 'card/card.twig' %} + +{% block pagetitle %} + Add Card +{% endblock %} +{% block pagecontent %} + <form action="/card/add" method="post" enctype="multipart/form-data"> + {% for key, field in fields %} + <p> + <label>{{ key }}</label><br> + <input type="text" name="{{ field }}"> + </p> + {% endfor %} + + <!-- + {% for meta in card.meta %} + <p> + <label>{{ meta.key }}</label><br> + <input type="text" name="{{ meta.key }}" value="{{ meta.value }}" disabled> + </p> + {% endfor %} + --> + + <h4>Artworks</h4> + <input type="file" name="images[]" multiple> + + <input type="submit" value="Add"> + </form> +{% endblock %} diff --git a/templates/card/card_display.twig b/templates/card/card_display.twig new file mode 100644 index 0000000..27d5bea --- /dev/null +++ b/templates/card/card_display.twig @@ -0,0 +1,25 @@ +{% extends 'card/card.twig' %} + +{% block pagetitle %} + Show Card +{% endblock %} +{% block pagecontent %} + {% for meta in card.meta %} + <p> + <label>{{ meta.key }}</label><br> + <input type="text" name="{{ meta.key }}" value="{{ meta.value }}" disabled> + </p> + {% endfor %} + + <button onclick="location.href='/card/{{ card.id }}/edit'">Edit</button> + + <h4>Artworks</h4> + {% for artwork in card.artworks %} + <p> + <img style='max-width: 100%; max-height:300px;' src='{{ artwork.path }}'> + <button>Up</button> + <button>Down</button> + <button onclick="location.href='/artwork/{{ artwork.id }}'">Show</button> + </p> + {% endfor %} +{% endblock %} diff --git a/templates/card/card_edit.twig b/templates/card/card_edit.twig new file mode 100644 index 0000000..a7f9aa7 --- /dev/null +++ b/templates/card/card_edit.twig @@ -0,0 +1,26 @@ +{% extends 'card/card.twig' %} + +{% block pagetitle %} + Edit Card +{% endblock %} +{% block pagecontent %} + <form action="/card/{{ card.id }}/edit" method="post" enctype="multipart/form-data"> + {% for meta in card.meta %} + <p> + <label>{{ meta.key }}</label><br> + <input type="text" name="{{ meta.key }}" value="{{ meta.value }}"> + </p> + {% endfor %} + + <input type="submit" value="Save"> + </form> + + <h4>Artworks</h4> + {% for artwork in card.artworks %} + <p> + <img style='max-width: 100%; max-height:300px;' src='{{ artwork.path }}'> + <button onclick="location.href='/artwork/{{ artwork.id }}/edit'">Edit</button> + <button onclick="location.href='/artwork/{{ artwork.id }}'">Delete</button> + </p> + {% endfor %} +{% endblock %} diff --git a/templates/card/card_list.twig b/templates/card/card_list.twig new file mode 100644 index 0000000..1714fc8 --- /dev/null +++ b/templates/card/card_list.twig @@ -0,0 +1,38 @@ +{% extends 'card/card.twig' %} + +{% block pagetitle %} +List +{% endblock %} +{% block pagecontent %} + <table> + <thead> + <tr> + <th>ID</th> + {% for meta in cards[0].getAllMeta() %} + <th>{{ meta.key }}</th> + {% endfor %} + <th>Votes</th> + <th></th> + </tr> + </thead> + <tbody> + {% for card in cards %} + <tr> + <td>{{ card.id }}</td> + {% for meta in card.getAllMeta() %} + <td>{{ meta.value }}</td> + {% endfor %} + <td> + {{ card.getVotesTotal() }} + <button>Up</button> + <button>Down</button> + </td> + <td> + <button onclick="location.href='/card/{{ card.id }}'">Card</button> + <button onclick="location.href='/card/{{ card.id }}/artworks'">Artworks</button> + </td> + </tr> + {% endfor %} + </tbody> + </table> +{% endblock %} diff --git a/templates/dashboard.twig b/templates/dashboard.twig new file mode 100644 index 0000000..31f2353 --- /dev/null +++ b/templates/dashboard.twig @@ -0,0 +1,12 @@ +{% extends 'base.twig' %} + +{% block subtitle %} + Dashboard +{% endblock %} +{% block main %} + <p>Home?</p> + <p>Updates to own cards?</p> + <p>Card of the day?</p> + <p>Events?</p> + <p>News?</p> +{% endblock %} diff --git a/templates/root.twig b/templates/root.twig new file mode 100644 index 0000000..9c3534e --- /dev/null +++ b/templates/root.twig @@ -0,0 +1,14 @@ +<html> +<head> + <title>Elements</title> + + {% block css %}{% endblock %} +</head> +<body> +{% block body %}{% endblock %} +<script> + +</script> +{% block js %}{% endblock %} +</body> +</html>
\ No newline at end of file |