Przeglądaj źródła

Introduce Block Into the Admin Bundle

Thomas Rabaix 13 lat temu
rodzic
commit
d6d6e5b892

+ 89 - 0
Block/AdminListBlockService.php

@@ -0,0 +1,89 @@
+<?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\AdminBundle\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;
+
+/**
+ *
+ * @author     Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ */
+class AdminListBlockService extends BaseBlockService
+{
+    protected $pool;
+
+    /**
+     * @param $name
+     * @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
+     * @param \Sonata\AdminBundle\Admin\Pool $pool
+     */
+    public function __construct($name, EngineInterface $templating, Pool $pool)
+    {
+        parent::__construct($name, $templating);
+
+        $this->pool = $pool;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function execute(BlockInterface $block, Response $response = null)
+    {
+        $settings = array_merge($this->getDefaultSettings(), $block->getSettings());
+
+        return $this->renderResponse('SonataAdminBundle:Block:block_admin_list.html.twig', array(
+            'block'     => $block,
+            'settings'  => $settings,
+            'admin_pool' => $this->pool
+        ), $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 'Admin List';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    function getDefaultSettings()
+    {
+        return array();
+    }
+}

+ 2 - 2
Controller/CoreController.php

@@ -36,9 +36,9 @@ class CoreController extends Controller
     public function dashboardAction()
     {
         return $this->render($this->container->get('sonata.admin.pool')->getTemplate('dashboard'), array(
-            'groups'          => $this->get('sonata.admin.pool')->getDashboardGroups(),
             'base_template'   => $this->getBaseTemplate(),
-            'admin_pool'      => $this->container->get('sonata.admin.pool')
+            'admin_pool'      => $this->container->get('sonata.admin.pool'),
+            'blocks'          => $this->container->getParameter('sonata.admin.configuration.dashboard_blocks')
         ));
     }
 }

+ 27 - 11
DependencyInjection/Configuration.php

@@ -68,18 +68,34 @@ class Configuration implements ConfigurationInterface
                 ->scalarNode('title')->defaultValue('Sonata Admin')->cannotBeEmpty()->end()
                 ->scalarNode('title_logo')->defaultValue('bundles/sonataadmin/logo_title.png')->cannotBeEmpty()->end()
 
-                ->arrayNode('dashboard_groups')
-                    ->useAttributeAsKey('id')
-                    ->prototype('array')
-                        ->fixXmlConfig('item')
-                        ->fixXmlConfig('item_add')
-                        ->children()
-                            ->scalarNode('label')->end()
-                            ->arrayNode('items')
-                                ->prototype('scalar')->end()
+                ->arrayNode('dashboard')
+                    ->children()
+                        ->arrayNode('groups')
+                            ->useAttributeAsKey('id')
+                            ->prototype('array')
+                                ->fixXmlConfig('item')
+                                ->fixXmlConfig('item_add')
+                                ->children()
+                                    ->scalarNode('label')->end()
+                                    ->arrayNode('items')
+                                        ->prototype('scalar')->end()
+                                    ->end()
+                                    ->arrayNode('item_adds')
+                                        ->prototype('scalar')->end()
+                                    ->end()
+                                ->end()
                             ->end()
-                            ->arrayNode('item_adds')
-                                ->prototype('scalar')->end()
+                        ->end()
+                        ->arrayNode('blocks')
+                            ->prototype('array')
+                                ->children()
+                                    ->scalarNode('type')->cannotBeEmpty()->end()
+                                    ->arrayNode('settings')
+                                        ->useAttributeAsKey('id')
+                                        ->prototype('scalar')->end()
+                                    ->end()
+                                    ->scalarNode('position')->defaultValue('right')->end()
+                                ->end()
                             ->end()
                         ->end()
                     ->end()

+ 3 - 1
DependencyInjection/SonataAdminExtension.php

@@ -54,6 +54,7 @@ class SonataAdminExtension extends Extension
         $loader->load('form_types.xml');
         $loader->load('validator.xml');
         $loader->load('route.xml');
+        $loader->load('block.xml');
 
         $configuration = new Configuration();
         $processor = new Processor();
@@ -65,7 +66,8 @@ class SonataAdminExtension extends Extension
 
         $container->setParameter('sonata.admin.configuration.templates', $config['templates']);
         $container->setParameter('sonata.admin.configuration.admin_services', $config['admin_services']);
-        $container->setParameter('sonata.admin.configuration.dashboard_groups', $config['dashboard_groups']);
+        $container->setParameter('sonata.admin.configuration.dashboard_groups', $config['dashboard']['groups']);
+        $container->setParameter('sonata.admin.configuration.dashboard_blocks', $config['dashboard']['blocks']);
 
         $container->setAlias('sonata.admin.security.handler', $config['security']['handler']);
 

+ 17 - 0
Resources/config/block.xml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<container xmlns="http://symfony.com/schema/dic/services"
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+    <services>
+        <service id="sonata.admin.block.admin_list" class="Sonata\AdminBundle\Block\AdminListBlockService">
+            <tag name="sonata.block"/>
+
+            <argument>sonata.admin.block.admin_list</argument>
+            <argument type="service" id="templating" />
+            <argument type="service" id="sonata.admin.pool" />
+        </service>
+    </services>
+
+</container>

+ 15 - 0
Resources/doc/reference/configuration.rst

@@ -33,3 +33,18 @@ Full Configuration Options
             list:    SonataAdminBundle:CRUD:list.html.twig
             show:    SonataAdminBundle:CRUD:show.html.twig
             edit:    SonataAdminBundle:CRUD:edit.html.twig
+
+        dashboard:
+            blocks:
+                # display a dashboard block
+                - { position: left, type: sonata.admin.block.admin_list }
+
+                # Customize this part to add new block configuration
+                - { position: right, type: sonata.block.service.text, settings: { content: "<h2>Welcome to the Sonata Admin</h2> <p>This is a <code>sonata.block.service.text</code> from the Block Bundle, you can create and add new block in these area by configuring the <code>sonata_admin</code> section.</p> <br /> For instance, here a RSS feed parser (<code>sonata.block.service.rss</code>):"} }
+                - { position: right, type: sonata.block.service.rss, settings: { title: Sonata Project's Feeds, url: http://sonata-project.org/blog/archive.rss }}
+
+    sonata_block:
+        default_contexts: [cms]
+        blocks:
+            sonata.admin.block.admin_list:
+                contexts:   [admin]

+ 10 - 8
Resources/doc/reference/installation.rst

@@ -26,6 +26,14 @@ in your ``deps`` file::
       git=http://github.com/sonata-project/SonataAdminBundle.git
       target=/bundles/Sonata/AdminBundle
 
+  [SonataBlockBundle]
+      git=http://github.com/sonata-project/SonataBlockBundle.git
+      target=/bundles/Sonata/BlockBundle
+
+  [SonataCacheBundle]
+      git=http://github.com/sonata-project/SonataCacheBundle.git
+      target=/bundles/Sonata/CacheBundle
+
   [SonatajQueryBundle]
       git=http://github.com/sonata-project/SonatajQueryBundle.git
       target=/bundles/Sonata/jQueryBundle
@@ -46,14 +54,6 @@ and run the vendors script to download bundles::
 
   php bin/vendors install
 
-If you prefer instead to use git submodules, then run the following:
-
-  git submodule add http://github.com/sonata-project/SonataAdminBundle.git vendor/bundles/Sonata/AdminBundle
-  git submodule add http://github.com/sonata-project/SonatajQueryBundle.git vendor/bundles/Sonata/jQueryBundle
-  git submodule add http://github.com/KnpLabs/KnpMenuBundle.git vendor/bundles/Knp/Bundle/MenuBundle
-  git submodule add http://github.com/KnpLabs/KnpMenu.git vendor/knp/menu
-  git submodule update --init
-
 Next, be sure to enable this bundles in your autoload.php and AppKernel.php
 files:
 
@@ -76,6 +76,8 @@ files:
         return array(
             // ...
             new Sonata\AdminBundle\SonataAdminBundle(),
+            new Sonata\BlockBundle\SonataBlockBundle(),
+            new Sonata\CacheBundle\SonataCacheBundle(),
             new Sonata\jQueryBundle\SonatajQueryBundle(),
             new Knp\Bundle\MenuBundle\KnpMenuBundle(),
             // ...

+ 50 - 0
Resources/views/Block/block_admin_list.html.twig

@@ -0,0 +1,50 @@
+{#
+
+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 %}
+    {% for group in admin_pool.dashboardGroups %}
+        <table class="zebra-striped sonata-ba-list bordered-table">
+            <thead>
+                <tr>
+                    <th colspan="3">{{ group.label|trans({}, 'SonataAdminBundle') }}</th>
+                </tr>
+            </thead>
+
+            <tbody>
+                {% for admin in group.items %}
+                    {% if admin.hasroute('create') and admin.isGranted('CREATE') or admin.hasroute('list') and admin.isGranted('LIST') %}
+                        <tr>
+                            <td class="sonata-ba-list-label">{{ admin.label|trans({}, admin.translationdomain) }}</td>
+                            <td>
+                                {% if admin.hasroute('create') and admin.isGranted('CREATE') %}
+                                    <a href="{{ admin.generateUrl('create')}}">
+                                        <img src="{{ asset('bundles/sonataadmin/famfamfam/add.png') }}"  alt="{%- trans from 'SonataAdminBundle' %}link_add{% endtrans -%}" />
+                                        {%- trans from 'SonataAdminBundle' %}link_add{% endtrans -%}
+                                    </a>
+                                {% endif %}
+                            </td>
+                            <td>
+                                {% if admin.hasroute('list') and admin.isGranted('LIST') %}
+                                    <a href="{{ admin.generateUrl('list')}}">
+                                        <img src="{{ asset('bundles/sonataadmin/famfamfam/application_view_list.png') }}" alt="{%- trans from 'SonataAdminBundle' %}link_list{% endtrans -%}" />
+                                        {%- trans from 'SonataAdminBundle' %}link_list{% endtrans -%}
+                                    </a>
+                                {% endif %}
+                            </td>
+                        </tr>
+                    {% endif %}
+                {% endfor %}
+            </tbody>
+        </table>
+    {% endfor %}
+{% endblock %}

+ 15 - 37
Resources/views/Core/dashboard.html.twig

@@ -14,42 +14,20 @@ file that was distributed with this source code.
 {% block title %}{% trans from 'SonataAdminBundle' %}title_dashboard{% endtrans %}{% endblock%}
 {% block breadcrumb %}{% endblock %}
 {% block content %}
-
-    <div class="span7">
-        {% for group in groups %}
-            <table class="zebra-striped sonata-ba-list bordered-table">
-                <thead>
-                    <tr>
-                        <th colspan="3">{{ group.label|trans({}, 'SonataAdminBundle') }}</th>
-                    </tr>
-                </thead>
-
-                <tbody>
-                    {% for admin in group.items %}
-                        {% if admin.hasroute('create') and admin.isGranted('CREATE') or admin.hasroute('list') and admin.isGranted('LIST') %}
-                            <tr>
-                                <td class="sonata-ba-list-label">{{ admin.label|trans({}, admin.translationdomain) }}</td>
-                                <td>
-                                    {% if admin.hasroute('create') and admin.isGranted('CREATE') %}
-                                        <a href="{{ admin.generateUrl('create')}}">
-                                            <img src="{{ asset('bundles/sonataadmin/famfamfam/add.png') }}"  alt="{%- trans from 'SonataAdminBundle' %}link_add{% endtrans -%}" />
-                                            {%- trans from 'SonataAdminBundle' %}link_add{% endtrans -%}
-                                        </a>
-                                    {% endif %}
-                                </td>
-                                <td>
-                                    {% if admin.hasroute('list') and admin.isGranted('LIST') %}
-                                        <a href="{{ admin.generateUrl('list')}}">
-                                            <img src="{{ asset('bundles/sonataadmin/famfamfam/application_view_list.png') }}" alt="{%- trans from 'SonataAdminBundle' %}link_list{% endtrans -%}" />
-                                            {%- trans from 'SonataAdminBundle' %}link_list{% endtrans -%}
-                                        </a>
-                                    {% endif %}
-                                </td>
-                            </tr>
-                        {% endif %}
-                    {% endfor %}
-                </tbody>
-            </table>
-        {% endfor %}
+    <div class="row">
+        <div class="span9">
+            {% for block in blocks %}
+                {% if block.position == 'left' %}
+                    {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                {% endif %}
+            {% endfor %}
+        </div>
+        <div class="span10">
+            {% for block in blocks %}
+                {% if block.position == 'right' %}
+                    {{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
+                {% endif %}
+            {% endfor %}
+        </div>
     </div>
 {% endblock %}