-
-
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
Added doctrine_mongo_autocomplete model filter #137
Changes from all commits
35fbc34
3d27740
86c1181
b41861c
0ce52d7
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 |
---|---|---|
|
@@ -28,6 +28,27 @@ public function apply($queryBuilder, $value) | |
$this->filter($queryBuilder, null, $this->getFieldName(), $value); | ||
} | ||
|
||
public function getFieldName() | ||
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. Can you add some PHPDoc please? |
||
{ | ||
$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; | ||
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. Please use simple quotes (run PHP CS Fixer). 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. this was before the PHP CS Fixer |
||
} 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} | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata package. | ||
* | ||
* (c) Thomas Rabaix <[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; | ||
|
||
class ModelAutocompleteFilter extends Filter | ||
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. Can you add some PHPDoc? |
||
{ | ||
|
||
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data) | ||
{ | ||
if (!$data || !is_array($data) || !array_key_exists('value', $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. Can be simplified by |
||
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); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* | ||
* @param ProxyQueryInterface $queryBuilder | ||
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. Please fix PHPDoc. |
||
* @param type $alias | ||
* @param type $field | ||
* @param type $data | ||
* @return type | ||
*/ | ||
protected function handleMultiple(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.
|
||
{ | ||
if (count($data['value']) == 0) { | ||
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. Always use strict operators ( |
||
return; | ||
} | ||
|
||
$ids = array(); | ||
foreach ($data['value'] as $value) { | ||
$ids[] = self::fixIdentifier($value->getId()); | ||
} | ||
|
||
if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) { | ||
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. Same here ( |
||
$queryBuilder->field($field)->notIn($ids); | ||
} else { | ||
$queryBuilder->field($field)->in($ids); | ||
} | ||
|
||
$this->active = true; | ||
} | ||
|
||
/** | ||
* | ||
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. Correct the PHPDoc. |
||
* @param ProxyQueryInterface $queryBuilder | ||
* @param type $alias | ||
* @param type $field | ||
* @param type $data | ||
* @return type | ||
*/ | ||
protected function handleScalar(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. data should be typehinted |
||
{ | ||
if (empty($data['value'])) { | ||
return; | ||
} | ||
|
||
$id = self::fixIdentifier($data['value']->getId()); | ||
|
||
if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) { | ||
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.
|
||
$queryBuilder->field($field)->notEqual($id); | ||
} else { | ||
$queryBuilder->field($field)->equals($id); | ||
} | ||
|
||
$this->active = true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function association(ProxyQueryInterface $queryBuilder, $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. Is this extension point necessary? Make the function 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. Don't mind my previous comment. It's inherited. |
||
{ | ||
$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) | ||
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. Why |
||
{ | ||
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() | ||
)); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,13 @@ | |
|
||
class ModelFilter extends Filter | ||
{ | ||
|
||
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. Why do you delete this docblock? |
||
/** | ||
* @param ProxyQueryInterface $queryBuilder | ||
* @param string $alias | ||
* @param string $field | ||
* @param mixed $data | ||
* @param string $alias | ||
* @param string $field | ||
* @param mixed $data | ||
* | ||
* @return | ||
*/ | ||
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data) | ||
{ | ||
|
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.
What's the use case of this new extension point?
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.
I tried to keep the same logic of MySQL PHP file for be easier to mantain both projects by just comparing them -- till common areas such this one become merged into Admin bundle
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.
👍