mirror of
https://we.phorge.it/source/phorge.git
synced 2025-02-03 10:28:23 +01:00
cf346d3c81
Summary: If a page has not been deleted, this adds an action button to delete the document in the menu on the Phriction Page. Test Plan: 1. Created a document, checked whether "Delete Document" button was visible. 2. Clicked on "Delete Document" button, checked that the document had been deleted. 3. Went back to document page, checked that the "Delete Document" button no longer existed. Reviewers: epriestley CC: aran, Korvin Maniphest Tasks: T2385 Differential Revision: https://secure.phabricator.com/D4636
265 lines
7.5 KiB
PHP
265 lines
7.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group phriction
|
|
*/
|
|
final class PhrictionEditController
|
|
extends PhrictionController {
|
|
|
|
private $id;
|
|
|
|
public function willProcessRequest(array $data) {
|
|
$this->id = idx($data, 'id');
|
|
}
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
$user = $request->getUser();
|
|
|
|
if ($this->id) {
|
|
$document = id(new PhrictionDocument())->load($this->id);
|
|
if (!$document) {
|
|
return new Aphront404Response();
|
|
}
|
|
|
|
$revert = $request->getInt('revert');
|
|
if ($revert) {
|
|
$content = id(new PhrictionContent())->loadOneWhere(
|
|
'documentID = %d AND version = %d',
|
|
$document->getID(),
|
|
$revert);
|
|
if (!$content) {
|
|
return new Aphront404Response();
|
|
}
|
|
} else {
|
|
$content = id(new PhrictionContent())->load($document->getContentID());
|
|
}
|
|
|
|
} else {
|
|
$slug = $request->getStr('slug');
|
|
$slug = PhabricatorSlug::normalize($slug);
|
|
if (!$slug) {
|
|
return new Aphront404Response();
|
|
}
|
|
|
|
$document = id(new PhrictionDocument())->loadOneWhere(
|
|
'slug = %s',
|
|
$slug);
|
|
|
|
if ($document) {
|
|
$content = id(new PhrictionContent())->load($document->getContentID());
|
|
} else {
|
|
if (PhrictionDocument::isProjectSlug($slug)) {
|
|
$project = id(new PhabricatorProject())->loadOneWhere(
|
|
'phrictionSlug = %s',
|
|
PhrictionDocument::getProjectSlugIdentifier($slug));
|
|
if (!$project) {
|
|
return new Aphront404Response();
|
|
}
|
|
}
|
|
$document = new PhrictionDocument();
|
|
$document->setSlug($slug);
|
|
|
|
$content = new PhrictionContent();
|
|
$content->setSlug($slug);
|
|
|
|
$default_title = PhabricatorSlug::getDefaultTitle($slug);
|
|
$content->setTitle($default_title);
|
|
}
|
|
}
|
|
|
|
if ($request->getBool('nodraft')) {
|
|
$draft = null;
|
|
$draft_key = null;
|
|
} else {
|
|
if ($document->getPHID()) {
|
|
$draft_key = $document->getPHID().':'.$content->getVersion();
|
|
} else {
|
|
$draft_key = 'phriction:'.$content->getSlug();
|
|
}
|
|
$draft = id(new PhabricatorDraft())->loadOneWhere(
|
|
'authorPHID = %s AND draftKey = %s',
|
|
$user->getPHID(),
|
|
$draft_key);
|
|
}
|
|
|
|
require_celerity_resource('phriction-document-css');
|
|
|
|
$e_title = true;
|
|
$notes = null;
|
|
$errors = array();
|
|
|
|
if ($request->isFormPost()) {
|
|
$title = $request->getStr('title');
|
|
$notes = $request->getStr('description');
|
|
|
|
if (!strlen($title)) {
|
|
$e_title = 'Required';
|
|
$errors[] = 'Document title is required.';
|
|
} else {
|
|
$e_title = null;
|
|
}
|
|
|
|
if ($document->getID()) {
|
|
if ($content->getTitle() == $title &&
|
|
$content->getContent() == $request->getStr('content')) {
|
|
|
|
$dialog = new AphrontDialogView();
|
|
$dialog->setUser($user);
|
|
$dialog->setTitle('No Edits');
|
|
$dialog->appendChild(
|
|
'<p>You did not make any changes to the document.</p>');
|
|
$dialog->addCancelButton($request->getRequestURI());
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
}
|
|
} else if (!strlen($request->getStr('content'))) {
|
|
|
|
// We trigger this only for new pages. For existing pages, deleting
|
|
// all the content counts as deleting the page.
|
|
|
|
$dialog = new AphrontDialogView();
|
|
$dialog->setUser($user);
|
|
$dialog->setTitle('Empty Page');
|
|
$dialog->appendChild(
|
|
'<p>You can not create an empty document.</p>');
|
|
$dialog->addCancelButton($request->getRequestURI());
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
}
|
|
|
|
if (!count($errors)) {
|
|
$editor = id(PhrictionDocumentEditor::newForSlug($document->getSlug()))
|
|
->setActor($user)
|
|
->setTitle($title)
|
|
->setContent($request->getStr('content'))
|
|
->setDescription($notes);
|
|
|
|
$editor->save();
|
|
|
|
if ($draft) {
|
|
$draft->delete();
|
|
}
|
|
|
|
$uri = PhrictionDocument::getSlugURI($document->getSlug());
|
|
return id(new AphrontRedirectResponse())->setURI($uri);
|
|
}
|
|
}
|
|
|
|
$error_view = null;
|
|
if ($errors) {
|
|
$error_view = id(new AphrontErrorView())
|
|
->setTitle('Form Errors')
|
|
->setErrors($errors);
|
|
}
|
|
|
|
if ($document->getID()) {
|
|
$panel_header = 'Edit Phriction Document';
|
|
$submit_button = 'Save Changes';
|
|
} else {
|
|
$panel_header = 'Create New Phriction Document';
|
|
$submit_button = 'Create Document';
|
|
}
|
|
|
|
$uri = $document->getSlug();
|
|
$uri = PhrictionDocument::getSlugURI($uri);
|
|
$uri = PhabricatorEnv::getProductionURI($uri);
|
|
|
|
$cancel_uri = PhrictionDocument::getSlugURI($document->getSlug());
|
|
|
|
if ($draft &&
|
|
strlen($draft->getDraft()) &&
|
|
($draft->getDraft() != $content->getContent())) {
|
|
$content_text = $draft->getDraft();
|
|
|
|
$discard = phutil_render_tag(
|
|
'a',
|
|
array(
|
|
'href' => $request->getRequestURI()->alter('nodraft', true),
|
|
),
|
|
'discard this draft');
|
|
|
|
$draft_note = new AphrontErrorView();
|
|
$draft_note->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
|
|
$draft_note->setTitle('Recovered Draft');
|
|
$draft_note->appendChild(
|
|
'<p>Showing a saved draft of your edits, you can '.$discard.'.</p>');
|
|
} else {
|
|
$content_text = $content->getContent();
|
|
$draft_note = null;
|
|
}
|
|
|
|
$form = id(new AphrontFormView())
|
|
->setUser($user)
|
|
->setWorkflow(true)
|
|
->setAction($request->getRequestURI()->getPath())
|
|
->addHiddenInput('slug', $document->getSlug())
|
|
->addHiddenInput('nodraft', $request->getBool('nodraft'))
|
|
->appendChild(
|
|
id(new AphrontFormTextControl())
|
|
->setLabel('Title')
|
|
->setValue($content->getTitle())
|
|
->setError($e_title)
|
|
->setName('title'))
|
|
->appendChild(
|
|
id(new AphrontFormStaticControl())
|
|
->setLabel('URI')
|
|
->setValue($uri))
|
|
->appendChild(
|
|
id(new PhabricatorRemarkupControl())
|
|
->setLabel('Content')
|
|
->setValue($content_text)
|
|
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
|
->setName('content')
|
|
->setID('document-textarea')
|
|
->setUser($user))
|
|
->appendChild(
|
|
id(new AphrontFormTextControl())
|
|
->setLabel('Edit Notes')
|
|
->setValue($notes)
|
|
->setError(null)
|
|
->setName('description'))
|
|
->appendChild(
|
|
id(new AphrontFormSubmitControl())
|
|
->addCancelButton($cancel_uri)
|
|
->setValue($submit_button));
|
|
|
|
$panel = id(new AphrontPanelView())
|
|
->setWidth(AphrontPanelView::WIDTH_WIDE)
|
|
->setHeader($panel_header)
|
|
->appendChild($form);
|
|
|
|
$preview_panel =
|
|
'<div class="aphront-panel-preview aphront-panel-preview-wide">
|
|
<div class="phriction-document-preview-header">
|
|
Document Preview
|
|
</div>
|
|
<div id="document-preview">
|
|
<div class="aphront-panel-preview-loading-text">
|
|
Loading preview...
|
|
</div>
|
|
</div>
|
|
</div>';
|
|
|
|
Javelin::initBehavior(
|
|
'phriction-document-preview',
|
|
array(
|
|
'preview' => 'document-preview',
|
|
'textarea' => 'document-textarea',
|
|
'uri' => '/phriction/preview/?draftkey='.$draft_key,
|
|
));
|
|
|
|
return $this->buildStandardPageResponse(
|
|
array(
|
|
$draft_note,
|
|
$error_view,
|
|
$panel,
|
|
$preview_panel,
|
|
),
|
|
array(
|
|
'title' => 'Edit Document',
|
|
));
|
|
}
|
|
|
|
}
|