Skip to content

Commit 88a83ba

Browse files
committed
issue #628 save_user_coords
1 parent 2333616 commit 88a83ba

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-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,105 @@
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' => 'my_notes|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+
'user_coords' => $latitude.'|'.$longitude
61+
);
62+
return Okapi::formatted_response($request, $result);
63+
}
64+
65+
private static function update_notes($cache_id, $user_id, $latitude, $longitude)
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,
89+
description
90+
) values (
91+
2, $latitude, $longitude
92+
)
93+
");
94+
} else {
95+
Db::query("
96+
update coordinates
97+
set latitude = '$latitude', longitude = '$longitude'
98+
where
99+
id = '".Db::escape_string($id)."'
100+
and type = 2
101+
");
102+
}
103+
}
104+
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 to change the content of the user's
6+
personal cache coordinates from one value to the other value.</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+
</returns>
25+
</xml>

0 commit comments

Comments
 (0)