Filter.php 5.0 KB

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