Skip to content

Commit

Permalink
Merge remote-tracking branch 'simplethings/FilterActions' into Filter…
Browse files Browse the repository at this point in the history
…Actions
  • Loading branch information
Thomas Rabaix committed Jun 27, 2011
2 parents 03ec3fb + 21e337c commit 2b38d60
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 14 deletions.
45 changes: 31 additions & 14 deletions Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,30 @@ protected function buildListFieldDescriptions()

return $this->listFieldDescriptions;
}

/**
* Get parameters that are currently bound to the filter.
*
* @return array
*/
public function getFilterParameters()
{
$parameters = array();
// build the values array
if ($this->hasRequest()) {
$parameters = array_merge(
$this->getModelManager()->getDefaultSortValues($this->getClass()),
$this->datagridValues,
$this->request->query->all()
);

// always force the parent value
if ($this->isChild() && $this->getParentAssociationMapping()) {
$parameters[$this->getParentAssociationMapping()] = $this->request->get($this->getParent()->getIdParameter());
}
}
return $parameters;
}

/**
* build the filter FieldDescription array
Expand All @@ -560,20 +584,7 @@ public function buildFilterFieldDescriptions()
$this->getDatagridBuilder()->fixFieldDescription($this, $fieldDescription);
}

$parameters = array();
// build the values array
if ($this->hasRequest()) {
$parameters = array_merge(
$this->getModelManager()->getDefaultSortValues($this->getClass()),
$this->datagridValues,
$this->request->query->all()
);

// always force the parent value
if ($this->isChild() && $this->getParentAssociationMapping()) {
$parameters[$this->getParentAssociationMapping()] = $this->request->get($this->getParent()->getIdParameter());
}
}
$parameters = $this->getFilterParameters();

// initialize the datagrid
$this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
Expand Down Expand Up @@ -757,6 +768,11 @@ public function getBatchActions()

return $actions;
}

public function getFilterActions()
{
return array();
}

/**
* Returns the list of available urls
Expand Down Expand Up @@ -813,6 +829,7 @@ public function buildRoutes()
$collection->add('list');
$collection->add('create');
$collection->add('batch');
$collection->add('filter');
$collection->add('edit', $this->getRouterIdParameter().'/edit');
$collection->add('delete', $this->getRouterIdParameter().'/delete');
$collection->add('view', $this->getRouterIdParameter().'/view');
Expand Down
32 changes: 32 additions & 0 deletions Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,38 @@ public function redirectTo($object)

return new RedirectResponse($url);
}

/**
* Execute a filter action, an action performed on all filter entries of a datagrid.
*
* @return Response
*/
public function filterAction()
{
if ($this->get('request')->getMethod() != 'POST') {
throw new \RuntimeException('invalid request type, POST expected');
}

$action = $this->get('request')->get('action');

if (!array_key_exists($action, $this->admin->getFilterActions())) {
throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
}

// execute the action, filterActionXxxxx
$action = \Sonata\AdminBundle\Admin\BaseFieldDescription::camelize($action);

$final_action = sprintf('filterAction%s', ucfirst($action));
if (!method_exists($this, $final_action)) {
throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
}

$grid = $this->admin->getDatagrid();
$grid->buildPager();
$query = $grid->getQuery();

return call_user_func(array($this, $final_action), $query);
}

/**
* return the Response object associated to the batch action
Expand Down
11 changes: 11 additions & 0 deletions Resources/views/CRUD/base_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ file that was distributed with this source code.

<a href="{{ admin.generateUrl('list') }}">{% trans from 'SonataAdminBundle' %}link_reset_filter{% endtrans %}</a>
</form>

{% if admin.filterActions %}
<form class="sonata-filter-action-form" action="{{ admin.generateUrl('filter') }}?{% for k,v in admin.filterParameters %}&{{k}}={{v}}{% endfor %}" method="POST">
<select name="action">
{% for action, label in admin.filterActions %}
<option value="{{ action }}">{{ label }}</option>
{% endfor %}
</select>
<input type="submit" value="{% trans from 'SonataAdminBundle' %}btn_batch{% endtrans %}" />
</form>
{% endif %}
{% endif %}
{% endblock %}

0 comments on commit 2b38d60

Please sign in to comment.