public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY)
{
$stored = TRUE;
$transaction = db_transaction();
try {
// Itemize an event so we can save it
$itemized = $event->itemize(new EventItemizer($event, $granularity));
//Write days
foreach ($itemized[Event::BAT_DAY] as $year => $months) {
foreach ($months as $month => $days) {
db_merge($this->day_table_no_prefix)->key(array('unit_id' => $event->getUnitId(), 'year' => $year, 'month' => $month))->fields($days)->execute();
}
}
if ($granularity == Event::BAT_HOURLY && isset($itemized[Event::BAT_HOUR])) {
// Write Hours
foreach ($itemized[Event::BAT_HOUR] as $year => $months) {
foreach ($months as $month => $days) {
foreach ($days as $day => $hours) {
// Count required as we may receive empty hours for granular events that start and end on midnight
if (count($hours) > 0) {
db_merge($this->hour_table_no_prefix)->key(array('unit_id' => $event->getUnitId(), 'year' => $year, 'month' => $month, 'day' => substr($day, 1)))->fields($hours)->execute();
}
}
}
}
//If we have minutes write minutes
foreach ($itemized[Event::BAT_MINUTE] as $year => $months) {
foreach ($months as $month => $days) {
foreach ($days as $day => $hours) {
foreach ($hours as $hour => $minutes) {
db_merge($this->minute_table_no_prefix)->key(array('unit_id' => $event->getUnitId(), 'year' => $year, 'month' => $month, 'day' => substr($day, 1), 'hour' => substr($hour, 1)))->fields($minutes)->execute();
}
}
}
}
}
} catch (\Exception $e) {
$stored = FALSE;
$transaction->rollback();
watchdog_exception('BAT Event Save Exception', $e);
}
return $stored;
}