function api_privatesquare_venues_checkin() { $venue_id = post_str("venue_id"); $status_id = post_int32("status_id"); if (!$venue_id) { api_output_error(999, "Missing venue ID"); } if (!isset($status_id)) { api_output_error(999, "Missing status ID"); } $fsq_user = foursquare_users_get_by_user_id($GLOBALS['cfg']['user']['id']); $checkin = array('user_id' => $GLOBALS['cfg']['user']['id'], 'venue_id' => $venue_id, 'status_id' => $status_id); # where am I? $venue = foursquare_venues_get_by_venue_id($venue_id); if (!$venue) { $rsp = foursquare_venues_archive_venue($venue_id); if ($rsp['ok']) { $venue = $rsp['venue']; } } if ($venue) { $checkin['locality'] = $venue['locality']; $checkin['latitude'] = $venue['latitude']; $checkin['longitude'] = $venue['longitude']; } # check to see if we're checking in to 4sq too if ($broadcast = post_str("broadcast")) { $method = 'checkins/add'; $args = array('oauth_token' => $fsq_user['oauth_token'], 'venueId' => $venue_id, 'broadcast' => $broadcast); $more = array('method' => 'POST'); $rsp = foursquare_api_call($method, $args, $more); if ($rsp['ok']) { $checkin['checkin_id'] = $rsp['rsp']['checkin']['id']; } # on error, then what? } if ($GLOBALS['cfg']['enable_feature_weather_tracking']) { loadlib("weather_google"); $rsp = weather_google_conditions($checkin['latitude'], $checkin['longitude']); if ($rsp['ok']) { $conditions = $rsp['conditions']; $conditions['source'] = $rsp['source']; $checkin['weather'] = json_encode($conditions); } } $rsp = privatesquare_checkins_create($checkin); if (!$rsp['ok']) { api_output_error(999, "Check in failed"); } $out = array('checkin' => $rsp['checkin']); api_output_ok($out); }
function _set_latlon($row, $more = array()) { $user = users_get_by_id($row['user_id']); $venue_id = $row['venue_id']; $venue = foursquare_venues_get_by_venue_id($venue_id); if (!$venue) { $venue = foursquare_venues_archive_venue($venue_id); } if (!$venue) { echo "can not sort out venue data for '{$venue_id}'\n"; return; } $lat = $venue['latitude']; $lon = $venue['longitude']; $update = array('latitude' => AddSlashes($lat), 'longitude' => AddSlashes($lon)); $enc_id = $row['id']; $where = "id='{$enc_id}'"; $cluster_id = $user['cluster_id']; $rsp = db_update_users($cluster_id, 'PrivatesquareCheckins', $update, $where); echo "{$venue_id} : {$lat}, {$lon} {$where}: {$rsp['ok']}\n"; }
<?php include "include/init.php"; loadlib("foursquare_venues"); loadlib("foursquare_checkins"); loadlib("privatesquare_checkins"); loadlib("privatesquare_export"); loadlib("reverse_geoplanet"); login_ensure_loggedin($_SERVER['REQUEST_URI']); $owner = $GLOBALS['cfg']['user']; $GLOBALS['smarty']->assign_by_ref("owner", $owner); $venue_id = get_str("venue_id"); $venue = foursquare_venues_get_by_venue_id($venue_id); if (!$venue) { error_404(); } $venue['data'] = json_decode($venue['data'], "as hash"); $venue['locality'] = reverse_geoplanet_get_by_woeid($venue['locality'], 'locality'); # TO DO: account for pagination and > n checkins $more = array('venue_id' => $venue_id); $checkins = privatesquare_checkins_for_user($owner, $more); $venue['checkins'] = $checkins['rows']; $status_map = privatesquare_checkins_status_map(); $broadcast_map = foursquare_checkins_broadcast_map(); $GLOBALS['smarty']->assign_by_ref("status_map", $status_map); $GLOBALS['smarty']->assign_by_ref("broadcast_map", $broadcast_map); $GLOBALS['smarty']->assign_by_ref("venue", $venue); $checkin_crumb = crumb_generate("api", "privatesquare.venues.checkin"); $GLOBALS['smarty']->assign("checkin_crumb", $checkin_crumb); # did we arrive here from a checkin page? $success = get_str("success") ? 1 : 0;
function privatesquare_checkins_for_user_nearby(&$user, $lat, $lon, $more = array()) { loadlib("geo_utils"); $dist = isset($more['dist']) ? floatval($more['dist']) : 0.2; $unit = geo_utils_is_valid_unit($more['unit']) ? $more['unit'] : 'm'; # TO DO: sanity check to ensure max $dist $bbox = geo_utils_bbox_from_point($lat, $lon, $dist, $unit); $cluster_id = $user['cluster_id']; $enc_user = AddSlashes($user['id']); # TO DO: group by venue_id in memory since the following will always # result in a filesort (20120301/straup) $sql = "SELECT venue_id, COUNT(id) AS count FROM PrivatesquareCheckins WHERE user_id='{$enc_user}'"; $sql .= " AND latitude BETWEEN {$bbox[0]} AND {$bbox[2]} AND longitude BETWEEN {$bbox[1]} AND {$bbox[3]}"; $sql .= " GROUP BY venue_id"; $rsp = db_fetch_users($cluster_id, $sql, $more); if (!$rsp['ok']) { return $rsp; } $tmp = array(); foreach ($rsp['rows'] as $row) { $tmp[$row['venue_id']] = $row['count']; } arsort($tmp); $venues = array(); foreach ($tmp as $venue_id => $count) { $venue = foursquare_venues_get_by_venue_id($venue_id); $venue['count_checkins'] = $count; $venues[] = $venue; } return okay(array('rows' => $venues)); }
function sync_user($fsq_user, $more = array()) { $user = users_get_by_id($fsq_user['user_id']); if (!$user['sync_foursquare']) { echo "'{$user['username']}' has not opted in to foursquare syncing, skipping...\n"; return; } echo "sync checkins for '{$user['username']}' : {$user['sync_foursquare']}\n"; $status_map = privatesquare_checkins_status_map("string keys"); $method = 'users/self/checkins'; $count = null; $offset = 0; $limit = 250; while (!isset($count) || $offset < $count) { $args = array('oauth_token' => $fsq_user['oauth_token'], 'limit' => $limit, 'offset' => $offset); # only sync updates since the user signed up for privatesquare # > 1 (or "2") would mean pull in all a users' checkins. # see also: account_foursquare_sync.php if ($user['sync_foursquare'] == 1) { $args['afterTimestamp'] = $user['created']; } $rsp = foursquare_api_call($method, $args); if (!isset($count)) { $count = $rsp['rsp']['checkins']['count']; } $count_items = count($rsp['rsp']['checkins']['items']); # As of 20120218 if you pass a date filter to the API it # still returns the count for the total number of checkins # without the date filter. I love that... (20120218/straup) if (!$count_items) { break; } foreach ($rsp['rsp']['checkins']['items'] as $fsq_checkin) { if (privatesquare_checkins_get_by_foursquare_id($user, $fsq_checkin['id'])) { continue; } $checkin = array('user_id' => $user['id'], 'checkin_id' => $fsq_checkin['id'], 'venue_id' => $fsq_checkin['venue']['id'], 'created' => $fsq_checkin['createdAt'], 'status_id' => $status_map['i am here'], 'latitude' => $fsq_checkin['location']['lat'], 'longitude' => $fsq_checkin['location']['lng']); $venue = foursquare_venues_get_by_venue_id($checkin['venue_id']); if (!$venue) { $rsp = foursquare_venues_archive_venue($checkin['venue_id']); if (!$rsp['ok']) { echo "failed to archive venue '{$checkin['venue_id']}' : {$rsp['error']}\n"; echo "skipping...\n"; continue; } $venue = $rsp['venue']; } if ($venue) { $checkin['locality'] = $venue['locality']; $checkin['latitude'] = $venue['latitude']; $checkin['longitude'] = $venue['longitude']; } $rsp = privatesquare_checkins_create($checkin); if (!$rsp['ok']) { echo "failed to archive checkin: {$rsp['error']}\n"; continue; } echo "archived 4sq checkin {$checkin['checkin_id']} with privatesquare ID: {$rsp['checkin']['id']}\n"; } # do stuff here... $offset += $limit; } }