This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
/
SD_FilterValue.php
120 lines (110 loc) · 3.52 KB
/
SD_FilterValue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Defines a class, SDFilterValue, representing a single value of an
* applied filter (i.e., an instance of the SDAppliedFilter class).
*
* @author Yaron Koren
*/
class SDFilterValue {
var $text;
var $is_none = false;
var $is_other = false;
var $is_numeric = false;
var $lower_limit = null;
var $upper_limit = null;
var $year = null;
var $month = null;
var $day = null;
var $end_year = null;
static function create( $actual_val, $filter = null ) {
$fv = new SDFilterValue();
$fv->text = str_replace( '_', ' ', $actual_val );
if ( $fv->text == ' none' ) {
$fv->is_none = true;
} elseif ( $fv->text == ' other' ) {
$fv->is_other = true;
}
// set other fields, if it's a date or number range
if ( $filter != null && $filter->property_type == 'date' ) {
// @TODO - this should ideally be handled via query
// string arrays - and this code merged in with
// date-range handling - instead of just doing string
// parsing on one string.
if ( strpos( $fv->text, ' - ' ) > 0 ) {
// If there's a dash, assume it's a year range
$years = explode( ' - ', $fv->text );
$fv->year = $years[0];
$fv->end_year = $years[1];
$fv->time_period = 'year range';
} else {
$date_parts = explode( ' ', $fv->text );
if ( count( $date_parts ) == 3 ) {
list( $month_str, $day_str, $year ) = explode( ' ', $fv->text );
$fv->month = SDUtils::stringToMonth( $month_str );
$fv->day = str_replace( ',', '', $day_str );
$fv->year = $year;
$fv->time_period = 'day';
} elseif ( count( $date_parts ) == 2 ) {
list( $month_str, $year ) = explode( ' ', $fv->text );
$fv->month = SDUtils::stringToMonth( $month_str );
$fv->year = $year;
$fv->time_period = 'month';
} else {
$fv->month = null;
$fv->year = $fv->text;
$fv->time_period = 'year';
}
}
} else {
if ( $fv->text == '' ) {
// do nothing
} elseif ( $fv->text { 0 } == '<' ) {
$possible_number = str_replace( ',', '', trim( substr( $fv->text, 1 ) ) );
if ( is_numeric( $possible_number ) ) {
$fv->upper_limit = $possible_number;
$fv->is_numeric = true;
}
} elseif ( $fv->text { 0 } == '>' ) {
$possible_number = str_replace( ',', '', trim( substr( $fv->text, 1 ) ) );
if ( is_numeric( $possible_number ) ) {
$fv->lower_limit = $possible_number;
$fv->is_numeric = true;
}
} else {
$elements = explode( '-', $fv->text );
if ( count( $elements ) == 2 ) {
$first_elem = str_replace( ',', '', trim( $elements[0] ) );
$second_elem = str_replace( ',', '', trim( $elements[1] ) );
if ( is_numeric( $first_elem ) && is_numeric( $second_elem ) ) {
$fv->lower_limit = $first_elem;
$fv->upper_limit = $second_elem;
$fv->is_numeric = true;
}
}
}
}
return $fv;
}
/**
* Used in sorting, when BrowseDataPage creates a new URL.
*/
public static function compare( $fv1, $fv2 ) {
if ( $fv1->is_none ) return 1;
if ( $fv2->is_none ) return - 1;
if ( $fv1->is_other ) return 1;
if ( $fv2->is_other ) return - 1;
if ( $fv1->year != null && $fv2->year != null ) {
if ( $fv1->year == $fv2->year ) {
if ( $fv1->month == $fv1->month ) return 0;
return ( $fv1->month > $fv2->month ) ? 1 : - 1;
}
return ( $fv1->year > $fv2->year ) ? 1 : - 1;
}
if ( $fv1->is_numeric ) {
if ( $fv1->lower_limit == null ) return - 1;
return ( $fv1->lower_limit > $fv2->lower_limit ) ? 1 : - 1;
}
if ( $fv1->text == $fv2->text ) return 0;
return ( $fv1->text > $fv2->text ) ? 1 : - 1;
}
}