src/CoreBundle/Component/Grid/GridView.php line 95

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\CoreBundle\Component\Grid;
  4. use Symfony\Component\Form\FormView;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Twig_Environment;
  9. class GridView
  10. {
  11.     /**
  12.      * @var \App\CoreBundle\Component\Grid\Grid
  13.      */
  14.     protected $grid;
  15.     /**
  16.      * @var array
  17.      */
  18.     protected $templateParameters;
  19.     /**
  20.      * @var \Twig_TemplateWrapper[]
  21.      */
  22.     protected $templates;
  23.     /**
  24.      * @var string|string[]|null
  25.      */
  26.     protected $theme;
  27.     /**
  28.      * @var \Symfony\Component\HttpFoundation\RequestStack
  29.      */
  30.     protected $requestStack;
  31.     /**
  32.      * @var \Symfony\Component\Routing\RouterInterface
  33.      */
  34.     protected $router;
  35.     /**
  36.      * @var \Twig_Environment
  37.      */
  38.     protected $twig;
  39.     /**
  40.      * @param \App\CoreBundle\Component\Grid\Grid $grid
  41.      * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
  42.      * @param \Symfony\Component\Routing\RouterInterface $router
  43.      * @param \Twig_Environment $twig
  44.      * @param string|string[] $theme
  45.      * @param array $templateParameters
  46.      */
  47.     public function __construct(
  48.         Grid $grid,
  49.         RequestStack $requestStack,
  50.         RouterInterface $router,
  51.         Twig_Environment $twig,
  52.         $theme,
  53.         array $templateParameters = []
  54.     ) {
  55.         $this->grid $grid;
  56.         $this->requestStack $requestStack;
  57.         $this->router $router;
  58.         $this->twig $twig;
  59.         $this->setTheme($theme$templateParameters);
  60.     }
  61.     public function render()
  62.     {
  63.         $this->renderBlock('grid');
  64.     }
  65.     /**
  66.      * @param array|null $removeParameters
  67.      */
  68.     public function renderHiddenInputs($removeParameters null)
  69.     {
  70.         $this->renderBlock('grid_hidden_inputs', [
  71.             'parameter' => $this->grid->getUrlGridParameters(null$removeParameters),
  72.         ]);
  73.     }
  74.     /**
  75.      * @param string $name
  76.      * @param array $parameters
  77.      * @param bool $echo
  78.      * @return string|null|void
  79.      */
  80.     public function renderBlock($name, array $parameters = [], $echo true)
  81.     {
  82.         foreach ($this->getTemplates() as $template) {
  83.             if ($template->hasBlock($name)) {
  84.                 $parameters array_merge(
  85.                     $parameters,
  86.                     $this->templateParameters,
  87.                     [
  88.                         'gridView' => $this,
  89.                         'grid' => $this->grid,
  90.                     ]
  91.                 );
  92.                 $templateParameters $this->twig->mergeGlobals($parameters);
  93.                 if ($echo) {
  94.                     echo $template->renderBlock($name$templateParameters);
  95.                     return;
  96.                 } else {
  97.                     return $template->renderBlock($name$templateParameters);
  98.                 }
  99.             }
  100.         }
  101.         throw new \InvalidArgumentException(sprintf('Block "%s" doesn\'t exist in grid template "%s".'$name$this->theme));
  102.     }
  103.     /**
  104.      * @param \App\CoreBundle\Component\Grid\Column $column
  105.      * @param array|null $row
  106.      * @param \Symfony\Component\Form\FormView|null $formView
  107.      */
  108.     public function renderCell(Column $column, ?array $row null, ?FormView $formView null)
  109.     {
  110.         if ($row !== null) {
  111.             $value $this->getCellValue($column$row);
  112.         } else {
  113.             $value null;
  114.         }
  115.         $blockParameters = [
  116.             'value' => $value,
  117.             'row' => $row,
  118.             'column' => $column,
  119.             'form' => $formView,
  120.         ];
  121.         if ($formView === null) {
  122.             $possibleBlocks = [
  123.                 'grid_value_cell_id_' $column->getId(),
  124.                 'grid_value_cell_type_' $this->getVariableType($value),
  125.                 'grid_value_cell',
  126.             ];
  127.         } else {
  128.             $possibleBlocks = [
  129.                 'grid_value_cell_edit_id_' $column->getId(),
  130.                 'grid_value_cell_edit_type_' $this->getVariableType($value),
  131.                 'grid_value_cell_edit',
  132.             ];
  133.         }
  134.         foreach ($possibleBlocks as $blockName) {
  135.             if ($this->blockExists($blockName)) {
  136.                 $this->renderBlock($blockName$blockParameters);
  137.                 break;
  138.             }
  139.         }
  140.     }
  141.     /**
  142.      * @param \App\CoreBundle\Component\Grid\GroupActionColumn $groupActionColumn
  143.      * @param array $row
  144.      */
  145.     public function renderGroupMenuActionCell(GroupActionColumn $groupActionColumn, array $row)
  146.     {
  147.         $posibleBlocks = [
  148.             'grid_group_menu_action_cell_type_' $groupActionColumn->getName(),
  149.             'grid_group_menu_action_cell',
  150.         ];
  151.         foreach ($posibleBlocks as $blockName) {
  152.             if ($this->blockExists($blockName)) {
  153.                 $this->renderBlock($blockName, ['groupActionColumn' => $groupActionColumn'row' => $row]);
  154.                 break;
  155.             }
  156.         }
  157.     }
  158.     /**
  159.      * @param \App\CoreBundle\Component\Grid\ActionColumn $actionColumn
  160.      * @param array $row
  161.      */
  162.     public function renderGroupActionCell(ActionColumn $actionColumn, array $row)
  163.     {
  164.         $posibleBlocks = [
  165.             'grid_group_action_cell_type_' $actionColumn->getType(),
  166.             'grid_group_action_cell',
  167.         ];
  168.         foreach ($posibleBlocks as $blockName) {
  169.             if ($this->blockExists($blockName)) {
  170.                 $this->renderBlock($blockName, ['actionColumn' => $actionColumn'row' => $row]);
  171.                 break;
  172.             }
  173.         }
  174.     }
  175.     /**
  176.      * @param \App\CoreBundle\Component\Grid\ActionColumn $actionColumn
  177.      * @param array $row
  178.      */
  179.     public function renderActionCell(ActionColumn $actionColumn, array $row)
  180.     {
  181.         $posibleBlocks = [
  182.             'grid_action_cell_type_' $actionColumn->getType(),
  183.             'grid_action_cell',
  184.         ];
  185.         foreach ($posibleBlocks as $blockName) {
  186.             if ($this->blockExists($blockName)) {
  187.                 $this->renderBlock($blockName, ['actionColumn' => $actionColumn'row' => $row]);
  188.                 break;
  189.             }
  190.         }
  191.     }
  192.     /**
  193.      * @param \App\CoreBundle\Component\Grid\Column $column
  194.      */
  195.     public function renderTitleCell(Column $column)
  196.     {
  197.         $posibleBlocks = [
  198.             'grid_title_cell_id_' $column->getId(),
  199.             'grid_title_cell',
  200.         ];
  201.         foreach ($posibleBlocks as $blockName) {
  202.             if ($this->blockExists($blockName)) {
  203.                 $this->renderBlock($blockName, ['column' => $column]);
  204.                 break;
  205.             }
  206.         }
  207.     }
  208.     /**
  209.      * @param array $parameters
  210.      * @param array|string|null $removeParameters
  211.      * @return string
  212.      */
  213.     public function getUrl(?array $parameters null$removeParameters null)
  214.     {
  215.         $masterRequest $this->requestStack->getMasterRequest();
  216.         $routeParameters $this->grid->getUrlParameters($parameters$removeParameters);
  217.         return $this->router->generate(
  218.             $masterRequest->attributes->get('_route'),
  219.             $routeParameters,
  220.             UrlGeneratorInterface::ABSOLUTE_URL
  221.         );
  222.     }
  223.     /**
  224.      * @param string $name
  225.      * @return bool
  226.      */
  227.     protected function blockExists($name)
  228.     {
  229.         foreach ($this->getTemplates() as $template) {
  230.             if ($template->hasBlock($name)) {
  231.                 return true;
  232.             }
  233.         }
  234.         return false;
  235.     }
  236.     /**
  237.      * @return string|array
  238.      */
  239.     public function getTheme()
  240.     {
  241.         return $this->theme;
  242.     }
  243.     /**
  244.      * @param string|string[] $theme
  245.      * @param array $parameters
  246.      */
  247.     protected function setTheme($theme, array $parameters = [])
  248.     {
  249.         $this->theme $theme;
  250.         $this->templateParameters $parameters;
  251.     }
  252.     /**
  253.      * @return \Twig_TemplateWrapper[]
  254.      */
  255.     protected function getTemplates()
  256.     {
  257.         if (empty($this->templates)) {
  258.             $this->templates = [];
  259.             if (is_array($this->theme)) {
  260.                 foreach ($this->theme as $theme) {
  261.                     $this->templates[] = $this->getTemplateFromString($theme);
  262.                 }
  263.             } else {
  264.                 $this->templates[] = $this->getTemplateFromString($this->theme);
  265.             }
  266.         }
  267.         return $this->templates;
  268.     }
  269.     /**
  270.      * @param string $theme
  271.      * @return \Twig_TemplateWrapper
  272.      */
  273.     protected function getTemplateFromString($theme)
  274.     {
  275.         return $this->twig->load($theme);
  276.     }
  277.     /**
  278.      * @param \App\CoreBundle\Component\Grid\Column $column
  279.      * @param array $row
  280.      * @return mixed
  281.      */
  282.     protected function getCellValue(Column $column$row)
  283.     {
  284.         return Grid::getValueFromRowBySourceColumnName($row$column->getSourceColumnName());
  285.     }
  286.     /**
  287.      * @param mixed $variable
  288.      * @return string
  289.      */
  290.     protected function getVariableType($variable)
  291.     {
  292.         switch (gettype($variable)) {
  293.             case 'boolean':
  294.                 return 'boolean';
  295.             case 'integer':
  296.             case 'double':
  297.                 return 'number';
  298.             case 'object':
  299.                 return str_replace('\\''_'get_class($variable));
  300.             case 'string':
  301.                 return 'string';
  302.             case 'NULL':
  303.                 return 'null';
  304.             default:
  305.                 return 'unknown';
  306.         }
  307.     }
  308. }