Filter.php 5.6 KB

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