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\SqlDBStore; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is a Drupal-specific implementation of the Store. |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class DrupalDBStore extends SqlDBStore { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* @param \DateTime $start_date |
22
|
|
|
* @param \DateTime $end_date |
23
|
|
|
* @param $unit_ids |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_ids) { |
28
|
|
|
|
29
|
|
|
$queries = $this->buildQueries($start_date, $end_date, $unit_ids); |
30
|
|
|
|
31
|
|
|
$results = array(); |
32
|
|
|
// Run each query and store results |
33
|
|
|
foreach ($queries as $type => $query) { |
34
|
|
|
$results[$type] = db_query($query); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$db_events = array(); |
38
|
|
|
|
39
|
|
|
// Cycle through day results and setup an event array |
40
|
|
|
while( $data = $results[Event::BAT_DAY]->fetchAssoc()) { |
41
|
|
|
// Figure out how many days the current month has |
42
|
|
|
$temp_date = new \DateTime($data['year'] . "-" . $data['month']); |
43
|
|
|
$days_in_month = (int)$temp_date->format('t'); |
44
|
|
|
for ($i = 1; $i<=$days_in_month; $i++) { |
45
|
|
|
$db_events[$data['unit_id']][Event::BAT_DAY][$data['year']][$data['month']]['d' . $i] = $data['d'.$i]; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// With the day events taken care off let's cycle through hours |
50
|
|
|
while( $data = $results[Event::BAT_HOUR]->fetchAssoc()) { |
51
|
|
View Code Duplication |
for ($i = 0; $i<=23; $i++) { |
|
|
|
|
52
|
|
|
$db_events[$data['unit_id']][Event::BAT_HOUR][$data['year']][$data['month']][$data['day']]['h'. $i] = $data['h'.$i]; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// With the hour events taken care off let's cycle through minutes |
57
|
|
|
while( $data = $results[Event::BAT_MINUTE]->fetchAssoc()) { |
58
|
|
View Code Duplication |
for ($i = 0; $i<=59; $i++) { |
|
|
|
|
59
|
|
|
if ($i <= 9) { |
60
|
|
|
$index = 'm0'.$i; |
61
|
|
|
} |
62
|
|
|
else { |
63
|
|
|
$index = 'm'.$i; |
64
|
|
|
} |
65
|
|
|
$db_events[$data['unit_id']][Event::BAT_MINUTE][$data['year']][$data['month']][$data['day']][$data['hour']][$index] = $data[$index]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $db_events; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param \Roomify\Bat\Event\Event $event |
74
|
|
|
* @param $granularity |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY) { |
79
|
|
|
$stored = TRUE; |
80
|
|
|
$transaction = db_transaction(); |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
// Itemize an event so we can save it |
84
|
|
|
$itemized = $event->itemizeEvent($granularity); |
85
|
|
|
|
86
|
|
|
//Write days |
87
|
|
|
foreach ($itemized[Event::BAT_DAY] as $year => $months) { |
88
|
|
|
foreach ($months as $month => $days) { |
89
|
|
|
db_merge($this->day_table) |
90
|
|
|
->key(array( |
91
|
|
|
'unit_id' => $event->unit_id, |
|
|
|
|
92
|
|
|
'year' => $year, |
93
|
|
|
'month' => $month |
94
|
|
|
)) |
95
|
|
|
->fields($days) |
96
|
|
|
->execute(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($granularity == Event::BAT_HOURLY) { |
101
|
|
|
// Write Hours |
102
|
|
View Code Duplication |
foreach ($itemized[Event::BAT_HOUR] as $year => $months) { |
|
|
|
|
103
|
|
|
foreach ($months as $month => $days) { |
104
|
|
|
foreach ($days as $day => $hours) { |
105
|
|
|
// Count required as we may receive empty hours for granular events that start and end on midnight |
106
|
|
|
if (count($hours) > 0) { |
107
|
|
|
db_merge($this->hour_table) |
108
|
|
|
->key(array( |
109
|
|
|
'unit_id' => $event->unit_id, |
|
|
|
|
110
|
|
|
'year' => $year, |
111
|
|
|
'month' => $month, |
112
|
|
|
'day' => substr($day, 1) |
113
|
|
|
)) |
114
|
|
|
->fields($hours) |
115
|
|
|
->execute(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
//If we have minutes write minutes |
122
|
|
|
foreach ($itemized[Event::BAT_MINUTE] as $year => $months) { |
123
|
|
View Code Duplication |
foreach ($months as $month => $days) { |
|
|
|
|
124
|
|
|
foreach ($days as $day => $hours) { |
125
|
|
|
foreach ($hours as $hour => $minutes) { |
126
|
|
|
db_merge($this->minute_table) |
127
|
|
|
->key(array( |
128
|
|
|
'unit_id' => $event->unit_id, |
|
|
|
|
129
|
|
|
'year' => $year, |
130
|
|
|
'month' => $month, |
131
|
|
|
'day' => substr($day, 1), |
132
|
|
|
'hour' => substr($hour, 1) |
133
|
|
|
)) |
134
|
|
|
->fields($minutes) |
135
|
|
|
->execute(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
catch (\Exception $e) { |
143
|
|
|
$saved = FALSE; |
|
|
|
|
144
|
|
|
$transaction->rollback(); |
145
|
|
|
watchdog_exception('BAT Event Save Exception', $e); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $stored; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
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.