forked from exodus4d/pathfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapmodel.php
More file actions
610 lines (497 loc) · 16.9 KB
/
Copy pathmapmodel.php
File metadata and controls
610 lines (497 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php
/**
* Created by PhpStorm.
* User: exodus4d
* Date: 16.02.15
* Time: 22:10
*/
namespace Model;
class MapModel extends BasicModel {
protected $table = 'map';
protected $fieldConf = [
'scopeId' => [
'belongs-to-one' => 'Model\MapScopeModel'
],
'typeId' => [
'belongs-to-one' => 'Model\MapTypeModel'
],
'systems' => [
'has-many' => ['Model\SystemModel', 'mapId']
],
'connections' => [
'has-many' => ['Model\ConnectionModel', 'mapId']
],
'mapUsers' => [
'has-many' => ['Model\UserMapModel', 'mapId']
],
'mapCorporations' => [
'has-many' => ['Model\CorporationMapModel', 'mapId']
],
'mapAlliances' => ['has-many' => [
'Model\AllianceMapModel', 'mapId']
]
];
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;
}
}
}
}
/**
* get map data
* -> this includes system and connection data as well!
* @return array
*/
public function getData(){
// check if there is cached data
$mapDataAll = $this->getCacheData();
if(is_null($mapDataAll)){
// no cached map data found
$mapData = (object) [];
$mapData->id = $this->id;
$mapData->name = $this->name;
$mapData->icon = $this->icon;
$mapData->created = strtotime($this->created);
$mapData->updated = strtotime($this->updated);
// map scope
$mapData->scope = (object) [];
$mapData->scope->id = $this->scopeId->id;
$mapData->scope->name = $this->scopeId->name;
$mapData->scope->label = $this->scopeId->label;
// map type
$mapData->type = (object) [];
$mapData->type->id = $this->typeId->id;
$mapData->type->name = $this->typeId->name;
$mapData->type->classTab = $this->typeId->classTab;
// map access
$mapData->access = (object) [];
$mapData->access->user = [];
$mapData->access->corporation = [];
$mapData->access->alliance = [];
// get access object data -------------------------------------
if($this->isPrivate()){
$users = $this->getUsers();
$userData = [];
foreach($users as $user){
$userData[] = $user->getSimpleData();
}
$mapData->access->user = $userData;
} elseif($this->isCorporation()){
$corporations = $this->getCorporations();
$corporationData = [];
foreach($corporations as $corporation){
$corporationData[] = $corporation->getData();
}
$mapData->access->corporation = $corporationData;
} elseif($this->isAlliance()){
$alliances = $this->getAlliances();
$allianceData = [];
foreach($alliances as $alliance){
$allianceData[] = $alliance->getData();
}
$mapData->access->alliance = $allianceData;
}
// merge all data ---------------------------------------------
$mapDataAll = (object) [];
$mapDataAll->mapData = $mapData;
// map system data --------------------------------------------
$mapDataAll->systems = $this->getSystemData();
// map connection data ----------------------------------------
$mapDataAll->connections = $this->getConnectionData();
// max caching time for a map
// the cached date has to be cleared manually on any change
// this includes system, connection,... changes (all dependencies)
$this->updateCacheData($mapDataAll, '', 300);
}
return $mapDataAll;
}
/**
* search for a system by id
* @param $systemId
* @return null
*/
public function getSystem($systemId){
$systems = $this->getSystems();
$searchSystem = null;
foreach($systems as $system){
if($system->id == $systemId){
$searchSystem = $system;
break;
}
}
return $searchSystem;
}
/**
* get all system models in this map
* @return array|mixed
*/
public function getSystems(){
// orderBy x-Coordinate for cleaner frontend animation (left to right)
$this->filter('systems', ['active = ?', 1], ['order' => 'posX']);
$systems = [];
if($this->systems){
$systems = $this->systems;
}
return $systems;
}
/**
* get all system data for all systems in this map
* @return array
*/
public function getSystemData(){
$systems = $this->getSystems();
$systemData = [];
foreach($systems as $system){
$systemData[] = $system->getData();
}
return $systemData;
}
/**
* get all connections in this map
* @return array|mixed
*/
public function getConnections(){
$this->filter('connections', ['active = ?', 1]);
$connections = [];
if($this->connections){
$connections = $this->connections;
}
return $connections;
}
/**
* get all connection data in this map
* @return array
*/
public function getConnectionData(){
$connections = $this->getConnections();
$connectionData = [];
foreach($connections as $connection){
$connectionData[] = $connection->getData();
}
return $connectionData;
}
/**
* set map access for an object (user, corporation or alliance)
* @param $obj
*/
public function setAccess($obj){
$newAccessGranted = false;
if($obj instanceof UserModel){
// private map
// check whether the user has already map access
$this->has('mapUsers', ['active = 1 AND userId = :userId', ':userId' => $obj->id]);
$result = $this->findone(['id = :id', ':id' => $this->id]);
if($result === false){
// grant access for the user
$userMap = self::getNew('UserMapModel');
$userMap->userId = $obj;
$userMap->mapId = $this;
$userMap->save();
$newAccessGranted = true;
}
} elseif($obj instanceof CorporationModel){
// check whether the corporation already has map access
$this->has('mapCorporations', ['active = 1 AND corporationId = :corporationId', ':corporationId' => $obj->id]);
$result = $this->findone(['id = :id', ':id' => $this->id]);
if($result === false){
// grant access for this corporation
$corporationMap = self::getNew('CorporationMapModel');
$corporationMap->corporationId = $obj;
$corporationMap->mapId = $this;
$corporationMap->save();
$newAccessGranted = true;
}
} elseif($obj instanceof AllianceModel){
// check whether the corporation already has map access
$this->has('mapAlliances', ['active = 1 AND allianceId = :allianceId', ':allianceId' => $obj->id]);
$result = $this->findone(['id = :id', ':id' => $this->id]);
if($result === false){
$allianceMap = self::getNew('AllianceMapModel');
$allianceMap->allianceId = $obj;
$allianceMap->mapId = $this;
$allianceMap->save();
$newAccessGranted = true;
}
}
if($newAccessGranted){
// mark this map as updated
$this->setUpdated();
}
}
/**
* clear access for a given type of objects
* @param $clearKeys
*/
public function clearAccess($clearKeys = ['user', 'corporation', 'alliance']){
foreach($clearKeys as $key){
switch($key){
case 'user':
foreach((array)$this->mapUsers as $obj){
$obj->erase();
};
break;
case 'corporation':
foreach((array)$this->mapCorporations as $obj){
$obj->erase();
};
break;
case 'alliance':
foreach((array)$this->mapAlliances as $obj){
$obj->erase();
};
break;
}
}
}
/**
* checks weather a user has access to this map or not
* @param $user
* @return bool
*/
public function hasAccess($user){
$hasAccess = false;
if(
!$this->dry() &&
$user instanceof UserModel
){
// get all maps the user has access to
// this includes corporation and alliance maps
$maps = $user->getMaps();
foreach($maps as $map){
if($map->id === $this->id){
$hasAccess = true;
break;
}
}
}
return $hasAccess;
}
/**
* get all user models that have access to this map
* note: This function is just for "private" maps
* @return array
*/
public function getUsers(){
$users = [];
if($this->isPrivate()){
$this->filter('mapUsers', ['active = ?', 1]);
if($this->mapUsers){
foreach($this->mapUsers as $mapUser){
$users[] = $mapUser->userId;
}
}
}
return $users;
}
/**
* get all character models that are currently online "viewing" this map
* @return array
*/
private function getCharacters(){
$characters = [];
if($this->isPrivate()){
$users = $this->getUsers();
foreach($users as $user){
// get all active character logs for a user
$tempActiveUserCharacters = $user->getActiveUserCharacters();
foreach($tempActiveUserCharacters as $tempActiveUserCharacter){
$characters[] = $tempActiveUserCharacter;
}
}
}elseif($this->isCorporation()){
$corporations = $this->getCorporations();
foreach($corporations as $corporation){
$characters = array_merge($characters, $corporation->getCharacters());
}
}elseif($this->isAlliance()){
$alliances = $this->getAlliances();
foreach($alliances as $alliance){
$characters = array_merge($characters, $alliance->getCharacters());
}
}
return $characters;
}
/**
* get data for all characters that are currently online "viewing" this map
* -> the result of this function is cached!
* @return array
*/
private function getCharactersData(){
// check if there is cached data
$charactersData = $this->getCacheData('CHARACTERS');
if(is_null($charactersData)){
$charactersData = [];
$characters = $this->getCharacters();
foreach($characters as $character){
$charactersData[] = $character->getData(true);
}
$this->updateCacheData($charactersData, 'CHARACTERS', 10);
}
return $charactersData;
}
/**
* get all corporations that have access to this map
* @return array
*/
public function getCorporations(){
$corporations = [];
if($this->isCorporation()){
$this->filter('mapCorporations', ['active = ?', 1]);
if($this->mapCorporations){
foreach($this->mapCorporations as $mapCorporation){
$corporations[] = $mapCorporation->corporationId;
}
}
}
return $corporations;
}
/**
* get all alliances that have access to this map
* @return array
*/
public function getAlliances(){
$alliances = [];
if($this->isAlliance()){
$this->filter('mapAlliances', ['active = ?', 1]);
if($this->mapAlliances){
foreach($this->mapAlliances as $mapAlliance){
$alliances[] = $mapAlliance->allianceId;
}
}
}
return $alliances;
}
/**
* delete this map and all dependencies
* @param $accessObject
*/
public function delete($accessObject){
if(!$this->dry()){
// check if editor has access
if($this->hasAccess($accessObject)){
// all map related tables will be deleted on cascade
// delete map
$this->erase();
}
}
}
/**
* checks weather this map is private map
* @return bool
*/
public function isPrivate(){
$isPrivate = false;
if($this->typeId->id == 2){
$isPrivate = true;
}
return $isPrivate;
}
/**
* checks weather this map is corporation map
* @return bool
*/
public function isCorporation(){
$isCorporation = false;
if($this->typeId->id == 3){
$isCorporation = true;
}
return $isCorporation;
}
/**
* checks weather this map is alliance map
* @return bool
*/
public function isAlliance(){
$isAlliance = false;
if($this->typeId->id == 4){
$isAlliance = true;
}
return $isAlliance;
}
/**
* get all active characters (with active log)
* grouped by systems
* @return object
*/
public function getUserData(){
// get systems for this map
// the getData() function is cached. So this is more suitable than getSystems();
$mapDataAll = $this->getData();
// get data of characters which have with map access
$activeUserCharactersData = $this->getCharactersData();
$mapUserData = (object)[];
$mapUserData->config = (object)[];
$mapUserData->config->id = $this->id;
$mapUserData->data = (object)[];
$mapUserData->data->systems = [];
foreach($mapDataAll->systems as $systemData){
$systemUserData = (object)[];
$systemUserData->id = $systemData->systemId;
$systemUserData->user = [];
// check if a system has active characters
foreach($activeUserCharactersData as $key => $activeUserCharacterData){
if(isset($activeUserCharacterData->log)){
// user as log data
if($activeUserCharacterData->log->system->id == $systemData->systemId){
$systemUserData->user[] = $activeUserCharacterData;
// remove user from array -> speed up looping over characters.
// each userCharacter can only be active in a SINGLE system
unset($activeUserCharactersData[$key]);
}
}else{
// user has NO log data. If its an corp/ally map not each member is active
// user is not relevant for this function!
unset($activeUserCharactersData[$key]);
}
}
// add system if active users were found
if(count($systemUserData->user) > 0){
$mapUserData->data->systems[] = $systemUserData;
}
}
return $mapUserData;
}
/**
* save a map
* @return mixed
*/
public function save(){
$mapModel = parent::save();
// check if map type has changed and clear access objects
if( !$mapModel->dry() ){
if( $mapModel->isPrivate() ){
$mapModel->clearAccess(['corporation', 'alliance']);
}elseif( $mapModel->isCorporation() ){
$mapModel->clearAccess(['user', 'alliance']);
}elseif( $mapModel->isAlliance() ){
$mapModel->clearAccess(['user', 'corporation']);
}
}
return $mapModel;
}
}