Filter.php 5.1 KB

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