AdminStatsBlockService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\BaseBlockService;
  13. use Sonata\BlockBundle\Block\BlockContextInterface;
  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 BaseBlockService
  23. {
  24. protected $pool;
  25. /**
  26. * @param string $name
  27. * @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
  28. * @param \Sonata\AdminBundle\Admin\Pool $pool
  29. */
  30. public function __construct($name, EngineInterface $templating, Pool $pool)
  31. {
  32. parent::__construct($name, $templating);
  33. $this->pool = $pool;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function execute(BlockContextInterface $blockContext, Response $response = null)
  39. {
  40. $admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('code'));
  41. $datagrid = $admin->getDatagrid();
  42. $filters = $blockContext->getSetting('filters');
  43. if (!isset($filters['_per_page'])) {
  44. $filters['_per_page'] = array('value' => $blockContext->getSetting('limit'));
  45. }
  46. foreach ($filters as $name => $data) {
  47. $datagrid->setValue($name, isset($data['type']) ? $data['type'] : null, $data['value']);
  48. }
  49. $datagrid->buildPager();
  50. return $this->renderPrivateResponse($blockContext->getTemplate(), array(
  51. 'block' => $blockContext->getBlock(),
  52. 'settings' => $blockContext->getSettings(),
  53. 'admin_pool' => $this->pool,
  54. 'admin' => $admin,
  55. 'pager' => $datagrid->getPager(),
  56. 'datagrid' => $datagrid,
  57. ), $response);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getName()
  63. {
  64. return 'Admin Stats';
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function configureSettings(OptionsResolver $resolver)
  70. {
  71. $resolver->setDefaults(array(
  72. 'icon' => 'fa-line-chart',
  73. 'text' => 'Statistics',
  74. 'color' => 'bg-aqua',
  75. 'code' => false,
  76. 'filters' => array(),
  77. 'limit' => 1000,
  78. 'template' => 'SonataAdminBundle:Block:block_stats.html.twig',
  79. ));
  80. }
  81. }