Filter.php 3.7 KB

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