AdminStatsBlockService.php 2.6 KB

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