123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- /*
- * This file is part of the Sonata Project 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\AdminBundle\Filter;
- /**
- * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
- */
- abstract class Filter implements FilterInterface
- {
- /**
- * @var string|null
- */
- protected $name = null;
- /**
- * @var mixed|null
- */
- protected $value = null;
- /**
- * @var array
- */
- protected $options = array();
- /**
- * @var string
- */
- protected $condition;
- /**
- * {@inheritdoc}
- */
- public function initialize($name, array $options = array())
- {
- $this->name = $name;
- $this->setOptions($options);
- }
- /**
- * {@inheritdoc}
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * {@inheritdoc}
- */
- public function getFormName()
- {
- /*
- Symfony default form class sadly can't handle
- form element with dots in its name (when data
- get bound, the default dataMapper is a PropertyPathMapper).
- So use this trick to avoid any issue.
- */
- return str_replace('.', '__', $this->name);
- }
- /**
- * {@inheritdoc}
- */
- public function getOption($name, $default = null)
- {
- if (array_key_exists($name, $this->options)) {
- return $this->options[$name];
- }
- return $default;
- }
- /**
- * {@inheritdoc}
- */
- public function setOption($name, $value)
- {
- $this->options[$name] = $value;
- }
- /**
- * {@inheritdoc}
- */
- public function getFieldType()
- {
- // NEXT_MAJOR: Remove ternary and keep 'Symfony\Component\Form\Extension\Core\Type\TextType'
- // (when requirement of Symfony is >= 2.8)
- return $this->getOption('field_type', method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
- ? 'Symfony\Component\Form\Extension\Core\Type\TextType'
- : 'text'
- );
- }
- /**
- * {@inheritdoc}
- */
- public function getFieldOptions()
- {
- return $this->getOption('field_options', array('required' => false));
- }
- /**
- * {@inheritdoc}
- */
- public function getFieldOption($name, $default = null)
- {
- if (isset($this->options['field_options'][$name]) && is_array($this->options['field_options'])) {
- return $this->options['field_options'][$name];
- }
- return $default;
- }
- /**
- * {@inheritdoc}
- */
- public function setFieldOption($name, $value)
- {
- $this->options['field_options'][$name] = $value;
- }
- /**
- * {@inheritdoc}
- */
- public function getLabel()
- {
- return $this->getOption('label');
- }
- /**
- * {@inheritdoc}
- */
- public function setLabel($label)
- {
- $this->setOption('label', $label);
- }
- /**
- * {@inheritdoc}
- */
- public function getFieldName()
- {
- $fieldName = $this->getOption('field_name');
- if (!$fieldName) {
- throw new \RuntimeException(sprintf('The option `field_name` must be set for field: `%s`', $this->getName()));
- }
- return $fieldName;
- }
- /**
- * {@inheritdoc}
- */
- public function getParentAssociationMappings()
- {
- return $this->getOption('parent_association_mappings', array());
- }
- /**
- * {@inheritdoc}
- */
- public function getFieldMapping()
- {
- $fieldMapping = $this->getOption('field_mapping');
- if (!$fieldMapping) {
- throw new \RuntimeException(sprintf('The option `field_mapping` must be set for field: `%s`', $this->getName()));
- }
- return $fieldMapping;
- }
- /**
- * {@inheritdoc}
- */
- public function getAssociationMapping()
- {
- $associationMapping = $this->getOption('association_mapping');
- if (!$associationMapping) {
- throw new \RuntimeException(sprintf('The option `association_mapping` must be set for field: `%s`', $this->getName()));
- }
- return $associationMapping;
- }
- /**
- * Set options.
- *
- * @param array $options
- */
- public function setOptions(array $options)
- {
- $this->options = array_merge(
- array('show_filter' => null, 'advanced_filter' => true),
- $this->getDefaultOptions(),
- $options
- );
- }
- /**
- * Get options.
- *
- * @return array
- */
- public function getOptions()
- {
- return $this->options;
- }
- /**
- * Set value.
- *
- * @param mixed $value
- */
- public function setValue($value)
- {
- $this->value = $value;
- }
- /**
- * Get value.
- *
- * @return mixed
- */
- public function getValue()
- {
- return $this->value;
- }
- /**
- * {@inheritdoc}
- */
- public function isActive()
- {
- $values = $this->getValue();
- return isset($values['value'])
- && false !== $values['value']
- && '' !== $values['value'];
- }
- /**
- * {@inheritdoc}
- */
- public function setCondition($condition)
- {
- $this->condition = $condition;
- }
- /**
- * {@inheritdoc}
- */
- public function getCondition()
- {
- return $this->condition;
- }
- /**
- * {@inheritdoc}
- */
- public function getTranslationDomain()
- {
- return $this->getOption('translation_domain');
- }
- }
|