forked from opencaching/opencaching-pl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge_map.php
104 lines (76 loc) · 2.94 KB
/
badge_map.php
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
<?php
use src\Controllers\MeritBadgeController;
use src\Utils\Database\OcDb;
use src\Utils\Uri\SimpleRouter;
use src\Controllers\MainMapController;
use src\Models\OcConfig\OcConfig;
use src\Models\ApplicationContainer;
require_once(__DIR__.'/lib/common.inc.php');
$loggedUser = ApplicationContainer::GetAuthorizedUser();
if (!$loggedUser) {
$target = urlencode(tpl_get_current_page());
tpl_redirect('login.php?target=' . $target);
exit;
}
if (isset($_REQUEST['user_id'])) {
$userid = $_REQUEST['user_id'];
} else {
$userid = $loggedUser->getUserId();
}
$badge_id = $_REQUEST['badge_id'];
$show = $_REQUEST['show'];
$meritBadgeCtrl = new MeritBadgeController;
$tmp_badge_map = "tmp_badge_map";
$gainedPositions = $meritBadgeCtrl->buildArrayGainedPositions($userid, $badge_id);
$belongingPositions = $meritBadgeCtrl->buildArrayBelongingPositions($userid, $badge_id);
$gainedList = getCachesList($gainedPositions);
$belongingList = getCachesList($belongingPositions); //TODO nawet zaarchiwizowane ? (brakuje caches.status = 1)
$db = OcDb::instance();
addCachesToTmpTable( $db, $tmp_badge_map, $show, $gainedList, $belongingList);
$borderQuery = "SELECT MAX(caches.longitude) AS maxlongitude, MAX(caches.latitude) AS maxlatitude,
MIN(caches.longitude) AS minlongitude, MIN(caches.latitude) AS minlatitude
FROM $tmp_badge_map
join caches on caches.cache_id = tmp_badge_map.cache_id";
$stmt= $db->simpleQuery($borderQuery);
$r = $db->dbResultFetchOneRowOnly($stmt);
$minlat = $r['minlatitude'];
$minlon = $r['minlongitude'];
$maxlat = $r['maxlatitude'];
$maxlon = $r['maxlongitude'];
$cacheQuery = "SELECT cache_id FROM $tmp_badge_map";
$stmt = $db->simpleQuery($cacheQuery);
$hash = uniqid();
$f = fopen(OcConfig::getDynFilesPath() . "searchdata/" . $hash, "w");
while ($r = $db->dbResultFetch($stmt)) {
fprintf($f, "%s\n", $r['cache_id']);
}
fclose($f);
tpl_redirect(SimpleRouter::getLink(MainMapController::class, 'fullscreen') .
"?userid=$userid&searchdata=$hash&bbox=$minlon|$minlat|$maxlon|$maxlat");
function getCachesList($positions){
foreach( $positions as &$pos){
$pos = '(' . $pos->getId() . ')';
}
return implode(',', iterator_to_array($positions));
}
function addCachesToTmpTable( $db, $tmp_badge_map, $show, $gainedList, $belongingList ){
$db->simpleQuery(
"CREATE TEMPORARY TABLE $tmp_badge_map(cache_id int(11)) ENGINE=MEMORY");
$insQuery = "INSERT INTO $tmp_badge_map values ";
//N - not gained
//Y - gained
if ( !(strpos($show, 'N') === false) ){ //not gained
if(!empty($belongingList)){
$db->simpleQuery($insQuery . $belongingList);
}
}
if ( strpos($show, 'Y') === false ){ //only not gained
if(!empty($gainedList)){
$db->simpleQuery("DELETE FROM $tmp_badge_map WHERE cache_id IN (" . $gainedList. ")");
}
} else { //gained
if(!empty($gainedList)){
$db->simpleQuery($insQuery . $gainedList);
}
}
}