From d6e03455453e83768b53dc23379680f6ef83abca Mon Sep 17 00:00:00 2001 From: Martijn van der Ven Date: Sun, 20 Oct 2019 09:30:33 +0200 Subject: Add new weight posting interface --- controllers/controllers.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'controllers') diff --git a/controllers/controllers.php b/controllers/controllers.php index a4baa0f..df8ddfd 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -382,6 +382,9 @@ $app->post('/settings/save', function() use($app) { $user->micropub_syndicate_field = $params['syndicate_field']; } + if(array_key_exists('weight_unit', $params) && $params['weight_unit']) + $user->weight_unit = $params['weight_unit']; + $user->save(); $app->response()['Content-type'] = 'application/json'; $app->response()->body(json_encode(array( @@ -886,3 +889,45 @@ $app->get('/airport-info', function() use($app){ $app->response()->body(json_encode($response)); } }); + +function create_weight(&$user, $weight_num, $weight_unit) { + $micropub_request = array( + 'type' => ['h-entry'], + 'properties' => [ + 'weight' => [[ + 'type' => ['h-measure'], + 'properties' => [ + 'num' => [$weight_num], + 'unit' => [$weight_unit] + ] + ]] + ] + ); + $r = micropub_post_for_user($user, $micropub_request, null, true); + + return $r; +} + +$app->get('/weight', function() use($app){ + if($user=require_login($app)) { + render('new-weight', array( + 'title' => 'New Weight', + 'unit' => $user->weight_unit + )); + } +}); + +$app->post('/weight', function() use($app) { + if($user=require_login($app)) { + $params = $app->request()->params(); + + $r = create_weight($user, $params['weight_num'], $user->weight_unit); + $location = $r['location']; + + $app->response()['Content-type'] = 'application/json'; + $app->response()->body(json_encode(array( + 'location' => $location, + 'error' => $r['error'] + ))); + } +}); -- cgit v1.2.3