Skip to content

Commit

Permalink
Fix filter binding, clean up some codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Rabaix committed May 1, 2011
1 parent 6c6fb91 commit e44fece
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 81 deletions.
57 changes: 30 additions & 27 deletions Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,35 @@ public function buildFilterFieldDescriptions()
foreach ($this->filterFieldDescriptions as $fieldDescription) {
$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());
}
}

// initialize the datagrid
$this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
$this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);

$mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);

// build the datagrid filter
$this->buildFilterFieldDescriptions();
$this->configureDatagridFilters($mapper);

foreach ($this->getFilterFieldDescriptions() as $fieldDescription) {
$mapper->add($fieldDescription);
}
}

/**
Expand Down Expand Up @@ -1001,33 +1030,7 @@ public function getList(array $options = array())
*/
public function getDatagrid()
{
if (!$this->datagrid) {
// build the values array
$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());
}

// initialize the datagrid
$this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
$this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);

$mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);

// build the datagrid filter
$this->buildFilterFieldDescriptions();
$this->configureDatagridFilters($mapper);

foreach ($this->getFilterFieldDescriptions() as $fieldDescription) {
$mapper->add($fieldDescription);
}
}
$this->buildFilterFieldDescriptions();

return $this->datagrid;
}
Expand Down
1 change: 1 addition & 0 deletions Builder/ORM/DatagridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public function getFilterFieldClass(FieldDescriptionInterface $fieldDescription)
public function getChoices(FieldDescriptionInterface $fieldDescription)
{
$targets = $fieldDescription->getAdmin()->getModelManager()
->getEntityManager()
->createQueryBuilder()
->select('t')
->from($fieldDescription->getTargetEntity(), 't')
Expand Down
5 changes: 4 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
01/04/2011
----------
* migrate to the new form framework

03/03/2011
----------
* add sortable option


08/02/2011
----------
* add prototype for nested admin
Expand Down
2 changes: 1 addition & 1 deletion Datagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function buildPager()
foreach ($this->getFilters() as $name => $filter) {
$value = isset($this->values[$name]) ? $this->values[$name] : null;

$filter->getField()->setData($value);
$filter->getField()->bind($value);
$filter->apply($this->query, $value);
}

Expand Down
12 changes: 5 additions & 7 deletions Datagrid/DatagridMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,38 @@ public function __construct(DatagridBuilderInterface $datagridBuilder, DatagridI
* @throws \RuntimeException
* @param string $name
* @param array $fieldDescriptionOptions
* @return \Sonata\AdminBundle\Datagrid\FilterInterface
* @return \Sonata\AdminBundle\Datagrid\DatagridMapper
*/
public function add($name, array $fieldDescriptionOptions = array())
{
if ($name instanceof FieldDescriptionInterface) {

$fieldDescription = $name;
$fieldDescription->mergeOptions($fieldDescriptionOptions);

} else if (is_string($name) && !$this->admin->hasFormFieldDescription($name)) {

$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
$this->admin->getClass(),
$name,
$fieldDescriptionOptions
);

$this->datagridBuilder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
$this->admin->addListFieldDescription($name, $fieldDescription);
$this->admin->addFilterFieldDescription($name, $fieldDescription);

} else if (is_string($name) && $this->admin->hasFormFieldDescription($name)) {

$fieldDescription = $this->admin->getFormFieldDescription($name);

} else {

throw new \RuntimeException('invalid state');
}

// add the field with the FormBuilder
return $this->datagridBuilder->addFilter(
$this->datagridBuilder->addFilter(
$this->datagrid,
$fieldDescription
);

return $this;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions Datagrid/ListMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(ListBuilderInterface $listBuilder, ListCollection $l
* @throws \RuntimeException
* @param string $name
* @param array $fieldDescriptionOptions
* @return
* @return \Sonata\AdminBundle\Datagrid\ListMapper
*/
public function add($name, array $fieldDescriptionOptions = array())
{
Expand Down Expand Up @@ -67,10 +67,12 @@ public function add($name, array $fieldDescriptionOptions = array())
}

// add the field with the FormBuilder
return $this->listBuilder->addField(
$this->listBuilder->addField(
$this->list,
$fieldDescription
);

return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getName()
public function getField()
{
if (!$this->field) {
throw new \RuntimeException('No field attached');
throw new \RuntimeException(sprintf('No field instance attached for the filter `%s`', $this->name));
}

return $this->field;
Expand Down
4 changes: 2 additions & 2 deletions Filter/ORM/BooleanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public function defineFieldBuilder(FormFactory $formFactory)
'true' => 'true',
'false' => 'false'
),
'required' => false
'required' => true
);

$options = array_merge($options, $this->getFieldDescription()->getOption('filter_field_options', array()));

$this->field = $formFactory->createNamedBuilder('choice', $this->getName(), null, $options);
$this->field = $formFactory->createNamedBuilder('choice', $this->getName(), null, $options)->getForm();
}
}
6 changes: 4 additions & 2 deletions Filter/ORM/CallbackFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function association($queryBuilder, $value)

public function filter($queryBuilder, $alias, $field, $value)
{
if (is_callable($this->getOption('filter'))) {
if (!is_callable($this->getOption('filter'))) {
throw new \RuntimeException('Please provide a valid callback option');
}

Expand Down Expand Up @@ -51,6 +51,8 @@ public function getDefaultOptions()

public function defineFieldBuilder(FormFactory $formFactory)
{
return $formFactory->createNamedBuilder($this->getOption('type'), $this->getName(), null);
$options = $this->getFieldDescription()->getOption('filter_field_options', array());

$this->field = $formFactory->createNamedBuilder($this->getOption('type'), $this->getName(), null, $options)->getForm();
}
}
6 changes: 5 additions & 1 deletion Filter/ORM/ChoiceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class ChoiceFilter extends Filter
{
public function filter($queryBuilder, $alias, $field, $value)
{
if (!is_array($value)) {
return;
}

if ($this->getField()->getAttribute('multiple')) {
if (in_array('all', $value)) {
return;
Expand Down Expand Up @@ -52,6 +56,6 @@ public function defineFieldBuilder(FormFactory $formFactory, $value = null)
{
$options = $this->getFieldDescription()->getOption('filter_field_options', array('required' => false));

$this->field = $formFactory->createNamedBuilder('choice', $this->getName(), $value, $options);
$this->field = $formFactory->createNamedBuilder('choice', $this->getName(), $value, $options)->getForm();
}
}
2 changes: 1 addition & 1 deletion Filter/ORM/IntegerFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public function defineFieldBuilder(FormFactory $formFactory)
{
$options = $this->fieldDescription->getOption('filter_field_options', array('required' => false));

$this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options);
$this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options)->getForm();
}
}
2 changes: 1 addition & 1 deletion Filter/ORM/StringFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public function defineFieldBuilder(FormFactory $formFactory)
{
$options = $this->fieldDescription->getOption('filter_field_options', array('required' => false));

$this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options);
$this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options)->getForm();
}
}
2 changes: 1 addition & 1 deletion Resources/views/CRUD/base_filter_field.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ file that was distributed with this source code.
<br />
{% endblock %}

