1 <?php
2 3 4 5
6
7 8 9 10 11 12 13 14
15 class WithinRangeFilter extends SearchFilter {
16
17 private $min;
18 private $max;
19
20 function setMin($min) {
21 $this->min = $min;
22 }
23
24 function setMax($max) {
25 $this->max = $max;
26 }
27
28 function setValue($val) {
29 if(is_string($val) && strpos($val, ',') !== false) {
30 list($min, $max) = explode(',', $val);
31 $this->setMin($min);
32 $this->setMax($max);
33 }
34 elseif(is_array($val) && array_key_exists('min', $val) && array_key_exists('max', $val)) {
35 $this->setMin($val['min']);
36 $this->setMax($val['max']);
37 } else {
38 $this->setMin($val);
39 $this->setMax($val);
40 }
41
42 }
43
44 function apply(SQLQuery $query) {
45 if ($this->min) {
46 $query->where(sprintf(
47 "%s >= '%s'",
48 $this->getDbName(),
49 Convert::raw2sql($this->min)
50 ));
51 }
52 if ($this->max) {
53 $query->where(sprintf(
54 "%s <= '%s'",
55 $this->getDbName(),
56 Convert::raw2sql($this->max)
57 ));
58 }
59 }
60
61 }
62
63 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-