Filter.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. abstract class Filter implements FilterInterface
  12. {
  13. protected $name = null;
  14. protected $value = null;
  15. protected $options = array();
  16. protected $condition;
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function initialize($name, array $options = array())
  21. {
  22. $this->name = $name;
  23. $this->setOptions($options);
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getName()
  29. {
  30. return $this->name;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getFormName()
  36. {
  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(
  138. array('show_filter' => true),
  139. $this->getDefaultOptions(),
  140. $options
  141. );
  142. }
  143. /**
  144. * Get options
  145. *
  146. * @return array
  147. */
  148. public function getOptions()
  149. {
  150. return $this->options;
  151. }
  152. /**
  153. * Set value
  154. *
  155. * @param mixed $value
  156. */
  157. public function setValue($value)
  158. {
  159. $this->value = $value;
  160. }
  161. /**
  162. * Get value
  163. *
  164. * @return mixed
  165. */
  166. public function getValue()
  167. {
  168. return $this->value;
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function isActive()
  174. {
  175. $values = $this->getValue();
  176. return isset($values['value'])
  177. && false !== $values['value']
  178. && "" !== $values['value'];
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function setCondition($condition)
  184. {
  185. $this->condition = $condition;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function getCondition()
  191. {
  192. return $this->condition;
  193. }
  194. /**
  195. * {@inheritDoc}
  196. */
  197. public function getTranslationDomain()
  198. {
  199. return $this->getOption('translation_domain');
  200. }
  201. }