public function doDataTransfer($data, Form $form)
{
$this->setCurrentActionType(self::ACTION_SNAPSHOT);
// Performs canView permission check by limiting visible projects
$project = $this->getCurrentProject();
if (!$project) {
return $this->project404Response();
}
$dataArchive = null;
// Validate direction.
if ($data['Direction'] == 'get') {
$validEnvs = $this->getCurrentProject()->DNEnvironmentList()->filterByCallback(function ($item) {
return $item->canBackup();
});
} else {
if ($data['Direction'] == 'push') {
$validEnvs = $this->getCurrentProject()->DNEnvironmentList()->filterByCallback(function ($item) {
return $item->canRestore();
});
} else {
throw new LogicException('Invalid direction');
}
}
// Validate $data['EnvironmentID'] by checking against $validEnvs.
$environment = $validEnvs->find('ID', $data['EnvironmentID']);
if (!$environment) {
throw new LogicException('Invalid environment');
}
$this->validateSnapshotMode($data['Mode']);
// Only 'push' direction is allowed an association with an existing archive.
if ($data['Direction'] == 'push' && isset($data['DataArchiveID']) && is_numeric($data['DataArchiveID'])) {
$dataArchive = DNDataArchive::get()->byId($data['DataArchiveID']);
if (!$dataArchive) {
throw new LogicException('Invalid data archive');
}
if (!$dataArchive->canDownload()) {
throw new SS_HTTPResponse_Exception('Not allowed to access archive', 403);
}
}
$transfer = DNDataTransfer::create();
$transfer->EnvironmentID = $environment->ID;
$transfer->Direction = $data['Direction'];
$transfer->Mode = $data['Mode'];
$transfer->DataArchiveID = $dataArchive ? $dataArchive->ID : null;
if ($data['Direction'] == 'push') {
$transfer->setBackupBeforePush(!empty($data['BackupBeforePush']));
}
$transfer->write();
$transfer->start();
return $this->redirect($transfer->Link());
}