static function _create($params, $options) { $title = Q_Utils::normalize($options['title']); $info = $params['info']; $options['name'] = "Places/interest/{$info['geohash']}/{$info['miles']}/{$title}"; return Streams::create(null, $params['publisherId'], 'Places/interest', $options); }
function Streams_0_8_8_Streams_mysql() { $communityId = Users::communityId(); $user = Users_User::fetch($communityId, true); Streams::create($communityId, $communityId, 'Streams/resource', array('name' => 'Streams/invitations', 'readLevel' => 0, 'writeLevel' => 0, 'adminLevel' => 0)); Streams_Access::insert(array('publisherId' => $communityId, 'streamName' => "Streams/invitations", 'ofUserId' => '', 'grantedByUserId' => null, 'ofContactLabel' => "{$app}/admins", 'readLevel' => Streams::$READ_LEVEL['messages'], 'writeLevel' => Streams::$WRITE_LEVEL['close'], 'adminLevel' => Streams::$ADMIN_LEVEL['invite']))->execute(); }
static function _create($params, $options) { $timestamp = $options['timestamp']; $timestamp = $timestamp - $timestamp % 3600; $info = $params['info']; $options['name'] = "Places/timeslot/{$info['geohash']}/{$info['miles']}/h/{$timestamp}"; return Streams::create(null, $params['publisherId'], 'Places/timeslot', $options); }
/** * Post one or more fields here to change the corresponding basic streams for the logged-in user. Fields can include: * "firstName": specify the first name directly * "lastName": specify the last name directly * "fullName": the user's full name, which if provided will be split into first and last name and override them * "gender": the user's gender * "birthday_year": the year the user was born * "birthday_month": the month the user was born * "birthday_day": the day the user was born */ function Streams_basic_post() { Q_Valid::nonce(true); $user = Users::loggedInUser(true); $request = $_REQUEST; $fields = array(); if (!empty($request['birthday_year']) && !empty($request['birthday_month']) && !empty($request['birthday_day'])) { $request['birthday'] = sprintf("%04d-%02d-%02d", $_REQUEST['birthday_year'], $_REQUEST['birthday_month'], $_REQUEST['birthday_day']); } // $request['icon'] = $user->icon; if (isset($request['fullName'])) { $name = Streams::splitFullName($request['fullName']); $request['firstName'] = $name['first']; $request['lastName'] = $name['last']; } foreach (array('firstName', 'lastName', 'birthday', 'gender') as $field) { if (isset($request[$field])) { $fields[] = $field; } } $p = new Q_Tree(); $p->load(STREAMS_PLUGIN_CONFIG_DIR . DS . 'streams.json'); $p->load(APP_CONFIG_DIR . DS . 'streams.json'); $names = array(); foreach ($fields as $field) { $names[] = "Streams/user/{$field}"; } $streams = Streams::fetch($user, $user->id, $names); foreach ($fields as $field) { $name = "Streams/user/{$field}"; $type = $p->get($name, "type", null); if (!$type) { throw new Q_Exception("Missing {$name} type", $field); } $title = $p->get($name, "title", null); if (!$title) { throw new Q_Exception("Missing {$name} title", $field); } $stream = $streams[$name]; if (isset($stream) and $stream->content === (string) $request[$field]) { continue; } if (!isset($stream)) { $stream = Streams::create($user->id, $user->id, $type, array('name' => $name)); } $messageType = $stream->wasRetrieved() ? 'Streams/changed' : 'Streams/created'; $stream->content = (string) $request[$field]; $stream->type = $type; $stream->title = $title; $stream->changed($user->id, $messageType); } }
/** * Get the logged-in user's credits stream * @method userStream * @param {string} [$userId=null] * The id of the user for which the stream is obtained. Defaults to logged-in user. * @param {string} [$asUserId=null] * The id of the user who is trying to obtain it. Defaults to logged-in user. * @param {boolean} [$throwIfNotLoggedIn=false] * Whether to throw a Users_Exception_NotLoggedIn if no user is logged in. * @return {Streams_Stream|null} * @throws {Users_Exception_NotLoggedIn} If user is not logged in and * $throwIfNotLoggedIn is true */ static function userStream($userId = null, $asUserId = null, $throwIfNotLoggedIn = false) { if (!isset($userId)) { $user = Users::loggedInUser($throwIfNotLoggedIn); if (!$user) { return null; } } else { $user = Users_User::fetch($userId, true); } $userId = $user->id; $streamName = 'Awards/user/credits'; $stream = Streams::fetchOne($asUserId, $userId, $streamName); if (!$stream) { $amount = Q_Config::get('Awards', 'credits', 'amounts', 'Users/insertUser', self::DEFAULT_AMOUNT); $stream = Streams::create($userId, $userId, 'Awards/credits', array('name' => 'Awards/user/credits', 'title' => "Credits", 'icon' => 'plugins/Awards/img/credits.png', 'content' => '', 'attributes' => Q::json_encode(compact('amount')))); } return $stream; }
function Streams_0_8_1_Streams_mysql() { $app = Q_Config::expect('Q', 'app'); // template for community stream $stream = new Streams_Stream(); $stream->publisherId = ''; $stream->name = 'Streams/community/'; $stream->type = 'Streams/template'; $stream->title = "Community"; $stream->content = ''; $readLevel = Streams::$READ_LEVEL['content']; $writeLevel = Streams::$WRITE_LEVEL['join']; $adminLevel = Streams::$ADMIN_LEVEL['invite']; $stream->save(); // app community stream, for announcements Streams::create($app, $app, 'Streams/community', array('skipAccess' => true, 'name' => 'Streams/community/main', 'title' => "{$app} Community")); // symlink the labels folder if (!file_exists('Streams')) { Q_Utils::symlink(STREAMS_PLUGIN_FILES_DIR . DS . 'Streams' . DS . 'icons' . DS . 'labels' . DS . 'Streams', USERS_PLUGIN_FILES_DIR . DS . 'Users' . DS . 'icons' . DS . 'Streams'); } }
function Streams_form_post($params = array()) { if (empty($_REQUEST['inputs'])) { throw new Q_Exception_RequiredField(array('field' => 'inputs')); } $inputs = Q::json_decode($_REQUEST['inputs'], true); $user = Users::loggedInUser(true); $r = array_merge($_REQUEST, $params); $streams = array(); foreach ($inputs as $name => $info) { $inputName = "input_{$name}"; if (!isset($r[$inputName])) { continue; } if (!is_array($info) or count($info) < 4) { throw new Q_Exception_WrongValue(array('field' => 'inputs', 'range' => 'array of name => (streamExists, publisherId, streamName, fieldName)')); } list($streamExists, $publisherId, $streamName, $fieldName) = $info; $stream = Streams::fetchOne(null, $publisherId, $streamName); if (!$stream) { if ($user->id !== $publisherId or !Q_Config::get('Streams', 'possibleUserStreams', $streamName, false)) { throw new Users_Exception_NotAuthorized(); } $stream = Streams::create(null, $publisherId, null, array('name' => $streamName)); } $attribute = substr($fieldName, 0, 10) === 'attribute:' ? substr($fieldName, 10) : null; if ($attribute) { $stream->setAttribute($attribute, $r[$inputName]); } else { $stream->{$fieldName} = $r[$inputName]; } $stream->save(); $streams[$stream->name] = $stream; } Q_Response::setSlot('streams', Db::exportArray($streams)); }
function Streams_after_Users_User_saveExecute($params) { // If the username or icon was somehow modified, // update all the avatars for this publisher $modifiedFields = $params['modifiedFields']; $user = $params['row']; $updates = array(); if (isset($modifiedFields['username'])) { $updates['username'] = $modifiedFields['username']; } if (isset($modifiedFields['icon'])) { $updates['icon'] = $modifiedFields['icon']; } if ($user->id === Users::communityId()) { $firstName = Users::communityName(); $lastName = Users::communitySuffix(); $firstName = $firstName ? $firstName : ""; $lastName = $lastName ? $lastName : ""; } else { $firstName = Q::ifset(Streams::$cache, 'register', 'first', ''); $lastName = Q::ifset(Streams::$cache, 'register', 'last', ''); } if ($params['inserted']) { // create some standard streams for them $onInsert = Q_Config::get('Streams', 'onInsert', 'Users_User', array()); if (!$onInsert) { return; } $p = new Q_Tree(); $p->load(STREAMS_PLUGIN_CONFIG_DIR . DS . 'streams.json'); $p->load(APP_CONFIG_DIR . DS . 'streams.json'); $values = array('Streams/user/firstName' => $firstName, 'Streams/user/lastName' => $lastName); // Check for user data from facebook if (!empty(Users::$cache['facebookUserData'])) { $userData = Users::$cache['facebookUserData']; foreach ($userData as $name_fb => $value) { foreach ($p->getAll() as $name => $info) { if (isset($info['name_fb']) and $info['name_fb'] === $name_fb) { $onInsert[] = $name; $values[$name] = $value; } } } } foreach ($onInsert as $name) { $stream = Streams::fetchOne($user->id, $user->id, $name); if (!$stream) { // it shouldn't really be in the db yet $stream = new Streams_Stream(); $stream->publisherId = $user->id; $stream->name = $name; } $stream->type = $p->expect($name, "type"); $stream->title = $p->expect($name, "title"); $stream->content = $p->get($name, "content", ''); // usually empty $stream->readLevel = $p->get($name, 'readLevel', Streams_Stream::$DEFAULTS['readLevel']); $stream->writeLevel = $p->get($name, 'writeLevel', Streams_Stream::$DEFAULTS['writeLevel']); $stream->adminLevel = $p->get($name, 'adminLevel', Streams_Stream::$DEFAULTS['adminLevel']); if ($name === "Streams/user/icon") { $sizes = Q_Config::expect('Users', 'icon', 'sizes'); sort($sizes); $stream->setAttribute('sizes', $sizes); $stream->icon = $user->iconUrl(); } if (isset($values[$name])) { $stream->content = $values[$name]; } $stream->save(); // this also inserts avatars $o = array('userId' => $user->id, 'skipAccess' => true); $so = $p->get($name, "subscribe", array()); if ($so === false) { $stream->join($o); } else { $stream->subscribe(array_merge($o, $so)); } } // Save a greeting stream, to be edited $communityId = Users::communityId(); Streams::create($user->id, $user->id, "Streams/greeting", array('name' => "Streams/greeting/{$communityId}")); // Create some standard labels $label = new Users_Label(); $label->userId = $user->id; $label->label = 'Streams/invited'; $label->icon = 'labels/Streams/invited'; $label->title = 'People I invited'; $label->save(true); $label2 = new Users_Label(); $label2->userId = $user->id; $label2->label = 'Streams/invitedMe'; $label2->icon = 'labels/Streams/invitedMe'; $label2->title = 'Who invited me'; $label2->save(true); // By default, users they invite should see their full name $access = new Streams_Access(); $access->publisherId = $user->id; $access->streamName = 'Streams/user/firstName'; $access->ofUserId = ''; $access->ofContactLabel = 'Streams/invited'; $access->grantedByUserId = $user->id; $access->readLevel = Streams::$READ_LEVEL['content']; $access->writeLevel = -1; $access->adminLevel = -1; $access->save(); $access = new Streams_Access(); $access->publisherId = $user->id; $access->streamName = 'Streams/user/lastName'; $access->ofUserId = ''; $access->ofContactLabel = 'Streams/invited'; $access->grantedByUserId = $user->id; $access->readLevel = Streams::$READ_LEVEL['content']; $access->writeLevel = -1; $access->adminLevel = -1; $access->save(); // NOTE: the above saving of access caused Streams::updateAvatar to run, // insert a Streams_Avatar row for the new user, and properly configure it. } else { if ($modifiedFields) { if ($updates) { Streams_Avatar::update()->set($updates)->where(array('publisherId' => $user->id))->execute(); } foreach ($modifiedFields as $field => $value) { $name = Q_Config::get('Streams', 'onUpdate', 'Users_User', $field, null); if (!$name) { continue; } $stream = isset(Streams::$beingSaved[$field]) ? Streams::$beingSaved[$field] : Streams::fetchOne($user->id, $user->id, $name); if (!$stream) { // it should probably already be in the db continue; } $stream->content = $value; if ($name === "Streams/user/icon") { $sizes = Q_Config::expect('Users', 'icon', 'sizes'); sort($sizes); $attributes = $stream->attributes; $stream->setAttribute('sizes', $sizes); $stream->icon = $changes['icon'] = $user->iconUrl(); } Streams::$beingSavedQuery = $stream->changed($user->id); } } } }
/** * Used by HTTP clients to create a new stream in the system. * @class Streams-stream * @method post * @param {array} [$params] Parameters that can come from the request * @param {string} $params.publisherId Required. The id of the user to publish the stream. * @param {string} $params.type Required. The type of the stream. * @param {string} [$params.Q_Streams_related_publisherId] Optionally indicate the publisher of the stream to relate the newly created to. Used together with the related.streamName option. * @param {string} [$params.Q_Streams_related_streamName] Optionally indicate the name of a stream to relate the newly crated stream to. This is often necessary in order to obtain permissions to create the stream. * @param {bool} [$params.dontSubscribe=false] Pass 1 or true here in order to skip auto-subscribing to the newly created stream. * @param {array} [$params.icon] This is used to upload a custom icon for the stream which will then be saved in different sizes. See fields for Q/image/post method * @param {string} [$params.icon.data] Required if $_FILES is empty. Base64-encoded data URI - see RFC 2397 * @param {string} [$params.icon.path="uploads"] parent path under web dir (see subpath) * @param {string} [$params.icon.subpath=""] subpath that should follow the path, to save the image under * @param {string} [$params.icon.merge=""] path under web dir for an optional image to use as a background * @param {string} [$params.icon.crop] array with keys "x", "y", "w", "h" to crop the original image * @param {string} [$params.icon.save=array("x" => "")] array of $size => $basename pairs * where the size is of the format "WxH", and either W or H can be empty. * @param {array} [$params.file] This is used to upload a custom icon for the stream which will then be saved in different sizes. See fields for Q/image/post method * @param {string} [$params.file.data] Required if $_FILES is empty. Base64-encoded data URI - see RFC 2397 * @param {string} [$params.file.path="uploads"] parent path under web dir (see subpath) * @param {string} [$params.file.subpath=""] subpath that should follow the path, to save the file under * @param {string} [$params.file.name] override name of the file, after the subpath */ function Streams_stream_post($params = array()) { $user = Users::loggedInUser(true); $publisherId = Streams::requestedPublisherId(); if (empty($publisherId)) { $publisherId = $_REQUEST['publisherId'] = $user->id; } $req = array_merge($_REQUEST, $params); $type = Streams::requestedType(true); $types = Q_Config::expect('Streams', 'types'); if (!array_key_exists($type, $types)) { throw new Q_Exception("This app doesn't support streams of type {$type}", 'type'); } if (empty($types[$type]['create'])) { throw new Q_Exception("This app doesn't support directly creating streams of type {$type}", 'type'); } // Should this stream be related to another stream? $relate = array(); $relate['streamName'] = Q_Request::special("Streams.related.streamName", null, $req); if (isset($relate['streamName'])) { $relate['publisherId'] = Q_Request::special("Streams.related.publisherId", $publisherId, $req); $relate['type'] = Q_Request::special("Streams.related.type", "", $req); $relate['weight'] = "+1"; // TODO: introduce ways to have "1" and "+1" for some admins etc. } // Hold on to any icon that was posted $icon = null; if (!empty($req['icon']) and is_array($req['icon'])) { $icon = $req['icon']; unset($req['icon']); } // Hold on to any file that was posted $file = null; if (!empty($req['file']) and is_array($req['file'])) { $file = $req['file']; unset($req['file']); } // Check if client can set the name of this stream if (!empty($req['name'])) { if ($user->id !== $publisherId or !Q_Config::get('Streams', 'possibleUserStreams', $req['name'], false)) { throw new Users_Exception_NotAuthorized(); } } // Create the stream $allowedFields = array_merge(array('publisherId', 'type', 'icon', 'file'), Streams::getExtendFieldNames($type, $user->id === $publisherId)); $fields = Q::take($req, $allowedFields); $stream = Streams::create($user->id, $publisherId, $type, $fields, $relate, $result); Q_Response::setSlot('messageTo', $result['messageTo']->exportArray()); // Process any icon that was posted if ($icon === true) { $icon = array(); } if (is_array($icon)) { if (empty($icon['path'])) { $icon['path'] = 'uploads/Streams'; } if (empty($icon['subpath'])) { $icon['subpath'] = "{$publisherId}/{$stream->name}/icon/" . time(); } Q_Response::setSlot('icon', Q::event("Q/image/post", $icon)); } // Process any file that was posted if ($file === true) { $file = array(); } if ($file) { if (empty($file['path'])) { $file['path'] = 'uploads/Streams'; } if (empty($file['subpath'])) { $file['subpath'] = "{$publisherId}/{$stream->name}/file/" . time(); } Q_Response::setSlot('file', Q::event("Q/file/post", $file)); } $file = Q::ifset($fieldNames, 'file', null); if (is_array($file)) { unset($fieldNames['file']); Q_Response::setSlot('file', Q::event("Q/file/post", $icon)); } // Re-fetch the stream object from the Streams::fetch cache, // since it might have been retrieved and modified to be different // from what is currently in $stream. // This also calculates the access levels on the stream. $stream = Streams::fetchOne($user->id, $publisherId, $stream->name); if (empty($req['dontSubscribe'])) { // autosubscribe to streams you yourself create, using templates $stream->subscribe(); } Streams::$cache['stream'] = $stream; }
/** * Starts a recurring subscription * @param {Streams_Stream} $plan The subscription plan stream * @param {string} [$payments=null] The type of payments processor, could be "authnet" or "stripe". If omitted, the subscription proceeds without any payments. * @param {array} [$options=array()] Options for the subscription * @param {date} [$options.startDate=today] The start date of the subscription * @param {date} [$options.endDate=today+year] The end date of the subscription * @param {Users_User} [$options.user=Users::loggedInUser()] Allows us to set the user to subscribe * @param {Users_User} [$options.publisherId=Users::communityId()] Allows us to override the publisher to subscribe to * @param {string} [$options.description=null] description of the charge, to be sent to customer * @param {string} [$options.metadata=null] any additional metadata to store with the charge * @param {string} [$options.subscription=null] if this charge is related to a subscription stream * @param {string} [$options.subscription.publisherId] * @param {string} [$options.subscription.streamName] * @throws Assets_Exception_DuplicateTransaction * @throws Assets_Exception_HeldForReview * @throws Assets_Exception_ChargeFailed * @return {Streams_Stream} A stream of type 'Assets/subscription' representing this subscription */ static function startSubscription($plan, $payments = null, $options = array()) { if (!isset($options['user'])) { $options['user'] = Users::loggedInUser(true); } $app = Q_Config::expect('Q', 'app'); $user = Q::ifset($options, 'user', Users::loggedInUser(true)); $currency = 'USD'; // TODO: may want to implement support for currency conversion $startDate = Q::ifset($options, 'startDate', date("Y-m-d")); $startDate = date('Y-m-d', strtotime($startDate)); $months = $plan->getAttribute('months', 12); $amount = $plan->getAttribute('amount'); $endDate = date("Y-m-d", strtotime("-1 day", strtotime("+{$months} month", strtotime($startDate)))); $endDate = date('Y-m-d', strtotime($endDate)); $publisherId = Q::ifset($options, 'publisherId', Users::communityId()); $publisher = Users_User::fetch($publisherId); $streamName = "Assets/subscription/{$user->id}/{$plan->name}"; if ($subscription = Streams::fetchOne($publisherId, $publisherId, $streamName)) { return $subscription; // it already started } $attributes = Q::json_encode(array('payments' => $payments, 'planPublisherId' => $plan->publisherId, 'planStreamName' => $plan->name, 'startDate' => $startDate, 'endDate' => $endDate, 'months' => $months, 'amount' => $amount, 'currency' => $currency)); $stream = Streams::create($publisherId, $publisherId, "Assets/subscription", array('name' => $streamName, 'title' => $plan->title, 'readLevel' => Streams::$READ_LEVEL['none'], 'writeLevel' => Streams::$WRITE_LEVEL['none'], 'adminLevel' => Streams::$ADMIN_LEVEL['none'], 'attributes' => $attributes)); $access = new Streams_Access(array('publisherId' => $publisherId, 'streamName' => $streamName, 'ofUserId' => $user->id, 'grantedByUserId' => $app, 'readLevel' => Streams::$READ_LEVEL['max'], 'writeLevel' => -1, 'adminLevel' => -1)); $access->save(); $amount = $plan->getAttribute('amount', null); if (!is_numeric($amount)) { throw new Q_Exception_WrongValue(array('field' => 'amount', 'range' => 'an integer')); } $options['stream'] = $stream; if ($payments) { Assets::charge($payments, $amount, $currency, $options); } /** * @event Assets/startSubscription {before} * @param {Streams_Stream} plan * @param {Streams_Stream} subscription * @param {string} startDate * @param {string} endDate * @return {Users_User} */ Q::event('Assets/startSubscription', compact('plan', 'user', 'publisher', 'stream', 'startDate', 'endDate', 'months', 'currency'), 'after'); return $stream; }
/** * Used to create a new stream * * @param {array} $_REQUEST * @param {String} [$_REQUEST.title] Required. The title of the interest. * @param {String} [$_REQUEST.publisherId] Optional. Defaults to the app name. * @param {String} [$_REQUEST.subscribe] Optional. Defauls to false. Whether to subscribe rather than just join the interest stream. * @return {void} */ function Streams_interest_post() { $user = Users::loggedInUser(true); $title = Q::ifset($_REQUEST, 'title', null); if (!isset($title)) { throw new Q_Exception_RequiredField(array('field' => 'title')); } $app = Q_Config::expect('Q', 'app'); $publisherId = Q::ifset($_REQUEST, 'publisherId', $app); $name = 'Streams/interest/' . Q_Utils::normalize($title); $stream = Streams::fetchOne(null, $publisherId, $name); if (!$stream) { $stream = Streams::create($publisherId, $publisherId, 'Streams/interest', array('name' => $name, 'title' => $title)); $parts = explode(': ', $title, 2); $keywords = implode(' ', $parts); try { $data = Q_Image::pixabay($keywords, array('orientation' => 'horizontal', 'min_width' => '500', 'safesearch' => 'true', 'image_type' => 'photo'), true); } catch (Exception $e) { Q::log("Exception during Streams/interest post: " . $e->getMessage()); $data = null; } if (!empty($data)) { $sizes = Q_Config::expect('Streams', 'icons', 'sizes'); ksort($sizes); $params = array('data' => $data, 'path' => "plugins/Streams/img/icons", 'subpath' => $name, 'save' => $sizes, 'skipAccess' => true); Q_Image::save($params); $stream->icon = $name; } $stream->save(); } $subscribe = !!Q::ifset($_REQUEST, 'subscribe', false); if ($subscribe) { if (!$stream->subscription($user->id)) { $stream->subscribe(); } } else { $stream->join(); } $myInterestsName = 'Streams/user/interests'; $myInterests = Streams::fetchOne($user->id, $user->id, $myInterestsName); if (!$myInterests) { $myInterests = new Streams_Stream(); $myInterests->publisherId = $user->id; $myInterests->name = $myInterestsName; $myInterests->type = 'Streams/category'; $myInterests->title = 'My Interests'; $myInterests->save(); } Streams::relate($user->id, $user->id, 'Streams/user/interests', 'Streams/interest', $publisherId, $name, array('weight' => '+1')); Q_Response::setSlot('publisherId', $publisherId); Q_Response::setSlot('streamName', $name); /** * Occurs when the logged-in user has successfully added an interest via HTTP * @event Streams/interest/post {after} * @param {string} publisherId The publisher of the interest stream * @param {string} title The title of the interest * @param {boolean} subscribe Whether the user subscribed to the interest stream * @param {Users_User} user The logged-in user * @param {Streams_Stream} stream The interest stream * @param {Streams_Stream} myInterests The user's "Streams/user/interests" stream */ Q::event("Streams/interest/add", compact('publisherId', 'title', 'subscribe', 'user', 'stream', 'myInterests'), 'after'); }
function Awards_0_3() { $app = Q_Config::expect('Q', 'app'); Streams::create($app, $app, 'Streams/category', array('name' => 'Awards/plans', 'title' => 'Subscription Plans')); }
/** * 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; }
/** * Adds a stream to represent an area within a location. * Also may add streams to represent the floor and column. * @method addArea * @static * @param {Streams_Stream} $location The location stream * @param {string} $title The title of the area * @param {string} [$floor] The number of the floor on which the area is located * @param {string} [$column] The name of the column on which the area is located * @param {array} [$options=array()] Any options to pass to Streams::create. Also can include: * @param {array} [$options.asUserId=null] Override the first parameter to Streams::create * @return {array} An array of ($area, $floor, $column) */ static function addArea($location, $title, $floor = null, $column = null, $options = array()) { $locationName = $location->name; $parts = explode('/', $locationName); $placeId = $parts[2]; $asUserId = Q::ifset($options, 'asUserId', null); $publisherId = $location->publisherId; $skipAccess = Q::ifset($options, 'skipAccess', true); $floorName = isset($floor) ? "Places/floor/{$placeId}/" . Q_Utils::normalize($floor) : null; $columnName = isset($column) ? "Places/column/{$placeId}/" . Q_Utils::normalize($column) : null; $name = "Places/area/{$placeId}/" . Q_Utils::normalize($title); $area = Streams::fetchOne($asUserId, $publisherId, $name, $options); if (!$area) { $attributes = array('locationName' => $locationName, 'locationTitle' => $location->title, 'locationAddress' => $location->getAttribute('address'), 'floorName' => $floorName, 'columnName' => $columnName); $area = Streams::create($asUserId, $publisherId, 'Places/area', compact('name', 'title', 'skipAccess', 'attributes')); $area->relateTo($location, 'location', $asUserId, $options); if ($floorName) { $name = $floorName; $title = $location->title . " floor {$floor}"; if (!($floor = Streams::fetchOne($asUserId, $publisherId, $name))) { $floor = Streams::create($asUserId, $publisherId, 'Places/floor', compact('name', 'title', 'skipAccess')); } $area->relateTo($floor, 'floor', $asUserId, $options); } if ($columnName) { $name = $columnName; $title = $location->title . " column {$column}"; if (!($column = Streams::fetchOne($asUserId, $publisherId, $name))) { $column = Streams::create($asUserId, $publisherId, 'Places/column', compact('name', 'title', 'skipAccess')); } $area->relateTo($column, 'column', $asUserId, $options); } } else { $column = $columnName ? Streams::fetchOne($asUserId, $publisherId, $columnName) : null; $floor = $floorName ? Streams::fetchOne($asUserId, $publisherId, $floorName) : null; } return array($area, $floor, $column); }
/** * Used by HTTP clients to create a new stream in the system. * @class HTTP Streams stream * @method post * @param {array} [$params] Parameters that can come from the request * @param {string} $params.publisherId Required. The id of the user to publish the stream. * @param {string} $params.type Required. The type of the stream. * @param {string} [$params.Q_Streams_related_publisherId] Optionally indicate the publisher of the stream to relate the newly created to. Used together with the related.streamName option. * @param {string} [$params.Q_Streams_related_streamName] Optionally indicate the name of a stream to relate the newly crated stream to. This is often necessary in order to obtain permissions to create the stream. * @param {bool} [$params.dontSubscribe=false] Pass 1 or true here in order to skip auto-subscribing to the newly created stream. * @param {array} [$params.icon] This is used to upload a custom icon for the stream which will then be saved in different sizes. See fields for Q/image/post method * @param {string} [$params.icon.data] Required if $_FILES is empty. Base64-encoded data URI - see RFC 2397 * @param {string} [$params.icon.path="uploads"] parent path under web dir (see subpath) * @param {string} [$params.icon.subpath=""] subpath that should follow the path, to save the image under * @param {string} [$params.icon.merge=""] path under web dir for an optional image to use as a background * @param {string} [$params.icon.crop] array with keys "x", "y", "w", "h" to crop the original image * @param {string} [$params.icon.save=array("x" => "")] array of $size => $basename pairs * where the size is of the format "WxH", and either W or H can be empty. * @param {array} [$params.file] This is used to upload a custom icon for the stream which will then be saved in different sizes. See fields for Q/image/post method * @param {string} [$params.file.data] Required if $_FILES is empty. Base64-encoded data URI - see RFC 2397 * @param {string} [$params.file.path="uploads"] parent path under web dir (see subpath) * @param {string} [$params.file.subpath=""] subpath that should follow the path, to save the file under * @param {string} [$params.file.name] override name of the file, after the subpath */ function Streams_stream_post($params = array()) { $user = Users::loggedInUser(true); $publisherId = Streams::requestedPublisherId(); if (empty($publisherId)) { $publisherId = $_REQUEST['publisherId'] = $user->id; } $req = array_merge($_REQUEST, $params); $type = Streams::requestedType(true); $types = Q_Config::expect('Streams', 'types'); if (!array_key_exists($type, $types)) { throw new Q_Exception("This app doesn't support streams of type {$type}", 'type'); } $create = Streams_Stream::getConfigField($type, 'create', false); if (!$create) { throw new Q_Exception("This app doesn't let clients directly create streams of type {$type}", 'type'); } // Should this stream be related to another stream? $relate = array(); $relate['streamName'] = Q_Request::special("Streams.related.streamName", null, $req); if (isset($relate['streamName'])) { $relate['publisherId'] = Q_Request::special("Streams.related.publisherId", $publisherId, $req); $relate['type'] = Q_Request::special("Streams.related.type", "", $req); $relate['weight'] = "+1"; // TODO: introduce ways to have "1" and "+1" for some admins etc. } // Split the id for saving files in the filesystem $splitId = Q_Utils::splitId($publisherId); // Hold on to any icon that was posted $icon = null; if (!empty($req['icon']) and is_array($req['icon'])) { $icon = $req['icon']; unset($req['icon']); } // Hold on to any file that was posted $file = null; if (!empty($req['file']) and is_array($req['file'])) { $file = $req['file']; unset($req['file']); } // Check if the user owns the stream if ($user->id === $publisherId) { $asOwner = true; } else { $streamTemplate = Streams_Stream::getStreamTemplate($publisherId, $type, 'Streams_Stream'); $asOwner = $streamTemplate ? $streamTemplate->testAdminLevel('own') : false; } // Check if client can set the name of this stream if (isset($req['name'])) { $possible = Q_Config::get('Streams', 'possibleUserStreams', $req['name'], false); if (!$asOwner or !$possible) { throw new Users_Exception_NotAuthorized(); } } // Get allowed fields $allowedFields = array_merge(array('publisherId', 'name', 'type', 'icon', 'file'), Streams::getExtendFieldNames($type, $asOwner)); $fields = Q::take($req, $allowedFields); // Prevent setting restricted fields if (is_array($create)) { $restrictedFields = array_diff($allowedFields, $create); foreach ($restrictedFields as $fieldName) { if (in_array($fieldName, array('publisherId', 'type'))) { continue; } if (isset($req[$fieldName])) { throw new Users_Exception_NotAuthorized(); } } } // Create the stream $stream = Streams::create($user->id, $publisherId, $type, $fields, $relate, $result); $messageTo = false; if (isset($result['messagesTo'])) { $messageTo = reset($result['messagesTo']); $messageTo = reset($messageTo); if (is_array($messageTo)) { $messageTo = reset($messageTo); } $messageTo = $messageTo->exportArray(); } Q_Response::setSlot('messageTo', $messageTo); // Process any icon that was posted if ($icon === true) { $icon = array(); } if (is_array($icon)) { if (empty($icon['path'])) { $icon['path'] = 'uploads/Streams'; } if (empty($icon['subpath'])) { $icon['subpath'] = "{$splitId}/{$stream->name}/icon/" . time(); } Q_Response::setSlot('icon', Q::event("Q/image/post", $icon)); // the Streams/after/Q_image_save hook saves some attributes } // Process any file that was posted if ($file === true) { $file = array(); } if (is_array($file)) { if (empty($file['path'])) { $file['path'] = 'uploads/Streams'; } if (empty($file['subpath'])) { $file['subpath'] = "{$splitId}/{$stream->name}/file/" . time(); } Q_Response::setSlot('file', Q::event("Q/file/post", $file)); // the Streams/after/Q_file_save hook saves some attributes } // Re-fetch the stream object from the Streams::fetch cache, // since it might have been retrieved and modified to be different // from what is currently in $stream. // This also calculates the access levels on the stream. $stream = Streams::fetchOne($user->id, $publisherId, $stream->name); if (empty($req['dontSubscribe'])) { // autosubscribe to streams you yourself create, using templates $stream->subscribe(); } Streams::$cache['stream'] = $stream; }
/** * Get (if exist) or create shipment stream with type=describing for current user * @method shipment * @static * @return Object */ static function shipment() { $env = self::getVars(); // Collect streams for shipments. Relations: "describing", "scheduled", "confirmed", "shipping", "canceled", "returned" $streamsRes = Streams::related($env->userId, $env->communityId, $env->shipmentsStreamName, true, array("type" => "describing")); // if stream exists - return one if ($streamsRes[1]) { return reset($streamsRes[1]); } // if no streams with relation "describing" - create this stream // get last scheduled shippment stream $streamsScheduled = self::getRealShipments(); // find most new stream processed (not describing) $lastScheduled = false; foreach ($streamsScheduled[0] as $streamScheduled) { if (!$lastScheduled) { $lastScheduled = $streamScheduled; continue; } if ($streamScheduled->fields["insertedTime"] > $lastScheduled->fields["insertedTime"]) { $lastScheduled = $streamScheduled; } } // set Stream object instead of related object if ($lastScheduled) { $lastScheduled = $streamsScheduled[1][$lastScheduled->fields["fromStreamName"]]; $lastCarrier = json_decode($lastScheduled->fields["carrier"]); } // default carrier if no scheduled shippments if (!isset($lastCarrier) || !is_object($lastCarrier)) { $lastCarrier = json_decode("{name: 'TNT'}"); } return Streams::create($env->communityId, $env->communityId, 'Shipping/shipment', array("readLevel" => Streams::$READ_LEVEL['messages'], "writeLevel" => Streams::$WRITE_LEVEL['edit'], "skipAccess" => true, 'attributes' => '{"carrier":"' . $lastCarrier->name . '"}', 'title' => ''), array('publisherId' => $env->communityId, 'streamName' => $env->shipmentsStreamName, 'type' => 'describing', 'weight' => time())); }