예제 #1
0
 /**
  * Get a Places/location stream published by a publisher for a given placeId.
  * This is used to cache information from the Google Places API.
  * @method stream
  * @static
  * @param {string} $publisherId
  * @param {string} $placeId The id of the place in Google Places
  * @param {boolean} $throwIfBadValue
  *  Whether to throw Q_Exception if the result contains a bad value
  * @return {Streams_Stream|null}
  * @throws {Q_Exception} if a bad value is encountered and $throwIfBadValue is true
  */
 static function stream($publisherId, $placeId, $throwIfBadValue = false)
 {
     if (empty($placeId)) {
         if ($throwIfBadValue) {
             throw new Q_Exception_RequiredField(array('field' => 'id'));
         }
         return null;
     }
     // sanitize the ID
     $characters = '/[^A-Za-z0-9]+/';
     $result = preg_replace($characters, '_', $placeId);
     // see if it's already in the system
     $location = new Streams_Stream();
     $location->publisherId = $publisherId;
     $location->name = "Places/location/{$placeId}";
     if ($location->retrieve()) {
         $ut = $location->updatedTime;
         if (isset($ut)) {
             $db = $location->db();
             $ut = $db->fromDateTime($ut);
             $ct = $db->getCurrentTimestamp();
             $cd = Q_Config::get('Places', 'cache', 'duration', 60 * 60 * 24 * 30);
             if ($ct - $ut < $cd) {
                 // there is a cached location stream that is still viable
                 return $location;
             }
         }
     }
     $key = Q_Config::expect('Places', 'google', 'keys', 'server');
     $query = http_build_query(array('key' => $key, 'placeid' => $placeId));
     $url = "https://maps.googleapis.com/maps/api/place/details/json?{$query}";
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $json = curl_exec($ch);
     curl_close($ch);
     $response = json_decode($json, true);
     if (empty($response['result'])) {
         throw new Q_Exception("Places_Location::stream: Couldn't obtain place information for {$placeId}");
     }
     if (!empty($response['error_message'])) {
         throw new Q_Exception("Places_Location::stream: " . $response['error_message']);
     }
     $result = $response['result'];
     $attributes = array('title' => $result['name'], 'latitude' => $result['geometry']['location']['lat'], 'longitude' => $result['geometry']['location']['lng'], 'viewport' => $result['geometry']['viewport'], 'phoneNumber' => Q::ifset($result, 'international_phone_number', null), 'phoneFormatted' => Q::ifset($result, 'formatted_phone_number', null), 'rating' => Q::ifset($result, 'rating', null), 'address' => Q::ifset($result, 'formatted_address', null));
     $location->title = $result['name'];
     $location->setAttribute($attributes);
     $location->type = 'Places/location';
     $location->save();
     return $location;
 }
예제 #2
0
 /**
  * Closes a stream, which prevents anyone from posting messages to it
  * unless they have WRITE_LEVEL >= "close", as well as attempting to remove
  * all relations to other streams. A "cron job" can later go and delete
  * closed streams. The reason you should avoid deleting streams right away
  * is that other subscribers may still want to receive the last messages
  * posted to the stream.
  * @method close
  * @param {string} $asUserId The id of the user who would be closing the stream
  * @param {string} $publisherId The id of the user publishing the stream
  * @param {string} $streamName The name of the stream
  * @param {array} [$options=array()] Can include "skipAccess"
  * @static
  */
 static function close($asUserId, $publisherId, $streamName, $options = array())
 {
     $stream = new Streams_Stream();
     $stream->publisherId = $publisherId;
     $stream->name = $streamName;
     if (!$stream->retrieve()) {
         throw new Q_Exception_MissingRow(array('table' => 'stream', 'criteria' => "{publisherId: '{$publisherId}', name: '{$streamName}'}"));
     }
     // Authorization check
     if (empty($options['skipAccess'])) {
         if ($asUserId !== $publisherId) {
             $stream->calculateAccess($asUserId);
             if (!$stream->testWriteLevel('close')) {
                 throw new Users_Exception_NotAuthorized();
             }
         }
     }
     // Clean up relations from other streams to this category
     list($relations, $related) = Streams::related($asUserId, $stream->publisherId, $stream->name, true);
     foreach ($relations as $r) {
         try {
             Streams::unrelate($asUserId, $r->fromPublisherId, $r->fromStreamName, $r->type, $stream->publisherId, $stream->name);
         } catch (Exception $e) {
         }
     }
     // Clean up relations from this stream to categories
     list($relations, $related) = Streams::related($asUserId, $stream->publisherId, $stream->name, false);
     foreach ($relations as $r) {
         try {
             Streams::unrelate($asUserId, $r->toPublisherId, $r->toStreamName, $r->type, $stream->publisherId, $stream->name);
         } catch (Exception $e) {
         }
     }
     $result = false;
     try {
         $db = $stream->db();
         $stream->closedTime = $closedTime = $db->toDateTime($db->getCurrentTimestamp());
         if ($stream->save()) {
             $stream->post($asUserId, array('type' => 'Streams/closed', 'content' => '', 'instructions' => compact('closedTime')), true);
             $result = true;
         }
     } catch (Exception $e) {
         throw $e;
     }
     return $result;
 }