Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e44fece

Browse files
author
Thomas Rabaix
committedMay 1, 2011
Fix filter binding, clean up some codes
1 parent 6c6fb91 commit e44fece

File tree

14 files changed

+63
-81
lines changed

14 files changed

+63
-81
lines changed
 

‎Admin/Admin.php

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,35 @@ public function buildFilterFieldDescriptions()
490490
foreach ($this->filterFieldDescriptions as $fieldDescription) {
491491
$this->getDatagridBuilder()->fixFieldDescription($this, $fieldDescription);
492492
}
493+
494+
$parameters = array();
495+
// build the values array
496+
if ($this->hasRequest()) {
497+
$parameters = array_merge(
498+
$this->getModelManager()->getDefaultSortValues($this->getClass()),
499+
$this->datagridValues,
500+
$this->request->query->all()
501+
);
502+
503+
// always force the parent value
504+
if ($this->isChild() && $this->getParentAssociationMapping()) {
505+
$parameters[$this->getParentAssociationMapping()] = $this->request->get($this->getParent()->getIdParameter());
506+
}
507+
}
508+
509+
// initialize the datagrid
510+
$this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
511+
$this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);
512+
513+
$mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
514+
515+
// build the datagrid filter
516+
$this->buildFilterFieldDescriptions();
517+
$this->configureDatagridFilters($mapper);
518+
519+
foreach ($this->getFilterFieldDescriptions() as $fieldDescription) {
520+
$mapper->add($fieldDescription);
521+
}
493522
}
494523

495524
/**
@@ -1001,33 +1030,7 @@ public function getList(array $options = array())
10011030
*/
10021031
public function getDatagrid()
10031032
{
1004-
if (!$this->datagrid) {
1005-
// build the values array
1006-
$parameters = array_merge(
1007-
$this->getModelManager()->getDefaultSortValues($this->getClass()),
1008-
$this->datagridValues,
1009-
$this->request->query->all()
1010-
);
1011-
1012-
// always force the parent value
1013-
if ($this->isChild() && $this->getParentAssociationMapping()) {
1014-
$parameters[$this->getParentAssociationMapping()] = $this->request->get($this->getParent()->getIdParameter());
1015-
}
1016-
1017-
// initialize the datagrid
1018-
$this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
1019-
$this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);
1020-
1021-
$mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
1022-
1023-
// build the datagrid filter
1024-
$this->buildFilterFieldDescriptions();
1025-
$this->configureDatagridFilters($mapper);
1026-
1027-
foreach ($this->getFilterFieldDescriptions() as $fieldDescription) {
1028-
$mapper->add($fieldDescription);
1029-
}
1030-
}
1033+
$this->buildFilterFieldDescriptions();
10311034

10321035
return $this->datagrid;
10331036
}

