From 3dc2f707aa2eff049f6e3f41c86bab74bb61e78c Mon Sep 17 00:00:00 2001 From: Exodus4D Date: Sun, 10 Apr 2016 16:32:51 +0200 Subject: [PATCH] closed #114 Added check for already existing system when adding a new one. (fixed PDO 'duplicate entry' error) --- app/main/controller/api/connection.php | 4 +- app/main/controller/api/map.php | 2 +- app/main/controller/api/system.php | 23 +++++---- app/main/model/mapmodel.php | 67 +++++++++++++++++--------- 4 files changed, 60 insertions(+), 36 deletions(-) diff --git a/app/main/controller/api/connection.php b/app/main/controller/api/connection.php index f4d6b460..849facce 100644 --- a/app/main/controller/api/connection.php +++ b/app/main/controller/api/connection.php @@ -49,8 +49,8 @@ class Connection extends Controller\AccessController{ $map->getById( (int)$mapData['id'] ); if( $map->hasAccess($activeCharacter) ){ - $source = $map->getSystem( (int)$connectionData['source'] ); - $target = $map->getSystem( (int)$connectionData['target'] ); + $source = $map->getSystemById( $connectionData['source'] ); + $target = $map->getSystemById( $connectionData['target'] ); if( !is_null($source) && diff --git a/app/main/controller/api/map.php b/app/main/controller/api/map.php index 4d232336..9138992c 100644 --- a/app/main/controller/api/map.php +++ b/app/main/controller/api/map.php @@ -679,7 +679,7 @@ class Map extends Controller\AccessController { // request signature data for a system if user has map access! if( $map->id === $requestSystemData->mapId ){ - $system = $map->getSystem( $requestSystemData->systemId ); + $system = $map->getSystemById( $requestSystemData->systemId ); if( !is_null($system) ){ // data for currently selected system diff --git a/app/main/controller/api/system.php b/app/main/controller/api/system.php index 636e1aea..d71ba5b2 100644 --- a/app/main/controller/api/system.php +++ b/app/main/controller/api/system.php @@ -196,7 +196,7 @@ class System extends \Controller\AccessController { $mapData = (array)$postData['mapData']; if( isset($systemData['id']) ){ - // update existing system + // update existing system (e.g. changed system description) ------------------- /** * @var $system Model\SystemModel @@ -210,24 +210,27 @@ class System extends \Controller\AccessController { } } }elseif( isset($mapData['id']) ){ - // save NEW system + // save NEW system ------------------------------------------------------------ /** * @var $map Model\MapModel */ $map = Model\BasicModel::getNew('MapModel'); $map->getById($mapData['id']); - if( !$map->dry() ){ - if( $map->hasAccess($activeCharacter) ){ - - $systemData['mapId'] = $map; - - // get static system data (CCP DB) + if( + !$map->dry() && + $map->hasAccess($activeCharacter) + ){ + // make sure system is not already on map + // --> (e.g. multiple simultaneously save() calls for the same system) + if( is_null( $systemModel = $map->getSystemByCCPId($systemData['systemId']) ) ){ + // system not found on map -> get static system data (CCP DB) $systemModel = array_values( $this->_getSystemModelByIds([$systemData['systemId']]) )[0]; - $systemModel->createdCharacterId = $activeCharacter; - } + + // map is not changeable for a system! (security) + $systemData['mapId'] = $map; } } } diff --git a/app/main/model/mapmodel.php b/app/main/model/mapmodel.php index 757029c3..b66a6547 100644 --- a/app/main/model/mapmodel.php +++ b/app/main/model/mapmodel.php @@ -210,40 +210,61 @@ class MapModel extends BasicModel { } /** - * search for a system by id - * @param $systemId - * @return null + * search for a system by CCPs systemId + * @param int $systemId + * @return null|SystemModel */ - public function getSystem($systemId){ - $searchSystem = null; - - if($systemId > 0){ - $systems = $this->getSystems(); - foreach($systems as $system){ - if($system->id == $systemId){ - $searchSystem = $system; - break; - } - } + public function getSystemByCCPId($systemId){ + $system = null; + if( !empty($systems = $this->getSystems('systemId', (int)$systemId) ) ){ + $system = $systems[0]; } - return $searchSystem; + return $system; } /** - * get all system models in this map + * search for a system by id + * @param int $systemId + * @return null|SystemModel + */ + public function getSystemById($systemId){ + $system = null; + if( !empty($systems = $this->getSystems('id', (int)$systemId) ) ){ + $system = $systems[0]; + } + + return $system; + } + + /** + * get either all system models in this map + * -> or get a specific system by column filter + * @param string $column + * @param string $value * @return array|mixed */ - public function getSystems(){ - // orderBy x-Coordinate for cleaner frontend animation (left to right) - $this->filter('systems', - ['active = :active AND id > 0', - ':active' => 1 - ], + public function getSystems($column = '', $value = ''){ + $systems = []; + + $filterQuery = ['active = :active AND id > 0', + ':active' => 1 + ]; + + // add more filter options.... + if( + !empty($column) && + !empty($value) + ){ + $filterQuery[0] .= ' AND ' . $column . ' = :value'; + $filterQuery[':value'] = $value; + } + + // orderBy x-Coordinate for smoother frontend animation (left to right) + $this->filter('systems', $filterQuery, ['order' => 'posX'] ); - $systems = []; if($this->systems){ $systems = $this->systems; }