- moved ajax endpoints (connections, systems) into new REST API endpoints, #274
- improved error handling in case an Exception is thrown
This commit is contained in:
@@ -1,51 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: exodus4d
|
||||
* Date: 01.03.15
|
||||
* Time: 18:37
|
||||
* User: Exodus 4D
|
||||
* Date: 10.11.2018
|
||||
* Time: 12:10
|
||||
*/
|
||||
|
||||
namespace Controller\Api;
|
||||
namespace Controller\Api\Rest;
|
||||
|
||||
use Controller;
|
||||
use Model;
|
||||
|
||||
class Connection extends Controller\AccessController {
|
||||
|
||||
class Connection extends AbstractRestController {
|
||||
|
||||
/**
|
||||
* save a new connection or updates an existing (drag/drop) between two systems
|
||||
* if a connection is changed (drag&drop) to another system. -> this function is called for update
|
||||
* @param \Base $f3
|
||||
* @throws \Exception
|
||||
* @throws \ZMQSocketException
|
||||
*/
|
||||
public function save(\Base $f3){
|
||||
$postData = (array)$f3->get('POST');
|
||||
|
||||
$return = (object) [];
|
||||
$return->error = [];
|
||||
$return->connectionData = (object) [];
|
||||
|
||||
if(
|
||||
isset($postData['connectionData']) &&
|
||||
isset($postData['mapData'])
|
||||
){
|
||||
$mapData = (array)$postData['mapData'];
|
||||
$connectionData = (array)$postData['connectionData'];
|
||||
public function put(\Base $f3){
|
||||
$requestData = $this->getRequestData($f3);
|
||||
$connectionData = [];
|
||||
|
||||
if($mapId = (int)$requestData['mapId']){
|
||||
$activeCharacter = $this->getCharacter();
|
||||
|
||||
// get map model and check map access
|
||||
/**
|
||||
* @var Model\MapModel $map
|
||||
*/
|
||||
$map = Model\BasicModel::getNew('MapModel');
|
||||
$map->getById( (int)$mapData['id'] );
|
||||
$map->getById($mapId);
|
||||
|
||||
if( $map->hasAccess($activeCharacter) ){
|
||||
$source = $map->getSystemById( $connectionData['source'] );
|
||||
$target = $map->getSystemById( $connectionData['target'] );
|
||||
if($map->hasAccess($activeCharacter)){
|
||||
$source = $map->getSystemById((int)$requestData['source']);
|
||||
$target = $map->getSystemById((int)$requestData['target']);
|
||||
|
||||
if(
|
||||
!is_null($source) &&
|
||||
@@ -55,7 +44,7 @@ class Connection extends Controller\AccessController {
|
||||
* @var $connection Model\ConnectionModel
|
||||
*/
|
||||
$connection = Model\BasicModel::getNew('ConnectionModel');
|
||||
$connection->getById( (int)$connectionData['id'] );
|
||||
$connection->getById((int)$requestData['id']);
|
||||
|
||||
$connection->mapId = $map;
|
||||
$connection->source = $source;
|
||||
@@ -66,30 +55,30 @@ class Connection extends Controller\AccessController {
|
||||
$connection->setDefaultTypeData();
|
||||
|
||||
if($connection->save($activeCharacter)){
|
||||
$return->connectionData = $connection->getData();
|
||||
$connectionData = $connection->getData();
|
||||
|
||||
// broadcast map changes
|
||||
$this->broadcastMapData($connection->mapId);
|
||||
}else{
|
||||
$return->error = $connection->getErrors();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode($return);
|
||||
$this->out($connectionData);
|
||||
}
|
||||
|
||||
/**
|
||||
* delete connection
|
||||
* @param \Base $f3
|
||||
* @param $params
|
||||
* @throws \Exception
|
||||
* @throws \ZMQSocketException
|
||||
*/
|
||||
public function delete(\Base $f3){
|
||||
$mapId = (int)$f3->get('POST.mapId');
|
||||
$connectionIds = (array)$f3->get('POST.connectionIds');
|
||||
public function delete(\Base $f3, $params){
|
||||
$requestData = $this->getRequestData($f3);
|
||||
$connectionIds = array_map('intval', explode(',', (string)$params['id']));
|
||||
$deletedConnectionIds = [];
|
||||
|
||||
if($mapId){
|
||||
if($mapId = (int)$requestData['mapId']){
|
||||
$activeCharacter = $this->getCharacter();
|
||||
|
||||
/**
|
||||
@@ -100,20 +89,21 @@ class Connection extends Controller\AccessController {
|
||||
|
||||
if($map->hasAccess($activeCharacter)){
|
||||
foreach($connectionIds as $connectionId){
|
||||
if( $connection = $map->getConnectionById($connectionId) ){
|
||||
if($connection = $map->getConnectionById($connectionId)){
|
||||
$connection->delete( $activeCharacter );
|
||||
|
||||
$connection->reset();
|
||||
$deletedConnectionIds[] = $connectionId;
|
||||
}
|
||||
}
|
||||
|
||||
// broadcast map changes
|
||||
$this->broadcastMapData($map);
|
||||
if(count($deletedConnectionIds)){
|
||||
$this->broadcastMapData($map);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
echo json_encode([]);
|
||||
$this->out($deletedConnectionIds);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,6 @@ class Log extends AbstractRestController {
|
||||
$this->out($connectionData);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* update log data
|
||||
* @param \Base $f3
|
||||
|
||||
190
app/main/controller/api/rest/system.php
Normal file
190
app/main/controller/api/rest/system.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Exodus 4D
|
||||
* Date: 09.11.2018
|
||||
* Time: 12:34
|
||||
*/
|
||||
|
||||
namespace Controller\Api\Rest;
|
||||
|
||||
use Model;
|
||||
|
||||
class System extends AbstractRestController {
|
||||
|
||||
/**
|
||||
* put (insert) system
|
||||
* @param \Base $f3
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function put(\Base $f3){
|
||||
$requestData = $this->getRequestData($f3);
|
||||
$systemData = [];
|
||||
|
||||
if($mapId = (int)$requestData['mapId']){
|
||||
$activeCharacter = $this->getCharacter();
|
||||
|
||||
/**
|
||||
* @var $map Model\MapModel
|
||||
*/
|
||||
$map = Model\BasicModel::getNew('MapModel');
|
||||
$map->getById($mapId);
|
||||
if($map->hasAccess($activeCharacter)){
|
||||
$system = $map->getNewSystem($requestData['systemId']);
|
||||
$systemData = $this->update($system, $requestData)->getData();
|
||||
}
|
||||
}
|
||||
|
||||
$this->out($systemData);
|
||||
}
|
||||
|
||||
/**
|
||||
* update existing system
|
||||
* @param \Base $f3
|
||||
* @param $params
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function patch(\Base $f3, $params){
|
||||
$requestData = $this->getRequestData($f3);
|
||||
$systemData = [];
|
||||
|
||||
if($systemId = (int)$params['id']){
|
||||
$activeCharacter = $this->getCharacter();
|
||||
|
||||
/**
|
||||
* @var $system Model\SystemModel
|
||||
*/
|
||||
$system = Model\BasicModel::getNew('SystemModel');
|
||||
$system->getById($systemId);
|
||||
|
||||
if($system->hasAccess($activeCharacter)){
|
||||
$systemData = $this->update($system, $requestData)->getData();
|
||||
}
|
||||
}
|
||||
|
||||
$this->out($systemData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Base $f3
|
||||
* @param $params
|
||||
* @throws \ZMQSocketException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function delete(\Base $f3, $params){
|
||||
$requestData = $this->getRequestData($f3);
|
||||
$systemIds = array_map('intval', explode(',', (string)$params['id']));
|
||||
$deletedSystemIds = [];
|
||||
|
||||
if($mapId = (int)$requestData['mapId']){
|
||||
$activeCharacter = $this->getCharacter();
|
||||
|
||||
/**
|
||||
* @var Model\MapModel $map
|
||||
*/
|
||||
$map = Model\BasicModel::getNew('MapModel');
|
||||
$map->getById($mapId);
|
||||
|
||||
if($map->hasAccess($activeCharacter)){
|
||||
$newSystemModel = Model\BasicModel::getNew('SystemModel');
|
||||
foreach($systemIds as $systemId){
|
||||
if($system = $map->getSystemById($systemId)){
|
||||
// check whether system should be deleted OR set "inactive"
|
||||
if($this->checkDeleteMode($map, $system)){
|
||||
// delete log
|
||||
// -> first set updatedCharacterId -> required for activity log
|
||||
$system->updatedCharacterId = $activeCharacter;
|
||||
$system->update();
|
||||
|
||||
// ... now get fresh object and delete..
|
||||
$newSystemModel->getById($system->_id, 0);
|
||||
$newSystemModel->erase();
|
||||
$newSystemModel->reset();
|
||||
}else{
|
||||
// keep data -> set "inactive"
|
||||
$system->setActive(false);
|
||||
$system->save($activeCharacter);
|
||||
}
|
||||
|
||||
$system->reset();
|
||||
|
||||
$deletedSystemIds[] = $systemId;
|
||||
}
|
||||
}
|
||||
// broadcast map changes
|
||||
if(count($deletedSystemIds)){
|
||||
$this->broadcastMapData($map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->out($deletedSystemIds);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* update system with new data
|
||||
* @param Model\SystemModel $system
|
||||
* @param array $systemData
|
||||
* @return Model\SystemModel
|
||||
* @throws \ZMQSocketException
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function update(Model\SystemModel $system, array $systemData) : Model\SystemModel {
|
||||
$activeCharacter = $this->getCharacter();
|
||||
|
||||
// statusId === 0 is 'auto' status -> keep current status
|
||||
// -> relevant systems that already have a status (inactive systems)
|
||||
if( (int)$systemData['statusId'] <= 0 ){
|
||||
unset($systemData['statusId']);
|
||||
}
|
||||
|
||||
if( !$system->dry() ){
|
||||
// activate system (e.g. was inactive))
|
||||
$system->setActive(true);
|
||||
}
|
||||
|
||||
$system->setData($systemData);
|
||||
$system->save($activeCharacter);
|
||||
|
||||
// get data from "fresh" model (e.g. some relational data has changed: "statusId")
|
||||
/**
|
||||
* @var $newSystem Model\SystemModel
|
||||
*/
|
||||
$newSystem = Model\BasicModel::getNew('SystemModel');
|
||||
$newSystem->getById($system->_id, 0);
|
||||
$newSystem->clearCacheData();
|
||||
|
||||
// broadcast map changes
|
||||
$this->broadcastMapData($newSystem->mapId);
|
||||
|
||||
return $newSystem;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks whether a system should be "deleted" or set "inactive" (keep some data)
|
||||
* @param Model\MapModel $map
|
||||
* @param Model\SystemModel $system
|
||||
* @return bool
|
||||
*/
|
||||
private function checkDeleteMode(Model\MapModel $map, Model\SystemModel $system) : bool {
|
||||
$delete = true;
|
||||
|
||||
if( !empty($system->description) ){
|
||||
// never delete systems with custom description set!
|
||||
$delete = false;
|
||||
}elseif(
|
||||
$map->persistentAliases &&
|
||||
!empty($system->alias) &&
|
||||
($system->alias != $system->name)
|
||||
){
|
||||
// map setting "persistentAliases" is active (default) AND
|
||||
// alias is set and != name
|
||||
$delete = false;
|
||||
}
|
||||
|
||||
return $delete;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,7 +10,6 @@ namespace Controller\Api;
|
||||
|
||||
use Controller;
|
||||
use Model;
|
||||
use Exception;
|
||||
|
||||
class System extends Controller\AccessController {
|
||||
|
||||
@@ -26,88 +25,6 @@ class System extends Controller\AccessController {
|
||||
return sprintf(self::CACHE_KEY_GRAPH, 'SYSTEM_' . $systemId);
|
||||
}
|
||||
|
||||
/**
|
||||
* save a new system to a a map
|
||||
* @param \Base $f3
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function save(\Base $f3){
|
||||
$postData = (array)$f3->get('POST');
|
||||
|
||||
$return = (object) [];
|
||||
$return->error = [];
|
||||
$return->systemData = (object) [];
|
||||
|
||||
if(
|
||||
isset($postData['systemData']) &&
|
||||
isset($postData['mapData'])
|
||||
){
|
||||
$activeCharacter = $this->getCharacter();
|
||||
$systemData = (array)$postData['systemData'];
|
||||
$mapData = (array)$postData['mapData'];
|
||||
$systemModel = null;
|
||||
|
||||
if( (int)$systemData['statusId'] <= 0 ){
|
||||
unset($systemData['statusId']);
|
||||
}
|
||||
|
||||
if( isset($systemData['id']) ){
|
||||
// update existing system (e.g. set description) ------------------------------------------------------
|
||||
/**
|
||||
* @var $system Model\SystemModel
|
||||
*/
|
||||
$system = Model\BasicModel::getNew('SystemModel');
|
||||
$system->getById($systemData['id']);
|
||||
if(
|
||||
!$system->dry() &&
|
||||
$system->hasAccess($activeCharacter)
|
||||
){
|
||||
// system model found
|
||||
// activate system (e.g. was inactive))
|
||||
$system->setActive(true);
|
||||
$systemModel = $system;
|
||||
}
|
||||
}elseif( isset($mapData['id']) ){
|
||||
// save NEW system ------------------------------------------------------------------------------------
|
||||
/**
|
||||
* @var $map Model\MapModel
|
||||
*/
|
||||
$map = Model\BasicModel::getNew('MapModel');
|
||||
$map->getById($mapData['id']);
|
||||
if($map->hasAccess($activeCharacter)){
|
||||
$systemModel = $map->getNewSystem($systemData['systemId']);
|
||||
}
|
||||
}
|
||||
|
||||
if( !is_null($systemModel) ){
|
||||
try{
|
||||
// set/update system custom data
|
||||
$systemModel->copyfrom($systemData, ['statusId', 'locked', 'rallyUpdated', 'position', 'description']);
|
||||
|
||||
if($systemModel->save($activeCharacter)){
|
||||
// get data from "fresh" model (e.g. some relational data has changed: "statusId")
|
||||
/**
|
||||
* @var $newSystemModel Model\SystemModel
|
||||
*/
|
||||
$newSystemModel = Model\BasicModel::getNew('SystemModel');
|
||||
$newSystemModel->getById( $systemModel->_id, 0);
|
||||
$newSystemModel->clearCacheData();
|
||||
$return->systemData = $newSystemModel->getData();
|
||||
|
||||
// broadcast map changes
|
||||
$this->broadcastMapData($newSystemModel->mapId);
|
||||
}else{
|
||||
$return->error = $systemModel->getErrors();
|
||||
}
|
||||
}catch(Exception\ValidationException $e){
|
||||
$return->error[] = $e->getError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode($return);
|
||||
}
|
||||
|
||||
/**
|
||||
* get system log data from CCP API import
|
||||
* system Kills, Jumps,....
|
||||
@@ -294,87 +211,5 @@ class System extends Controller\AccessController {
|
||||
echo json_encode($return);
|
||||
}
|
||||
|
||||
/**
|
||||
* delete systems and all its connections from map
|
||||
* -> set "active" flag
|
||||
* @param \Base $f3
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function delete(\Base $f3){
|
||||
$mapId = (int)$f3->get('POST.mapId');
|
||||
$systemIds = array_map('intval', (array)$f3->get('POST.systemIds'));
|
||||
|
||||
$return = (object) [];
|
||||
$return->deletedSystemIds = [];
|
||||
|
||||
if($mapId){
|
||||
$activeCharacter = $this->getCharacter();
|
||||
|
||||
/**
|
||||
* @var Model\MapModel $map
|
||||
*/
|
||||
$map = Model\BasicModel::getNew('MapModel');
|
||||
$map->getById($mapId);
|
||||
|
||||
if($map->hasAccess($activeCharacter)){
|
||||
$newSystemModel = Model\BasicModel::getNew('SystemModel');
|
||||
foreach($systemIds as $systemId){
|
||||
if( $system = $map->getSystemById($systemId) ){
|
||||
// check whether system should be deleted OR set "inactive"
|
||||
if( $this->checkDeleteMode($map, $system) ){
|
||||
// delete log
|
||||
// -> first set updatedCharacterId -> required for activity log
|
||||
$system->updatedCharacterId = $activeCharacter;
|
||||
$system->update();
|
||||
|
||||
// ... now get fresh object and delete..
|
||||
$newSystemModel->getById( $system->id, 0);
|
||||
$newSystemModel->erase();
|
||||
$newSystemModel->reset();
|
||||
}else{
|
||||
// keep data -> set "inactive"
|
||||
$system->setActive(false);
|
||||
$system->save($activeCharacter);
|
||||
}
|
||||
|
||||
$system->reset();
|
||||
|
||||
$return->deletedSystemIds[] = $systemId;
|
||||
}
|
||||
}
|
||||
// broadcast map changes
|
||||
if(count($return->deletedSystemIds)){
|
||||
$this->broadcastMapData($map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode($return);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks whether a system should be "deleted" or set "inactive" (keep some data)
|
||||
* @param Model\MapModel $map
|
||||
* @param Model\SystemModel $system
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkDeleteMode(Model\MapModel $map, Model\SystemModel $system){
|
||||
$delete = true;
|
||||
|
||||
if( !empty($system->description) ){
|
||||
// never delete systems with custom description set!
|
||||
$delete = false;
|
||||
}elseif(
|
||||
$map->persistentAliases &&
|
||||
!empty($system->alias) &&
|
||||
($system->alias != $system->name)
|
||||
){
|
||||
// map setting "persistentAliases" is active (default) AND
|
||||
// alias is set and != name
|
||||
$delete = false;
|
||||
}
|
||||
|
||||
return $delete;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user