Files
pathfinder/app/main/model/structuremodel.php
Mark Friedrich efd768974f - New "Intel module" for Citadel tracking, closed #246
- updated "Select2" js lib `4.0.3` -> `4.0.6-rc.1`
- fixed some login Issues
- fixed broken `map/*` reroute URL wildcard replacement
- fixed broken cache layer for Universe models
2018-05-01 19:51:17 +02:00

218 lines
6.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Created by PhpStorm.
* User: exodu
* Date: 15.04.2018
* Time: 19:41
*/
namespace Model;
use DB\SQL\Schema;
class StructureModel extends BasicModel {
protected $table = 'structure';
/**
* categoryId (from CCP´s SDE) that holds all "groups" with structure "types"
*/
const CATEGORY_STRUCTURE_ID = 65;
protected $fieldConf = [
'active' => [
'type' => Schema::DT_BOOL,
'nullable' => false,
'default' => 1,
'index' => true
],
'structureId' => [
'type' => Schema::DT_INT,
'index' => true
],
'corporationId' => [
'type' => Schema::DT_INT,
'index' => true,
'belongs-to-one' => 'Model\CorporationModel',
'constraint' => [
[
'table' => 'corporation',
'on-delete' => 'SET NULL'
]
]
],
'systemId' => [
'type' => Schema::DT_INT,
'index' => true,
'validate' => true
],
'statusId' => [
'type' => Schema::DT_INT,
'index' => true,
'belongs-to-one' => 'Model\StructureStatusModel',
'constraint' => [
[
'table' => 'structure_status',
'on-delete' => 'SET NULL'
]
]
],
'name' => [
'type' => Schema::DT_VARCHAR128,
'nullable' => false,
'default' => ''
],
'description' => [
'type' => Schema::DT_VARCHAR512,
'nullable' => false,
'default' => ''
],
'structureCorporations' => [
'has-many' => ['Model\CorporationStructureModel', 'structureId']
]
];
/**
* get structure data
* @return \stdClass
* @throws \Exception
*/
public function getData() : \stdClass {
$structureData = (object) [];
$structureData->id = $this->_id;
$structureData->systemId = $this->systemId;
$structureData->status = $this->statusId->getData();
$structureData->name = $this->name;
$structureData->description = $this->description;
if($this->structureId){
$structureData->structure = $this->getUniverseTypeData($this->structureId);
}
if($this->corporationId){
$structureData->owner = (object) [];
$structureData->owner->id = $this->corporationId->_id;
$structureData->owner->name = $this->corporationId->name;
}
$structureData->updated = (object) [];
$structureData->updated->updated = strtotime($this->updated);
return $structureData;
}
/**
* set structureId (universeType) for this structure
* @param $structureId
* @return int|null
*/
public function set_structureId($structureId){
$structureId = (int)$structureId;
$structureId = $structureId ? : null;
return $structureId;
}
/**
* set corporationId (owner) for this structure
* -> if corporation does not exists in DB -> load from API
* @param $corporationId
* @return int|null
*/
public function set_corporationId($corporationId){
$oldCorporationId = $this->get('corporationId', true) ? : 0;
$corporationId = !is_string($corporationId) ? : (int)$corporationId;
if($corporationId){
if($corporationId !== $oldCorporationId){
// make sure there is already corporation data stored for new corporationId
/**
* @var CorporationModel $corporation
*/
$corporation = $this->rel('corporationId');
$corporation->getById($corporationId);
if($corporation->dry()){
$corporationId = null;
}
}
}else{
$corporationId = null;
}
return $corporationId;
}
/**
* validates systemId
* @param string $key
* @param string $val
* @return bool
*/
protected function validate_systemId(string $key, string $val): bool {
$valid = true;
if( !$this->dry() && $this->systemId !== (int)$val ){
// structure always belongs to the same system
$valid = false;
}
return $valid;
}
/**
* check access by chraacter
* @param CharacterModel $characterModel
* @return bool
*/
public function hasAccess(CharacterModel $characterModel) : bool {
$access = false;
if($this->dry()){
$access = true;
}elseif($characterModel->hasCorporation()){
$this->filter('structureCorporations', ['active = ?', 1]);
$this->has('structureCorporations.corporationId', ['active = ?', 1]);
$this->has('structureCorporations.corporationId', ['id = ?', $characterModel->get('corporationId', true)]);
if($this->structureCorporations){
$access = true;
}
}
return $access;
}
/**
* get structure data grouped by corporations
* @return array
* @throws \Exception
*/
public function getDataByCorporations() : array {
$structuresData = [];
foreach((array)$this->structureCorporations as $structureCorporation){
if($structureCorporation->isActive() && $structureCorporation->corporationId->isActive()){
$structuresData[$structureCorporation->corporationId->_id] = [
'id' => $structureCorporation->corporationId->_id,
'name' => $structureCorporation->corporationId->name,
'structures' => [$this->getData()]
];
}
}
return $structuresData;
}
/**
* get universe type data for structureId
* @param int $structureId
* @return \stdClass
* @throws \Exception
*/
protected function getUniverseTypeData(int $structureId) : \stdClass {
/**
* @var $type Universe\TypeModel
*/
$type = Universe\BasicUniverseModel::getNew('TypeModel')->getById($structureId);
return $type->dry() ? (object)[] : $type->getData();
}
}