/** * Gets relations. At most one of toStreamName, toStreamName can be an array. * @method getRelations * @private * @param {string} $asUserId * The user who is fetching * @param {string} $toPublisherId * The publisher of the category * @param {string|array} $toStreamName * The name of the category. May be turned into an array. * @param {string} $type * The type of the relation. * @param {string} $fromPublisherId * The publisher of the member stream(s) * @param {string|array} $fromStreamName * The name of the member stream(s). May be turned into an array. * @param {array} $relatedTo reference to array of Streams_RelatedTo to fill * @param {array} $relatedFrom reference to array of Streams_RelatedFrom to fill * @param {array} $categories reference to array of Streams_Stream to fill with categories * @param {array} $streams reference to array of Streams_Stream to fill with streams * @param {array} $options=array() An array of options that can include: * @param {boolean} [$options.skipAccess=false] If true, skips the access checks and just relates the stream to the category */ private static function getRelations(&$asUserId, $toPublisherId, &$toStreamName, $type, $fromPublisherId, &$fromStreamName, &$relatedTo, &$relatedFrom, &$categories, &$streams, &$arrayField, &$options = array()) { if (!isset($asUserId)) { $asUserId = Users::loggedInUser(true)->id; } else { if ($asUserId instanceof Users_User) { $asUserId = $asUserId->id; } } if ($toPublisherId instanceof Users_User) { $toPublisherId = $toPublisherId->id; } if (!isset($options)) { $options = array(); } if (is_array($toStreamName)) { if (is_array($fromStreamName)) { throw new Q_Exception("toStreamName and fromStreamName can't both be arrays"); } $arrayField = 'toStreamName'; } else { if (is_array($fromStreamName)) { $arrayField = 'fromStreamName'; } else { $toStreamName = array($toStreamName); $arrayField = 'toStreamName'; } } // Check access to category stream, the stream to which other streams are related $categories = Streams::fetch($asUserId, $toPublisherId, $toStreamName); if (empty($options['skipAccess'])) { foreach ($categories as $c) { if (!$c->testWriteLevel('relate')) { throw new Users_Exception_NotAuthorized(); } } } // Fetch member streams, the streams which are being related $streams = Streams::fetch($asUserId, $fromPublisherId, $fromStreamName); $criteria = compact('toPublisherId', 'toStreamName', 'type', 'fromPublisherId', 'fromStreamName'); // Fetch relatedTo if ($relatedTo !== false) { $relatedTo = Streams_RelatedTo::select('*')->where($criteria)->fetchDbRows(null, null, $arrayField); } // Fetch relatedFrom if ($relatedFrom !== false) { $relatedFrom = Streams_RelatedFrom::select('*')->where($criteria)->fetchDbRows(null, null, $arrayField); } // Recover from inconsistency: // if one exists but not the other, delete both $removeRT = $removeRF = $fieldRT = $fieldRF = array(); foreach ($relatedTo as $sn => $rt) { if (empty($relatedFrom[$sn])) { $removeRT[] = $sn; $fieldRT[] = $rt->{$arrayField}; } } foreach ($relatedFrom as $sn => $rf) { if (empty($relatedTo[$sn])) { $removeRF[] = $sn; $fieldRF[] = $rf->{$arrayField}; } } if ($removeRT) { foreach ($removeRT as $sn) { unset($relatedTo[$sn]); } $criteria2 = $criteria; $criteria2[$arrayField] = $fieldRT; Streams_RelatedTo::delete()->where($criteria2)->execute(); } if ($removeRF) { foreach ($removeRF as $sn) { unset($relatedFrom[$sn]); } $criteria2 = $criteria; $criteria2[$arrayField] = $fieldRF; Streams_RelatedFrom::delete()->where($criteria2)->execute(); } }
/** * Update relation of shipment stream to $newState * @method updateShipment * @static * @param object $stream shipment stream object * @param string $newState new relation */ static function updateShipment($stream, $newState) { $env = self::getVars(); $asUserId = $stream->getAttribute("asUser", $env->userId); $toStreamName = $env->shipmentsStreamName; // asUserId shipmentsStreamName if ($asUserId != $env->userId) { $toStreamName = "Shipping/shipments/" . $asUserId; } $toStream = Streams::fetchOne($env->communityId, $env->communityId, $toStreamName); Streams_RelatedTo::delete()->where(array('toPublisherId' => $env->communityId, 'toStreamName' => $toStreamName, 'fromPublisherId' => $stream->publisherId, 'fromStreamName' => $stream->name))->execute(); $stream->relateTo($toStream, $newState, $env->communityId, array("weight" => time())); //$stream->unrelateTo($toStream, $oldState, $userId); $stream->setAttribute("relation", $newState); $stream->save(); }