closed #114 Added check for already existing system when adding a new one. (fixed PDO 'duplicate entry' error)

This commit is contained in:
Exodus4D
2016-04-10 16:32:51 +02:00
parent c6206172ac
commit 3dc2f707aa
4 changed files with 60 additions and 36 deletions

View File

@@ -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) &&

View File

@@ -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

View File

@@ -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;
}
}
}

View File

@@ -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;
}