117 lines
3.9 KiB
PHP
117 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\Grid;
|
|
use App\Entity\Map;
|
|
use App\Entity\Region;
|
|
use App\Form\GridType;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\DBAL\Types\Types;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use LogicException;
|
|
use Symfony\Component\Form\FormFactoryInterface;
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\Routing\RouterInterface;
|
|
|
|
/**
|
|
* @method Grid|null find($id, $lockMode=null, $lockVersion=null)
|
|
* @method Grid|null findOneBy(array $criteria, array $orderBy=null)
|
|
* @method Grid[] findAll()
|
|
* @method Grid[] findBy(array $criteria, array $orderBy=null, $limit=null, $offset=null)
|
|
*/
|
|
class GridRepository extends ServiceEntityRepository {
|
|
public function __construct(ManagerRegistry $registry,
|
|
private RouterInterface $router,
|
|
private FormFactoryInterface $formFactory,
|
|
private MapRepository $mapRepository) {
|
|
parent::__construct($registry, Grid::class);
|
|
}
|
|
|
|
public function getForm(string $route, Grid $grid): FormInterface {
|
|
$params=$grid->getId() !== null ? array('id'=>$grid->getId()) : array();
|
|
|
|
return $this->formFactory->create(GridType::class, $grid, array(
|
|
'action'=>$this->router->generate($route, $params),
|
|
'method'=>'post',
|
|
'data_route'=>$route,
|
|
));
|
|
}
|
|
|
|
public function getGridForm(string $route, Region $region): FormInterface {
|
|
return $this->formFactory->create(GridType::class, null, array(
|
|
'action'=>$this->router->generate($route, array('regionId'=>$region->getId())),
|
|
'method'=>'post',
|
|
'data_route'=>$route,
|
|
));
|
|
}
|
|
|
|
/**
|
|
* @param Region $region
|
|
* @return Grid[]
|
|
*/
|
|
public function getGridCells(Region $region): array {
|
|
$qb=$this->_em->createQueryBuilder();
|
|
|
|
$qb->select("grid")
|
|
->from(Grid::class, "grid")
|
|
->andWhere($qb->expr()->eq("grid.region", ":param_region"))
|
|
->orderBy("grid.row")
|
|
->addOrderBy("grid.col");
|
|
|
|
$qb->setParameter('param_region', $region->getId(), Types::INTEGER);
|
|
|
|
return $qb->getQuery()->getResult();
|
|
}
|
|
|
|
public function buildWorldmap(float $version, array $cells, array $maps, bool $withForm=false): array {
|
|
$grid=array();
|
|
|
|
/** @var Map $map */
|
|
foreach($maps as $key=>$map) {
|
|
if(!($map instanceof Map)) {
|
|
throw new LogicException(sprintf(
|
|
'Variable passed must be an instance of "%s". "%s" given',
|
|
Map::class,
|
|
gettype($map)
|
|
));
|
|
}
|
|
|
|
$maps["cell_{$map->getGrid()->getId()}"]=array(
|
|
'map'=>$map,
|
|
'form'=>$withForm ? $this->mapRepository->getForm('bo_map_edit', $map->getGrid(), $map)->createView() : null
|
|
);
|
|
|
|
unset($maps[$key]);
|
|
}
|
|
|
|
/** @var Grid $cell */
|
|
foreach($cells as $cell) {
|
|
if(!($cell instanceof Grid)) {
|
|
throw new LogicException(sprintf(
|
|
'Variable passed must be an instance of "%s". "%s" given',
|
|
Grid::class,
|
|
gettype($map)
|
|
));
|
|
}
|
|
|
|
if(!array_key_exists("cell_{$cell->getId()}", $maps)) {
|
|
$_map=new Map();
|
|
$_map->setVersion($version);
|
|
|
|
$maps["cell_{$cell->getId()}"]=array(
|
|
'map'=>$_map,
|
|
'form'=>$withForm ? $this->mapRepository->getForm('bo_map_new', $cell, $_map)->createView() : null
|
|
);
|
|
}
|
|
|
|
$grid[$cell->getRow()][$cell->getCol()]=array(
|
|
'id'=>$cell->getId(),
|
|
'map_data'=>$maps["cell_{$cell->getId()}"],
|
|
);
|
|
}
|
|
|
|
return $grid;
|
|
}
|
|
}
|