/** * Saves (updates or creates) a square. * * @param Square $square * @throws Exception * @return Square */ public function save(Square $square) { $connection = $this->squareTable->getAdapter()->getDriver()->getConnection(); if (!$connection->inTransaction()) { $connection->beginTransaction(); $transaction = true; } else { $transaction = false; } try { if ($square->get('sid')) { /* Update existing square */ /* Determine updated properties */ $updates = array(); foreach ($square->need('updatedProperties') as $property) { $updates[$property] = $square->get($property); } if ($updates) { $this->squareTable->update($updates, array('sid' => $square->get('sid'))); } /* Determine new meta properties */ foreach ($square->need('insertedMetaProperties') as $metaProperty) { $this->squareMetaTable->insert(array('sid' => $square->get('sid'), 'key' => $metaProperty, 'value' => $square->needMeta($metaProperty), 'locale' => $square->getMetaLocale($metaProperty))); } /* Determine updated meta properties */ foreach ($square->need('updatedMetaProperties') as $metaProperty) { $locale = $square->getMetaLocale($metaProperty); $where = array('sid' => $square->get('sid'), 'key' => $metaProperty); if (is_null($locale)) { $where[] = new IsNull('locale'); } else { $where['locale'] = $locale; } $this->squareMetaTable->update(array('value' => $square->needMeta($metaProperty)), $where); } /* Determine removed meta properties */ foreach ($square->need('removedMetaProperties') as $metaProperty) { $this->squareMetaTable->delete(array('sid' => $square->get('sid'), 'key' => $metaProperty, 'locale' => $square->getMetaLocale($metaProperty))); } $square->reset(); $this->getEventManager()->trigger('save.update', $square); } else { /* Insert square */ if ($square->getExtra('nsid')) { $sid = $square->getExtra('nsid'); } else { $sid = null; } $this->squareTable->insert(array('sid' => $sid, 'name' => $square->need('name'), 'status' => $square->get('status', 'enabled'), 'priority' => $square->need('priority'), 'capacity' => $square->need('capacity'), 'capacity_heterogenic' => $square->need('capacity_heterogenic'), 'time_start' => $square->need('time_start'), 'time_end' => $square->need('time_end'), 'time_block' => $square->need('time_block'), 'time_block_bookable' => $square->need('time_block_bookable'), 'time_block_bookable_max' => $square->need('time_block_bookable_max'), 'range_book' => $square->need('range_book'), 'range_cancel' => $square->need('range_cancel'))); $sid = $this->squareTable->getLastInsertValue(); if (!(is_numeric($sid) && $sid > 0)) { throw new RuntimeException('Failed to save square'); } foreach ($square->need('meta') as $key => $value) { $this->squareMetaTable->insert(array('sid' => $sid, 'key' => $key, 'value' => $value)); if (!$this->squareMetaTable->getLastInsertValue()) { throw new RuntimeException(sprintf('Failed to save square meta key "%s"', $key)); } } $square->add('sid', $sid); $this->getEventManager()->trigger('save.insert', $square); } if ($transaction) { $connection->commit(); } $this->getEventManager()->trigger('save', $square); return $square; } catch (Exception $e) { if ($transaction) { $connection->rollback(); } throw $e; } }