<?php
namespace Czech\Controller;
use App\Entity\Client\Store\StoreGroup;
use App\Service\AppManager;
use Czech\Form\Type\StoreGroupType;
use Czech\Model\StoreGroupModel;
use Czech\Service\StoreManager;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
class StoreGroupController extends TableController
{
public function index(Request $request, PaginatorInterface $paginator, AppManager $appManager)
{
$queryBuilder = $appManager->getRepository(StoreGroup::class)
->createQueryBuilder('sg');
return self::tableIndex('czech/store_group/index.html.twig', $queryBuilder, $request, $paginator, 'czech/store_group/list.html.twig');
}
public function create(Request $request, StoreManager $storeManager)
{
$storeGroupModel = new StoreGroupModel();
$form = $this->createForm(StoreGroupType::class, $storeGroupModel);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$storeManager->createStoreGroup($storeGroupModel);
return $this->redirectToRoute('czech_homepage');
} catch (\Exception $exception) {
}
}
return $this->render('czech/store_group/create.html.twig', [
'form' => $form->createView()
]);
}
public function update(StoreManager $storeManager, int $id, Request $request, AppManager $appManager)
{
/** @var StoreGroup $storeGroup */
$storeGroup = $appManager->getRepository(StoreGroup::class)->find($id);
if (!$storeGroup instanceof StoreGroup) {
throw new HttpException(404, "StoreGroup Not Found");
}
if($storeGroup->getDeletedAt() !== null) {
throw new HttpException(404, "StoreGroup Already Deleted");
}
$storeGroupModel = new StoreGroupModel();
$storeGroupModel->setStoreGroupName($storeGroup->getName());
$storeGroupModel->setStores($storeGroup->getStores());
$form = $this->createForm(StoreGroupType::class, $storeGroupModel);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$storeManager->updateStoreGroup($id, $storeGroupModel);
return $this->redirectToRoute('czech_homepage');
} catch (\Exception $exception) {
}
}
return $this->render('czech/store_group/update.html.twig', [
'form' => $form->createView(),
'storeGroup' => $storeGroup
]);
}
public function delete(int $id, AppManager $appManager)
{
/** @var StoreGroup $storeGroup */
$storeGroup = $appManager->getRepository(StoreGroup::class)->find($id);
if (!$storeGroup instanceof StoreGroup) {
throw new HttpException(404, "Store Group Not Found");
}
if ($storeGroup->getDeletedAt() !== null) {
throw new HttpException(404, "Store Group Already Deleted");
}
$storeGroup->setDeletedAt(new \DateTime());
$appManager->flush();
return new JsonResponse(null, 204);
}
}