AdminStatsBlockService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Block;
  11. use Sonata\AdminBundle\Admin\Pool;
  12. use Sonata\BlockBundle\Block\BlockContextInterface;
  13. use Sonata\BlockBundle\Block\Service\AbstractBlockService;
  14. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\OptionsResolver\OptionsResolver;
  17. /**
  18. * Class AdminStatsBlockService.
  19. *
  20. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  21. */
  22. class AdminStatsBlockService extends AbstractBlockService
  23. {
  24. /**
  25. * @var Pool
  26. */
  27. protected $pool;
  28. /**
  29. * @param string $name
  30. * @param EngineInterface $templating
  31. * @param Pool $pool
  32. */
  33. public function __construct($name, EngineInterface $templating, Pool $pool)
  34. {
  35. parent::__construct($name, $templating);
  36. $this->pool = $pool;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function execute(BlockContextInterface $blockContext, Response $response = null)
  42. {
  43. $admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('code'));
  44. $datagrid = $admin->getDatagrid();
  45. $filters = $blockContext->getSetting('filters');
  46. if (!isset($filters['_per_page'])) {
  47. $filters['_per_page'] = array('value' => $blockContext->getSetting('limit'));
  48. }
  49. foreach ($filters as $name => $data) {
  50. $datagrid->setValue($name, isset($data['type']) ? $data['type'] : null, $data['value']);
  51. }
  52. $datagrid->buildPager();
  53. return $this->renderPrivateResponse($blockContext->getTemplate(), array(
  54. 'block' => $blockContext->getBlock(),
  55. 'settings' => $blockContext->getSettings(),
  56. 'admin_pool' => $this->pool,
  57. 'admin' => $admin,
  58. 'pager' => $datagrid->getPager(),
  59. 'datagrid' => $datagrid,
  60. ), $response);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getName()
  66. {
  67. return 'Admin Stats';
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function configureSettings(OptionsResolver $resolver)
  73. {
  74. $resolver->setDefaults(array(
  75. 'icon' => 'fa-line-chart',
  76. 'text' => 'Statistics',
  77. 'color' => 'bg-aqua',
  78. 'code' => false,
  79. 'filters' => array(),
  80. 'limit' => 1000,
  81. 'template' => 'SonataAdminBundle:Block:block_stats.html.twig',
  82. ));
  83. }
  84. }