‎Builder/ORM/DatagridBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public function getFilterFieldClass(FieldDescriptionInterface $fieldDescription)
137137
public function getChoices(FieldDescriptionInterface $fieldDescription)
138138
{
139139
$targets = $fieldDescription->getAdmin()->getModelManager()
140+
->getEntityManager()
140141
->createQueryBuilder()
141142
->select('t')
142143
->from($fieldDescription->getTargetEntity(), 't')

‎CHANGES

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
01/04/2011
2+
----------
3+
* migrate to the new form framework
4+
15
03/03/2011
26
----------
37
* add sortable option
48

5-
69
08/02/2011
710
----------
811
* add prototype for nested admin

‎Datagrid/Datagrid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function buildPager()
7171
foreach ($this->getFilters() as $name => $filter) {
7272
$value = isset($this->values[$name]) ? $this->values[$name] : null;
7373

74-
$filter->getField()->setData($value);
74+
$filter->getField()->bind($value);
7575
$filter->apply($this->query, $value);
7676
}
7777

‎Datagrid/DatagridMapper.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,38 @@ public function __construct(DatagridBuilderInterface $datagridBuilder, DatagridI
3939
* @throws \RuntimeException
4040
* @param string $name
4141
* @param array $fieldDescriptionOptions
42-
* @return \Sonata\AdminBundle\Datagrid\FilterInterface
42+
* @return \Sonata\AdminBundle\Datagrid\DatagridMapper
4343
*/
4444
public function add($name, array $fieldDescriptionOptions = array())
4545
{
4646
if ($name instanceof FieldDescriptionInterface) {
47-
4847
$fieldDescription = $name;
4948
$fieldDescription->mergeOptions($fieldDescriptionOptions);
5049

5150
} else if (is_string($name) && !$this->admin->hasFormFieldDescription($name)) {
52-
5351
$fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
5452
$this->admin->getClass(),
5553
$name,
5654
$fieldDescriptionOptions
5755
);
5856

5957
$this->datagridBuilder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
60-
$this->admin->addListFieldDescription($name, $fieldDescription);
58+
$this->admin->addFilterFieldDescription($name, $fieldDescription);
6159

6260
} else if (is_string($name) && $this->admin->hasFormFieldDescription($name)) {
63-
6461
$fieldDescription = $this->admin->getFormFieldDescription($name);
6562

6663
} else {
67-
6864
throw new \RuntimeException('invalid state');
6965
}
7066

7167
// add the field with the FormBuilder
72-
return $this->datagridBuilder->addFilter(
68+
$this->datagridBuilder->addFilter(
7369
$this->datagrid,
7470
$fieldDescription
7571
);
72+
73+
return $this;
7674
}
7775

7876
/**

‎Datagrid/ListMapper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(ListBuilderInterface $listBuilder, ListCollection $l
3939
* @throws \RuntimeException
4040
* @param string $name
4141
* @param array $fieldDescriptionOptions
42-
* @return
42+
* @return \Sonata\AdminBundle\Datagrid\ListMapper
4343
*/
4444
public function add($name, array $fieldDescriptionOptions = array())
4545
{
@@ -67,10 +67,12 @@ public function add($name, array $fieldDescriptionOptions = array())
6767
}
6868

6969
// add the field with the FormBuilder
70-
return $this->listBuilder->addField(
70+
$this->listBuilder->addField(
7171
$this->list,
7272
$fieldDescription
7373
);
74+
75+
return $this;
7476
}
7577

7678
/**

‎Filter/Filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getName()
4545
public function getField()
4646
{
4747
if (!$this->field) {
48-
throw new \RuntimeException('No field attached');
48+
throw new \RuntimeException(sprintf('No field instance attached for the filter `%s`', $this->name));
4949
}
5050

5151
return $this->field;

‎Filter/ORM/BooleanFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public function defineFieldBuilder(FormFactory $formFactory)
6262
'true' => 'true',
6363
'false' => 'false'
6464
),
65-
'required' => false
65+
'required' => true
6666
);
6767

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

70-
$this->field = $formFactory->createNamedBuilder('choice', $this->getName(), null, $options);
70+
$this->field = $formFactory->createNamedBuilder('choice', $this->getName(), null, $options)->getForm();
7171
}
7272
}

‎Filter/ORM/CallbackFilter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function association($queryBuilder, $value)
2323

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

@@ -51,6 +51,8 @@ public function getDefaultOptions()
5151

5252
public function defineFieldBuilder(FormFactory $formFactory)
5353
{
54-
return $formFactory->createNamedBuilder($this->getOption('type'), $this->getName(), null);
54+
$options = $this->getFieldDescription()->getOption('filter_field_options', array());
55+
56+
$this->field = $formFactory->createNamedBuilder($this->getOption('type'), $this->getName(), null, $options)->getForm();
5557
}
5658
}

‎Filter/ORM/ChoiceFilter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class ChoiceFilter extends Filter
1818
{
1919
public function filter($queryBuilder, $alias, $field, $value)
2020
{
21+
if (!is_array($value)) {
22+
return;
23+
}
24+
2125
if ($this->getField()->getAttribute('multiple')) {
2226
if (in_array('all', $value)) {
2327
return;
@@ -52,6 +56,6 @@ public function defineFieldBuilder(FormFactory $formFactory, $value = null)
5256
{
5357
$options = $this->getFieldDescription()->getOption('filter_field_options', array('required' => false));
5458

55-
$this->field = $formFactory->createNamedBuilder('choice', $this->getName(), $value, $options);
59+
$this->field = $formFactory->createNamedBuilder('choice', $this->getName(), $value, $options)->getForm();
5660
}
5761
}

‎Filter/ORM/IntegerFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ public function defineFieldBuilder(FormFactory $formFactory)
4545
{
4646
$options = $this->fieldDescription->getOption('filter_field_options', array('required' => false));
4747

48-
$this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options);
48+
$this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options)->getForm();
4949
}
5050
}

‎Filter/ORM/StringFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public function defineFieldBuilder(FormFactory $formFactory)
4646
{
4747
$options = $this->fieldDescription->getOption('filter_field_options', array('required' => false));
4848

49-
$this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options);
49+
$this->field = $formFactory->createNamedBuilder('text', $this->getName(), null, $options)->getForm();
5050
}
5151
}

‎Resources/views/CRUD/base_filter_field.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ file that was distributed with this source code.
1919
<br />
2020
{% endblock %}
2121

22-
<div class="sonata-ba-field{% if filter_form.vars.errors %} sonata-ba-field-error{% endif %}">
22+
<div class="sonata-ba-field{% if filter_form.vars.errors %} sonata-ba-field-error"{% endif %}">
2323
{% block field %}{{ form_widget(filter_form) }}{% endblock %}
2424
</div>
2525
</div>

‎Twig/Extension/SonataAdminExtension.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
class SonataAdminExtension extends \Twig_Extension
1919
{
2020

21-
protected $templating;
22-
2321
/**
2422
* {@inheritdoc}
2523
*/
@@ -44,10 +42,7 @@ public function getFilters()
4442

4543
public function getTokenParsers()
4644
{
47-
48-
return array(
49-
50-
);
45+
return array();
5146
}
5247

5348
/**
@@ -70,7 +65,6 @@ public function getName()
7065
*/
7166
public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
7267
{
73-
7468
$template = $this->environment->loadTemplate($fieldDescription->getTemplate());
7569

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

8579
public function output(FieldDescriptionInterface $fieldDescription, $content)
8680
{
87-
8881
return sprintf("\n<!-- fieldName: %s, template: %s -->\n%s\n", $fieldDescription->getFieldName(), $fieldDescription->getTemplate(), $content);
8982
}
9083

@@ -99,10 +92,8 @@ public function output(FieldDescriptionInterface $fieldDescription, $content)
9992
*/
10093
public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
10194
{
102-
10395
if (isset($params['loop']) && $object instanceof \ArrayAccess) {
10496
throw new \RuntimeException('remove the loop requirement');
105-
// $object = $object[$params['loop']['index0']];
10697
}
10798

10899
$value = $fieldDescription->getValue($object);
@@ -111,7 +102,6 @@ public function getValueFromFieldDescription($object, FieldDescriptionInterface
111102
// if so, create an empty object instance
112103
// fixme: not sure this is the best place to do that
113104
if (!$value && $fieldDescription->getAssociationAdmin()) {
114-
115105
$value = $fieldDescription->getAssociationAdmin()->getNewInstance();
116106
}
117107

@@ -121,7 +111,7 @@ public function getValueFromFieldDescription($object, FieldDescriptionInterface
121111
/**
122112
* render a filter element
123113
*
124-
* @param Filter $filter
114+
* @param \Sonata\AdminBundle\Filter\FilterInterface $filter
125115
* @param array $params
126116
* @return
127117
*/
@@ -133,7 +123,7 @@ public function renderFilterElement(FilterInterface $filter, array $params = arr
133123

134124
return $template->render(array_merge($params, array(
135125
'filter' => $filter,
136-
'filter_form' => $filter->getField()->getForm()->createView()
126+
'filter_form' => $filter->getField()->createView()
137127
)));
138128
}
139129

@@ -192,26 +182,5 @@ public function renderFormElement(FieldDescriptionInterface $fieldDescription, F
192182
'base_template' => $fieldDescription->getOption('base_template', $base_template)
193183
))));
194184
}
195-
196-
/**
197-
* set the templating engine
198-
*
199-
* @param $templating
200-
* @return void
201-
*/
202-
public function setTemplating($templating)
203-
{
204-
$this->templating = $templating;
205-
}
206-
207-
/**
208-
* return the templating engine
209-
*
210-
* @return Engine
211-
*/
212-
public function getTemplating()
213-
{
214-
return $this->templating;
215-
}
216185
}
217186

0 commit comments

Comments
 (0)
Please sign in to comment.