Filter.php 4.5 KB

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