1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?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;
- class NumberFilter extends Filter
- {
- /**
- * @param QueryBuilder $queryBuilder
- * @param string $alias
- * @param string $field
- * @param string $data
- * @return
- */
- public function filter($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
- $this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $this->getName()));
- $queryBuilder->setParameter($this->getName(), $data['value']);
- }
- /**
- * @param $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;
- }
- /**
- * @return array
- */
- public function getDefaultOptions()
- {
- return array();
- }
- public function getRenderSettings()
- {
- return array('sonata_type_filter_number', array(
- 'field_type' => $this->getFieldType(),
- 'field_options' => $this->getFieldOptions(),
- 'label' => $this->getLabel()
- ));
- }
- }
|