AuditBlockService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  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\DoctrineORMAdminBundle\Block;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  13. use Sonata\AdminBundle\Form\FormMapper;
  14. use Sonata\AdminBundle\Validator\ErrorElement;
  15. use Sonata\AdminBundle\Admin\Pool;
  16. use Sonata\BlockBundle\Model\BlockInterface;
  17. use Sonata\BlockBundle\Block\BaseBlockService;
  18. use SimpleThings\EntityAudit\AuditReader;
  19. /**
  20. *
  21. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  22. */
  23. class AuditBlockService extends BaseBlockService
  24. {
  25. protected $auditReader;
  26. /**
  27. * @param string $name
  28. * @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
  29. * @param \SimpleThings\EntityAudit\AuditReader $auditReader
  30. */
  31. public function __construct($name, EngineInterface $templating, AuditReader $auditReader)
  32. {
  33. parent::__construct($name, $templating);
  34. $this->auditReader = $auditReader;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function execute(BlockInterface $block, Response $response = null)
  40. {
  41. $settings = array_merge($this->getDefaultSettings(), $block->getSettings());
  42. $revisions = array();
  43. foreach ($this->auditReader->findRevisionHistory($settings['limit'], 0) as $revision) {
  44. $revisions[] = array(
  45. 'revision' => $revision,
  46. 'entities' => $this->auditReader->findEntitesChangedAtRevision($revision->getRev())
  47. );
  48. }
  49. return $this->renderResponse('SonataDoctrineORMAdminBundle:Block:block_audit.html.twig', array(
  50. 'block' => $block,
  51. 'settings' => $settings,
  52. 'revisions' => $revisions,
  53. ), $response);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
  59. {
  60. // TODO: Implement validateBlock() method.
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
  66. {
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getName()
  72. {
  73. return 'Audit List';
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. function getDefaultSettings()
  79. {
  80. return array(
  81. 'limit' => 10
  82. );
  83. }
  84. }