Exemple #1
0
 /**
  * Call this function to find zipcodes near a certain location
  * @param {double} $latitude The latitude of the coordinates to search around
  * @param {double} $longitude The longitude of the coordinates to search around
  * @param {double} $miles The radius, in miles, around the central point of the zipcode
  * @param {double} $limit Limit on how many to return. Defaults to 100.
  * @return {array} Returns an array of Places_Zipcode objects, if any are found.
  */
 public static function nearby($latitude, $longitude, $miles, $limit = 100)
 {
     // First, get a bounding box that's big enough to avoid false negatives
     $latGrid = $miles / 69.1703234283616;
     $longGrid = abs($latGrid / cos(deg2rad($latitude)));
     // Now, select zipcodes in a bounding box using one of the indexes
     $q = Places_Zipcode::select('*')->where(array('latitude >' => $latitude - $latGrid, 'latitude <' => $latitude + $latGrid));
     $longitudes = array('longitude >' => max($longitude - $longGrid, -180), 'longitude <' => min($longitude + $longGrid, 180));
     if ($latitude + $longGrid > 180) {
         $q->andWhere($longitudes, array('longitude >' => -180, 'longitude <' => $longitude + $longGrid - 180 * 2));
     } else {
         if ($latitude - $longGrid < -180) {
             $q->andWhere($longitudes, array('longitude <=' => 180, 'longitude >' => $longitude - $longGrid + 180 * 2));
         } else {
             $q->andWhere($longitudes);
         }
     }
     $latitude = substr($latitude, 0, 10);
     $longitude = substr($longitude, 0, 10);
     $q = $q->orderBy("POW(latitude - ({$latitude}), 2) + POW(longitude - ({$longitude}), 2)");
     if ($limit) {
         $q = $q->limit($limit);
     }
     return $q->fetchDbRows();
 }
Exemple #2
0
function Places_zipcode_response()
{
    if (Q_Request::method() !== 'GET') {
        return null;
    }
    $zip = array();
    if (isset($_REQUEST['zipcodes'])) {
        $zip = $_REQUEST['zipcodes'];
    } else {
        if (isset($_REQUEST['zipcode'])) {
            $zip = $_REQUEST['zipcode'];
        }
    }
    if (is_string($zip)) {
        $zip = explode(',', $zip);
    }
    $zipcodes = Places_Zipcode::select('*')->where(array('zipcode' => $zip))->fetchDbRows();
    Q_Response::setSlot('zipcodes', $zipcodes);
}
Exemple #3
0
function Places_geolocation_response_data()
{
    $requireLogin = Q_Config::get('Places', 'geolocation', 'requireLogin', false);
    if (!$requireLogin and isset($_REQUEST['latitude']) and isset($_REQUEST['longitude'])) {
        if (!Q_Config::get('Places', 'geolocation', 'allowClientQueries', false)) {
            throw new Q_Exception("Client queries are restricted, as per Places/geolocation/allowClientQueries");
        }
        $latitude = $_REQUEST['latitude'];
        $longitude = $_REQUEST['longitude'];
    } else {
        $user = Users::loggedInUser(true);
        $stream = Places_Location::userStream();
        $latitude = $stream->getAttribute('latitude');
        $longitude = $stream->getAttribute('longitude');
        $miles = $stream->getAttribute('miles');
    }
    $miles = Q::ifset($_REQUEST, 'miles', Q_Config::expect('Places', 'nearby', 'defaultMiles'));
    $zipcodes = Places_Zipcode::nearby($latitude, $longitude, $miles, 1);
    $zipcode = $zipcodes ? reset($zipcodes) : null;
    $result = array('requested' => compact('latitude', 'longitude'), 'countryCode' => $zipcode->countryCode, 'zipcode' => $zipcode->zipcode, 'placeName' => $zipcode->placeName, 'stateName' => $zipcode->stateName, 'state' => $zipcode->state, 'regionName' => $zipcode->regionName, 'region' => $zipcode->region, 'latitude' => $zipcode->latitude, 'longitude' => $zipcode->longitude);
    return $result;
}
Exemple #4
0
 /**
  * Fetch (and create, if necessary) stream on which messages are posted relating
  * to things happening a given number of $miles around the given location.
  * @method stream
  * @static
  * @param {double} $latitude The latitude of the coordinates to search around
  * @param {double} $longitude The longitude of the coordinates to search around
  * @param {double} $miles The radius, in miles, around this location.
  *  Should be one of the array values in the Places/nearby/miles config.
  * @param {string} $publisherId The id of the publisher to publish this stream
  *  Defaults to the app name in Q/app config.
  * @param {string} $streamName The name of the stream to create.
  *  Defaults to Places_Nearby::streamName($latitude, $longitude, $miles).
  * @return {Streams_Stream} Returns the stream object that was created or fetched.
  */
 static function stream($latitude, $longitude, $miles, $publisherId = null, $streamName = null)
 {
     list($latitude, $longGrid) = Places::quantize($latitude, $longitude, $miles);
     $zipcodes = Places_Zipcode::nearby($latitude, $longitude, $miles, 1);
     if (!isset($publisherId)) {
         $publisherId = Users::communityId();
     }
     if (!isset($streamName)) {
         $streamName = self::streamName($latitude, $longitude, $miles);
     }
     if ($stream = Streams::fetchOne(null, $publisherId, $streamName)) {
         return $stream;
     }
     $zipcode = $zipcodes ? reset($zipcodes) : null;
     $attributes = compact('latitude', 'longitude');
     if ($zipcode) {
         foreach (array('zipcode', 'placeName', 'state') as $attr) {
             $attributes[$attr] = $zipcode->{$attr};
         }
     }
     $stream = Streams::create($publisherId, $publisherId, 'Places/nearby', array('name' => $streamName, 'title' => $zipcode ? "Nearby ({$latitude}, {$longitude}): {$zipcode->placeName}, zipcode {$zipcode->zipcode}" : "Nearby ({$latitude}, {$longitude})", 'attributes' => Q::json_encode($attributes)));
     return $stream;
 }
