array( 'belongs-to-one' => 'Model\MapScopeModel' ), 'typeId' => array( 'belongs-to-one' => 'Model\MapTypeModel' ) ); protected $validate = [ 'name' => [ 'length' => [ 'min' => 3 ] ], 'icon' => [ 'length' => [ 'min' => 3 ] ], 'scopeId' => [ 'regex' => '/^[1-9]+$/' ], 'typeId' => [ 'regex' => '/^[1-9]+$/' ], ]; /** * set map data by an associative array * @param $data */ public function setData($data){ foreach((array)$data as $key => $value){ if(!is_array($value)){ if($this->exists($key)){ $this->$key = $value; } }else{ // special array data if($key == 'scope'){ $this->scopeId = $value['id']; }elseif($key == 'type'){ $this->typeId = $value['id']; } } } } /** * get map data for for response * @return array */ public function getData(){ $mapData = [ 'id' => $this->id, 'name' => $this->name, 'scope' => [ 'id' => $this->scopeId->id, 'name' => $this->scopeId->name, 'label' => $this->scopeId->label ], 'type' => [ 'id' => $this->typeId->id, 'name' => $this->typeId->name, 'classTab' => $this->typeId->classTab ], 'icon' => $this->icon, 'updated' => strtotime($this->updated) ]; return $mapData; } /** * get all system data for all systems in this map * @return array */ public function getSystemData(){ $systems = $this->getRelatedModels('SystemModel', 'mapId', null, 5); $systemData = []; if(is_object($systems)){ foreach($systems as $system){ $systemData[] = $system->getData(); } } return $systemData; } /** * get all connection data in this map * @return array */ public function getConnectionData(){ $connections = $this->getRelatedModels('ConnectionModel', 'mapId', null, 5); $connectionData = []; if(is_object($connections)){ foreach($connections as $connection){ $connectionData[] = $connection->getData(); } } return $connectionData; } /** * set map access for an object (user or alliance) * @param $obj */ public function setAccess($obj){ if($obj instanceof UserModel){ // private map // get all userModels who have map access $userMaps = $this->getRelatedModels('UserMapModel', 'mapId'); $userFound = false; if($userMaps){ foreach($userMaps as $userMap){ if($userMap->userId->id !== $obj->id){ // remove map access $userMap->erase(); }else{ $userFound = true; } } } if(!$userFound){ // set user who has access to this map $userMap = self::getNew('UserMapModel'); $userMap->userId = $obj; $userMap->mapId = $this; $userMap->save(); } } } /** * checks weather an object (user or alliance) has * @param $accessObject * @return bool */ public function hasAccess($accessObject){ $hasAccess = false; if($accessObject instanceof UserModel){ // get all userModels who have map access $userMaps = $this->getRelatedModels('UserMapModel', 'mapId'); if($userMaps){ foreach($userMaps as $userMap){ if($userMap->userId->id === $accessObject->id){ $hasAccess = true; break; } } } } return $hasAccess; } /** * delete this map and all dependencies */ public function delete($accessObject){ if(!$this->dry()){ // check if editor has access if($this->hasAccess($accessObject)){ // get all userModels who have map access $userMaps = $this->getRelatedModels('UserMapModel', 'mapId'); if(is_object($userMaps)){ foreach($userMaps as $userMap){ $userMap->erase(); } } // get all connections $connections = $this->getRelatedModels('ConnectionModel', 'mapId'); if(is_object($connections)){ foreach($connections as $connection){ $connection->erase(); } } // get all systems $systems = $this->getRelatedModels('SystemModel', 'mapId'); if(is_object($systems)){ foreach($systems as $system){ $system->erase(); } } // delete map $this->erase(); } } } /** * checks weather a map is private or not * @return bool */ public function isPrivate(){ $isPrivate = false; if($this->typeId->id == 2){ $isPrivate = true; } return $isPrivate; } }