src/Czech/Controller/StoreGroupController.php line 17

Open in your IDE?
  1. <?php
  2. namespace Czech\Controller;
  3. use App\Entity\Client\Store\StoreGroup;
  4. use App\Service\AppManager;
  5. use Czech\Form\Type\StoreGroupType;
  6. use Czech\Model\StoreGroupModel;
  7. use Czech\Service\StoreManager;
  8. use Knp\Component\Pager\PaginatorInterface;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Exception\HttpException;
  12. class StoreGroupController extends TableController
  13. {
  14.     public function index(Request $requestPaginatorInterface $paginatorAppManager $appManager)
  15.     {
  16.         $queryBuilder $appManager->getRepository(StoreGroup::class)
  17.             ->createQueryBuilder('sg');
  18.         return self::tableIndex('czech/store_group/index.html.twig'$queryBuilder$request$paginator'czech/store_group/list.html.twig');
  19.     }
  20.     public function create(Request $requestStoreManager $storeManager)
  21.     {
  22.         $storeGroupModel = new StoreGroupModel();
  23.         $form $this->createForm(StoreGroupType::class, $storeGroupModel);
  24.         $form->handleRequest($request);
  25.         if ($form->isSubmitted() && $form->isValid()) {
  26.             try {
  27.                 $storeManager->createStoreGroup($storeGroupModel);
  28.                 return $this->redirectToRoute('czech_homepage');
  29.             } catch (\Exception $exception) {
  30.             }
  31.         }
  32.         return $this->render('czech/store_group/create.html.twig', [
  33.             'form' => $form->createView()
  34.         ]);
  35.     }
  36.     public function update(StoreManager $storeManagerint $idRequest $requestAppManager $appManager)
  37.     {
  38.         /** @var StoreGroup $storeGroup */
  39.         $storeGroup $appManager->getRepository(StoreGroup::class)->find($id);
  40.         if (!$storeGroup instanceof StoreGroup) {
  41.             throw new HttpException(404"StoreGroup Not Found");
  42.         }
  43.         if($storeGroup->getDeletedAt() !== null) {
  44.             throw new HttpException(404"StoreGroup Already Deleted");
  45.         }
  46.         $storeGroupModel = new StoreGroupModel();
  47.         $storeGroupModel->setStoreGroupName($storeGroup->getName());
  48.         $storeGroupModel->setStores($storeGroup->getStores());
  49.         $form $this->createForm(StoreGroupType::class, $storeGroupModel);
  50.         $form->handleRequest($request);
  51.         if ($form->isSubmitted() && $form->isValid()) {
  52.             try {
  53.                 $storeManager->updateStoreGroup($id$storeGroupModel);
  54.                 return $this->redirectToRoute('czech_homepage');
  55.             } catch (\Exception $exception) {
  56.             }
  57.         }
  58.         return $this->render('czech/store_group/update.html.twig', [
  59.             'form' => $form->createView(),
  60.             'storeGroup' => $storeGroup
  61.         ]);
  62.     }
  63.     public function delete(int $idAppManager $appManager)
  64.     {
  65.         /** @var StoreGroup $storeGroup */
  66.         $storeGroup $appManager->getRepository(StoreGroup::class)->find($id);
  67.         if (!$storeGroup instanceof StoreGroup) {
  68.             throw new HttpException(404"Store Group Not Found");
  69.         }
  70.         if ($storeGroup->getDeletedAt() !== null) {
  71.             throw new HttpException(404"Store Group Already Deleted");
  72.         }
  73.         $storeGroup->setDeletedAt(new \DateTime());
  74.         $appManager->flush();
  75.         return new JsonResponse(null204);
  76.     }
  77. }