/** * 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; }
/** * The default implementation. */ function Q_errors($params) { extract($params); /** * @var Exception $exception * @var boolean $startedResponse */ if (!empty($exception)) { Q_Response::addError($exception); } $errors = Q_Response::getErrors(); $errors_array = Q_Exception::toArray($errors); // Simply return the errors, if this was an AJAX request if ($is_ajax = Q_Request::isAjax()) { try { $errors_json = @Q::json_encode($errors_array); } catch (Exception $e) { $errors_array = array_slice($errors_array, 0, 1); unset($errors_array[0]['trace']); $errors_json = @Q::json_encode($errors_array); } $json = "{\"errors\": {$errors_json}}"; $callback = Q_Request::callback(); switch (strtolower($is_ajax)) { case 'iframe': if (!Q_Response::$batch) { header("Content-type: text/html"); } echo <<<EOT <!doctype html><html lang=en> <head><meta charset=utf-8><title>Q Result</title></head> <body> <script type="text/javascript"> window.result = function () { return {$json} }; </script> </body> </html> EOT; break; case 'json': default: header("Content-type: " . ($callback ? "application/javascript" : "application/json")); echo $callback ? "{$callback}({$json})" : $json; } return; } // Forward internally, if it was requested if ($onErrors = Q_Request::special('onErrors', null)) { $uri1 = Q_Dispatcher::uri(); $uri2 = Q_Uri::from($onErrors); $url2 = $uri2->toUrl(); if (!isset($uri2)) { throw new Q_Exception_WrongValue(array('field' => 'onErrors', 'range' => 'an internal URI reachable from a URL')); } if ($uri1->toUrl() !== $url2) { Q_Dispatcher::forward($uri2); return; // we don't really need this, but it's here anyway } } $params2 = compact('errors', 'exception', 'errors_array', 'exception_array'); if (Q::eventStack('Q/response')) { // Errors happened while rendering response. Just render errors view. return Q::view('Q/errors.php', $params2); } if (!$startedResponse) { try { // Try rendering the response, expecting it to // display the errors along with the rest. $ob = new Q_OutputBuffer(); Q::event('Q/response', $params2); $ob->endFlush(); return; } catch (Exception $e) { if (get_class($e) === 'Q_Exception_DispatcherForward') { throw $e; // if forwarding was requested, do it // for all other errors, continue trying other things } $output = $ob->getClean(); } } if ($errors) { // Try rendering the app's errors response, if any. $app = Q::app(); if (Q::canHandle("{$app}/errors/response/content")) { Q_Dispatcher::forward("{$app}/errors"); } else { echo Q::view("Q/errors.php", compact('errors')); } } if (!empty($e)) { return Q::event('Q/exception', array('exception' => $e)); } }