src/CoreBundle/Component/Grid/Grid.php line 785

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\CoreBundle\Component\Grid;
  4. use App\CoreBundle\Component\Grid\InlineEdit\GridInlineEditInterface;
  5. use App\CoreBundle\Component\Grid\ModalEdit\GridModalEditInterface;
  6. use App\CoreBundle\Component\Router\Security\RouteCsrfProtector;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Twig\Environment as Twig_Environment;
  10. class Grid
  11. {
  12.     public const GET_PARAMETER 'g';
  13.     public const DEFAULT_VIEW_THEME 'Admin/Grid/Grid.html.twig';
  14.     public const DEFAULT_LIMIT 10;
  15.     /**
  16.      * @var string
  17.      */
  18.     protected $id;
  19.     /**
  20.      * @var \App\CoreBundle\Component\Grid\Column[]
  21.      */
  22.     protected $columnsById = [];
  23.     /**
  24.      * @var \App\CoreBundle\Component\Grid\ActionColumn[]
  25.      */
  26.     protected $actionColumns = [];
  27.     /**
  28.      * @var \App\CoreBundle\Component\Grid\GroupActionColumn[]
  29.      */
  30.     protected $groupActionColumns = [];
  31.     /**
  32.      * @var bool
  33.      */
  34.     protected $enablePaging false;
  35.     /**
  36.      * @var bool
  37.      */
  38.     protected $enableSelecting false;
  39.     /**
  40.      * @var array
  41.      */
  42.     protected $allowedLimits = [103010020050010002500500010000];
  43.     /**
  44.      * @var int
  45.      */
  46.     protected $limit;
  47.     /**
  48.      * @var bool
  49.      */
  50.     protected $isLimitFromRequest false;
  51.     /**
  52.      * @var int
  53.      */
  54.     protected $page 1;
  55.     /**
  56.      * @var int|null
  57.      */
  58.     protected $totalCount;
  59.     /**
  60.      * @var int|null
  61.      */
  62.     protected $pageCount;
  63.     /**
  64.      * @var string|null
  65.      */
  66.     protected $orderSourceColumnName;
  67.     /**
  68.      * @var string|null
  69.      */
  70.     protected $orderDirection;
  71.     /**
  72.      * @var bool
  73.      */
  74.     protected $isOrderFromRequest false;
  75.     /**
  76.      * @var array
  77.      */
  78.     protected $rows = [];
  79.     /**
  80.      * @var \Symfony\Component\HttpFoundation\RequestStack
  81.      */
  82.     protected $requestStack;
  83.     /**
  84.      * @var \Symfony\Component\Routing\RouterInterface
  85.      */
  86.     protected $router;
  87.     /**
  88.      * @var \App\CoreBundle\Component\Router\Security\RouteCsrfProtector
  89.      */
  90.     protected $routeCsrfProtector;
  91.     /**
  92.      * @var \Twig\Environment
  93.      */
  94.     protected $twig;
  95.     /**
  96.      * @var \App\CoreBundle\Component\Grid\DataSourceInterface
  97.      */
  98.     protected $dataSource;
  99.     /**
  100.      * @var string
  101.      */
  102.     protected $actionColumnClassAttribute '';
  103.     /**
  104.      * @var \App\CoreBundle\Component\Grid\InlineEdit\GridInlineEditInterface|null
  105.      */
  106.     protected $inlineEditService;
  107.     /**
  108.      * @var \App\CoreBundle\Component\Grid\ModalEdit\GridModalEditInterface|null
  109.      */
  110.     protected $modalEditService;
  111.     /**
  112.      * @var string|null
  113.      */
  114.     protected $orderingEntityClass;
  115.     /**
  116.      * @var \App\CoreBundle\Component\Paginator\PaginationResult
  117.      */
  118.     protected $paginationResults;
  119.     /**
  120.      * @var string|string[]|null
  121.      */
  122.     protected $viewTheme;
  123.     /**
  124.      * @var array
  125.      */
  126.     protected $viewTemplateParameters;
  127.     /**
  128.      * @var array
  129.      */
  130.     protected $selectedRowIds;
  131.     /**
  132.      * @var bool
  133.      */
  134.     protected $multipleDragAndDrop;
  135.     /**
  136.      * @param string $id
  137.      * @param \App\CoreBundle\Component\Grid\DataSourceInterface $dataSource
  138.      * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
  139.      * @param \Symfony\Component\Routing\RouterInterface $router
  140.      * @param \App\CoreBundle\Component\Router\Security\RouteCsrfProtector $routeCsrfProtector
  141.      * @param \Twig\Environment $twig
  142.      */
  143.     public function __construct(
  144.         $id,
  145.         DataSourceInterface $dataSource,
  146.         RequestStack $requestStack,
  147.         RouterInterface $router,
  148.         RouteCsrfProtector $routeCsrfProtector,
  149.         Twig_Environment $twig
  150.     ) {
  151.         if (empty($id)) {
  152.             $message 'Grid id cannot be empty.';
  153.             throw new \App\CoreBundle\Component\Grid\Exception\EmptyGridIdException($message);
  154.         }
  155.         $this->id $id;
  156.         $this->dataSource $dataSource;
  157.         $this->requestStack $requestStack;
  158.         $this->router $router;
  159.         $this->routeCsrfProtector $routeCsrfProtector;
  160.         $this->twig $twig;
  161.         $this->limit = static::DEFAULT_LIMIT;
  162.         $this->page 1;
  163.         $this->viewTheme = static::DEFAULT_VIEW_THEME;
  164.         $this->viewTemplateParameters = [];
  165.         $this->selectedRowIds = [];
  166.         $this->multipleDragAndDrop false;
  167.         $this->loadFromRequest();
  168.     }
  169.     /**
  170.      * @param string $id
  171.      * @param string $sourceColumnName
  172.      * @param string $title
  173.      * @param bool $sortable
  174.      * @return \App\CoreBundle\Component\Grid\Column
  175.      */
  176.     public function addColumn($id$sourceColumnName$title$sortable false$modalEdit false)
  177.     {
  178.         if (array_key_exists($id$this->columnsById)) {
  179.             throw new \App\CoreBundle\Component\Grid\Exception\DuplicateColumnIdException(
  180.                 'Duplicate column id "' $id '" in grid "' $this->id '"'
  181.             );
  182.         }
  183.         $column = new Column($id$sourceColumnName$title$sortable$modalEdit);
  184.         $this->columnsById[$id] = $column;
  185.         return $column;
  186.     }
  187.     /**
  188.      * @param string $type
  189.      * @param string $name
  190.      * @param string $route
  191.      * @param array $bindingRouteParams
  192.      * @param array $additionalRouteParams
  193.      * @param string|null $group
  194.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  195.      */
  196.     public function addActionColumn(
  197.         $type,
  198.         $name,
  199.         $route,
  200.         array $bindingRouteParams = [],
  201.         array $additionalRouteParams = [],
  202.         ?string $group null
  203.     ) {
  204.         $actionColumn = new ActionColumn(
  205.             $this->router,
  206.             $this->routeCsrfProtector,
  207.             $type,
  208.             $name,
  209.             $route,
  210.             $bindingRouteParams,
  211.             $additionalRouteParams
  212.         );
  213.         if ($group) {
  214.             $this->groupActionColumns[$group]->addActionColumn($actionColumn);
  215.         } else {
  216.             $this->actionColumns[] = $actionColumn;
  217.         }
  218.         return $actionColumn;
  219.     }
  220.     /**
  221.      * @param string $name
  222.      * @return \App\CoreBundle\Component\Grid\GroupActionColumn
  223.      */
  224.     public function addActionGroupColumn($name) {
  225.         $groupActionColumn = new GroupActionColumn($name);
  226.         $this->groupActionColumns[$name] = new GroupActionColumn($name);
  227.         return $groupActionColumn;
  228.     }
  229.     /**
  230.      * @param string $route
  231.      * @param array $bindingRouteParams
  232.      * @param array $additionalRouteParams
  233.      * @param string|null $group
  234.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  235.      */
  236.     public function addShowActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $group null)
  237.     {
  238.         return $this->addActionColumn(ActionColumn::TYPE_SHOWt('Show'), $route$bindingRouteParams$additionalRouteParams$group);
  239.     }
  240.     /**
  241.      * @param string $route
  242.      * @param array $bindingRouteParams
  243.      * @param array $additionalRouteParams
  244.      * @param string|null $group
  245.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  246.      */
  247.     public function addProductMatchingActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $group null)
  248.     {
  249.         return $this->addActionColumn(ActionColumn::TYPE_PRODUCT_MATCHt('Product match'), $route$bindingRouteParams$additionalRouteParams$group);
  250.     }
  251.     /**
  252.      * @param string $route
  253.      * @param array $bindingRouteParams
  254.      * @param array $additionalRouteParams
  255.      * @param string|null $group
  256.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  257.      */
  258.     public function addEditActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $group null)
  259.     {
  260.         return $this->addActionColumn(ActionColumn::TYPE_EDITt('Edit'), $route$bindingRouteParams$additionalRouteParams$group);
  261.     }
  262.     /**
  263.      * @param string $route
  264.      * @param array $bindingRouteParams
  265.      * @param array $additionalRouteParams
  266.      * @param string|null $group
  267.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  268.      */
  269.     public function addSetAsPaidActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $group null)
  270.     {
  271.         return $this->addActionColumn(ActionColumn::TYPE_SET_AS_PAIDt('Set as paid'), $route$bindingRouteParams$additionalRouteParams$group);
  272.     }
  273.     /**
  274.      * @param string $route
  275.      * @param array $bindingRouteParams
  276.      * @param array $additionalRouteParams
  277.      * @param string|null $group
  278.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  279.      */
  280.     public function addDeleteActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $group null)
  281.     {
  282.         return $this->addActionColumn(ActionColumn::TYPE_DELETEt('Delete'), $route$bindingRouteParams$additionalRouteParams$group);
  283.     }
  284.     /**
  285.      * @param string $route
  286.      * @param array $bindingRouteParams
  287.      * @param array $additionalRouteParams
  288.      * @param string $id
  289.      * @param string|null $group
  290.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  291.      */
  292.     public function addShowLogActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $id ''$group null)
  293.     {
  294.         return $this->addActionColumn(ActionColumn::TYPE_SHOW$id$route$bindingRouteParams$additionalRouteParams$group);
  295.     }
  296.     /**
  297.      * @param string $route
  298.      * @param array $bindingRouteParams
  299.      * @param array $additionalRouteParams
  300.      * @param string $id
  301.      * @param string|null $group
  302.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  303.      */
  304.     public function addActivateActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $id ''$group null)
  305.     {
  306.         return $this->addActionColumn(ActionColumn::TYPE_ACTIVATE$id$route$bindingRouteParams$additionalRouteParams$group);
  307.     }
  308.     /**
  309.      * @param string $route
  310.      * @param array $bindingRouteParams
  311.      * @param array $additionalRouteParams
  312.      * @param string $id
  313.      * @param string|null $group
  314.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  315.      */
  316.     public function addRenewActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $id ''$group null)
  317.     {
  318.         return $this->addActionColumn(ActionColumn::TYPE_RENEW$id$route$bindingRouteParams$additionalRouteParams$group);
  319.     }
  320.     /**
  321.      * @param string $route
  322.      * @param array $bindingRouteParams
  323.      * @param array $additionalRouteParams
  324.      * @param string $id
  325.      * @param string|null $group
  326.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  327.      */
  328.     public function addDuplicateActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $id ''$group null)
  329.     {
  330.         return $this->addActionColumn(ActionColumn::TYPE_DUPLICATE$id$route$bindingRouteParams$additionalRouteParams$group);
  331.     }
  332.     /**
  333.      * @param string $route
  334.      * @param array $bindingRouteParams
  335.      * @param array $additionalRouteParams
  336.      * @param string $id
  337.      * @param string|null $group
  338.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  339.      */
  340.     public function addUpgradeActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $id ''$group null)
  341.     {
  342.         return $this->addActionColumn(ActionColumn::TYPE_UPGRADE$id$route$bindingRouteParams$additionalRouteParams$group);
  343.     }
  344.     /**
  345.      * @param string $route
  346.      * @param array $bindingRouteParams
  347.      * @param array $additionalRouteParams
  348.      * @param string $id
  349.      * @param string|null $group
  350.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  351.      */
  352.     public function addRunActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $id ''$group null)
  353.     {
  354.         return $this->addActionColumn(ActionColumn::TYPE_RUN$id$route$bindingRouteParams$additionalRouteParams$group);
  355.     }
  356.     /**
  357.      * @param string $route
  358.      * @param array $bindingRouteParams
  359.      * @param array $additionalRouteParams
  360.      * @param string $id
  361.      * @param string|null $group
  362.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  363.      */
  364.     public function addLoginActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $id ''$group null)
  365.     {
  366.         return $this->addActionColumn(ActionColumn::TYPE_LOGIN$id$route$bindingRouteParams$additionalRouteParams$group);
  367.     }
  368.     /**
  369.      * @param string $route
  370.      * @param array $bindingRouteParams
  371.      * @param array $additionalRouteParams
  372.      * @param string $id
  373.      * @param string|null $group
  374.      * @return \App\CoreBundle\Component\Grid\ActionColumn
  375.      */
  376.     public function addCancelActionColumn($route, array $bindingRouteParams = [], array $additionalRouteParams = [], $id ''$group null)
  377.     {
  378.         return $this->addActionColumn(ActionColumn::TYPE_CANCEL$id$route$bindingRouteParams$additionalRouteParams$group);
  379.     }
  380.     /**
  381.      * @param \App\CoreBundle\Component\Grid\InlineEdit\GridInlineEditInterface $inlineEditService
  382.      */
  383.     public function setInlineEditService(GridInlineEditInterface $inlineEditService)
  384.     {
  385.         $this->inlineEditService $inlineEditService;
  386.     }
  387.     /**
  388.      * @return bool
  389.      */
  390.     public function isInlineEdit()
  391.     {
  392.         return $this->inlineEditService !== null;
  393.     }
  394.     /**
  395.      * @return \App\CoreBundle\Component\Grid\InlineEdit\GridInlineEditInterface|null
  396.      */
  397.     public function getInlineEditService()
  398.     {
  399.         return $this->inlineEditService;
  400.     }
  401.     /**
  402.      * @param \App\CoreBundle\Component\Grid\ModalEdit\GridModalEditInterface $modalEditService
  403.      */
  404.     public function setModalEditService(GridModalEditInterface $modalEditService)
  405.     {
  406.         $this->modalEditService $modalEditService;
  407.     }
  408.     /**
  409.      * @return bool
  410.      */
  411.     public function isModalEdit()
  412.     {
  413.         return $this->modalEditService !== null;
  414.     }
  415.     /**
  416.      * @return \App\CoreBundle\Component\Grid\ModalEdit\GridModalEditInterface|null
  417.      */
  418.     public function getModalEditService()
  419.     {
  420.         return $this->modalEditService;
  421.     }
  422.     /**
  423.      * @param array $row
  424.      * @return mixed
  425.      */
  426.     public function getRowId($row)
  427.     {
  428.         return self::getValueFromRowBySourceColumnName($row$this->dataSource->getRowIdSourceColumnName());
  429.     }
  430.     /**
  431.      * @param string $classAttribute
  432.      */
  433.     public function setActionColumnClassAttribute($classAttribute)
  434.     {
  435.         $this->actionColumnClassAttribute $classAttribute;
  436.     }
  437.     /**
  438.      * @param string|string[] $viewTheme
  439.      * @param array $viewParameters
  440.      */
  441.     public function setTheme($viewTheme, array $viewParameters = [])
  442.     {
  443.         $this->viewTheme $viewTheme;
  444.         $this->viewTemplateParameters $viewParameters;
  445.     }
  446.     /**
  447.      * @return \App\CoreBundle\Component\Grid\GridView
  448.      */
  449.     public function createView()
  450.     {
  451.         $gridView $this->createViewWithoutRows();
  452.         if ($this->isEnabledPaging()) {
  453.             $this->executeTotalQuery();
  454.         }
  455.         $this->loadRows();
  456.         return $gridView;
  457.     }
  458.     /**
  459.      * @param int $rowId
  460.      * @return \App\CoreBundle\Component\Grid\GridView
  461.      */
  462.     public function createViewWithOneRow($rowId)
  463.     {
  464.         $gridView $this->createViewWithoutRows();
  465.         $this->loadRowsWithOneRow($rowId);
  466.         return $gridView;
  467.     }
  468.     /**
  469.      * @return \App\CoreBundle\Component\Grid\GridView
  470.      */
  471.     public function createViewWithoutRows()
  472.     {
  473.         $this->rows = [];
  474.         $gridView = new GridView(
  475.             $this,
  476.             $this->requestStack,
  477.             $this->router,
  478.             $this->twig,
  479.             $this->viewTheme,
  480.             $this->viewTemplateParameters
  481.         );
  482.         return $gridView;
  483.     }
  484.     public function enablePaging()
  485.     {
  486.         $this->enablePaging true;
  487.     }
  488.     public function enableSelecting()
  489.     {
  490.         $this->enableSelecting true;
  491.     }
  492.     /**
  493.      * @param int $limit
  494.      */
  495.     public function setDefaultLimit($limit)
  496.     {
  497.         if (!$this->isLimitFromRequest) {
  498.             $this->setLimit((int)$limit);
  499.         }
  500.     }
  501.     /**
  502.      * @param string $columnId
  503.      * @param string $direction
  504.      */
  505.     public function setDefaultOrder($columnId$direction DataSourceInterface::ORDER_ASC)
  506.     {
  507.         if (!$this->isOrderFromRequest) {
  508.             $prefix $direction === DataSourceInterface::ORDER_DESC '-' '';
  509.             $this->setOrderingByOrderString($prefix $columnId);
  510.         }
  511.     }
  512.     /**
  513.      * @return string
  514.      */
  515.     public function getId()
  516.     {
  517.         return $this->id;
  518.     }
  519.     /**
  520.      * @return \App\CoreBundle\Component\Grid\Column[]
  521.      */
  522.     public function getColumnsById()
  523.     {
  524.         return $this->columnsById;
  525.     }
  526.     /**
  527.      * @param string $columnId
  528.      * @return bool
  529.      */
  530.     public function existsColumn($columnId)
  531.     {
  532.         return array_key_exists($columnId$this->columnsById);
  533.     }
  534.     /**
  535.      * @return \App\CoreBundle\Component\Grid\ActionColumn[]
  536.      */
  537.     public function getActionColumns()
  538.     {
  539.         return $this->actionColumns;
  540.     }
  541.     /**
  542.      * @return \App\CoreBundle\Component\Grid\GroupActionColumn[]
  543.      */
  544.     public function getGroupActionColumns()
  545.     {
  546.         return $this->groupActionColumns;
  547.     }
  548.     /**
  549.      * @return array
  550.      */
  551.     public function getRows()
  552.     {
  553.         return $this->rows;
  554.     }
  555.     /**
  556.      * @return bool
  557.      */
  558.     public function isEnabledPaging()
  559.     {
  560.         return $this->enablePaging;
  561.     }
  562.     /**
  563.      * @return bool
  564.      */
  565.     public function isEnabledSelecting()
  566.     {
  567.         return $this->enableSelecting;
  568.     }
  569.     /**
  570.      * @param array $row
  571.      * @return bool
  572.      */
  573.     public function isRowSelected(array $row)
  574.     {
  575.         $rowId $this->getRowId($row);
  576.         return in_array($rowId$this->selectedRowIdstrue);
  577.     }
  578.     /**
  579.      * @return array
  580.      */
  581.     public function getSelectedRowIds()
  582.     {
  583.         return $this->selectedRowIds;
  584.     }
  585.     /**
  586.      * @return int
  587.      */
  588.     public function getLimit()
  589.     {
  590.         return $this->limit;
  591.     }
  592.     /**
  593.      * @param int $limit
  594.      */
  595.     protected function setLimit($limit)
  596.     {
  597.         if (in_array($limit$this->allowedLimitstrue)) {
  598.             $this->limit $limit;
  599.         }
  600.     }
  601.     /**
  602.      * @return array
  603.      */
  604.     public function getAllowedLimits()
  605.     {
  606.         return $this->allowedLimits;
  607.     }
  608.     /**
  609.      * @return int|null
  610.      */
  611.     public function getTotalCount()
  612.     {
  613.         return $this->totalCount;
  614.     }
  615.     /**
  616.      * @return int
  617.      */
  618.     public function getPage()
  619.     {
  620.         return $this->page;
  621.     }
  622.     /**
  623.      * @return int
  624.      */
  625.     public function getPageCount()
  626.     {
  627.         return $this->pageCount;
  628.     }
  629.     /**
  630.      * @return string|null
  631.      */
  632.     public function getOrderSourceColumnName()
  633.     {
  634.         return $this->orderSourceColumnName;
  635.     }
  636.     /**
  637.      * @return string|null
  638.      */
  639.     public function getOrderSourceColumnNameWithDirection()
  640.     {
  641.         $prefix '';
  642.         if ($this->getOrderDirection() === DataSourceInterface::ORDER_DESC) {
  643.             $prefix '-';
  644.         }
  645.         return $prefix $this->getOrderSourceColumnName();
  646.     }
  647.     /**
  648.      * @return string|null
  649.      */
  650.     public function getOrderDirection()
  651.     {
  652.         return $this->orderDirection;
  653.     }
  654.     /**
  655.      * @return string
  656.      */
  657.     public function getActionColumnClassAttribute()
  658.     {
  659.         return $this->actionColumnClassAttribute;
  660.     }
  661.     /**
  662.      * @return \App\CoreBundle\Component\Paginator\PaginationResult
  663.      */
  664.     public function getPaginationResults()
  665.     {
  666.         return $this->paginationResults;
  667.     }
  668.     /**
  669.      * @param string $orderString
  670.      */
  671.     protected function setOrderingByOrderString($orderString)
  672.     {
  673.         if (substr((string)$orderString01) === '-') {
  674.             $this->orderDirection DataSourceInterface::ORDER_DESC;
  675.         } else {
  676.             $this->orderDirection DataSourceInterface::ORDER_ASC;
  677.         }
  678.         $this->orderSourceColumnName trim($orderString'-');
  679.     }
  680.     protected function loadFromRequest()
  681.     {
  682.         $queryData $this->requestStack->getMasterRequest()->query->get(self::GET_PARAMETER, []);
  683.         if (array_key_exists($this->id$queryData)) {
  684.             $gridQueryData $queryData[$this->id];
  685.             if (array_key_exists('limit'$gridQueryData)) {
  686.                 $this->setLimit((int)trim($gridQueryData['limit']));
  687.                 $this->isLimitFromRequest true;
  688.             }
  689.             if (array_key_exists('page'$gridQueryData)) {
  690.                 $this->page max((int)trim($gridQueryData['page']), 1);
  691.             }
  692.             if (array_key_exists('order'$gridQueryData)) {
  693.                 $this->setOrderingByOrderString(trim($gridQueryData['order']));
  694.                 $this->isOrderFromRequest true;
  695.             }
  696.         }
  697.         $requestData $this->requestStack->getMasterRequest()->request->get(self::GET_PARAMETER, []);
  698.         if (array_key_exists($this->id$requestData)) {
  699.             $gridRequestData $requestData[$this->id];
  700.             if (array_key_exists('selectedRowIds'$gridRequestData) && is_array($gridRequestData['selectedRowIds'])) {
  701.                 $this->selectedRowIds array_map('json_decode'$gridRequestData['selectedRowIds']);
  702.             }
  703.         }
  704.     }
  705.     /**
  706.      * @param array|string $removeParameters
  707.      * @return array
  708.      */
  709.     public function getGridParameters($removeParameters = [])
  710.     {
  711.         $gridParameters = [];
  712.         if ($this->isEnabledPaging()) {
  713.             $gridParameters['limit'] = $this->getLimit();
  714.             if ($this->getPage() > 1) {
  715.                 $gridParameters['page'] = $this->getPage();
  716.             }
  717.         }
  718.         if ($this->getOrderSourceColumnName() !== null) {
  719.             $gridParameters['order'] = $this->getOrderSourceColumnNameWithDirection();
  720.         }
  721.         foreach ((array)$removeParameters as $parameterToRemove) {
  722.             if (array_key_exists($parameterToRemove$gridParameters)) {
  723.                 unset($gridParameters[$parameterToRemove]);
  724.             }
  725.         }
  726.         return $gridParameters;
  727.     }
  728.     /**
  729.      * @param array|string|null $parameters
  730.      * @param array|string|null $removeParameters
  731.      * @return array
  732.      */
  733.     public function getUrlGridParameters($parameters null$removeParameters null)
  734.     {
  735.         $gridParameters array_replace_recursive(
  736.             $this->getGridParameters($removeParameters),
  737.             (array)$parameters
  738.         );
  739.         return [self::GET_PARAMETER => [$this->getId() => $gridParameters]];
  740.     }
  741.     /**
  742.      * @param array|string|null $parameters
  743.      * @param array|string|null $removeParameters
  744.      * @return array
  745.      */
  746.     public function getUrlParameters($parameters null$removeParameters null)
  747.     {
  748.         return array_replace_recursive(
  749.             $this->requestStack->getMasterRequest()->query->all(),
  750.             $this->requestStack->getMasterRequest()->attributes->get('_route_params'),
  751.             $this->getUrlGridParameters($parameters$removeParameters)
  752.         );
  753.     }
  754.     protected function loadRows()
  755.     {
  756.         if (array_key_exists($this->orderSourceColumnName$this->columnsById)
  757.             && $this->columnsById[$this->orderSourceColumnName]->isSortable()
  758.         ) {
  759.             $orderSourceColumnName $this->columnsById[$this->orderSourceColumnName]->getOrderSourceColumnName();
  760.         } else {
  761.             $orderSourceColumnName null;
  762.         }
  763.         $orderDirection $this->orderDirection;
  764.         if ($this->isDragAndDrop()) {
  765.             $orderSourceColumnName null;
  766.             $orderDirection null;
  767.         }
  768.         $this->paginationResults $this->dataSource->getPaginatedRows(
  769.             $this->enablePaging $this->limit null,
  770.             $this->page,
  771.             $orderSourceColumnName,
  772.             $orderDirection
  773.         );
  774.         $this->rows $this->paginationResults->getResults();
  775.     }
  776.     /**
  777.      * @param int $rowId
  778.      */
  779.     protected function loadRowsWithOneRow($rowId)
  780.     {
  781.         $this->rows = [$this->dataSource->getOneRow($rowId)];
  782.     }
  783.     protected function executeTotalQuery()
  784.     {
  785.         $this->totalCount $this->dataSource->getTotalRowsCount();
  786.         $this->pageCount max(ceil($this->totalCount $this->limit), 1);
  787.         $this->page min($this->page$this->pageCount);
  788.     }
  789.     /**
  790.      * @param array $row
  791.      * @param string $sourceColumnName
  792.      * @return mixed
  793.      */
  794.     public static function getValueFromRowBySourceColumnName(array $row$sourceColumnName)
  795.     {
  796.         $sourceColumnNameParts explode('.'$sourceColumnName);
  797.         if (count($sourceColumnNameParts) === 1) {
  798.             return $row[$sourceColumnNameParts[0]];
  799.         } elseif (count($sourceColumnNameParts) === 2) {
  800.             if (array_key_exists($sourceColumnNameParts[0], $row)
  801.                 && array_key_exists($sourceColumnNameParts[1], $row[$sourceColumnNameParts[0]])
  802.             ) {
  803.                 return $row[$sourceColumnNameParts[0]][$sourceColumnNameParts[1]];
  804.             } elseif (array_key_exists($sourceColumnNameParts[1], $row)) {
  805.                 return $row[$sourceColumnNameParts[1]];
  806.             } else {
  807.                 return $row[$sourceColumnName];
  808.             }
  809.         }
  810.         return $row[$sourceColumnName];
  811.     }
  812.     /**
  813.      * @param string $entityClass
  814.      */
  815.     public function enableDragAndDrop($entityClass)
  816.     {
  817.         $this->orderingEntityClass $entityClass;
  818.     }
  819.     public function enableMultipleDragAndDrop()
  820.     {
  821.         $this->multipleDragAndDrop true;
  822.     }
  823.     /**
  824.      * @return bool
  825.      */
  826.     public function isDragAndDrop()
  827.     {
  828.         return $this->orderingEntityClass !== null;
  829.     }
  830.     /**
  831.      * @return string|null
  832.      */
  833.     public function getOrderingEntityClass()
  834.     {
  835.         return $this->orderingEntityClass;
  836.     }
  837.     /**
  838.      * @return bool
  839.      */
  840.     public function isMultipleDragAndDrop()
  841.     {
  842.         return $this->multipleDragAndDrop;
  843.     }
  844. }