<div class="sonata-ba-field{% if filter_form.vars.errors %} sonata-ba-field-error{% endif %}">
<div class="sonata-ba-field{% if filter_form.vars.errors %} sonata-ba-field-error"{% endif %}">
{% block field %}{{ form_widget(filter_form) }}{% endblock %}
</div>
</div>
37 changes: 3 additions & 34 deletions Twig/Extension/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
class SonataAdminExtension extends \Twig_Extension
{

protected $templating;

/**
* {@inheritdoc}
*/
Expand All @@ -44,10 +42,7 @@ public function getFilters()

public function getTokenParsers()
{

return array(

);
return array();
}

/**
Expand All @@ -70,7 +65,6 @@ public function getName()
*/
public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
{

$template = $this->environment->loadTemplate($fieldDescription->getTemplate());

return $this->output($fieldDescription, $template->render(array_merge($params, array(
Expand All @@ -84,7 +78,6 @@ public function renderListElement($object, FieldDescriptionInterface $fieldDescr

public function output(FieldDescriptionInterface $fieldDescription, $content)
{

return sprintf("\n<!-- fieldName: %s, template: %s -->\n%s\n", $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $content);
}

Expand All @@ -99,10 +92,8 @@ public function output(FieldDescriptionInterface $fieldDescription, $content)
*/
public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
{

if (isset($params['loop']) && $object instanceof \ArrayAccess) {
throw new \RuntimeException('remove the loop requirement');
// $object = $object[$params['loop']['index0']];
}

$value = $fieldDescription->getValue($object);
Expand All @@ -111,7 +102,6 @@ public function getValueFromFieldDescription($object, FieldDescriptionInterface
// if so, create an empty object instance
// fixme: not sure this is the best place to do that
if (!$value && $fieldDescription->getAssociationAdmin()) {

$value = $fieldDescription->getAssociationAdmin()->getNewInstance();
}

Expand All @@ -121,7 +111,7 @@ public function getValueFromFieldDescription($object, FieldDescriptionInterface
/**
* render a filter element
*
* @param Filter $filter
* @param \Sonata\AdminBundle\Filter\FilterInterface $filter
* @param array $params
* @return
*/
Expand All @@ -133,7 +123,7 @@ public function renderFilterElement(FilterInterface $filter, array $params = arr

return $template->render(array_merge($params, array(
'filter' => $filter,
'filter_form' => $filter->getField()->getForm()->createView()
'filter_form' => $filter->getField()->createView()
)));
}

Expand Down Expand Up @@ -192,26 +182,5 @@ public function renderFormElement(FieldDescriptionInterface $fieldDescription, F
'base_template' => $fieldDescription->getOption('base_template', $base_template)
))));
}

/**
* set the templating engine
*
* @param $templating
* @return void
*/
public function setTemplating($templating)
{
$this->templating = $templating;
}

/**
* return the templating engine
*
* @return Engine
*/
public function getTemplating()
{
return $this->templating;
}
}

0 comments on commit e44fece

Please sign in to comment.