From 5e21857d67e55bbc6e9987d10b5dcf0ec2625247 Mon Sep 17 00:00:00 2001 From: Mark Friedrich Date: Fri, 6 Jul 2018 23:20:41 +0200 Subject: [PATCH] - minor fixes and improvements to the "map import" function --- app/main/controller/api/map.php | 171 ++++++++++-------- app/main/model/mapmodel.php | 1 - js/app/ui/dialog/map_settings.js | 10 +- .../js/v1.3.6/app/ui/dialog/map_settings.js | 10 +- 4 files changed, 107 insertions(+), 85 deletions(-) diff --git a/app/main/controller/api/map.php b/app/main/controller/api/map.php index a5080f6b..ab468405 100644 --- a/app/main/controller/api/map.php +++ b/app/main/controller/api/map.php @@ -248,99 +248,115 @@ class Map extends Controller\AccessController { $map = Model\BasicModel::getNew('MapModel'); /** - * @var $system Model\SystemModel + * @var $mapType Model\MapTypeModel */ - $system = Model\BasicModel::getNew('SystemModel'); - $system->setActivityLogging(false); + $mapType = Model\BasicModel::getNew('MapTypeModel'); + $mapType->getById((int)$importData['typeId']); - /** - * @var $connection Model\ConnectionModel - */ - $connection = Model\BasicModel::getNew('ConnectionModel'); - $connection->setActivityLogging(false); - - // to many systems for import - $mapTypeModel = Model\BasicModel::getNew('MapTypeModel'); - $mapTypeModel->getById( (int)$importData['typeId'] ); - - if( !$mapTypeModel->dry() ){ - $defaultConfig = Config::getMapsDefaultConfig($mapTypeModel->name); + if( !$mapType->dry() ){ + $defaultConfig = Config::getMapsDefaultConfig($mapType->name); foreach($importData['mapData'] as $mapData){ if( isset($mapData['config']) && isset($mapData['data']) ){ + $mapDataConfig = (array)$mapData['config']; + $mapDataData = (array)$mapData['data']; - if( - isset($mapData['data']['systems']) && - isset($mapData['data']['connections']) - ){ - $systemCount = count($mapData['data']['systems']); - if( $systemCount <= $defaultConfig['max_systems']){ + /** + * @var $mapScope Model\MapScopeModel + */ + $mapScope = Model\BasicModel::getNew('MapScopeModel'); + $mapScope->getById((int)$mapDataConfig['scope']['id']); - $map->setData($mapData['config']); - $map->typeId = (int)$importData['typeId']; - $map->save($activeCharacter); + if( !$mapScope->dry() ){ + if( + isset($mapDataData['systems']) && + isset($mapDataData['connections']) + ){ + $mapDataSystems = (array)$mapDataData['systems']; + $mapDataConnections = (array)$mapDataData['connections']; + $systemCount = count($mapDataSystems); + if( $systemCount <= $defaultConfig['max_systems']){ - // new system IDs will be generated - // therefore we need to temp store a mapping between IDs - $tempSystemIdMapping = []; + $map->copyfrom($mapDataConfig, ['name', 'icon', 'position', 'locked', 'rallyUpdated', 'rallyPoke']); + $map->typeId = $mapType; + $map->scopeId = $mapScope; + $map->save($activeCharacter); - foreach($mapData['data']['systems'] as $systemData){ - if(isset($systemData['id'])){ - $oldId = (int)$systemData['id']; + // new system IDs will be generated + // therefore we need to temp store a mapping between IDs + $tempSystemIdMapping = []; - $system->setData($systemData); - $system->mapId = $map; - $system->save($activeCharacter); + foreach($mapDataSystems as $systemData){ + if( + ($oldId = (int)$systemData['id']) && + ($systemId = (int)$systemData['systemId']) + ){ + $system = $map->getNewSystem($systemId); + $system->copyfrom($systemData, ['alias', 'status', 'locked', 'rallyUpdated', 'rallyPoke', 'position']); + $system = $map->saveSystem($system, $activeCharacter, $system->posX, $system->posY); - $tempSystemIdMapping[$oldId] = $system->id; - $system->reset(); + $tempSystemIdMapping[$oldId] = $system->_id; + } } - } - foreach($mapData['data']['connections'] as $connectionData){ - // check if source and target IDs match with new system ID - if( - isset( $tempSystemIdMapping[$connectionData['source']] ) && - isset( $tempSystemIdMapping[$connectionData['target']] ) - ){ - $connection->setData($connectionData); - $connection->mapId = $map; - $connection->source = $tempSystemIdMapping[$connectionData['source']]; - $connection->target = $tempSystemIdMapping[$connectionData['target']]; - $connection->save($activeCharacter); + /** + * @var $connection Model\ConnectionModel + */ + $connection = Model\BasicModel::getNew('ConnectionModel'); + $connection->setActivityLogging(false); - $connection->reset(); - } - } + foreach($mapDataConnections as $connectionData){ + // check if source and target IDs match with new system ID + if( + ($sourceSystemId = $tempSystemIdMapping[(int)$connectionData['source']]) && + ($targetSystemId = $tempSystemIdMapping[(int)$connectionData['target']]) + ){ + $connection->source = $sourceSystemId; + $connection->target = $targetSystemId; + $connection->copyfrom($connectionData, ['scope', 'type']); + $map->saveConnection($connection, $activeCharacter); - // map access info should not automatically imported - if($map->isPrivate()){ - $map->setAccess($activeCharacter); - }elseif($map->isCorporation()){ - if($corporation = $activeCharacter->getCorporation()){ - $map->setAccess($corporation); + $connection->reset(); + } } - }elseif($map->isAlliance()){ - if($alliance = $activeCharacter->getAlliance()){ - $map->setAccess($alliance); + + // map access info should not automatically imported + if($map->isPrivate()){ + $map->setAccess($activeCharacter); + }elseif($map->isCorporation()){ + if($corporation = $activeCharacter->getCorporation()){ + $map->setAccess($corporation); + } + }elseif($map->isAlliance()){ + if($alliance = $activeCharacter->getAlliance()){ + $map->setAccess($alliance); + } } + + // broadcast map Access -> and send map Data + $this->broadcastMapAccess($map); + }else{ + $maxSystemsError = (object) []; + $maxSystemsError->type = 'error'; + $maxSystemsError->message = 'Map has to many systems (' . $systemCount . ').' + .' Max system count is ' . $defaultConfig['max_systems'] . ' for ' . $mapType->name . ' maps.'; + $return->error[] = $maxSystemsError; } }else{ - $maxSystemsError = (object) []; - $maxSystemsError->type = 'error'; - $maxSystemsError->message = 'Map has to many systems (' . $systemCount . ').' - .' Max system count is ' . $defaultConfig['max_systems'] . ' for ' . $mapTypeModel->name . ' maps.'; - $return->error[] = $maxSystemsError; + // systems || connections missing + $missingConfigError = (object) []; + $missingConfigError->type = 'error'; + $missingConfigError->message = 'Map data not valid (systems || connections) missing'; + $return->error[] = $missingConfigError; } }else{ - // systems || connections missing - $missingConfigError = (object) []; - $missingConfigError->type = 'error'; - $missingConfigError->message = 'Map data not valid (systems || connections) missing'; - $return->error[] = $missingConfigError; + $unknownMapScope= (object) []; + $unknownMapScope->type = 'error'; + $unknownMapScope->message = 'Map scope unknown!'; + $return->error[] = $unknownMapScope; } }else{ // map config || systems/connections missing @@ -530,14 +546,8 @@ class Map extends Controller\AccessController { // this makes sure all data is up2date $map->getById( $map->_id, 0 ); - - $charactersData = $map->getCharactersData(); - $characterIds = array_map(function ($data){ - return $data->id; - }, $charactersData); - // broadcast map Access -> and send map Data - $this->broadcastMapAccess($map, $characterIds); + $this->broadcastMapAccess($map); $return->mapData = $map->getData(); }catch(Exception\ValidationException $e){ @@ -547,7 +557,6 @@ class Map extends Controller\AccessController { $validationError->message = $e->getMessage(); $return->error[] = $validationError; } - }else{ // map access denied $captchaError = (object) []; @@ -603,15 +612,17 @@ class Map extends Controller\AccessController { * broadcast characters with map access rights to WebSocket server * -> if characters with map access found -> broadcast mapData to them * @param Model\MapModel $map - * @param array $characterIds * @throws Exception * @throws Exception\PathfinderException * @throws \ZMQSocketException */ - protected function broadcastMapAccess($map, $characterIds){ + protected function broadcastMapAccess(Model\MapModel $map){ + $mapAccess = [ 'id' => $map->_id, - 'characterIds' => $characterIds + 'characterIds' => array_map(function ($data){ + return $data->id; + }, $map->getCharactersData()) ]; (new Socket( Config::getSocketUri() ))->sendData('mapAccess', $mapAccess); diff --git a/app/main/model/mapmodel.php b/app/main/model/mapmodel.php index 3d73143c..c208d51f 100644 --- a/app/main/model/mapmodel.php +++ b/app/main/model/mapmodel.php @@ -8,7 +8,6 @@ namespace Model; -use Controller\Api\System; use DB\SQL\Schema; use data\file\FileHandler; use lib\Config; diff --git a/js/app/ui/dialog/map_settings.js b/js/app/ui/dialog/map_settings.js index 8b1f8af0..faffb9f7 100644 --- a/js/app/ui/dialog/map_settings.js +++ b/js/app/ui/dialog/map_settings.js @@ -541,7 +541,9 @@ define([ filesCount === files.length && filesCountFail === 0 ){ - importMaps(importData); + importMaps(importData, () => { + mapInfoDialog.modal('hide'); + }); } }; @@ -651,7 +653,7 @@ define([ * import new map(s) data * @param importData */ - let importMaps = function(importData){ + let importMaps = (importData, callback) => { let importForm = $('#' + config.dialogMapImportFormId); importForm.hideFormMessage('all'); @@ -675,6 +677,10 @@ define([ importForm.showFormMessage(responseData.warning); } + if(callback){ + callback(); + } + Util.showNotify({title: 'Import finished', text: 'Map(s) imported', type: 'success'}); } }).fail(function( jqXHR, status, error) { diff --git a/public/js/v1.3.6/app/ui/dialog/map_settings.js b/public/js/v1.3.6/app/ui/dialog/map_settings.js index 8b1f8af0..faffb9f7 100644 --- a/public/js/v1.3.6/app/ui/dialog/map_settings.js +++ b/public/js/v1.3.6/app/ui/dialog/map_settings.js @@ -541,7 +541,9 @@ define([ filesCount === files.length && filesCountFail === 0 ){ - importMaps(importData); + importMaps(importData, () => { + mapInfoDialog.modal('hide'); + }); } }; @@ -651,7 +653,7 @@ define([ * import new map(s) data * @param importData */ - let importMaps = function(importData){ + let importMaps = (importData, callback) => { let importForm = $('#' + config.dialogMapImportFormId); importForm.hideFormMessage('all'); @@ -675,6 +677,10 @@ define([ importForm.showFormMessage(responseData.warning); } + if(callback){ + callback(); + } + Util.showNotify({title: 'Import finished', text: 'Map(s) imported', type: 'success'}); } }).fail(function( jqXHR, status, error) {