Skip to content

Commit 966b46a

Browse files
committed
Merge pull request #160 from tobias-93/filter-options
Allow filter options, for better customization of filter types
2 parents 92985f7 + 9591833 commit 966b46a

File tree

7 files changed

+163
-33
lines changed

7 files changed

+163
-33
lines changed

Builder/Admin/BaseBuilder.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,18 @@ protected function createColumn($columnName, $withForms = false)
153153

154154
if ($this->getYamlKey() === 'list') {
155155
// Filters
156-
$column->setFormOptions($this->getFieldOption(
156+
$column->setFilterOptions($this->getFieldOption(
157157
$column,
158-
'formOptions',
159-
$this->getFieldGuesser()->getFormOptions(
160-
$column->getFilterType(),
161-
$filteredFieldDbType,
162-
$this->getVariable('model'),
163-
$column->getFilterOn()
158+
'filterOptions',
159+
$this->getFieldOption(
160+
$column,
161+
'formOptions',
162+
$this->getFieldGuesser()->getFilterOptions(
163+
$column->getFilterType(),
164+
$filteredFieldDbType,
165+
$this->getVariable('model'),
166+
$column->getFilterOn()
167+
)
164168
)
165169
));
166170
} else {
@@ -185,6 +189,12 @@ protected function createColumn($columnName, $withForms = false)
185189
if (array_key_exists('addFormOptions', $fieldOptions)) {
186190
$column->setAddFormOptions($fieldOptions['addFormOptions']);
187191
}
192+
193+
if (array_key_exists('addFilterOptions', $fieldOptions)) {
194+
$column->setAddFilterOptions($fieldOptions['addFilterOptions']);
195+
} elseif (array_key_exists('addFormOptions', $fieldOptions)) {
196+
$column->setAddFilterOptions($fieldOptions['addFormOptions']);
197+
}
188198
}
189199

190200
return $column;

Generator/Column.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class Column
8888
*/
8989
protected $formOptions = array();
9090

91+
/**
92+
* @var array
93+
*/
94+
protected $filterOptions = array();
95+
9196
/**
9297
* @var string
9398
*/
@@ -144,8 +149,8 @@ class Column
144149
protected $debug = array();
145150

146151
/**
147-
* @param string $name
148-
* @param boolean $debug
152+
* @param string $name
153+
* @param array $debug
149154
*/
150155
public function __construct($name, $debug)
151156
{
@@ -315,6 +320,16 @@ public function getFilterType()
315320
return $this->filterType;
316321
}
317322

323+
public function setFilterOptions($filterOptions)
324+
{
325+
$this->filterOptions = $filterOptions;
326+
}
327+
328+
public function getFilterOptions()
329+
{
330+
return $this->filterOptions;
331+
}
332+
318333
public function setLocalizedDateFormat($localizedDateFormat)
319334
{
320335
$this->localizedDateFormat = $localizedDateFormat;
@@ -342,6 +357,13 @@ public function setAddFormOptions(array $additionalOptions = array())
342357
}
343358
}
344359

360+
public function setAddFilterOptions(array $additionalOptions = array())
361+
{
362+
foreach ($additionalOptions as $name => $option) {
363+
$this->filterOptions[$name] = $this->parseOption($option);
364+
}
365+
}
366+
345367
public function setExtras(array $values)
346368
{
347369
$this->extras = $values;

Guesser/DoctrineODMFieldGuesser.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,39 @@ public function getFilterType($dbType, $class, $columnName)
157157
);
158158
}
159159

160+
/**
161+
* @param $formType
162+
* @param $dbType
163+
* @param $model
164+
* @param $fieldPath
165+
* @return array
166+
*/
160167
public function getFormOptions($formType, $dbType, $model, $fieldPath)
168+
{
169+
return $this->getOptions($formType, $dbType, $model, $fieldPath, false);
170+
}
171+
172+
/**
173+
* @param $filterType
174+
* @param $dbType
175+
* @param $model
176+
* @param $fieldPath
177+
* @return array
178+
*/
179+
public function getFilterOptions($filterType, $dbType, $model, $fieldPath)
180+
{
181+
return $this->getOptions($filterType, $dbType, $model, $fieldPath, true);
182+
}
183+
184+
/**
185+
* @param $type
186+
* @param $dbType
187+
* @param $model
188+
* @param $fieldPath
189+
* @param bool $filter
190+
* @return array
191+
*/
192+
protected function getOptions($type, $dbType, $model, $fieldPath, $filter = false)
161193
{
162194
if ('virtual' === $dbType) {
163195
return array();
@@ -168,7 +200,7 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
168200
$columnName = $resolved['field'];
169201

170202
if ('boolean' == $dbType &&
171-
(preg_match("#^choice#i", $formType) || preg_match("#choice$#i", $formType))) {
203+
(preg_match("#^choice#i", $type) || preg_match("#choice$#i", $type))) {
172204
return array(
173205
'choices' => array(
174206
0 => 'boolean.no',
@@ -180,13 +212,13 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
180212
}
181213

182214
if ('boolean' == $dbType &&
183-
(preg_match("#^checkbox#i", $formType) || preg_match("#checkbox#i", $formType))) {
215+
(preg_match("#^checkbox#i", $type) || preg_match("#checkbox#i", $type))) {
184216
return array(
185217
'required' => false,
186218
);
187219
}
188220

189-
if (preg_match("#^document#i", $formType) || preg_match("#document$#i", $formType)) {
221+
if (preg_match("#^document#i", $type) || preg_match("#document$#i", $type)) {
190222
$mapping = $this->getMetadatas($class)->getFieldMapping($columnName);
191223

192224
return array(
@@ -195,7 +227,7 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
195227
);
196228
}
197229

198-
if (preg_match("#^collection#i", $formType) || preg_match("#collection$#i", $formType)) {
230+
if (preg_match("#^collection#i", $type) || preg_match("#collection$#i", $type)) {
199231
$mapping = $this->getMetadatas($class)->getFieldMapping($columnName);
200232

201233
return array(
@@ -218,7 +250,7 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
218250
);
219251
}
220252

221-
return array('required' => $this->isRequired($class, $columnName));
253+
return array('required' => $filter ? false : $this->isRequired($class, $columnName));
222254
}
223255

224256
protected function isRequired($class, $fieldName)

Guesser/DoctrineORMFieldGuesser.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,33 @@ public function getFilterType($dbType, $class, $columnName)
173173
* @throws \Exception
174174
*/
175175
public function getFormOptions($formType, $dbType, $model, $fieldPath)
176+
{
177+
return $this->getOptions($formType, $dbType, $model, $fieldPath, false);
178+
}
179+
180+
/**
181+
* @param $filterType
182+
* @param $dbType
183+
* @param $model
184+
* @param $fieldPath
185+
* @return array
186+
* @throws \Exception
187+
*/
188+
public function getFilterOptions($filterType, $dbType, $model, $fieldPath)
189+
{
190+
return $this->getOptions($filterType, $dbType, $model, $fieldPath, true);
191+
}
192+
193+
/**
194+
* @param $type
195+
* @param $dbType
196+
* @param $model
197+
* @param $fieldPath
198+
* @param bool $filter
199+
* @return array
200+
* @throws \Exception
201+
*/
202+
protected function getOptions($type, $dbType, $model, $fieldPath, $filter = false)
176203
{
177204
if ('virtual' === $dbType) {
178205
return array();
@@ -183,7 +210,7 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
183210
$columnName = $resolved['field'];
184211

185212
if ('boolean' == $dbType &&
186-
(preg_match("#^choice#i", $formType) || preg_match("#choice$#i", $formType))) {
213+
(preg_match("#^choice#i", $type) || preg_match("#choice$#i", $type))) {
187214
return array(
188215
'choices' => array(
189216
0 => 'boolean.no',
@@ -195,13 +222,13 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
195222
}
196223

197224
if ('boolean' == $dbType &&
198-
(preg_match("#^checkbox#i", $formType) || preg_match("#checkbox#i", $formType))) {
225+
(preg_match("#^checkbox#i", $type) || preg_match("#checkbox#i", $type))) {
199226
return array(
200227
'required' => false,
201228
);
202229
}
203230

204-
if ('number' === $formType) {
231+
if ('number' === $type) {
205232
$mapping = $this->getMetadatas($class)->getFieldMapping($columnName);
206233

207234
if (isset($mapping['scale'])) {
@@ -214,22 +241,22 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
214241

215242
return array(
216243
'precision' => isset($precision) ? $precision : '',
217-
'required' => $this->isRequired($class, $columnName)
244+
'required' => $filter ? false : $this->isRequired($class, $columnName)
218245
);
219246
}
220247

221-
if (preg_match("#^entity#i", $formType) || preg_match("#entity$#i", $formType)) {
248+
if (preg_match("#^entity#i", $type) || preg_match("#entity$#i", $type)) {
222249
$mapping = $this->getMetadatas($class)->getAssociationMapping($columnName);
223250

224251
return array(
225252
'multiple' => ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY || $mapping['type'] === ClassMetadataInfo::ONE_TO_MANY),
226253
'em' => $this->getObjectManagerName($mapping['targetEntity']),
227254
'class' => $mapping['targetEntity'],
228-
'required' => $this->isRequired($class, $columnName),
255+
'required' => $filter ? false : $this->isRequired($class, $columnName),
229256
);
230257
}
231258

232-
if (preg_match("#^collection#i", $formType) || preg_match("#collection$#i", $formType)) {
259+
if (preg_match("#^collection#i", $type) || preg_match("#collection$#i", $type)) {
233260
$mapping = $this->getMetadatas($class)->getAssociationMapping($columnName);
234261

235262
return array(

Guesser/PropelORMFieldGuesser.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,36 @@ public function getFilterType($dbType, $class, $columnName)
212212
/**
213213
* @param $formType
214214
* @param $dbType
215-
* @param $columnName
215+
* @param $model
216+
* @param $fieldPath
216217
* @return array
217218
*/
218219
public function getFormOptions($formType, $dbType, $model, $fieldPath)
220+
{
221+
return $this->getOptions($formType, $dbType, $model, $fieldPath, false);
222+
}
223+
224+
/**
225+
* @param $filterType
226+
* @param $dbType
227+
* @param $model
228+
* @param $fieldPath
229+
* @return array
230+
*/
231+
public function getFilterOptions($filterType, $dbType, $model, $fieldPath)
232+
{
233+
return $this->getOptions($filterType, $dbType, $model, $fieldPath, true);
234+
}
235+
236+
/**
237+
* @param $type
238+
* @param $dbType
239+
* @param $model
240+
* @param $fieldPath
241+
* @param bool $filter
242+
* @return array
243+
*/
244+
protected function getOptions($type, $dbType, $model, $fieldPath, $filter = false)
219245
{
220246
if ('virtual' === $dbType) {
221247
return array();
@@ -226,7 +252,7 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
226252
$columnName = $resolved['field'];
227253

228254
if ((\PropelColumnTypes::BOOLEAN == $dbType || \PropelColumnTypes::BOOLEAN_EMU == $dbType) &&
229-
(preg_match("#^choice#i", $formType) || preg_match("#choice$#i", $formType))) {
255+
(preg_match("#^choice#i", $type) || preg_match("#choice$#i", $type))) {
230256
return array(
231257
'choices' => array(
232258
0 => 'boolean.no',
@@ -237,14 +263,15 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
237263
);
238264
}
239265

240-
if ((\PropelColumnTypes::BOOLEAN == $dbType || \PropelColumnTypes::BOOLEAN_EMU == $dbType) &&
241-
(preg_match("#^checkbox#i", $formType) || preg_match("#checkbox#i", $formType))) {
266+
if (!$filter &&
267+
(\PropelColumnTypes::BOOLEAN == $dbType || \PropelColumnTypes::BOOLEAN_EMU == $dbType) &&
268+
(preg_match("#^checkbox#i", $type) || preg_match("#checkbox#i", $type))) {
242269
return array(
243270
'required' => false
244271
);
245272
}
246273

247-
if (preg_match("#^model#i", $formType) || preg_match("#model$#i", $formType)) {
274+
if (preg_match("#^model#i", $type) || preg_match("#model$#i", $type)) {
248275
$relation = $this->getRelation($columnName, $class);
249276
if ($relation) {
250277
if (\RelationMap::MANY_TO_ONE === $relation->getType()) {
@@ -261,7 +288,7 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
261288
}
262289
}
263290

264-
if (preg_match("#^collection#i", $formType) || preg_match("#collection$#i", $formType)) {
291+
if (preg_match("#^collection#i", $type) || preg_match("#collection$#i", $type)) {
265292
$relation = $this->getRelation($columnName, $class);
266293

267294
return array(
@@ -279,12 +306,12 @@ public function getFormOptions($formType, $dbType, $model, $fieldPath)
279306
$valueSet = $this->getMetadatas($class)->getColumn($class, $columnName)->getValueSet();
280307

281308
return array(
282-
'required' => $this->isRequired($class, $columnName),
309+
'required' => $filter ? false : $this->isRequired($class, $columnName),
283310
'choices' => array_combine($valueSet, $valueSet),
284311
);
285312
}
286313

287-
return array('required' => $this->isRequired($class, $columnName));
314+
return array('required' => $filter ? false : $this->isRequired($class, $columnName));
288315
}
289316

290317
protected function isRequired($class, $fieldName)

Resources/templates/CommonAdmin/FiltersType/type.php.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class FiltersType extends BaseType
4949
'label': column.label,
5050
'translation_domain': i18n_catalog|default('Admin'),
5151
'required': false
52-
}|merge(column.formOptions)|as_php|convert_as_form(column.formType) }}, $builderOptions, $options
52+
}|merge(column.filterOptions)|as_php|convert_as_form(column.filterType) }}, $builderOptions, $options
5353
);
5454
}
5555

Tests/Generator/ColumnTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function testSetProperty()
4949
'dbType' => 'text',
5050
'formType' => 'choices',
5151
'formOptions' => array('foo' => 'bar'),
52+
'filterType' => 'choice',
53+
'filterOptions' => array('bar' => 'foo'),
5254
);
5355

5456
$column = new Column("test", false);
@@ -59,15 +61,25 @@ public function testSetProperty()
5961
}
6062
}
6163

62-
public function testSetAddFormOptionsPhpFunction()
64+
public function testSetAddFormFilterOptionsPhpFunction()
6365
{
6466
$column = new Column("test", false);
6567

66-
$column->setAddFormOptions(array('years' => array('.range' => array('from' => 1900, 'to' => 1915, 'step'=> 5 ))));
68+
$addOptions = array('years' => array('.range' => array('from' => 1900, 'to' => 1915, 'step' => 5)));
69+
70+
$column->setAddFormOptions($addOptions);
6771

6872
$options = $column->getFormOptions();
6973

70-
$this->assertEquals(array(1900, 1905, 1910, 1915), $options['years']);
74+
$testOptions = array(1900, 1905, 1910, 1915);
75+
76+
$this->assertEquals($testOptions, $options['years']);
77+
78+
$column->setAddFilterOptions($addOptions);
79+
80+
$options = $column->getFilterOptions();
81+
82+
$this->assertEquals($testOptions, $options['years']);
7183
}
7284

7385
public function testFiltersGroups()

0 commit comments

Comments
 (0)