Skip to content

Commit 0462c38

Browse files
committed
issue #628 save_user_coords
1 parent 2333616 commit 0462c38

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

okapi/core/OkapiServiceRunner.php

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class OkapiServiceRunner
3939
'services/caches/geocaches',
4040
'services/caches/mark',
4141
'services/caches/save_personal_notes',
42+
'services/caches/save_user_coords',
4243
'services/caches/formatters/gpx',
4344
'services/caches/formatters/garmin',
4445
'services/caches/formatters/ggz',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
namespace okapi\services\caches\save_user_coords;
4+
5+
use okapi\core\Db;
6+
use okapi\core\Exception\ParamMissing;
7+
use okapi\core\Exception\InvalidParam;
8+
use okapi\core\Okapi;
9+
use okapi\core\OkapiServiceRunner;
10+
use okapi\core\Request\OkapiInternalRequest;
11+
use okapi\core\Request\OkapiRequest;
12+
use okapi\Settings;
13+
14+
class WebService
15+
{
16+
public static function options()
17+
{
18+
return array(
19+
'min_auth_level' => 3
20+
);
21+
}
22+
23+
public static function call(OkapiRequest $request)
24+
{
25+
26+
$user_coords = $request->get_parameter('user_coords');
27+
if ($user_coords == null)
28+
throw new ParamMissing('user_coords');
29+
$parts = explode('|', $user_coords);
30+
if (count($parts) != 2)
31+
throw new InvalidParam('user_coords', "Expecting 2 pipe-separated parts, got ".count($parts).".");
32+
foreach ($parts as &$part_ref)
33+
{
34+
if (!preg_match("/^-?[0-9]+(\.?[0-9]*)$/", $part_ref))
35+
throw new InvalidParam('user_coords', "'$part_ref' is not a valid float number.");
36+
$part_ref = floatval($part_ref);
37+
}
38+
list($latitude, $longitude) = $parts;
39+
40+
# Verify cache_code
41+
42+
$cache_code = $request->get_parameter('cache_code');
43+
if ($cache_code == null)
44+
throw new ParamMissing('cache_code');
45+
$geocache = OkapiServiceRunner::call(
46+
'services/caches/geocache',
47+
new OkapiInternalRequest($request->consumer, $request->token, array(
48+
'cache_code' => $cache_code,
49+
'fields' => 'internal_id'
50+
))
51+
);
52+
$cache_id = $geocache['internal_id'];
53+
54+
self::update_coordinates($cache_id, $request->token->user_id, $latitude, $longitude);
55+
56+
$result = array(
57+
'success' => true
58+
);
59+
return Okapi::formatted_response($request, $result);
60+
}
61+
62+
private static function update_coordinates($cache_id, $user_id, $latitude, $longitude)
63+
{
64+
if (Settings::get('OC_BRANCH') == 'oc.de')
65+
{
66+
67+
/* See:
68+
*
69+
* - https://github.com/OpencachingDeutschland/oc-server3/tree/development/htdocs/src/Oc/Libse/CacheNote
70+
* - https://www.opencaching.de/okapi/devel/dbstruct
71+
*/
72+
73+
$rs = Db::query("
74+
select max(id) as id
75+
from coordinates
76+
where
77+
type = 2 -- personal note
78+
and cache_id = '".Db::escape_string($cache_id)."'
79+
and user_id = '".Db::escape_string($user_id)."'
80+
");
81+
$id = null;
82+
if($row = Db::fetch_assoc($rs)) {
83+
$id = $row['id'];
84+
}
85+
if ($id == null) {
86+
Db::query("
87+
insert into coordinates (
88+
type, latitude, longitude, cache_id, user_id, description
89+
) values (
90+
2,
91+
'".Db::escape_string($latitude)."',
92+
'".Db::escape_string($longitude)."',
93+
'".Db::escape_string($cache_id)."',
94+
'".Db::escape_string($user_id)."',
95+
'".Db::escape_string("")."'
96+
)
97+
");
98+
} else {
99+
Db::query("
100+
update coordinates
101+
set latitude = '".Db::escape_string($latitude)."',
102+
longitude = '".Db::escape_string($longitude)."'
103+
where
104+
id = '".Db::escape_string($id)."'
105+
and type = 2
106+
");
107+
}
108+
}
109+
else # oc.pl branch
110+
{
111+
$rs = Db::query("
112+
select max(id) as id
113+
from cache_mod_cords
114+
where
115+
cache_id = '".Db::escape_string($cache_id)."'
116+
and user_id = '".Db::escape_string($user_id)."'
117+
");
118+
$id = null;
119+
if($row = Db::fetch_assoc($rs)) {
120+
$id = $row['id'];
121+
}
122+
if ($id == null) {
123+
Db::query("
124+
insert into cache_mod_cords (
125+
cache_id, user_id, latitude, longitude
126+
) values (
127+
2,
128+
'".Db::escape_string($cache_id)."',
129+
'".Db::escape_string($user_id)."',
130+
'".Db::escape_string($latitude)."',
131+
'".Db::escape_string($longitude)."'
132+
)
133+
");
134+
} else {
135+
Db::query("
136+
update cache_mod_cords
137+
set latitude = '".Db::escape_string($latitude)."',
138+
longitude = '".Db::escape_string($longitude)."'
139+
where
140+
id = '".Db::escape_string($id)."'
141+
");
142+
}
143+
}
144+
}
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<xml>
2+
<brief>Update personal coordinates of a geocache</brief>
3+
<issue-id>629</issue-id>
4+
<desc>
5+
<p>This method allows your users to update the coordinates of their
6+
personal geocache coordinates.</p>
7+
8+
<p>Current personal coordinates for the geocache can be retrieved
9+
using the <b>alt_wpts</b> field in the
10+
<a href="%OKAPI:methodargref:services/caches/geocache#fields%">services/caches/geocache</a>
11+
method.</p>
12+
</desc>
13+
<req name='cache_code'>
14+
<p>Code of the geocache</p>
15+
</req>
16+
<req name='user_coords'>
17+
<p>The coordinates are defined by a string in the format "lat|lon"</p>
18+
<p>Use positive numbers for latitudes in the northern hemisphere and longitudes
19+
in the eastern hemisphere (and negative for southern and western hemispheres
20+
accordingly). These are full degrees with a dot as a decimal point (ex. "48.7|15.89").</p>
21+
</req>
22+
<common-format-params/>
23+
<returns>
24+
<p>A dictionary of the following structure:</p>
25+
<ul>
26+
<li>success - true</li>
27+
</ul>
28+
</returns>
29+
</xml>

0 commit comments

Comments
 (0)