コード例 #1
0
ファイル: Stream.php プロジェクト: dmitriz/Platform
 /**
  * Set access data for the stream. Access data is calculated:
  *	<ol>
  * 		<li>from read/write/admin level fields of the stream</li>
  *		<li>from labels. Streams_Access record may contain &lt;publisherId&gt;, &lt;streamName&gt;
  *			(allowed exact match or generic name "&lt;streamType&gt;/") and 
  *			&lt;ofContactLabel&gt;. If &lt;publisherId&gt; is recorded in Users_Contact
  *			to have either current user or &lt;ofContactLabel&gt; as contact, access claculation is 
  *			considering such record.</li>
  *		<li>from user. Stream_Access record may contain &lt;publisherId&gt;, &lt;streamName&gt;
  *			(allowed exact match or generic name "&lt;streamType&gt;/") and 
  *			&lt;ofUserId&gt;. Such record is considered in access calculation.</li>
  *	</ol>
  * @method calculateAccess
  * @param {string} $asUserId=null The user relative to whom the access is calculated
  *  If this matches the publisherId, just sets full access and calls publishedByFetcher(true).
  *  If this is '', only returns the streams anybody can see.
  *  If this is null, the logged-in user's id is used, or '' if no one is logged in
  * @param {boolean} $recalculate=false Pass true here to force recalculating even if access was already calculated
  * @param {string} [$actualPublisherId] for internal use only
  * @chainable
  */
 function calculateAccess($asUserId = null, $recalculate = false, $actualPublisherId = null)
 {
     Streams::calculateAccess($asUserId, $this->publisherId, array($this), $recalculate, $actualPublisherId);
     return $this;
 }
コード例 #2
0
ファイル: Streams.php プロジェクト: atirjavid/Platform
 /**
  * Fetches streams from the database.
  * @method fetch
  * @static
  * @param {string} $asUserId
  *  Set this to the user for which you are fetching the streams.
  *  If this matches the publisherId, just returns the streams.
  *  If this is '', only returns the streams anybody can see.
  *  Otherwise, return the streams joined with the calculated access settings.
  *  If you pass null here, then either the logged-in user's id or '' will be used.
  * @param {string} $publisherId
  *  The id of the user publishing these streams
  * @param {string|array|Db_Range} $name
  *  The name of the stream to fetch. Can end in "/" for template streams.
  *  Also it can be an array of stream names, or a custom Db_Range for stream names
  * @param {string} [$fields='*']
  *  Comma delimited list of fields to retrieve in the stream.
  *  Must include at least "publisherId" and "name".
  *  since make up the primary key of the stream table.
  * @param {array} [$options=array()]
  *  Provide additional query options like 'limit', 'offset', 'orderBy', 'where' etc.
  *  See Db_Query_Mysql::options().
  *  @param {boolean} [$options.refetch] Ignore cache of previous calls to fetch, 
  *   and save a new cache if necessary.
  *  @param {boolean} [$options.dontCache] Do not cache the results of
  *   fetching the streams
  * @return {array}
  *  Returns an array of Streams_Stream objects with access info calculated
  *  specifically for $asUserId . Make sure to call the methods 
  *  testReadLevel(), testWriteLevel() and testAdminLevel()
  *  on these streams before using them on the user's behalf.
  */
 static function fetch($asUserId, $publisherId, $name, $fields = '*', $options = array())
 {
     if (!isset($asUserId)) {
         $asUserId = Users::loggedInUser();
         if (!$asUserId) {
             $asUserId = "";
         }
     }
     if ($asUserId instanceof Users_User) {
         $asUserId = $asUserId->id;
     }
     if ($publisherId instanceof Users_User) {
         $publisherId = $publisherId->id;
     }
     if (empty($publisherId) or empty($name)) {
         return array();
     }
     if (is_array($fields)) {
         $options = $fields;
         $fields = '*';
     }
     $allCached = array();
     if (empty($options['refetch'])) {
         $arr = is_array($name) ? $name : array($name);
         $namesToFetch = array();
         foreach ($arr as $n) {
             if (isset(self::$fetch[$asUserId][$publisherId][$n][$fields])) {
                 $allCached[$n] = self::$fetch[$asUserId][$publisherId][$n][$fields];
             } else {
                 $namesToFetch[] = $n;
             }
         }
         if (!is_array($name)) {
             $namesToFetch = $namesToFetch ? $namesToFetch[0] : null;
         }
     } else {
         $namesToFetch = $name;
     }
     $criteria = array('publisherId' => $publisherId, 'name' => $namesToFetch);
     // Get streams and set their default access info
     $allRetrieved = $namesToFetch ? Streams_Stream::select($fields)->where($criteria)->ignoreCache()->options($options)->fetchDbRows(null, '', 'name') : array();
     $streams = $allCached ? array_merge($allCached, $allRetrieved) : $allRetrieved;
     Streams::calculateAccess($asUserId, $publisherId, $streams, false);
     if (is_array($name) and count($name) > 1) {
         // put the streams back in the same internal PHP array order
         // and in the process honor any duplicate names that might have been passed
         $temp = $streams;
         $streams = array();
         foreach ($name as $n) {
             $streams[$n] = isset($temp[$n]) ? $temp[$n] : null;
         }
     }
     $types = array();
     foreach ($streams as $stream) {
         if ($stream) {
             $types[$stream->type] = true;
         }
     }
     $types = array_keys($types);
     self::afterFetchExtended($publisherId, $streams);
     foreach ($types as $type) {
         $cached = array();
         $retrieved = array();
         foreach ($allCached as $n => $s) {
             if ($s->type === $type) {
                 $cached[$n] = $s;
             }
         }
         foreach ($allRetrieved as $n => $s) {
             if ($s->type === $type) {
                 $retrieved[$n] = $s;
             }
         }
         $params = array('streams' => &$streams, 'cached' => $cached, 'retrieved' => $retrieved, 'allCached' => $allCached, 'allRetrieved' => $allRetrieved, 'asUserId' => $asUserId, 'publisherId' => $publisherId, 'name' => $name, 'criteria' => $criteria, 'fields' => $fields, 'options' => $options, 'type' => $type);
         /**
          * @event Streams/fetch/$streamType {after}
          * @param {&array} streams
          * @param {string} asUserId
          * @param {string} publisherId
          * @param {string} name
          * @param {array} criteria
          * @param {string} fields
          * @param {array} options
          */
         Q::event("Streams/fetch/{$type}", $params, 'after', false, $streams);
     }
     if (!empty($option['dontCache'])) {
         foreach ($streams as $n => $stream) {
             self::$fetch[$asUserId][$publisherId][$n][$fields] = $stream;
         }
     }
     return $streams;
 }