@@ -86,9 +86,10 @@ public function getEventData(\DateTime $start_date, \DateTime $end_date, $unit_i
86
86
*/
87
87
public function storeEvent (EventInterface $ event , $ granularity = Event::BAT_HOURLY ) {
88
88
$ stored = TRUE ;
89
+ $ unit_id = $ event ->getUnitId ();
89
90
90
91
// Get existing event data from db
91
- $ existing_events = $ this ->getEventData ($ event ->getStartDate (), $ event ->getEndDate (), array ($ event -> getUnitId () ));
92
+ $ existing_events = $ this ->getEventData ($ event ->getStartDate (), $ event ->getEndDate (), array ($ unit_id ));
92
93
93
94
try {
94
95
// Itemize an event so we can save it
@@ -100,17 +101,20 @@ public function storeEvent(EventInterface $event, $granularity = Event::BAT_HOUR
100
101
$ values = array_values ($ days );
101
102
$ keys = array_keys ($ days );
102
103
// Because SQLite does not have a nice merge first we have to check if a row exists to determine whether to do an insert or an update
103
- if (isset ($ existing_events [$ event -> getUnitId () ][EVENT ::BAT_DAY ][$ year ][$ month ])) {
104
+ if (isset ($ existing_events [$ unit_id ][EVENT ::BAT_DAY ][$ year ][$ month ])) {
104
105
$ command = "UPDATE $ this ->day_table SET " ;
105
106
foreach ($ days as $ day => $ value ) {
107
+ if ($ granularity === Event::BAT_HOURLY ) {
108
+ $ this ->itemizeSplitDay ($ existing_events , $ itemized , $ value , $ unit_id , $ year , $ month , $ day );
109
+ }
106
110
$ command .= "$ day = $ value, " ;
107
111
}
108
112
$ command = rtrim ($ command , ', ' );
109
- $ command .= " WHERE unit_id = " . $ event -> getUnitId () . " AND year = $ year AND month = $ month " ;
113
+ $ command .= " WHERE unit_id = " . $ unit_id . " AND year = $ year AND month = $ month " ;
110
114
$ this ->pdo ->exec ($ command );
111
115
}
112
116
else {
113
- $ this ->pdo ->exec ("INSERT INTO $ this ->day_table (unit_id, year, month, " . implode (', ' , $ keys ) . ") VALUES ( " . $ event -> getUnitId () . ", $ year, $ month, " . implode (', ' , $ values ) . ") " );
117
+ $ this ->pdo ->exec ("INSERT INTO $ this ->day_table (unit_id, year, month, " . implode (', ' , $ keys ) . ") VALUES ( " . $ unit_id . ", $ year, $ month, " . implode (', ' , $ values ) . ") " );
114
118
}
115
119
}
116
120
}
@@ -124,16 +128,22 @@ public function storeEvent(EventInterface $event, $granularity = Event::BAT_HOUR
124
128
if (count ($ hours ) > 0 ) {
125
129
$ values = array_values ($ hours );
126
130
$ keys = array_keys ($ hours );
127
- if (isset ($ existing_events [$ event -> getUnitId () ][EVENT ::BAT_HOUR ][$ year ][$ month ][$ day ])) {
131
+ if (isset ($ existing_events [$ unit_id ][EVENT ::BAT_HOUR ][$ year ][$ month ][$ day ])) {
128
132
$ command = "UPDATE $ this ->hour_table SET " ;
129
133
foreach ($ hours as $ hour => $ value ){
134
+ $ this ->itemizeSplitHour ($ existing_events , $ itemized , $ value , $ unit_id , $ year , $ month , $ day , $ hour );
130
135
$ command .= "$ hour = $ value, " ;
131
136
}
132
137
$ command = rtrim ($ command , ', ' );
133
- $ command .= " WHERE unit_id = " . $ event -> getUnitId () . " AND year = $ year AND month = $ month AND day = " . substr ($ day ,1 );
138
+ $ command .= " WHERE unit_id = " . $ unit_id . " AND year = $ year AND month = $ month AND day = " . substr ($ day ,1 );
134
139
$ this ->pdo ->exec ($ command );
135
140
} else {
136
- $ this ->pdo ->exec ("INSERT INTO $ this ->hour_table (unit_id, year, month, day, " . implode (', ' , $ keys ) . ") VALUES ( " . $ event ->getUnitId () . ", $ year, $ month, " . substr ($ day , 1 ) . ", " . implode (', ' , $ values ) . ") " );
141
+ if (isset ($ existing_events [$ unit_id ][EVENT ::BAT_DAY ][$ year ][$ month ][$ day ])) {
142
+ foreach ($ hours as $ hour => $ value ) {
143
+ $ this ->itemizeSplitHour ($ existing_events , $ itemized , $ value , $ unit_id , $ year , $ month , $ day , $ hour );
144
+ }
145
+ }
146
+ $ this ->pdo ->exec ("INSERT INTO $ this ->hour_table (unit_id, year, month, day, " . implode (', ' , $ keys ) . ") VALUES ( " . $ unit_id . ", $ year, $ month, " . substr ($ day , 1 ) . ", " . implode (', ' , $ values ) . ") " );
137
147
}
138
148
}
139
149
}
@@ -147,16 +157,16 @@ public function storeEvent(EventInterface $event, $granularity = Event::BAT_HOUR
147
157
foreach ($ hours as $ hour => $ minutes ) {
148
158
$ values = array_values ($ minutes );
149
159
$ keys = array_keys ($ minutes );
150
- if (isset ($ existing_events [$ event -> getUnitId () ][EVENT ::BAT_MINUTE ][$ year ][$ month ][$ day ][$ hour ])) {
160
+ if (isset ($ existing_events [$ unit_id ][EVENT ::BAT_MINUTE ][$ year ][$ month ][$ day ][$ hour ])) {
151
161
$ command = "UPDATE $ this ->minute_table SET " ;
152
162
foreach ($ minutes as $ minute => $ value ){
153
163
$ command .= "$ minute = $ value, " ;
154
164
}
155
165
$ command = rtrim ($ command , ', ' );
156
- $ command .= " WHERE unit_id = " . $ event -> getUnitId () . " AND year = $ year AND month = $ month AND day = " . substr ($ day ,1 ) . " AND hour = " . substr ($ hour ,1 );
166
+ $ command .= " WHERE unit_id = " . $ unit_id . " AND year = $ year AND month = $ month AND day = " . substr ($ day ,1 ) . " AND hour = " . substr ($ hour ,1 );
157
167
$ this ->pdo ->exec ($ command );
158
168
} else {
159
- $ this ->pdo ->exec ("INSERT INTO $ this ->minute_table (unit_id, year, month, day, hour, " . implode (', ' , $ keys ) . ") VALUES ( " . $ event -> getUnitId () . ", $ year, $ month, " . substr ($ day , 1 ) . ", " . substr ($ hour , 1 ) . ", " . implode (', ' , $ values ) . ") " );
169
+ $ this ->pdo ->exec ("INSERT INTO $ this ->minute_table (unit_id, year, month, day, hour, " . implode (', ' , $ keys ) . ") VALUES ( " . $ unit_id . ", $ year, $ month, " . substr ($ day , 1 ) . ", " . substr ($ hour , 1 ) . ", " . implode (', ' , $ values ) . ") " );
160
170
}
161
171
}
162
172
}
0 commit comments