1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Class DrupalDBStore |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Roomify\Bat\Store; |
9
|
|
|
|
10
|
|
|
use Roomify\Bat\Event\Event; |
11
|
|
|
use Roomify\Bat\Store\Store; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is a Drupal-specific implementation of the Store. |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class DrupalDBStore extends Store { |
18
|
|
|
|
19
|
|
|
// There are two types of stores - for event ids and status |
20
|
|
|
const BAT_EVENT = 'event'; |
21
|
|
|
const BAT_STATE = 'state'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The table that holds day data. |
25
|
|
|
* @var |
26
|
|
|
*/ |
27
|
|
|
public $day_table; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The table that holds hour data. |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
public $hour_table; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The table that holds minute data. |
37
|
|
|
* @var |
38
|
|
|
*/ |
39
|
|
|
public $minute_table; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The event type we are dealing with. |
43
|
|
|
* @var |
44
|
|
|
*/ |
45
|
|
|
public $event_type; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* DrupalDBStore constructor. |
50
|
|
|
* |
51
|
|
|
* Provided with the event type it will determine the appropriate table names to |
52
|
|
|
* store data in. This assumes standard behaviour from Bat_Event |
53
|
|
|
* @param $event_type |
54
|
|
|
* @param string $event_data |
55
|
|
|
*/ |
56
|
|
|
public function __construct($event_type, $event_data = 'state') { |
57
|
|
|
|
58
|
|
|
$this->event_type = $event_type; |
59
|
|
|
|
60
|
|
View Code Duplication |
if ($event_data == DrupalDBStore::BAT_STATE) { |
|
|
|
|
61
|
|
|
$this->day_table = 'bat_event_' . $event_type . '_day_' . DrupalDBStore::BAT_STATE; |
62
|
|
|
$this->hour_table = 'bat_event_' . $event_type . '_hour_' . DrupalDBStore::BAT_STATE; |
63
|
|
|
$this->minute_table = 'bat_event_' . $event_type . '_minute_' . DrupalDBStore::BAT_STATE; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
if ($event_data == DrupalDBStore::BAT_EVENT) { |
|
|
|
|
67
|
|
|
$this->day_table = 'bat_event_' . $event_type . '_day_' . DrupalDBStore::BAT_EVENT; |
68
|
|
|
$this->hour_table = 'bat_event_' . $event_type . '_hour_' . DrupalDBStore::BAT_EVENT; |
69
|
|
|
$this->minute_table = 'bat_event_' . $event_type . '_minute_' . DrupalDBStore::BAT_EVENT; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
* @param \DateTime $start_date |
77
|
|
|
* @param \DateTime $end_date |
78
|
|
|
* @param $unit_ids |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) { |
83
|
|
|
|
84
|
|
|
$queries = $this->buildQueries($start_date, $end_date, $unit_ids); |
85
|
|
|
|
86
|
|
|
$results = array(); |
87
|
|
|
// Run each query and store results |
88
|
|
|
foreach ($queries as $type => $query) { |
89
|
|
|
$results[$type] = db_query($query); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$db_events = array(); |
93
|
|
|
|
94
|
|
|
// Cycle through day results and setup an event array |
95
|
|
|
while( $data = $results[Event::BAT_DAY]->fetchAssoc()) { |
96
|
|
|
// Figure out how many days the current month has |
97
|
|
|
$temp_date = new \DateTime($data['year'] . "-" . $data['month']); |
98
|
|
|
$days_in_month = (int)$temp_date->format('t'); |
99
|
|
View Code Duplication |
for ($i = 1; $i<=$days_in_month; $i++) { |
|
|
|
|
100
|
|
|
$db_events[$data['unit_id']][Event::BAT_DAY][$data['year']][$data['month']]['d' . $i] = $data['d'.$i]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// With the day events taken care off let's cycle through hours |
105
|
|
|
while( $data = $results[Event::BAT_HOUR]->fetchAssoc()) { |
106
|
|
View Code Duplication |
for ($i = 0; $i<=23; $i++) { |
|
|
|
|
107
|
|
|
$db_events[$data['unit_id']][Event::BAT_HOUR][$data['year']][$data['month']][$data['day']]['h'. $i] = $data['h'.$i]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// With the hour events taken care off let's cycle through minutes |
112
|
|
|
while( $data = $results[Event::BAT_MINUTE]->fetchAssoc()) { |
113
|
|
|
for ($i = 0; $i<=59; $i++) { |
114
|
|
|
if ($i <= 9) { |
115
|
|
|
$index = 'm0'.$i; |
116
|
|
|
} |
117
|
|
|
else { |
118
|
|
|
$index = 'm'.$i; |
119
|
|
|
} |
120
|
|
|
$db_events[$data['unit_id']][Event::BAT_MINUTE][$data['year']][$data['month']][$data['day']][$data['hour']][$index] = $data[$index]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $db_events; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param \Roomify\Bat\Event\Event $event |
129
|
|
|
* @param $granularity |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) { |
134
|
|
|
$stored = TRUE; |
135
|
|
|
$transaction = db_transaction(); |
136
|
|
|
|
137
|
|
|
try { |
138
|
|
|
// Itemize an event so we can save it |
139
|
|
|
$itemized = $event->itemizeEvent($granularity); |
140
|
|
|
|
141
|
|
|
//Write days |
142
|
|
|
foreach ($itemized[Event::BAT_DAY] as $year => $months) { |
143
|
|
|
foreach ($months as $month => $days) { |
144
|
|
|
db_merge($this->day_table) |
145
|
|
|
->key(array( |
146
|
|
|
'unit_id' => $event->unit_id, |
147
|
|
|
'year' => $year, |
148
|
|
|
'month' => $month |
149
|
|
|
)) |
150
|
|
|
->fields($days) |
151
|
|
|
->execute(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($granularity == Event::BAT_HOURLY) { |
156
|
|
|
// Write Hours |
157
|
|
View Code Duplication |
foreach ($itemized[Event::BAT_HOUR] as $year => $months) { |
|
|
|
|
158
|
|
|
foreach ($months as $month => $days) { |
159
|
|
|
foreach ($days as $day => $hours) { |
160
|
|
|
// Count required as we may receive empty hours for granular events that start and end on midnight |
161
|
|
|
if (count($hours) > 0) { |
162
|
|
|
db_merge($this->hour_table) |
163
|
|
|
->key(array( |
164
|
|
|
'unit_id' => $event->unit_id, |
165
|
|
|
'year' => $year, |
166
|
|
|
'month' => $month, |
167
|
|
|
'day' => substr($day, 1) |
168
|
|
|
)) |
169
|
|
|
->fields($hours) |
170
|
|
|
->execute(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
//If we have minutes write minutes |
177
|
|
|
foreach ($itemized[Event::BAT_MINUTE] as $year => $months) { |
178
|
|
View Code Duplication |
foreach ($months as $month => $days) { |
|
|
|
|
179
|
|
|
foreach ($days as $day => $hours) { |
180
|
|
|
foreach ($hours as $hour => $minutes) { |
181
|
|
|
db_merge($this->minute_table) |
182
|
|
|
->key(array( |
183
|
|
|
'unit_id' => $event->unit_id, |
184
|
|
|
'year' => $year, |
185
|
|
|
'month' => $month, |
186
|
|
|
'day' => substr($day, 1), |
187
|
|
|
'hour' => substr($hour, 1) |
188
|
|
|
)) |
189
|
|
|
->fields($minutes) |
190
|
|
|
->execute(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
catch (\Exception $e) { |
198
|
|
|
$saved = FALSE; |
|
|
|
|
199
|
|
|
$transaction->rollback(); |
200
|
|
|
watchdog_exception('BAT Event Save Exception', $e); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $stored; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param \DateTime $start_date |
208
|
|
|
* @param \DateTime $end_date |
209
|
|
|
* @param $unit_ids |
210
|
|
|
* |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
public function buildQueries(\DateTime $start_date, \DateTime $end_date, $unit_ids) { |
214
|
|
|
$queries = array(); |
215
|
|
|
|
216
|
|
|
$queries[Event::BAT_DAY] = 'SELECT * FROM ' . $this->day_table . ' WHERE '; |
217
|
|
|
$queries[Event::BAT_HOUR] = 'SELECT * FROM ' . $this->hour_table . ' WHERE '; |
218
|
|
|
$queries[Event::BAT_MINUTE] = 'SELECT * FROM ' . $this->minute_table . ' WHERE '; |
219
|
|
|
|
220
|
|
|
$hours_query = TRUE; |
|
|
|
|
221
|
|
|
$minutes_query = TRUE; |
|
|
|
|
222
|
|
|
|
223
|
|
|
// Create a mock event which we will use to determine how to query the database |
224
|
|
|
$mock_event = new Event($start_date, $end_date, 0, -10); |
225
|
|
|
// We don't need a granular event even if we are retrieving granular data - since we don't |
226
|
|
|
// know what the event break-down is going to be we need to get the full range of data from |
227
|
|
|
// days, hours and minutes. |
228
|
|
|
$itemized = $mock_event->itemizeEvent(Event::BAT_DAILY); |
229
|
|
|
|
230
|
|
|
$year_count = 0; |
231
|
|
|
$hour_count = 0; |
|
|
|
|
232
|
|
|
$minute_count = 0; |
|
|
|
|
233
|
|
|
|
234
|
|
|
$query_parameters = ''; |
235
|
|
|
|
236
|
|
|
foreach($itemized[Event::BAT_DAY] as $year => $months) { |
237
|
|
|
if ($year_count > 0) { |
238
|
|
|
// We are dealing with multiple years so add an OR |
239
|
|
|
$query_parameters .= ' OR '; |
240
|
|
|
} |
241
|
|
|
$query_parameters .= 'year IN (' . $year . ') '; |
242
|
|
|
$query_parameters .= 'AND month IN (' . implode("," ,array_keys($months)) .') '; |
243
|
|
|
if (count($unit_ids) > 0) { |
244
|
|
|
// Unit ids are defined so add this as a filter |
245
|
|
|
$query_parameters .= 'AND unit_id in (' . implode("," , $unit_ids) .') '; |
246
|
|
|
} |
247
|
|
|
$year_count++; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// Add parameters to each query |
251
|
|
|
$queries[Event::BAT_DAY] .= $query_parameters; |
252
|
|
|
$queries[Event::BAT_HOUR] .= $query_parameters; |
253
|
|
|
$queries[Event::BAT_MINUTE] .= $query_parameters; |
254
|
|
|
|
255
|
|
|
// Clean up and add ordering information |
256
|
|
|
$queries[Event::BAT_DAY] .= ' ORDER BY unit_id, year, month'; |
257
|
|
|
$queries[Event::BAT_HOUR] .= ' ORDER BY unit_id, year, month, day'; |
258
|
|
|
$queries[Event::BAT_MINUTE] .= ' ORDER BY unit_id, year, month, day, hour'; |
259
|
|
|
|
260
|
|
|
return $queries; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.