Filter.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Filter;
  11. use Sonata\AdminBundle\Filter\FilterInterface;
  12. abstract class Filter implements FilterInterface
  13. {
  14. protected $name = null;
  15. protected $value = null;
  16. protected $options = array();
  17. protected $condition;
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function initialize($name, array $options = array())
  22. {
  23. $this->name = $name;
  24. $this->setOptions($options);
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function getName()
  30. {
  31. return $this->name;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getFormName()
  37. {
  38. /* Symfony default form class sadly can't handle
  39. form element with dots in its name (when data
  40. get bound, the default dataMapper is a PropertyPathMapper).
  41. So use this trick to avoid any issue.
  42. */
  43. return str_replace('.', '__', $this->name);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getOption($name, $default = null)
  49. {
  50. if (array_key_exists($name, $this->options)) {
  51. return $this->options[$name];
  52. }
  53. return $default;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function setOption($name, $value)
  59. {
  60. $this->options[$name] = $value;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getFieldType()
  66. {
  67. return $this->getOption('field_type', 'text');
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getFieldOptions()
  73. {
  74. return $this->getOption('field_options', array('required' => false));
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getLabel()
  80. {
  81. return $this->getOption('label');
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function setLabel($label)
  87. {
  88. $this->setOption('label', $label);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getFieldName()
  94. {
  95. $fieldName = $this->getOption('field_name');
  96. if (!$fieldName) {
  97. throw new \RuntimeException(sprintf('The option `field_name` must be set for field: `%s`', $this->getName()));
  98. }
  99. return $fieldName;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getParentAssociationMappings()
  105. {
  106. return $this->getOption('parent_association_mappings', array());
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getFieldMapping()
  112. {
  113. $fieldMapping = $this->getOption('field_mapping');
  114. if (!$fieldMapping) {
  115. throw new \RuntimeException(sprintf('The option `field_mapping` must be set for field: `%s`', $this->getName()));
  116. }
  117. return $fieldMapping;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function getAssociationMapping()
  123. {
  124. $associationMapping = $this->getOption('association_mapping');
  125. if (!$associationMapping) {
  126. throw new \RuntimeException(sprintf('The option `association_mapping` must be set for field: `%s`', $this->getName()));
  127. }
  128. return $associationMapping;
  129. }
  130. /**
  131. * Set options
  132. *
  133. * @param array $options
  134. */
  135. public function setOptions(array $options)
  136. {
  137. $this->options = array_merge($this->getDefaultOptions(), $options);
  138. }
  139. /**
  140. * Get options
  141. *
  142. * @return array
  143. */
  144. public function getOptions()
  145. {
  146. return $this->options;
  147. }
  148. /**
  149. * Set value
  150. *
  151. * @param mixed $value
  152. */
  153. public function setValue($value)
  154. {
  155. $this->value = $value;
  156. }
  157. /**
  158. * Get value
  159. *
  160. * @return mixed
  161. */
  162. public function getValue()
  163. {
  164. return $this->value;
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function isActive()
  170. {
  171. $values = $this->getValue();
  172. return isset($values['value'])
  173. && false !== $values['value']
  174. && "" !== $values['value'];
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function setCondition($condition)
  180. {
  181. $this->condition = $condition;
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function getCondition()
  187. {
  188. return $this->condition;
  189. }
  190. /**
  191. * {@inheritDoc}
  192. */
  193. public function getTranslationDomain()
  194. {
  195. return $this->getOption('translation_domain');
  196. }
  197. }