Example #1
0
 /**
  * Does necessary preparations for saving a stream in the database.
  * @method beforeSave
  * @param {array} $modifiedFields
  *	The array of fields
  * @return {array}
  * @throws {Exception}
  *	If mandatory field is not set
  */
 function beforeSave($modifiedFields)
 {
     if (empty($this->attributes)) {
         $this->attributes = '{}';
     }
     if (!$this->retrieved) {
         // Generate a unique name for the stream
         if (!isset($modifiedFields['name'])) {
             $this->name = $modifiedFields['name'] = Streams::db()->uniqueId(Streams_Stream::table(), 'name', array('publisherId' => $this->publisherId), array('prefix' => $this->type . '/Q'));
         }
         // we don't want user to update private fields but will set initial values to them
         $privateFieldNames = self::getConfigField($this->type, 'private', array());
         // magic fields are handled by parent method
         $magicFieldNames = array('insertedTime', 'updatedTime');
         $privateFieldNames = array_diff($privateFieldNames, $magicFieldNames);
         $streamTemplate = $this->getStreamTemplate('Streams_Stream');
         $fieldNames = Streams_Stream::fieldNames();
         if ($streamTemplate) {
             // if template exists copy all non-PK and non-magic fields from template
             foreach (array_diff($fieldNames, $this->getPrimaryKey(), $magicFieldNames) as $field) {
                 if (in_array($field, $privateFieldNames) || !array_key_exists($field, $modifiedFields)) {
                     $this->{$field} = $modifiedFields[$field] = $streamTemplate->{$field};
                 }
             }
         } else {
             // otherwise (no template) set all private fields to defaults
             foreach ($privateFieldNames as $field) {
                 $defaults = self::getConfigField($this->type, 'defaults', Streams_Stream::$DEFAULTS);
                 $this->{$field} = $modifiedFields[$field] = Q::ifset($defaults, $field, null);
             }
         }
         // Assign default values to fields that haven't been set yet
         foreach (array_diff($fieldNames, $magicFieldNames) as $field) {
             if (!array_key_exists($field, $this->fields) and !array_key_exists($field, $modifiedFields)) {
                 $defaults = self::getConfigField($this->type, 'defaults', Streams_Stream::$DEFAULTS);
                 $this->{$field} = $modifiedFields[$field] = Q::ifset($defaults, $field, null);
             }
         }
         // Get all access templates and save corresponding access
         $type = true;
         $accessTemplates = $this->getStreamTemplate('Streams_Access', $type);
         for ($i = 1; $i <= 3; ++$i) {
             foreach ($accessTemplates[$i] as $template) {
                 $access = new Streams_Access();
                 $access->copyFrom($template->toArray());
                 $access->publisherId = $this->publisherId;
                 $access->streamName = $this->name;
                 if (!$access->save(true)) {
                     return false;
                     // JUNK: this leaves junk in the database, but preserves consistency
                 }
             }
         }
     }
     /**
      * @event Streams/Stream/save/$streamType {before}
      * @param {Streams_Stream} stream
      * @return {false} To cancel further processing
      */
     $params = array('stream' => $this, 'modifiedFields' => $modifiedFields);
     if (false === Q::event("Streams/Stream/save/{$this->type}", $params, 'before')) {
         return false;
     }
     foreach ($this->fields as $name => $value) {
         if (!empty($this->fieldsModified[$name])) {
             $modifiedFields[$name] = $value;
         }
     }
     $this->beforeSaveExtended($modifiedFields);
     $result = parent::beforeSave($modifiedFields);
     // Assume that the stream's name is not being changed
     $fields = array('Streams/user/firstName' => false, 'Streams/user/lastName' => false, 'Streams/user/username' => 'username', 'Streams/user/icon' => 'icon');
     if (!isset($fields[$this->name])) {
         return $result;
     }
     $field = $this->name === 'Streams/user/icon' ? 'icon' : 'content';
     $wasModified = !empty($this->fieldsModified[$field]) or !empty($this->fieldsModified['readLevel']);
     if (!$wasModified) {
         return $result;
     }
     if ($publicField = $fields[$this->name] and !Q::eventStack('Db/Row/Users_User/saveExecute')) {
         Streams::$beingSaved[$publicField] = $this;
         try {
             $user = Users_User::fetch($this->publisherId, true);
             $user->{$publicField} = $modifiedFields[$field];
             $user->save();
         } catch (Exception $e) {
             Streams::$beingSaved[$publicField] = array();
             throw $e;
         }
         Streams::$beingSaved[$publicField] = array();
         return Streams::$beingSavedQuery;
     }
     if ($this->retrieved and !$publicField) {
         // Update all avatars corresponding to access rows for this stream
         $taintedAccess = Streams_Access::select('*')->where(array('publisherId' => $this->publisherId, 'streamName' => $this->name))->fetchDbRows();
         Streams::updateAvatars($this->publisherId, $taintedAccess, $this, true);
     }
     return $result;
 }