Esempio n. 1
0
 /**
  * @param \Roomify\Bat\Event\Event $event
  * @param $granularity
  *
  * @return bool
  */
 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;
 }
Esempio n. 2
0
 /**
  * @param \Roomify\Bat\Event\Event $event
  * @param $granularity
  *
  * @return bool
  */
 public function storeEvent(Event $event, $granularity = Event::BAT_HOURLY)
 {
     $stored = TRUE;
     // Get existing event data from db
     $existing_events = $this->getEventData($event->getStartDate(), $event->getEndDate(), array($event->getUnitId()));
     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) {
                 $values = array_values($days);
                 $keys = array_keys($days);
                 // 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
                 if (isset($existing_events[$event->getUnitId()][EVENT::BAT_DAY][$year][$month])) {
                     $command = "UPDATE {$this->day_table} SET ";
                     foreach ($days as $day => $value) {
                         $command .= "{$day} = {$value},";
                     }
                     $command = rtrim($command, ',');
                     $command .= " WHERE unit_id = " . $event->getUnitId() . " AND year = {$year} AND month = {$month}";
                     $this->pdo->exec($command);
                 } else {
                     $this->pdo->exec("INSERT INTO {$this->day_table} (unit_id, year, month, " . implode(', ', $keys) . ") VALUES (" . $event->getUnitId() . ", {$year}, {$month}, " . implode(', ', $values) . ")");
                 }
             }
         }
         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) {
                             $values = array_values($hours);
                             $keys = array_keys($hours);
                             if (isset($existing_events[$event->getUnitId()][EVENT::BAT_HOUR][$year][$month][$day])) {
                                 $command = "UPDATE {$this->hour_table} SET ";
                                 foreach ($hours as $hour => $value) {
                                     $command .= "{$hour} = {$value},";
                                 }
                                 $command = rtrim($command, ',');
                                 $command .= " WHERE unit_id = " . $event->getUnitId() . " AND year = {$year} AND month = {$month} AND day = " . substr($day, 1);
                                 $this->pdo->exec($command);
                             } else {
                                 $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) . ")");
                             }
                         }
                     }
                 }
             }
             // 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) {
                             $values = array_values($minutes);
                             $keys = array_keys($minutes);
                             if (isset($existing_events[$event->getUnitId()][EVENT::BAT_MINUTE][$year][$month][$day][$hour])) {
                                 $command = "UPDATE {$this->minute_table} SET ";
                                 foreach ($minutes as $minute => $value) {
                                     $command .= "{$minute} = {$value},";
                                 }
                                 $command = rtrim($command, ',');
                                 $command .= " WHERE unit_id = " . $event->getUnitId() . " AND year = {$year} AND month = {$month} AND day = " . substr($day, 1) . " AND hour = " . substr($hour, 1);
                                 $this->pdo->exec($command);
                             } else {
                                 $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) . ")");
                             }
                         }
                     }
                 }
             }
         }
     } catch (\Exception $e) {
         $stored = FALSE;
     }
     return $stored;
 }