Parcourir la source

Merge pull request #1019 from lopsided/master

Persistent filters
Thomas il y a 12 ans
Parent
commit
e2799a8966

+ 27 - 1
Admin/Admin.php

@@ -204,6 +204,13 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
      */
     protected $label;
 
+    /**
+     * Whether or not to persist the filters in the session
+     *
+     * @var boolean
+     */
+    protected $persistFilters = false;
+
     /**
      * Array of routes related to this admin
      *
@@ -683,10 +690,21 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
         // build the values array
         if ($this->hasRequest()) {
+            $filters = $this->request->query->get('filter', array());
+
+            // if persisting filters, save filters to session, or pull them out of session if no new filters set
+            if ($this->persistFilters) {
+                if ($filters == array()) {
+                    $filters = $this->request->getSession()->get($this->getCode().'.filter.parameters', array());
+                } else {
+                    $this->request->getSession()->set($this->getCode().'.filter.parameters', $filters);
+                }
+            }
+
             $parameters = array_merge(
                 $this->getModelManager()->getDefaultSortValues($this->getClass()),
                 $this->datagridValues,
-                $this->request->query->get('filter', array())
+                $filters
             );
 
             // always force the parent value
@@ -1376,6 +1394,14 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         return $this->label;
     }
 
+    /**
+     * @param boolean $persist
+     */
+    public function setPersistFilters($persist)
+    {
+        $this->persistFilters = $persist;
+    }
+
     /**
      * @param int $maxPerPage
      */

+ 8 - 0
DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php

@@ -205,6 +205,14 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
 
         $definition->addMethodCall('setLabel', array($label));
 
+        if (isset($attributes['persist_filters'])) {
+            $persistFilters = (bool) $attributes['persist_filters'];
+        } else {
+            $persistFilters = (bool) $container->getParameter('sonata.admin.configuration.filters.persist');
+        }
+
+        $definition->addMethodCall('setPersistFilters', array($persistFilters));
+
         $this->fixTemplates($container, $definition);
 
         if ($container->hasParameter('sonata.admin.configuration.security.information') && !$definition->hasMethodCall('setSecurityInformation')) {

+ 3 - 0
DependencyInjection/Configuration.php

@@ -141,6 +141,9 @@ class Configuration implements ConfigurationInterface
                         ->scalarNode('delete')->defaultValue('SonataAdminBundle:CRUD:delete.html.twig')->cannotBeEmpty()->end()
                     ->end()
                 ->end()
+
+                ->scalarNode('persist_filters')->defaultValue(false)->cannotBeEmpty()->end()
+
             ->end()
         ->end();
 

+ 3 - 0
DependencyInjection/SonataAdminExtension.php

@@ -124,5 +124,8 @@ class SonataAdminExtension extends Extension
         if (!isset($bundles['JMSTranslationBundle'])) {
             $container->removeDefinition('sonata.admin.translator.extractor.jms_translator_bundle');
         }
+
+        // set filter persistence
+        $container->setParameter('sonata.admin.configuration.filters.persist', $config['persist_filters']);
     }
 }

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

@@ -45,6 +45,9 @@ Full Configuration Options
                 - { 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 }}
 
+        # set to true to persist filter settings per admin module in the user's session
+        persist_filters: false
+
     sonata_block:
         default_contexts: [cms]
         blocks: