Filter.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /*
  39. Symfony default form class sadly can't handle
  40. form element with dots in its name (when data
  41. get bound, the default dataMapper is a PropertyPathMapper).
  42. So use this trick to avoid any issue.
  43. */
  44. return str_replace('.', '__', $this->name);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getOption($name, $default = null)
  50. {
  51. if (array_key_exists($name, $this->options)) {
  52. return $this->options[$name];
  53. }
  54. return $default;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function setOption($name, $value)
  60. {
  61. $this->options[$name] = $value;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getFieldType()
  67. {
  68. return $this->getOption('field_type', 'text');
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getFieldOptions()
  74. {
  75. return $this->getOption('field_options', array('required' => false));
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getLabel()
  81. {
  82. return $this->getOption('label');
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function setLabel($label)
  88. {
  89. $this->setOption('label', $label);
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getFieldName()
  95. {
  96. $fieldName = $this->getOption('field_name');
  97. if (!$fieldName) {
  98. throw new \RuntimeException(sprintf('The option `field_name` must be set for field: `%s`', $this->getName()));
  99. }
  100. return $fieldName;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getParentAssociationMappings()
  106. {
  107. return $this->getOption('parent_association_mappings', array());
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getFieldMapping()
  113. {
  114. $fieldMapping = $this->getOption('field_mapping');
  115. if (!$fieldMapping) {
  116. throw new \RuntimeException(sprintf('The option `field_mapping` must be set for field: `%s`', $this->getName()));
  117. }
  118. return $fieldMapping;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getAssociationMapping()
  124. {
  125. $associationMapping = $this->getOption('association_mapping');
  126. if (!$associationMapping) {
  127. throw new \RuntimeException(sprintf('The option `association_mapping` must be set for field: `%s`', $this->getName()));
  128. }
  129. return $associationMapping;
  130. }
  131. /**
  132. * Set options
  133. *
  134. * @param array $options
  135. */
  136. public function setOptions(array $options)
  137. {
  138. $this->options = array_merge(
  139. array('show_filter' => true),
  140. $this->getDefaultOptions(),
  141. $options
  142. );
  143. }
  144. /**
  145. * Get options
  146. *
  147. * @return array
  148. */
  149. public function getOptions()
  150. {
  151. return $this->options;
  152. }
  153. /**
  154. * Set value
  155. *
  156. * @param mixed $value
  157. */
  158. public function setValue($value)
  159. {
  160. $this->value = $value;
  161. }
  162. /**
  163. * Get value
  164. *
  165. * @return mixed
  166. */
  167. public function getValue()
  168. {
  169. return $this->value;
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function isActive()
  175. {
  176. $values = $this->getValue();
  177. return isset($values['value'])
  178. && false !== $values['value']
  179. && "" !== $values['value'];
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function setCondition($condition)
  185. {
  186. $this->condition = $condition;
  187. }
  188. /**
  189. * {@inheritdoc}
  190. */
  191. public function getCondition()
  192. {
  193. return $this->condition;
  194. }
  195. /**
  196. * {@inheritDoc}
  197. */
  198. public function getTranslationDomain()
  199. {
  200. return $this->getOption('translation_domain');
  201. }
  202. }