Exemple #5
0
 /**
  * Fetch a stream on which messages are posted relating to things happening
  * a given number of $miles around the given location.
  * If it doesn't exist, create it.
  * @method stream
  * @static
  * @param {double} $latitude The latitude of the coordinates to search around
  * @param {double} $longitude The longitude of the coordinates to search around
  * @param {double} $miles The radius, in miles, around this location.
  *  Should be one of the array values in the Places/nearby/miles config.
  * @param {string} $publisherId The id of the publisher to publish this stream
  *  Defaults to the app name in Q/app config.
  * @param {string} $streamName The name of the stream to create.
  *  Defaults to Places_Nearby::streamName($latitude, $longitude, $miles).
  * @return {Streams_Stream} Returns the stream object that was created or fetched.
  */
 static function stream($latitude, $longitude, $miles, $publisherId = null, $streamName = null)
 {
     list($latitude, $longGrid) = Places::quantize($latitude, $longitude, $miles);
     $zipcodes = Places_Zipcode::nearby($latitude, $longitude, $miles, 1);
     if (!isset($publisherId)) {
         $publisherId = Q_Config::expect('Q', 'app');
     }
     if (!isset($streamName)) {
         $streamName = self::streamName($latitude, $longitude, $miles);
     }
     if ($stream = Streams::fetchOne(null, $publisherId, $streamName)) {
         return $stream;
     }
     $zipcode = $zipcodes ? reset($zipcodes) : null;
     $stream = new Streams_Stream();
     $stream->publisherId = $publisherId;
     $stream->name = $streamName;
     $stream->type = "Places/nearby";
     $stream->title = $zipcode ? "Nearby ({$latitude}, {$longitude}): {$zipcode->placeName}, zipcode {$zipcode->zipcode}" : "Nearby ({$latitude}, {$longitude})";
     $stream->setAttribute('latitude', $latitude);
     $stream->setAttribute('longitude', $longitude);
     if ($zipcode) {
         $stream->setAttribute('zipcode', $zipcode->zipcode);
         $stream->setAttribute('placeName', $zipcode->placeName);
         $stream->setAttribute('state', $zipcode->state);
     }
     $stream->save();
     return $stream;
 }
