Filter.php 5.5 KB

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