Filter.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * @return void
  135. */
  136. public function setOptions(array $options)
  137. {
  138. $this->options = array_merge($this->getDefaultOptions(), $options);
  139. }
  140. /**
  141. * @return array
  142. */
  143. public function getOptions()
  144. {
  145. return $this->options;
  146. }
  147. /**
  148. * @param $value
  149. * @return void
  150. */
  151. public function setValue($value)
  152. {
  153. $this->value = $value;
  154. }
  155. /**
  156. * @return mixed
  157. */
  158. public function getValue()
  159. {
  160. return $this->value;
  161. }
  162. /**
  163. * @return string
  164. */
  165. public function isActive()
  166. {
  167. $values = $this->getValue();
  168. return ! empty($values['value']);
  169. }
  170. /**
  171. * @param $condition
  172. * @return void
  173. */
  174. public function setCondition($condition)
  175. {
  176. $this->condition = $condition;
  177. }
  178. /**
  179. * @return
  180. */
  181. public function getCondition()
  182. {
  183. return $this->condition;
  184. }
  185. }