public function exportData($export, $reporter)
{
$this->conn->beginTransaction();
try {
$lastExportedAt = (int) $export['lastExportedAt'];
$areas = $this->conn->fetchAll('SELECT a.`id`, a.`name`, t.`id` AS `territoryId`, t.`name` AS `territoryName`, a.`customData`, a.`lastUpdatedAt` ' . 'FROM `' . CoreTables::AREA_TBL . '` a ' . 'INNER JOIN `' . CoreTables::TERRITORY_TBL . '` t ON t.`id` = a.`territoryId` ' . 'WHERE a.`projectId` = :projectId AND a.`statusId` = :statusId', [':projectId' => $export['projectId'], ':statusId' => $export['areaStatusId']]);
$block = new ExportBlock();
foreach ($areas as $area) {
$block->addId($area['id']);
if ($area['lastUpdatedAt'] > $lastExportedAt) {
$area['customData'] = json_decode($area['customData']);
$block->addUpdatedId($area['id']);
$block->addUpdate($area);
}
}
$event = new ExportEvent($export['projectId'], $export['lastExportedAt'], $reporter);
$event->addBlock('area', $block);
$event = $this->eventDispatcher->dispatch(ExportEvents::EXPORT_ONGOING, $event);
$this->conn->executeQuery('UPDATE `' . ExportTables::DATA_EXPORT_TBL . '` SET `lastExportedAt` = :time WHERE `id` = :id', [':time' => time(), ':id' => $export['id']]);
$this->conn->commit();
return $event->output();
} catch (Exception $ex) {
$this->conn->rollBack();
throw $ex;
}
}