1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Provides the view handling functions for fivestar.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
*
|
10
|
*/
|
11
|
class fivestar_views_handler_filter_in_operator extends views_handler_filter_in_operator {
|
12
|
|
13
|
/**
|
14
|
* Initialize handler.
|
15
|
*/
|
16
|
public function init(&$view, &$options) {
|
17
|
parent::init($view, $options);
|
18
|
// We handle empty/not empty ourselves.
|
19
|
$this->definition['allow empty'] = FALSE;
|
20
|
}
|
21
|
|
22
|
/**
|
23
|
* Get the possible options (the number of stars).
|
24
|
*/
|
25
|
public function get_value_options() {
|
26
|
if (isset($this->value_options)) {
|
27
|
return;
|
28
|
}
|
29
|
|
30
|
// Get field info to figure out the number of stars configured for it.
|
31
|
$field_name = $this->definition['field_name'];
|
32
|
$field_info = field_info_field($field_name);
|
33
|
$bundles = reset($field_info['bundles']);
|
34
|
$entity_type = key($field_info['bundles']);
|
35
|
$bundle_name = $bundles[0];
|
36
|
$field_instance = field_info_instance($entity_type, $field_name, $bundle_name);
|
37
|
|
38
|
$max_stars = !empty($field_instance['settings']['stars']) ? $field_instance['settings']['stars'] : 5;
|
39
|
$options = array(0 => t('No stars'));
|
40
|
for ($i = 1; $i <= $max_stars; $i++) {
|
41
|
$percentage = ceil($i * 100 / $max_stars);
|
42
|
$options[$percentage] = format_plural($i, '1 star', '@count stars');
|
43
|
}
|
44
|
$this->value_options = $options;
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Operator callback.
|
49
|
*/
|
50
|
public function op_simple() {
|
51
|
if (empty($this->value)) {
|
52
|
return;
|
53
|
}
|
54
|
$this->ensure_my_table();
|
55
|
|
56
|
// Zero needs special handling.
|
57
|
$has_nul = in_array(0, $this->value);
|
58
|
|
59
|
$or_statement = db_or();
|
60
|
$and_statement = db_and();
|
61
|
// Add regular condition if we have any values other than zero.
|
62
|
if (count($this->value) > ($has_nul ? 1 : 0)) {
|
63
|
$or_statement->condition("$this->table_alias.$this->real_field", array_values($this->value), $this->operator);
|
64
|
}
|
65
|
|
66
|
$selected_val_count = count(array_values($this->value));
|
67
|
$first_element = reset($this->value);
|
68
|
// 'IN' Condition.
|
69
|
if ($this->operator == 'in') {
|
70
|
if ($selected_val_count == 1) {
|
71
|
if ($first_element == 0) {
|
72
|
$nul_operator = ($has_nul == 0) ? 'IS NOT NULL' : 'IS NULL';
|
73
|
$or_statement->condition("$this->table_alias.$this->real_field", NULL, $nul_operator);
|
74
|
$this->query->add_where($this->options['group'], $or_statement);
|
75
|
}
|
76
|
elseif ($first_element != 0) {
|
77
|
$operator = 'IN';
|
78
|
$and_statement->condition("$this->table_alias.$this->real_field", array_values($this->value), $operator);
|
79
|
$this->query->add_where($this->options['group'], $and_statement);
|
80
|
}
|
81
|
}
|
82
|
elseif ($selected_val_count > 1) {
|
83
|
if ($first_element == 0) {
|
84
|
$nul_operator = ($has_nul == 0) ? 'IS NOT NULL' : 'IS NULL';
|
85
|
$or_statement->condition("$this->table_alias.$this->real_field", NULL, $nul_operator);
|
86
|
$this->query->add_where($this->options['group'], $or_statement);
|
87
|
}
|
88
|
elseif ($first_element != 0) {
|
89
|
$operator = 'IN';
|
90
|
$and_statement->condition("$this->table_alias.$this->real_field", array_values($this->value), $operator);
|
91
|
$this->query->add_where($this->options['group'], $and_statement);
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
// 'NOT IN' Condition.
|
96
|
elseif ($this->operator == 'not in') {
|
97
|
if ($has_nul == 1 && $first_element == 0) {
|
98
|
if ($selected_val_count == 1) {
|
99
|
$nul_operator = 'IS NOT NULL';
|
100
|
$and_statement->condition("$this->table_alias.$this->real_field", array_values($this->value), $nul_operator);
|
101
|
$this->query->add_where($this->options['group'], $and_statement);
|
102
|
}
|
103
|
elseif ($selected_val_count > 1) {
|
104
|
$operator = 'NOT IN';
|
105
|
$nul_operator = 'IS NOT NULL';
|
106
|
$and_statement->condition("$this->table_alias.$this->real_field", array_values($this->value), $operator);
|
107
|
$and_statement->condition("$this->table_alias.$this->real_field", NULL, $nul_operator);
|
108
|
$this->query->add_where($this->options['group'], $and_statement);
|
109
|
}
|
110
|
}
|
111
|
elseif ($has_nul == 0) {
|
112
|
$nul_operator = 'IS NULL';
|
113
|
$or_statement->condition("$this->table_alias.$this->real_field", NULL, $nul_operator);
|
114
|
$this->query->add_where($this->options['group'], $or_statement);
|
115
|
}
|
116
|
}
|
117
|
}
|
118
|
|
119
|
}
|