-
-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autcomplete feature #149
Autcomplete feature #149
Changes from all commits
35fbc34
3d27740
86c1181
b41861c
0ce52d7
37f3904
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,17 +19,35 @@ abstract class Filter extends BaseFilter | |
protected $active = false; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* @param mixed $queryBuilder | ||
* @param mixed $value | ||
*/ | ||
public function apply($queryBuilder, $value) | ||
{ | ||
$this->value = $value; | ||
|
||
$this->filter($queryBuilder, null, $this->getFieldName(), $value); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing doc block. |
||
public function getFieldName() | ||
{ | ||
$fieldName = $this->getOption('field_name'); | ||
if (is_array($this->getOption('parent_association_mappings'))) { | ||
foreach($this->getOption('parent_association_mappings') as $map) { | ||
if(!empty($map['name'])) { | ||
$fieldName = $map['name'] . "." . $fieldName; | ||
} elseif (!empty($map['fieldName'])) { | ||
$fieldName = $map['fieldName'] . $fieldName; | ||
} | ||
} | ||
} | ||
if (!$fieldName) { | ||
throw new \RuntimeException(sprintf('The option `field_name` must be set for field: `%s`', $this->getName())); | ||
} | ||
return $fieldName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* @return bool | ||
*/ | ||
public function isActive() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\DoctrineMongoDBAdminBundle\Filter; | ||
|
||
use Sonata\CoreBundle\Form\Type\EqualType; | ||
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; | ||
use Doctrine\Common\Collections\Collection; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing class comment. |
||
/** | ||
* Class ModelAutocompleteFilter | ||
* @author Jose Lopes <[email protected]> | ||
* @package Sonata\DoctrineMongoDBAdminBundle\Filter | ||
*/ | ||
class ModelAutocompleteFilter extends Filter | ||
{ | ||
|
||
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing doc block. |
||
{ | ||
if (!$data || !is_array($data) || !array_key_exists('value', $data)) { | ||
return; | ||
} | ||
if ($data['value'] instanceof Collection) { | ||
$data['value'] = $data['value']->toArray(); | ||
} | ||
$field = $this->getIdentifierField($field); | ||
if (is_array($data['value'])) { | ||
$this->handleMultiple($queryBuilder, $alias, $field, $data); | ||
} else { | ||
$this->handleScalar($queryBuilder, $alias, $field, $data); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line not needed. |
||
* @param ProxyQueryInterface $queryBuilder | ||
* @param type $alias | ||
* @param type $field | ||
* @param type $data | ||
* @return type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line needed before return. |
||
*/ | ||
protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $field, $data) | ||
{ | ||
if (count($data['value']) == 0) { | ||
return; | ||
} | ||
$ids = array(); | ||
foreach ($data['value'] as $value) { | ||
$ids[] = self::fixIdentifier($value->getId()); | ||
} | ||
|
||
if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) { | ||
$queryBuilder->field($field)->notIn($ids); | ||
} else { | ||
$queryBuilder->field($field)->in($ids); | ||
} | ||
$this->active = true; | ||
} | ||
|
||
/** | ||
* | ||
* @param ProxyQueryInterface $queryBuilder | ||
* @param type $alias | ||
* @param type $field | ||
* @param type $data | ||
* @return type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line needed before return. |
||
*/ | ||
protected function handleScalar(ProxyQueryInterface $queryBuilder, $alias, $field, $data) | ||
{ | ||
if (empty($data['value'])) { | ||
return; | ||
} | ||
$id = self::fixIdentifier($data['value']->getId()); | ||
if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) { | ||
$queryBuilder->field($field)->notEqual($id); | ||
} else { | ||
$queryBuilder->field($field)->equals($id); | ||
} | ||
$this->active = true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function association(ProxyQueryInterface $queryBuilder, $data) | ||
{ | ||
$associationMappings = $this->getParentAssociationMappings(); | ||
$associationMappings[] = $this->getAssociationMapping(); | ||
$alias = $queryBuilder->entityJoin($associationMappings); | ||
|
||
return array($alias, false); | ||
} | ||
|
||
/** | ||
* Return \MongoId if $id is MongoId in string representation, otherwise custom string | ||
* | ||
* @param mixed $id | ||
* @return \MongoId|string | ||
*/ | ||
protected static function fixIdentifier($id) | ||
{ | ||
try { | ||
return new \MongoId($id); | ||
} catch (\MongoException $ex) { | ||
return $id; | ||
} | ||
} | ||
|
||
/** | ||
* Identifier field name is 'field' if mapping type is simple; otherwise, it's 'field.$id' | ||
* | ||
* @param string $field | ||
* @return string | ||
*/ | ||
protected function getIdentifierField($field) | ||
{ | ||
$field_mapping = $this->getFieldMapping(); | ||
|
||
return (true === $field_mapping['simple']) ? $field : $field . '.$id'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefaultOptions() | ||
{ | ||
return array( | ||
'field_name' => false, | ||
'field_type' => 'sonata_type_model_autocomplete', | ||
'field_options' => array(), | ||
'operator_type' => 'sonata_type_equal', | ||
'operator_options' => array(), | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRenderSettings() | ||
{ | ||
return array('sonata_type_filter_default', array( | ||
'field_type' => $this->getFieldType(), | ||
'field_options' => $this->getFieldOptions(), | ||
'operator_type' => $this->getOption('operator_type'), | ||
'operator_options' => $this->getOption('operator_options'), | ||
'label' => $this->getLabel() | ||
)); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why removing
inheritdoc
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason, should i put it back ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course. If this method as a parent, it must have this tag.