1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?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\ChoiceType;
- class StringFilter 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)) {
- return;
- }
- $data['value'] = trim($data['value']);
- if (strlen($data['value']) == 0) {
- return;
- }
- $data['type'] = !isset($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type'];
- $operator = $this->getOperator((int) $data['type']);
- if (!$operator) {
- $operator = 'LIKE';
- }
- // c.name > '1' => c.name OPERATOR :FIELDNAME
- $parameterName = $this->getNewParameterName();
- $this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName));
- if ($data['type'] == ChoiceType::TYPE_EQUAL) {
- $queryBuilder->setParameter($parameterName, $data['value']);
- } else {
- $queryBuilder->setParameter($parameterName, sprintf($this->getOption('format'), $data['value']));
- }
- }
- /**
- * @param $type
- * @return bool
- */
- private function getOperator($type)
- {
- $choices = array(
- ChoiceType::TYPE_CONTAINS => 'LIKE',
- ChoiceType::TYPE_NOT_CONTAINS => 'NOT LIKE',
- ChoiceType::TYPE_EQUAL => '=',
- );
- return isset($choices[$type]) ? $choices[$type] : false;
- }
- /**
- * @return array
- */
- public function getDefaultOptions()
- {
- return array(
- 'format' => '%%%s%%'
- );
- }
- public function getRenderSettings()
- {
- return array('sonata_type_filter_choice', array(
- 'field_type' => $this->getFieldType(),
- 'field_options' => $this->getFieldOptions(),
- 'label' => $this->getLabel()
- ));
- }
- }
|