Exemple #6
0
/**
 * Used to set the user's location from geolocation data.
 * @param $_REQUEST
 * @param [$_REQUEST.latitude] The new latitude. If set, must also specify longitude.
 * @param [$_REQUEST.longitude] The new longitude. If set, must also specify latitude.
 * @param [$_REQUEST.zipcode] The new zip code. Can be set instead of latitude, longitude.
 * @param [$_REQUEST.miles] The distance around their location around that the user is interested in
 * @param [$_REQUEST.subscribe] Whether to subscribe to all the local interests at the new location.
 * @param [$_REQUEST.unsubscribe] Whether to unsubscribe from all the local interests at the old location.
 * @param [$_REQUEST.accuracy]
 * @param [$_REQUEST.altitude]
 * @param [$_REQUEST.altitudeAccuracy]
 * @param [$_REQUEST.heading]
 * @param [$_REQUEST.speed]
 * @param [$_REQUEST.timezone]
 * @param [$_REQUEST.placeName] optional
 * @param [$_REQUEST.state] optional
 * @param [$_REQUEST.country] optional
 */
function Places_geolocation_post()
{
    $user = Users::loggedInUser(true);
    $stream = Places_Location::userStream();
    $oldLatitude = $stream->getAttribute('latitude');
    $oldLongitude = $stream->getAttribute('longitude');
    $oldMiles = $stream->getAttribute('miles');
    $fields = array('accuracy', 'altitude', 'altitudeAccuracy', 'heading', 'latitude', 'longitude', 'speed', 'miles', 'zipcode', 'timezone', 'placeName', 'state', 'country');
    $attributes = Q::take($_REQUEST, $fields);
    if (isset($attributes['latitude']) xor isset($attributes['longitude'])) {
        throw new Q_Exception("When specifying latitude,longitude you must specify both", array('latitude', 'longitude'));
    }
    if (!empty($attributes['zipcode']) and !isset($attributes['latitude'])) {
        $z = new Places_Zipcode();
        $z->countryCode = 'US';
        $z->zipcode = $attributes['zipcode'];
        if ($z->retrieve()) {
            $attributes['latitude'] = $z->latitude;
            $attributes['longitude'] = $z->longitude;
            $attributes['country'] = $z->countryCode;
        } else {
            throw new Q_Exception_MissingRow(array('table' => 'zipcode', 'criteria' => $attributes['zipcode']), 'zipcode');
        }
    }
    $attributes['miles'] = Q::ifset($attributes, 'miles', $stream->getAttribute('miles', Q_Config::expect('Places', 'nearby', 'defaultMiles')));
    if (empty($attributes['zipcode']) and isset($attributes['latitude'])) {
        $zipcodes = Places_Zipcode::nearby($attributes['latitude'], $attributes['longitude'], $attributes['miles'], 1);
        if ($zipcode = $zipcodes ? reset($zipcodes) : null) {
            $attributes['zipcode'] = $zipcode->zipcode;
            $attributes['placeName'] = $zipcode->placeName;
            $attributes['state'] = $zipcode->state;
            $attributes['country'] = $zipcode->countryCode;
        }
    }
    $stream->setAttribute($attributes);
    $stream->save();
    $stream->post($user->id, array('type' => 'Places/location/updated', 'content' => '', 'instructions' => $stream->getAllAttributes()), true);
    $shouldUnsubscribe = !empty($_REQUEST['unsubscribe']) && isset($oldMiles);
    $shouldSubscribe = !empty($_REQUEST['subscribe']);
    $noChange = false;
    $latitude = $stream->getAttribute('latitude');
    $longitude = $stream->getAttribute('longitude');
    $miles = $stream->getAttribute('miles');
    if ($shouldUnsubscribe and $shouldSubscribe and abs($latitude - $oldLatitude) < 0.0001 and abs($longitude - $oldLongitude) < 0.0001 and abs($miles - $oldMiles) < 0.001) {
        $noChange = true;
    }
    if (!$noChange) {
        if ($shouldUnsubscribe or $shouldSubscribe) {
            $myInterests = Streams_Category::getRelatedTo($user->id, 'Streams/user/interests', 'Streams/interest');
            if (!isset($myInterests)) {
                $myInterests = array();
            }
        }
        if ($shouldUnsubscribe) {
            // TODO: implement mass unsubscribe
            foreach ($myInterests as $weight => $info) {
                Places_Nearby::unsubscribe($oldLatitude, $oldLongitude, $oldMiles, $info[0], array('transform' => array('Places_Interest', '_transform'), 'title' => $info[2], 'skipAccess' => true));
            }
            $attributes['unsubscribed'] = Places_Nearby::unsubscribe($oldLatitude, $oldLongitude, $oldMiles);
        }
        if ($shouldSubscribe) {
            // TODO: implement mass subscribe
            foreach ($myInterests as $weight => $info) {
                Places_Nearby::subscribe($latitude, $longitude, $miles, $info[0], array('transform' => array('Places_Interest', '_transform'), 'create' => array('Places_Interest', '_create'), 'title' => $info[2], 'skipAccess' => true));
            }
            $attributes['subscribed'] = Places_Nearby::subscribe($latitude, $longitude, $miles);
        }
    }
    $attributes['stream'] = $stream;
    Q_Response::setSlot('attributes', $attributes);
    Q::event("Places/geolocation", $attributes, 'after');
}
Exemple #7
0
/**
 * Used to set the user's location from geolocation data.
 * @class HTTP Places geolocation
 * @method post
 * @param $_REQUEST
 * @param [$_REQUEST.latitude] The new latitude. If set, must also specify longitude.
 * @param [$_REQUEST.longitude] The new longitude. If set, must also specify latitude.
 * @param [$_REQUEST.zipcode] The new zip code. Can be set instead of latitude, longitude.
 * @param [$_REQUEST.miles] The distance around their location around that the user is interested in
 * @param [$_REQUEST.subscribe] Whether to subscribe to all the local interests at the new location.
 * @param [$_REQUEST.unsubscribe] Whether to unsubscribe from all the local interests at the old location.
 * @param [$_REQUEST.accuracy]
 * @param [$_REQUEST.altitude]
 * @param [$_REQUEST.altitudeAccuracy]
 * @param [$_REQUEST.heading]
 * @param [$_REQUEST.speed]
 * @param [$_REQUEST.timezone]
 * @param [$_REQUEST.placeName] optional
 * @param [$_REQUEST.state] optional
 * @param [$_REQUEST.country] optional
 */
