12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\DoctrineORMAdminBundle\Filter;
- use Sonata\AdminBundle\Form\Type\Filter\NumberType;
- use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
- class NumberFilter extends Filter
- {
- /**
- * {@inheritdoc}
- */
- public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
- {
- if (!$data || !is_array($data) || !array_key_exists('value', $data) || !is_numeric($data['value'])) {
- return;
- }
- $type = isset($data['type']) ? $data['type'] : false;
- $operator = $this->getOperator($type);
- if (!$operator) {
- $operator = '=';
- }
- // c.name > '1' => c.name OPERATOR :FIELDNAME
- $parameterName = $this->getNewParameterName($queryBuilder);
- $this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName));
- $queryBuilder->setParameter($parameterName, $data['value']);
- }
- /**
- * @param string $type
- *
- * @return bool
- */
- private function getOperator($type)
- {
- $choices = array(
- NumberType::TYPE_EQUAL => '=',
- NumberType::TYPE_GREATER_EQUAL => '>=',
- NumberType::TYPE_GREATER_THAN => '>',
- NumberType::TYPE_LESS_EQUAL => '<=',
- NumberType::TYPE_LESS_THAN => '<',
- );
- return isset($choices[$type]) ? $choices[$type] : false;
- }
- /**
- * {@inheritdoc}
- */
- public function getDefaultOptions()
- {
- return array();
- }
- /**
- * {@inheritdoc}
- */
- public function getRenderSettings()
- {
- return array('sonata_type_filter_number', array(
- 'field_type' => $this->getFieldType(),
- 'field_options' => $this->getFieldOptions(),
- 'label' => $this->getLabel()
- ));
- }
- }
|