浏览代码

first draft of the audit block

Thomas Rabaix 13 年之前
父节点
当前提交
1d8cf00874
共有 4 个文件被更改,包括 145 次插入5 次删除
  1. 102 0
      Block/AuditBlockService.php
  2. 4 4
      Model/AuditReader.php
  3. 7 1
      Resources/config/audit.xml
  4. 32 0
      Resources/views/Block/block_audit.html.twig

+ 102 - 0
Block/AuditBlockService.php

@@ -0,0 +1,102 @@
+<?php
+
+/*
+ * This file is part of the Sonata project.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\DoctrineORMAdminBundle\Block;
+
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
+
+use Sonata\AdminBundle\Form\FormMapper;
+use Sonata\AdminBundle\Validator\ErrorElement;
+use Sonata\AdminBundle\Admin\Pool;
+
+use Sonata\BlockBundle\Model\BlockInterface;
+use Sonata\BlockBundle\Block\BaseBlockService;
+
+use SimpleThings\EntityAudit\AuditReader;
+
+/**
+ *
+ * @author     Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ */
+class AuditBlockService extends BaseBlockService
+{
+    protected $auditReader;
+
+    /**
+     * @param $name
+     * @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
+     * @param \SimpleThings\EntityAudit\AuditReader $auditReader
+     */
+    public function __construct($name, EngineInterface $templating, AuditReader $auditReader)
+    {
+        parent::__construct($name, $templating);
+
+        $this->auditReader = $auditReader;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function execute(BlockInterface $block, Response $response = null)
+    {
+        $settings = array_merge($this->getDefaultSettings(), $block->getSettings());
+
+        $revisions = array();
+
+        foreach ($this->auditReader->findRevisionHistory($settings['limit'], 0) as $revision) {
+            $revisions[] = array(
+                'revision' => $revision,
+                'entities' => $this->auditReader->findEntitesChangedAtRevision($revision->getRev())
+            );
+        }
+
+        return $this->renderResponse('SonataDoctrineORMAdminBundle:Block:block_audit.html.twig', array(
+            'block'     => $block,
+            'settings'  => $settings,
+            'revisions' => $revisions,
+        ), $response);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
+    {
+        // TODO: Implement validateBlock() method.
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
+    {
+
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getName()
+    {
+        return 'Audit List';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    function getDefaultSettings()
+    {
+        return array(
+            'limit' => 10
+        );
+    }
+}

+ 4 - 4
Model/AuditReader.php

@@ -32,7 +32,7 @@ class AuditReader implements AuditReaderInterface
      * @param $revision
      * @return mixed
      */
-    function find($className, $id, $revision)
+    public function find($className, $id, $revision)
     {
         return $this->auditReader->find($className, $id, $revision);
     }
@@ -43,7 +43,7 @@ class AuditReader implements AuditReaderInterface
      * @param int $offset
      * @return mixed
      */
-    function findRevisionHistory($className, $limit = 20, $offset = 0)
+    public function findRevisionHistory($className, $limit = 20, $offset = 0)
     {
         return $this->auditReader->findRevisionHistory($limit, $offset);
     }
@@ -53,7 +53,7 @@ class AuditReader implements AuditReaderInterface
      * @param $revision
      * @return mixed
      */
-    function findRevision($classname, $revision)
+    public function findRevision($classname, $revision)
     {
         return $this->auditReader->findRevision($revision);
     }
@@ -63,7 +63,7 @@ class AuditReader implements AuditReaderInterface
      * @param $id
      * @return mixed
      */
-    function findRevisions($className, $id)
+    public function findRevisions($className, $id)
     {
         return $this->auditReader->findRevisions($className, $id);
     }

+ 7 - 1
Resources/config/audit.xml

@@ -5,10 +5,16 @@
            xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
 
     <services>
-
         <service id="sonata.admin.audit.orm.reader" class="Sonata\DoctrineORMAdminBundle\Model\AuditReader">
             <argument type="service" id="simplethings_entityaudit.reader" on-invalid="ignore" />
         </service>
 
+        <service id="sonata.admin_doctrine_orm.block.audit" class="Sonata\DoctrineORMAdminBundle\Block\AuditBlockService">
+            <tag name="sonata.block" />
+
+            <argument>sonata.admin_doctrine_orm.block.audit</argument>
+            <argument type="service" id="templating" />
+            <argument type="service" id="simplethings_entityaudit.reader" />
+        </service>
     </services>
 </container>

+ 32 - 0
Resources/views/Block/block_audit.html.twig

@@ -0,0 +1,32 @@
+{#
+
+This file is part of the Sonata package.
+
+(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+
+For the full copyright and license information, please view the LICENSE
+file that was distributed with this source code.
+
+#}
+{% extends 'SonataBlockBundle:Block:block_base.html.twig' %}
+
+{% block block %}
+    <h3>{{ "title_audit_log"|trans({}, 'SonataAdminBundle') }}</h3>
+
+    <div>
+        {% for revision in revisions %}
+            <div>
+                {{ revision.revision.rev }} - {{ revision.revision.username }} - {{ revision.revision.timestamp | format_date }}
+
+                <ul>
+                    {% for changedEntity in revision.entities %}
+                        <li>
+                            {{ changedEntity.entity }} / {{ changedEntity.revisionType }} / {{ changedEntity.className }} - {{ changedEntity.id }}
+                        </li>
+                    {% endfor %}
+                </ul>
+            </div>
+        {% endfor %}
+
+    </div>
+{% endblock %}