Filter.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. return $this->getOption('field_type', 'text');
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getFieldOptions()
  90. {
  91. return $this->getOption('field_options', array('required' => false));
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getFieldOption($name, $default = null)
  97. {
  98. if (isset($this->options['field_options'][$name]) && is_array($this->options['field_options'])) {
  99. return $this->options['field_options'][$name];
  100. }
  101. return $default;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function setFieldOption($name, $value)
  107. {
  108. $this->options['field_options'][$name] = $value;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function getLabel()
  114. {
  115. return $this->getOption('label');
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function setLabel($label)
  121. {
  122. $this->setOption('label', $label);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function getFieldName()
  128. {
  129. $fieldName = $this->getOption('field_name');
  130. if (!$fieldName) {
  131. throw new \RuntimeException(sprintf('The option `field_name` must be set for field: `%s`', $this->getName()));
  132. }
  133. return $fieldName;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function getParentAssociationMappings()
  139. {
  140. return $this->getOption('parent_association_mappings', array());
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function getFieldMapping()
  146. {
  147. $fieldMapping = $this->getOption('field_mapping');
  148. if (!$fieldMapping) {
  149. throw new \RuntimeException(sprintf('The option `field_mapping` must be set for field: `%s`', $this->getName()));
  150. }
  151. return $fieldMapping;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function getAssociationMapping()
  157. {
  158. $associationMapping = $this->getOption('association_mapping');
  159. if (!$associationMapping) {
  160. throw new \RuntimeException(sprintf('The option `association_mapping` must be set for field: `%s`', $this->getName()));
  161. }
  162. return $associationMapping;
  163. }
  164. /**
  165. * Set options.
  166. *
  167. * @param array $options
  168. */
  169. public function setOptions(array $options)
  170. {
  171. $this->options = array_merge(
  172. array('show_filter' => null, 'advanced_filter' => true),
  173. $this->getDefaultOptions(),
  174. $options
  175. );
  176. }
  177. /**
  178. * Get options.
  179. *
  180. * @return array
  181. */
  182. public function getOptions()
  183. {
  184. return $this->options;
  185. }
  186. /**
  187. * Set value.
  188. *
  189. * @param mixed $value
  190. */
  191. public function setValue($value)
  192. {
  193. $this->value = $value;
  194. }
  195. /**
  196. * Get value.
  197. *
  198. * @return mixed
  199. */
  200. public function getValue()
  201. {
  202. return $this->value;
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function isActive()
  208. {
  209. $values = $this->getValue();
  210. return isset($values['value'])
  211. && false !== $values['value']
  212. && '' !== $values['value'];
  213. }
  214. /**
  215. * {@inheritdoc}
  216. */
  217. public function setCondition($condition)
  218. {
  219. $this->condition = $condition;
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. public function getCondition()
  225. {
  226. return $this->condition;
  227. }
  228. /**
  229. * {@inheritdoc}
  230. */
  231. public function getTranslationDomain()
  232. {
  233. return $this->getOption('translation_domain');
  234. }
  235. }