Skip to content

Commit 091137a

Browse files
committed
issue #628 save_user_coords
1 parent 2333616 commit 091137a

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-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,103 @@
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_notes($cache_id, $request->token->user_id, $latitude, $longitude);
55+
56+
$ret_value = 'ok';
57+
58+
$result = array(
59+
'status' => $ret_value
60+
);
61+
return Okapi::formatted_response($request, $result);
62+
}
63+
64+
private static function update_notes($cache_id, $user_id, $latitude, $longitude)
65+
{
66+
/* See:
67+
*
68+
* - https://github.com/OpencachingDeutschland/oc-server3/tree/development/htdocs/src/Oc/Libse/CacheNote
69+
* - https://www.opencaching.de/okapi/devel/dbstruct
70+
*/
71+
72+
$rs = Db::query("
73+
select max(id) as id
74+
from coordinates
75+
where
76+
type = 2 -- personal note
77+
and cache_id = '".Db::escape_string($cache_id)."'
78+
and user_id = '".Db::escape_string($user_id)."'
79+
");
80+
$id = null;
81+
if($row = Db::fetch_assoc($rs)) {
82+
$id = $row['id'];
83+
}
84+
if ($id == null) {
85+
Db::query("
86+
insert into coordinates (
87+
type, latitude, longitude, cache_id, user_id
88+
) values (
89+
2, $latitude, $longitude, $cache_id, $user_id
90+
)
91+
");
92+
} else {
93+
Db::query("
94+
update coordinates
95+
set latitude = '$latitude', longitude = '$longitude'
96+
where
97+
id = '".Db::escape_string($id)."'
98+
and type = 2
99+
");
100+
}
101+
}
102+
103+
}
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>status - ok</li>
27+
</ul>
28+
</returns>
29+
</xml>

0 commit comments

Comments
 (0)