function Places_geolocation_post()
{
    $user = Users::loggedInUser(true);
    $stream = Places_Location::userStream();
    $oldLatitude = $stream->getAttribute('latitude');
    $oldLongitude = $stream->getAttribute('longitude');
    $oldMiles = $stream->getAttribute('miles');
    $fields = array('accuracy', 'altitude', 'altitudeAccuracy', 'heading', 'latitude', 'longitude', 'speed', 'miles', 'zipcode', 'timezone', 'placeName', 'state', 'country');
    $attributes = Q::take($_REQUEST, $fields);
    if (isset($attributes['latitude']) xor isset($attributes['longitude'])) {
        throw new Q_Exception("When specifying latitude,longitude you must specify both", array('latitude', 'longitude'));
    }
    if (!empty($attributes['zipcode']) and !isset($attributes['latitude'])) {
        $z = new Places_Zipcode();
        $z->countryCode = 'US';
        $z->zipcode = $attributes['zipcode'];
        if ($z->retrieve()) {
            $attributes['latitude'] = $z->latitude;
            $attributes['longitude'] = $z->longitude;
            $attributes['country'] = $z->countryCode;
        } else {
            throw new Q_Exception_MissingRow(array('table' => 'zipcode', 'criteria' => $attributes['zipcode']), 'zipcode');
        }
    }
    $attributes['miles'] = Q::ifset($attributes, 'miles', $stream->getAttribute('miles', Q_Config::expect('Places', 'nearby', 'defaultMiles')));
    if (empty($attributes['zipcode']) and isset($attributes['latitude'])) {
        $zipcodes = Places_Zipcode::nearby($attributes['latitude'], $attributes['longitude'], $attributes['miles'], 1);
        if ($zipcode = $zipcodes ? reset($zipcodes) : null) {
            $attributes['zipcode'] = $zipcode->zipcode;
            $attributes['placeName'] = $zipcode->placeName;
            $attributes['state'] = $zipcode->state;
            $attributes['country'] = $zipcode->countryCode;
        }
    }
    $stream->setAttribute($attributes);
    $stream->save();
    $stream->post($user->id, array('type' => 'Places/location/updated', 'content' => '', 'instructions' => $stream->getAllAttributes()), true);
    $shouldUnsubscribe = !empty($_REQUEST['unsubscribe']) && isset($oldMiles);
    $shouldSubscribe = !empty($_REQUEST['subscribe']);
    $noChange = false;
    $latitude = $stream->getAttribute('latitude');
    $longitude = $stream->getAttribute('longitude');
    $miles = $stream->getAttribute('miles');
    if ($shouldUnsubscribe and $shouldSubscribe and abs($latitude - $oldLatitude) < 0.0001 and abs($longitude - $oldLongitude) < 0.0001 and abs($miles - $oldMiles) < 0.001) {
        $noChange = true;
    }
    $attributes['stream'] = $stream;
    Q_Response::setSlot('attributes', $attributes);
    if (!$noChange) {
        // Send the response and keep going.
        // WARN: this potentially ties up the PHP thread for a long time
        $timeLimit = Q_Config::get('Places', 'geolocation', 'timeLimit', 100000);
        ignore_user_abort(true);
        set_time_limit($timeLimit);
        Q_Dispatcher::response(true);
        session_write_close();
        if ($shouldUnsubscribe or $shouldSubscribe) {
            $myInterests = Streams_Category::getRelatedTo($user->id, 'Streams/user/interests', 'Streams/interests');
            if (!isset($myInterests)) {
                $myInterests = array();
            }
        }
        if ($shouldUnsubscribe and $oldLatitude and $oldLongitude and $oldMiles) {
            $results = array();
            foreach ($myInterests as $weight => $info) {
                $publisherId = $info[0];
                if (!isset($results[$publisherId])) {
                    $results[$publisherId] = array();
                }
                $results[$publisherId] = array_merge($results[$publisherId], Places_Interest::streams($publisherId, $oldLatitude, $oldLongitude, $info[2], array('miles' => $oldMiles, 'skipAccess' => true, 'forSubscribers' => true)));
            }
            foreach ($results as $publisherId => $streams) {
                Streams::unsubscribe($user->id, $publisherId, $streams, array('skipAccess' => true));
            }
            $attributes['unsubscribed'] = Places_Nearby::unsubscribe($oldLatitude, $oldLongitude, $oldMiles);
        }
        if ($shouldSubscribe) {
            $results = array();
            foreach ($myInterests as $weight => $info) {
                $publisherId = $info[0];
                if (!isset($results[$publisherId])) {
                    $results[$publisherId] = array();
                }
                $results[$publisherId] = array_merge($results[$publisherId], Places_Interest::streams($publisherId, $latitude, $longitude, $info[2], array('miles' => $miles, 'skipAccess' => true, 'forSubscribers' => true)));
            }
            foreach ($results as $publisherId => $streams) {
                Streams::subscribe($user->id, $publisherId, $streams, array('skipAccess' => true));
            }
            $attributes['subscribed'] = Places_Nearby::subscribe($latitude, $longitude, $miles);
        }
    }
    Q::event("Places/geolocation", $attributes, 'after');
}