src/Controller/AdminAttributeGroupController.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Admin\AttributeGroupAdmin;
  4. use App\Entity\AttributeGroup;
  5. use Pix\SortableBehaviorBundle\Services\PositionHandler;
  6. use Sonata\AdminBundle\Controller\CRUDController;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Session\Session;
  9. use Symfony\Component\PropertyAccess\PropertyAccess;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class AdminAttributeGroupController extends CRUDController
  12. {
  13.     /** @var AttributeGroupAdmin */
  14.     protected $admin;
  15.     public function setparentAction($parentId$nextPage)
  16.     {
  17.         $parentId = (int)$parentId;
  18.         $parent $this->admin->getRepository()->find($parentId);
  19.         if (empty($parent)) {
  20.             return new RedirectResponse($this->admin->generateUrl($nextPage));
  21.         }
  22.         /** @var Session $session */
  23.         $session $this->container->get('session');
  24.         $session->set(AttributeGroupAdmin::SESSION_PARENT_ID_NAME$parentId);
  25.         return new RedirectResponse($this->admin->generateUrl($nextPage));
  26.     }
  27.     public function resetparentAction($nextPage)
  28.     {
  29.         /** @var Session $session */
  30.         $session $this->container->get('session');
  31.         $session->remove(AttributeGroupAdmin::SESSION_PARENT_ID_NAME);
  32.         return new RedirectResponse($this->admin->generateUrl($nextPage));
  33.     }
  34.     /**
  35.      * Move element
  36.      *
  37.      * @param string $position
  38.      *
  39.      * @return RedirectResponse|\Symfony\Component\HttpFoundation\Response
  40.      */
  41.     public function moveAction($position)
  42.     {
  43.         /** @var AttributeGroup $attributeGroup */
  44.         $attributeGroup $this->admin->getSubject();
  45.         /** @var AttributeGroupAdmin\ $admin */
  46.         $admin $this->admin;
  47.         if (!$this->isXmlHttpRequest()) {
  48.             return $this->renderJson(array(
  49.                 'result' => 'ok',
  50.                 'objectId' => $this->admin->getNormalizedIdentifier($attributeGroup)
  51.             ));
  52.         }
  53.         $currentPosition $attributeGroup->getPosition();
  54.         $position = (int)$position;
  55.         $numberOfMoves $position $currentPosition;
  56.         if (empty($numberOfMoves)) {
  57.             return $this->renderJson(array(
  58.                 'result' => 'ok',
  59.                 'objectId' => $this->admin->getNormalizedIdentifier($attributeGroup)
  60.             ));
  61.         }
  62.         $repository $admin->getRepository();
  63.         if ($numberOfMoves 0) {
  64.             $repository->moveDown($attributeGroup$numberOfMoves);
  65.         } else {
  66.             $numberOfMoves abs($numberOfMoves);
  67.             $repository->moveUp($attributeGroup$numberOfMoves);
  68.         }
  69.         $attributeGroups $repository->findBy(['parent' => $attributeGroup->getParent()], ['lft' => 'ASC']);
  70.         $order 0;
  71.         foreach ($attributeGroups as $item) {
  72.             $item->setPosition($order);
  73.             $this->getDoctrine()->getManager()->persist($item);
  74.             $this->getDoctrine()->getManager()->flush();
  75.             $order++;
  76.         }
  77.         return $this->renderJson(array(
  78.             'result' => 'ok',
  79.             'objectId' => $this->admin->getNormalizedIdentifier($attributeGroup)
  80.         ));
  81.     }
  82. }