/** * Returns a 'success' response. * * @param $entry * @return void */ private function _returnSuccess($entry, $faked = false) { $successEvent = new GuestEntriesEvent($this, array('entry' => $entry, 'faked' => $faked)); craft()->guestEntries->onSuccess($successEvent); if (craft()->request->isAjaxRequest()) { $return['success'] = true; $return['id'] = $entry->id; $return['title'] = $entry->title; if (craft()->request->isCpRequest()) { $return['cpEditUrl'] = $entry->getCpEditUrl(); } $return['authorUsername'] = $entry->getAuthor()->username; $return['dateCreated'] = DateTimeHelper::toIso8601($entry->dateCreated); $return['dateUpdated'] = DateTimeHelper::toIso8601($entry->dateUpdated); $return['postDate'] = $entry->postDate ? DateTimeHelper::toIso8601($entry->postDate) : null; $this->returnJson($return); } else { craft()->userSession->setNotice(Craft::t('Entry saved.')); // TODO: Remove for 2.0 if (isset($_POST['redirect']) && mb_strpos($_POST['redirect'], '{entryId}') !== false) { Craft::log('The {entryId} token within the ‘redirect’ param on entries/saveEntry requests has been deprecated. Use {id} instead.', LogLevel::Warning); $_POST['redirect'] = str_replace('{entryId}', '{id}', $_POST['redirect']); } $this->redirectToPostedUrl($entry); } }
/** * Saves an entry. * * @return null */ public function actionSaveEntry() { $this->requirePostRequest(); $entry = $this->_getEntryModel(); // Permission enforcement $this->enforceEditEntryPermissions($entry); $userSessionService = craft()->userSession; $currentUser = $userSessionService->getUser(); if ($entry->id) { // Is this another user's entry (and it's not a Single)? if ($entry->authorId != $currentUser->id && $entry->getSection()->type != SectionType::Single) { if ($entry->enabled) { // Make sure they have permission to make live changes to those $userSessionService->requirePermission('publishPeerEntries:' . $entry->sectionId); } } } // Populate the entry with post data $this->_populateEntryModel($entry); // Even more permission enforcement if ($entry->enabled) { if ($entry->id) { $userSessionService->requirePermission('publishEntries:' . $entry->sectionId); } else { if (!$currentUser->can('publishEntries:' . $entry->sectionId)) { $entry->enabled = false; } } } // Save the entry (finally!) if (craft()->entries->saveEntry($entry)) { if (craft()->request->isAjaxRequest()) { $return['success'] = true; $return['id'] = $entry->id; $return['title'] = $entry->title; $return['cpEditUrl'] = $entry->getCpEditUrl(); $author = $entry->getAuthor()->getAttributes(); if (isset($author['password'])) { unset($author['password']); } $return['author'] = $author; $return['dateCreated'] = DateTimeHelper::toIso8601($entry->dateCreated); $return['dateUpdated'] = DateTimeHelper::toIso8601($entry->dateUpdated); $return['postDate'] = $entry->postDate ? DateTimeHelper::toIso8601($entry->postDate) : null; $this->returnJson($return); } else { $userSessionService->setNotice(Craft::t('Entry saved.')); if (isset($_POST['redirect']) && mb_strpos($_POST['redirect'], '{entryId}') !== false) { craft()->deprecator->log('EntriesController::actionSaveEntry():entryId_redirect', 'The {entryId} token within the ‘redirect’ param on entries/saveEntry requests has been deprecated. Use {id} instead.'); $_POST['redirect'] = str_replace('{entryId}', '{id}', $_POST['redirect']); } $this->redirectToPostedUrl($entry); } } else { if (craft()->request->isAjaxRequest()) { $this->returnJson(array('errors' => $entry->getErrors())); } else { $userSessionService->setError(Craft::t('Couldn’t save entry.')); // Send the entry back to the template craft()->urlManager->setRouteVariables(array('entry' => $entry)); } } }