Filter.php 4.3 KB

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