diff --git a/public/img/judges/dr-agnes-soos.jpg b/public/img/judges/agnes-soos.jpg similarity index 100% rename from public/img/judges/dr-agnes-soos.jpg rename to public/img/judges/agnes-soos.jpg diff --git a/public/img/judges/dr-bertalan-mesko.jpg b/public/img/judges/bertalan-mesko.jpg similarity index 100% rename from public/img/judges/dr-bertalan-mesko.jpg rename to public/img/judges/bertalan-mesko.jpg diff --git a/public/img/judges/dr-edit-nemeth.jpg b/public/img/judges/edit-nemeth.jpg similarity index 100% rename from public/img/judges/dr-edit-nemeth.jpg rename to public/img/judges/edit-nemeth.jpg diff --git a/public/img/judges/dr-gabor-szabo.jpg b/public/img/judges/gabor-szabo.jpg similarity index 100% rename from public/img/judges/dr-gabor-szabo.jpg rename to public/img/judges/gabor-szabo.jpg diff --git a/public/img/judges/dr-gyula-patko.jpg b/public/img/judges/gyula-patko.jpg similarity index 100% rename from public/img/judges/dr-gyula-patko.jpg rename to public/img/judges/gyula-patko.jpg diff --git a/public/img/judges/dr-maria-judit-molnar.jpg b/public/img/judges/maria-judit-molnar.jpg similarity index 100% rename from public/img/judges/dr-maria-judit-molnar.jpg rename to public/img/judges/maria-judit-molnar.jpg diff --git a/public/img/judges/dr-miklos-antalovits.jpg b/public/img/judges/miklos-antalovits.jpg similarity index 100% rename from public/img/judges/dr-miklos-antalovits.jpg rename to public/img/judges/miklos-antalovits.jpg diff --git a/public/img/judges/dr-miklos-bendzsel.jpg b/public/img/judges/miklos-bendzsel.jpg similarity index 100% rename from public/img/judges/dr-miklos-bendzsel.jpg rename to public/img/judges/miklos-bendzsel.jpg diff --git a/public/img/judges/dr-szabolcs-farkas.jpg b/public/img/judges/szabolcs-farkas.jpg similarity index 100% rename from public/img/judges/dr-szabolcs-farkas.jpg rename to public/img/judges/szabolcs-farkas.jpg diff --git a/src/App/Entity/Awardee.php b/src/App/Entity/Awardee.php index 2899481..65677f9 100644 --- a/src/App/Entity/Awardee.php +++ b/src/App/Entity/Awardee.php @@ -69,7 +69,7 @@ class Awardee implements JsonSerializable * @param int $id * @return Awardee */ - public function setId(int $id): Awardee + public function setId(?int $id): Awardee { $this->id = $id; return $this; @@ -78,7 +78,7 @@ class Awardee implements JsonSerializable /** * @return int */ - public function getYear(): int + public function getYear(): ?int { return $this->year; } @@ -96,7 +96,7 @@ class Awardee implements JsonSerializable /** * @return string */ - public function getName(): string + public function getName(): ?string { return $this->name; } @@ -114,7 +114,7 @@ class Awardee implements JsonSerializable /** * @return string */ - public function getText(): string + public function getText(): ?string { return $this->text; } @@ -132,7 +132,7 @@ class Awardee implements JsonSerializable /** * @return string */ - public function getImageLabel(): string + public function getImageLabel(): ?string { return $this->imageLabel; } @@ -141,7 +141,7 @@ class Awardee implements JsonSerializable * @param string $imageLabel * @return Awardee */ - public function setImageLabel(string $imageLabel): Awardee + public function setImageLabel(?string $imageLabel): Awardee { $this->imageLabel = $imageLabel; return $this; @@ -150,7 +150,7 @@ class Awardee implements JsonSerializable /** * @return string */ - public function getSlug(): string + public function getSlug(): ?string { return $this->slug; } diff --git a/src/App/Entity/Judge.php b/src/App/Entity/Judge.php index fff4e93..468b327 100644 --- a/src/App/Entity/Judge.php +++ b/src/App/Entity/Judge.php @@ -34,6 +34,12 @@ class Judge implements JsonSerializable */ private $name; + /** + * @ORM\Column(name="prefix", type="string", length=14, nullable=true) + * @var string + */ + private $prefix; + /** * @ORM\Column(name="slug", type="string", length=250) * @Gedmo\Slug(fields={"name"}) @@ -42,12 +48,11 @@ class Judge implements JsonSerializable private $slug; /** - * @ORM\OneToMany(targetEntity="JudgeTitle", mappedBy="judge") + * @ORM\OneToMany(targetEntity="JudgeTitle", mappedBy="judge", cascade={"persist", "remove"}, orphanRemoval=true) * @var Collection|JudgeTitle[] */ private $titles; - public function __construct() { $this->titles = new ArrayCollection(); @@ -56,7 +61,7 @@ class Judge implements JsonSerializable /** * @return int */ - public function getId(): int + public function getId(): ?int { return $this->id; } @@ -65,7 +70,7 @@ class Judge implements JsonSerializable * @param int $id * @return Judge */ - public function setId(int $id): Judge + public function setId(?int $id): Judge { $this->id = $id; return $this; @@ -74,7 +79,7 @@ class Judge implements JsonSerializable /** * @return string */ - public function getName(): string + public function getName(): ?string { return $this->name; } @@ -92,7 +97,25 @@ class Judge implements JsonSerializable /** * @return string */ - public function getSlug(): string + public function getPrefix(): ?string + { + return $this->prefix; + } + + /** + * @param string $prefix + * @return Judge + */ + public function setPrefix(?string $prefix): Judge + { + $this->prefix = $prefix; + return $this; + } + + /** + * @return string + */ + public function getSlug(): ?string { return $this->slug; } @@ -110,11 +133,23 @@ class Judge implements JsonSerializable /** * @return JudgeTitle[]|Collection */ - public function getTitles() + public function getTitles(): ?Collection { return $this->titles; } + /** + * @param \Traversable $titles + * @return Judge + */ + public function addTitles(\Traversable $titles): Judge + { + foreach ($titles as $title) { + $this->addTitle($title); + } + return $this; + } + /** * @param JudgeTitle $title * @return Judge @@ -123,6 +158,7 @@ class Judge implements JsonSerializable { if(!$this->titles->contains($title)) { $this->titles->add($title); + $title->setJudge($this); } return $this; } @@ -135,6 +171,19 @@ class Judge implements JsonSerializable { if($this->titles->contains($title)) { $this->titles->removeElement($title); + $title->setJudge(null); + } + return $this; + } + + /** + * @param \Traversable $titles + * @return Judge + */ + public function removeTitles(\Traversable $titles): Judge + { + foreach ($titles as $title) { + $this->removeTitle($title); } return $this; } @@ -143,7 +192,7 @@ class Judge implements JsonSerializable * @param JudgeTitle[]|Collection $titles * @return Judge */ - public function setTitles($titles) + public function setTitles($titles): Judge { $this->titles = $titles; return $this; @@ -157,6 +206,7 @@ class Judge implements JsonSerializable return [ 'id' => $this->getId(), 'name' => $this->getName(), + 'prefix' => $this->getPrefix(), 'titles' => $this->getTitles()->getValues(), 'slug' => $this->getSlug(), ]; diff --git a/src/App/Entity/JudgeTitle.php b/src/App/Entity/JudgeTitle.php index 812d1ec..4dffcce 100644 --- a/src/App/Entity/JudgeTitle.php +++ b/src/App/Entity/JudgeTitle.php @@ -39,7 +39,7 @@ class JudgeTitle implements JsonSerializable /** * @ORM\ManyToOne(targetEntity="Judge", inversedBy="titles") - * @ORM\JoinColumn(name="title_id", referencedColumnName="id") + * @ORM\JoinColumn(name="judge_id", referencedColumnName="id") * @var Judge */ private $judge; @@ -47,7 +47,7 @@ class JudgeTitle implements JsonSerializable /** * @return int */ - public function getId(): int + public function getId(): ?int { return $this->id; } @@ -56,7 +56,7 @@ class JudgeTitle implements JsonSerializable * @param int $id * @return JudgeTitle */ - public function setId(int $id): JudgeTitle + public function setId(?int $id): JudgeTitle { $this->id = $id; return $this; @@ -65,7 +65,7 @@ class JudgeTitle implements JsonSerializable /** * @return int */ - public function getYear(): int + public function getYear(): ?int { return $this->year; } @@ -83,7 +83,7 @@ class JudgeTitle implements JsonSerializable /** * @return string */ - public function getTitle(): string + public function getTitle(): ?string { return $this->title; } @@ -101,7 +101,7 @@ class JudgeTitle implements JsonSerializable /** * @return Judge */ - public function getJudge(): Judge + public function getJudge(): ?Judge { return $this->judge; } @@ -110,7 +110,7 @@ class JudgeTitle implements JsonSerializable * @param Judge $judge * @return JudgeTitle */ - public function setJudge(Judge $judge): JudgeTitle + public function setJudge(?Judge $judge): JudgeTitle { $this->judge = $judge; return $this; diff --git a/src/App/Handler/Api/AwardeeHandler.php b/src/App/Handler/Api/AwardeeHandler.php index 912e2ab..1bdfcee 100644 --- a/src/App/Handler/Api/AwardeeHandler.php +++ b/src/App/Handler/Api/AwardeeHandler.php @@ -20,16 +20,63 @@ class AwardeeHandler extends AbstractCrudHandler $this->awardeeManager = $awardeeManager; } + /** + * @param ServerRequestInterface $request + * @return ResponseInterface + */ public function getList(ServerRequestInterface $request): ResponseInterface { $entities = $this->awardeeManager->getAwardees(); return new JsonResponse($entities); } + /** + * @param ServerRequestInterface $request + * @return ResponseInterface + */ public function get(ServerRequestInterface $request): ResponseInterface { $id = $request->getAttribute(static::IDENTIFIER_NAME); $entity = $this->awardeeManager->getAwardee((int)$id); return new JsonResponse($entity); } + + /** + * @param ServerRequestInterface $request + * @return JsonResponse + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ + public function create(ServerRequestInterface $request): ResponseInterface + { + $data = $this->getRequestData($request); + $entity = $this->awardeeManager->create($data); + return new JsonResponse($entity); + } + + /** + * @param ServerRequestInterface $request + * @return JsonResponse + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ + public function update(ServerRequestInterface $request): ResponseInterface + { + $id = $request->getAttribute(static::IDENTIFIER_NAME); + $data = $this->getRequestData($request); + $entity = $this->awardeeManager->update((int)$id, $data); + return new JsonResponse($entity); + } + + /** + * @param ServerRequestInterface $request + * @return ResponseInterface + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ + public function delete(ServerRequestInterface $request): ResponseInterface + { + $id = $request->getAttribute(static::IDENTIFIER_NAME); + return new JsonResponse($this->awardeeManager->delete((int)$id)); + } } diff --git a/src/App/Handler/Api/JudgesHandler.php b/src/App/Handler/Api/JudgesHandler.php index 5d2cab0..42795fb 100644 --- a/src/App/Handler/Api/JudgesHandler.php +++ b/src/App/Handler/Api/JudgesHandler.php @@ -67,4 +67,16 @@ class JudgesHandler extends AbstractCrudHandler $entity = $this->judgeManager->update((int)$id, $data); return new JsonResponse($entity); } + + /** + * @param ServerRequestInterface $request + * @return ResponseInterface + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ + public function delete(ServerRequestInterface $request): ResponseInterface + { + $id = $request->getAttribute(static::IDENTIFIER_NAME); + return new JsonResponse($this->judgeManager->delete((int)$id)); + } } diff --git a/src/App/Handler/AwardeeHandler.php b/src/App/Handler/AwardeeHandler.php index 710049a..448bb94 100644 --- a/src/App/Handler/AwardeeHandler.php +++ b/src/App/Handler/AwardeeHandler.php @@ -10,7 +10,6 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Zend\Diactoros\Response\HtmlResponse; -use Zend\Diactoros\Response\RedirectResponse; use Zend\Expressive\Helper\UrlHelper; use Zend\Expressive\Template\TemplateRendererInterface; @@ -33,8 +32,7 @@ class AwardeeHandler implements RequestHandlerInterface AwardeeManager $awardeeManager, UrlHelper $urlHelper, JudgeManager $judgeManager - ) - { + ) { $this->template = $template; $this->awardeeManager = $awardeeManager; $this->urlHelper = $urlHelper; diff --git a/src/App/Handler/AwardeeRedirectHandler.php b/src/App/Handler/AwardeeRedirectHandler.php index 8c2a6e4..48fbe51 100644 --- a/src/App/Handler/AwardeeRedirectHandler.php +++ b/src/App/Handler/AwardeeRedirectHandler.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace App\Handler; -use App\Entity\Awardee; +use App\Entity\JudgeTitle; use Doctrine\ORM\EntityManager; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -37,7 +37,7 @@ class AwardeeRedirectHandler implements RequestHandlerInterface { $qb = $this->entityManager->createQueryBuilder(); $maxYear = $qb->select('max(a.year)') - ->from(Awardee::class, 'a') + ->from(JudgeTitle::class, 'a') ->getQuery() ->getSingleScalarResult(); diff --git a/src/App/Plates/NavigationExtension.php b/src/App/Plates/NavigationExtension.php index 5adbbdc..bc5c5ee 100644 --- a/src/App/Plates/NavigationExtension.php +++ b/src/App/Plates/NavigationExtension.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Plates; use App\Entity\Awardee; +use App\Service\YearManager; use Doctrine\ORM\EntityManager; use Knp\Menu\ItemInterface; use Knp\Menu\Matcher\Matcher; @@ -24,16 +25,20 @@ class NavigationExtension implements ExtensionInterface /** @var EntityManager */ private $entityManager; + /** @var YearManager */ + private $yearManager; + /** @var MenuItem */ private $menu; public function __construct( RouterInterface $router, - EntityManager $entityManager - ) - { + EntityManager $entityManager, + YearManager $yearManager + ) { $this->router = $router; $this->entityManager = $entityManager; + $this->yearManager = $yearManager; } /** @@ -84,10 +89,6 @@ class NavigationExtension implements ExtensionInterface 'uri' => $this->getUriFromRouter('the-prize.article', ['article' => 'gran-prize-award-events']) ]); -// $this->menu->addChild(strtoupper("Judges"), [ -// 'uri' => $this->getUriFromRouter('judges') -// ]); - $awardeesMenu = $this->menu->addChild(strtoupper("Awards"), [ 'uri' => $this->getUriFromRouter('awardees') ]); @@ -101,17 +102,7 @@ class NavigationExtension implements ExtensionInterface private function populateAwardeesSubmenu(ItemInterface $awardeesMenu) { - $qb = $this->entityManager->createQueryBuilder(); - $result = $qb->select('a.year') - ->from(Awardee::class, 'a') - ->orderBy('a.year', 'DESC') - ->getQuery() - ->getScalarResult(); - - $years = array_unique(array_map(function ($item) { - return $item['year']; - }, $result)); - + $years = $this->yearManager->getYears(); foreach ($years as $year) { $yearItem = $awardeesMenu->addChild($year, [ 'uri' => $this->getUriFromRouter('awardees-by-year', ['year' => $year]) diff --git a/src/App/Plates/NavigationExtensionFactory.php b/src/App/Plates/NavigationExtensionFactory.php index d38e846..b9ee995 100644 --- a/src/App/Plates/NavigationExtensionFactory.php +++ b/src/App/Plates/NavigationExtensionFactory.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Plates; +use App\Service\YearManager; use Doctrine\ORM\EntityManager; use Psr\Container\ContainerInterface; use Zend\Expressive\Plates\Exception\MissingHelperException; @@ -23,6 +24,7 @@ class NavigationExtensionFactory $router = $container->get(RouterInterface::class); $entityManager = $container->get(EntityManager::class); - return new NavigationExtension($router, $entityManager); + $yearManager = $container->get(YearManager::class); + return new NavigationExtension($router, $entityManager, $yearManager); } } diff --git a/src/App/Service/AwardeeManager.php b/src/App/Service/AwardeeManager.php index 2e27197..146b9d9 100644 --- a/src/App/Service/AwardeeManager.php +++ b/src/App/Service/AwardeeManager.php @@ -6,15 +6,22 @@ namespace App\Service; use App\Entity\Awardee; use Doctrine\ORM\EntityManager; +use DoctrineExpressiveModule\Hydrator\DoctrineObject; class AwardeeManager { /** @var EntityManager */ private $entityManager; - public function __construct(EntityManager $entityManager) - { + /** @var DoctrineObject */ + private $hydrator; + + public function __construct( + EntityManager $entityManager, + DoctrineObject $hydrator + ) { $this->entityManager = $entityManager; + $this->hydrator = $hydrator; } public function getAwardees(): ?array @@ -54,4 +61,50 @@ class AwardeeManager ]); return $awardee; } + + /** + * @return Awardee + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ + public function create($data): Awardee + { + /** @var Awardee $awardee */ + $awardee = $this->hydrator->hydrate($data, new Awardee()); + $this->entityManager->persist($awardee); + $this->entityManager->flush(); + return $awardee; + } + + /** + * @param int $id + * @param $data + * @return Awardee + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ + public function update(int $id, $data): Awardee + { + $awardee = $this->entityManager->getRepository(Awardee::class)->find($id); + /** @var Awardee $awardee */ + $awardee = $this->hydrator->hydrate($data, $awardee); + $this->entityManager->persist($awardee); + $this->entityManager->flush(); + return $awardee; + } + + /** + * @param int $id + * @return bool + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ + public function delete(int $id): bool { + if (null !== ($entity = $this->getAwardee($id))) { + $this->entityManager->remove($entity); + $this->entityManager->flush(); + return true; + } + return false; + } } diff --git a/src/App/Service/AwardeeManagerFactory.php b/src/App/Service/AwardeeManagerFactory.php index daf265f..5683608 100644 --- a/src/App/Service/AwardeeManagerFactory.php +++ b/src/App/Service/AwardeeManagerFactory.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Service; use Doctrine\ORM\EntityManager; +use DoctrineExpressiveModule\Hydrator\DoctrineObject; use Psr\Container\ContainerInterface; class AwardeeManagerFactory @@ -12,6 +13,7 @@ class AwardeeManagerFactory public function __invoke(ContainerInterface $container) : AwardeeManager { $entityManager = $container->get(EntityManager::class); - return new AwardeeManager($entityManager); + $hydrator = $container->get(DoctrineObject::class); + return new AwardeeManager($entityManager, $hydrator); } } diff --git a/src/App/Service/JudgeManager.php b/src/App/Service/JudgeManager.php index e296656..2df996d 100644 --- a/src/App/Service/JudgeManager.php +++ b/src/App/Service/JudgeManager.php @@ -1,5 +1,7 @@ entityManager->createQueryBuilder(); - return $qb->select('j') + return $qb->select('j,t') ->from(Judge::class, 'j') ->innerJoin('j.titles', 't') ->where('t.year = :year') + ->orderBy('j.name', 'ASC') ->setParameter('year', $year) ->getQuery() ->getArrayResult(); @@ -89,4 +93,20 @@ class JudgeManager $this->entityManager->flush(); return $judge; } + + /** + * @param int $id + * @return bool + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ + public function delete(int $id): bool + { + if (null !== ($entity = $this->getJudge($id))) { + $this->entityManager->remove($entity); + $this->entityManager->flush(); + return true; + } + return false; + } } diff --git a/src/App/Service/YearManager.php b/src/App/Service/YearManager.php index bbefb7f..42e6258 100644 --- a/src/App/Service/YearManager.php +++ b/src/App/Service/YearManager.php @@ -29,11 +29,6 @@ class YearManager return $year['year']; }, $years); - $thisYear = date("Y"); - if (!in_array($thisYear, $filteredYears)) { - array_unshift($filteredYears, $thisYear); - } - return $filteredYears; } } diff --git a/templates/app/judges.inc.phtml b/templates/app/judges.inc.phtml index 2fbefb5..f6ef129 100644 --- a/templates/app/judges.inc.phtml +++ b/templates/app/judges.inc.phtml @@ -3,8 +3,8 @@
-
- +
+