public function __invoke(Reservation $reservation, $dateStart = null, $dateEnd = null, $search = null) { $view = $this->getView(); $html = ''; $booking = $reservation->needExtra('booking'); switch ($booking->need('status')) { case 'cancelled': $attr = ' class="gray"'; break; default: $attr = null; break; } $html .= sprintf('<tr %s>', $attr); $html .= sprintf('<td class="status-col right-text">%s</td>', $view->t($booking->getStatus())); $html .= sprintf('<td>%s</td>', $booking->need('bid')); if ($booking->getExtra('user')) { $userName = $booking->getExtra('user')->get('alias'); } else { $userName = $booking->need('uid'); } $html .= sprintf('<td><b>%s</b></td>', $userName); /* Date and time col */ $date = new \DateTime($reservation->get('date')); $fullDate = $view->dateFormat($date, \IntlDateFormatter::FULL); $fullDateParts = explode(', ', $fullDate); $html .= sprintf('<td>%s</td>', $fullDateParts[0]); $html .= sprintf('<td>%s</td>', $view->dateFormat($date, \IntlDateFormatter::MEDIUM)); $html .= sprintf('<td>%s</td>', $view->timeRange($reservation->get('time_start'), $reservation->get('time_end'), '%s to %s')); /* Square col */ if ($booking->get('sid')) { $squareName = $this->squareManager->get($booking->get('sid'))->get('name'); } else { $squareName = '-'; } $html .= sprintf('<td>%s</td>', $squareName); /* Notes col */ $notes = $booking->getMeta('notes'); if ($notes) { if (strlen($notes) > 48) { $notes = substr($notes, 0, 48) . '…'; } $notes = '<span class="small-text">' . $notes . '</span>'; } else { $notes = '-'; } $html .= sprintf('<td class="notes-col">%s</td>', $notes); /* Actions col */ if ($booking->get('status') == 'cancelled') { $html .= sprintf('<td class="actions-col no-print"><a href="%s" class="unlined gray symbolic symbolic-edit">%s</a></td>', $view->url('backend/booking/edit', [], ['query' => ['ds' => $date->format('Y-m-d'), 'ts' => substr($reservation->get('time_start'), 0, 5), 'te' => substr($reservation->get('time_end'), 0, 5), 's' => $booking->get('sid'), 'r' => $reservation->get('rid')]]), $view->t('Edit')); } else { $html .= sprintf('<td class="actions-col no-print"><a href="%s" class="unlined gray symbolic symbolic-edit">%s</a></td>', $view->url('backend/booking/edit', [], ['query' => ['ds' => $date->format('Y-m-d'), 'ts' => substr($reservation->get('time_start'), 0, 5), 'te' => substr($reservation->get('time_end'), 0, 5), 's' => $booking->get('sid'), 'r' => $reservation->get('rid')]]), $view->t('Edit')); } $html .= '</tr>'; return $html; }
/** * Saves (updates or creates) a reservation. * * @param Reservation $reservation * @return Reservation * @throws RuntimeException */ public function save(Reservation $reservation) { $connection = $this->reservationTable->getAdapter()->getDriver()->getConnection(); if (!$connection->inTransaction()) { $connection->beginTransaction(); $transaction = true; } else { $transaction = false; } try { if ($reservation->get('rid')) { /* Update existing reservation */ /* Determine updated properties */ $updates = array(); foreach ($reservation->need('updatedProperties') as $property) { $updates[$property] = $reservation->get($property); } if ($updates) { $this->reservationTable->update($updates, array('rid' => $reservation->get('rid'))); } /* Determine new meta properties */ foreach ($reservation->need('insertedMetaProperties') as $metaProperty) { $this->reservationMetaTable->insert(array('rid' => $reservation->get('rid'), 'key' => $metaProperty, 'value' => $reservation->needMeta($metaProperty))); } /* Determine updated meta properties */ foreach ($reservation->need('updatedMetaProperties') as $metaProperty) { $this->reservationMetaTable->update(array('value' => $reservation->needMeta($metaProperty)), array('rid' => $reservation->get('rid'), 'key' => $metaProperty)); } /* Determine removed meta properties */ foreach ($reservation->need('removedMetaProperties') as $metaProperty) { $this->reservationMetaTable->delete(array('rid' => $reservation->get('rid'), 'key' => $metaProperty)); } $reservation->reset(); $this->getEventManager()->trigger('save.update', $reservation); } else { /* Insert reservation */ if ($reservation->getExtra('nrid')) { $rid = $reservation->getExtra('nrid'); } else { $rid = null; } $this->reservationTable->insert(array('rid' => $rid, 'bid' => $reservation->need('bid'), 'date' => $reservation->need('date'), 'time_start' => $reservation->need('time_start'), 'time_end' => $reservation->need('time_end'))); $rid = $this->reservationTable->getLastInsertValue(); if (!(is_numeric($rid) && $rid > 0)) { throw new RuntimeException('Failed to save reservation'); } foreach ($reservation->need('meta') as $key => $value) { $this->reservationMetaTable->insert(array('rid' => $rid, 'key' => $key, 'value' => $value)); if (!$this->reservationMetaTable->getLastInsertValue()) { throw new RuntimeException(sprintf('Failed to save reservation meta key "%s"', $key)); } } $reservation->add('rid', $rid); $this->getEventManager()->trigger('save.insert', $reservation); } if ($transaction) { $connection->commit(); } $this->getEventManager()->trigger('save', $reservation); return $reservation; } catch (Exception $e) { if ($transaction) { $connection->rollback(); } throw $e; } }