2011-07-11 21:34:53 +02:00
|
|
|
<?php
|
|
|
|
|
2011-09-14 17:02:31 +02:00
|
|
|
/**
|
|
|
|
* @group phriction
|
|
|
|
*/
|
2012-03-10 00:46:25 +01:00
|
|
|
final class PhrictionEditController
|
2011-07-11 21:34:53 +02:00
|
|
|
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();
|
|
|
|
}
|
2011-07-17 20:06:02 +02:00
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
2011-08-31 21:00:34 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$slug = $request->getStr('slug');
|
2012-04-10 23:18:20 +02:00
|
|
|
$slug = PhabricatorSlug::normalize($slug);
|
2011-08-31 21:00:34 +02:00
|
|
|
if (!$slug) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2011-07-12 17:08:03 +02:00
|
|
|
$document = id(new PhrictionDocument())->loadOneWhere(
|
|
|
|
'slug = %s',
|
|
|
|
$slug);
|
2011-07-11 21:34:53 +02:00
|
|
|
|
|
|
|
if ($document) {
|
|
|
|
$content = id(new PhrictionContent())->load($document->getContentID());
|
|
|
|
} else {
|
2012-05-20 17:54:25 +02:00
|
|
|
if (PhrictionDocument::isProjectSlug($slug)) {
|
|
|
|
$project = id(new PhabricatorProject())->loadOneWhere(
|
|
|
|
'phrictionSlug = %s',
|
|
|
|
PhrictionDocument::getProjectSlugIdentifier($slug));
|
|
|
|
if (!$project) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
}
|
2011-07-11 21:34:53 +02:00
|
|
|
$document = new PhrictionDocument();
|
|
|
|
$document->setSlug($slug);
|
|
|
|
|
|
|
|
$content = new PhrictionContent();
|
|
|
|
$content->setSlug($slug);
|
|
|
|
|
2012-04-10 23:18:20 +02:00
|
|
|
$default_title = PhabricatorSlug::getDefaultTitle($slug);
|
2011-07-11 21:34:53 +02:00
|
|
|
$content->setTitle($default_title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-12 20:57:22 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-07-17 03:25:45 +02:00
|
|
|
require_celerity_resource('phriction-document-css');
|
|
|
|
|
2011-07-11 21:34:53 +02:00
|
|
|
$e_title = true;
|
2012-07-16 18:40:10 +02:00
|
|
|
$notes = null;
|
2011-07-11 21:34:53 +02:00
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
if ($request->isFormPost()) {
|
|
|
|
$title = $request->getStr('title');
|
2012-07-16 18:40:10 +02:00
|
|
|
$notes = $request->getStr('description');
|
2011-07-11 21:34:53 +02:00
|
|
|
|
|
|
|
if (!strlen($title)) {
|
2013-02-08 18:54:27 +01:00
|
|
|
$e_title = pht('Required');
|
|
|
|
$errors[] = pht('Document title is required.');
|
2011-07-11 21:34:53 +02:00
|
|
|
} else {
|
|
|
|
$e_title = null;
|
|
|
|
}
|
|
|
|
|
2012-07-16 18:40:10 +02:00
|
|
|
if ($document->getID()) {
|
|
|
|
if ($content->getTitle() == $title &&
|
|
|
|
$content->getContent() == $request->getStr('content')) {
|
|
|
|
|
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($user);
|
2013-02-08 18:54:27 +01:00
|
|
|
$dialog->setTitle(pht('No Edits'));
|
2012-07-16 18:40:10 +02:00
|
|
|
$dialog->appendChild(
|
2013-02-08 18:54:27 +01:00
|
|
|
'<p>'.pht('You did not make any changes to the document.').'</p>');
|
2012-07-16 18:40:10 +02:00
|
|
|
$dialog->addCancelButton($request->getRequestURI());
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
2012-10-18 00:13:03 +02:00
|
|
|
} 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);
|
2013-02-08 18:54:27 +01:00
|
|
|
$dialog->setTitle(pht('Empty Page'));
|
2012-10-18 00:13:03 +02:00
|
|
|
$dialog->appendChild(
|
2013-02-08 18:54:27 +01:00
|
|
|
'<p>'.pht('You can not create an empty document.').'</p>');
|
2012-10-18 00:13:03 +02:00
|
|
|
$dialog->addCancelButton($request->getRequestURI());
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
2012-07-16 18:40:10 +02:00
|
|
|
}
|
|
|
|
|
2011-07-11 21:34:53 +02:00
|
|
|
if (!count($errors)) {
|
2011-08-31 21:00:34 +02:00
|
|
|
$editor = id(PhrictionDocumentEditor::newForSlug($document->getSlug()))
|
2012-10-10 19:18:23 +02:00
|
|
|
->setActor($user)
|
2011-08-26 21:50:28 +02:00
|
|
|
->setTitle($title)
|
|
|
|
->setContent($request->getStr('content'))
|
2012-07-16 18:40:10 +02:00
|
|
|
->setDescription($notes);
|
2011-07-11 21:34:53 +02:00
|
|
|
|
2011-08-26 21:50:28 +02:00
|
|
|
$editor->save();
|
2011-07-11 21:34:53 +02:00
|
|
|
|
2012-01-12 20:57:22 +01:00
|
|
|
if ($draft) {
|
|
|
|
$draft->delete();
|
|
|
|
}
|
|
|
|
|
2011-08-31 21:00:34 +02:00
|
|
|
$uri = PhrictionDocument::getSlugURI($document->getSlug());
|
2011-07-11 21:34:53 +02:00
|
|
|
return id(new AphrontRedirectResponse())->setURI($uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$error_view = null;
|
|
|
|
if ($errors) {
|
|
|
|
$error_view = id(new AphrontErrorView())
|
2013-02-08 18:54:27 +01:00
|
|
|
->setTitle(pht('Form Errors'))
|
2011-07-11 21:34:53 +02:00
|
|
|
->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($document->getID()) {
|
2013-02-08 18:54:27 +01:00
|
|
|
$panel_header = pht('Edit Phriction Document');
|
|
|
|
$submit_button = pht('Save Changes');
|
2011-07-11 21:34:53 +02:00
|
|
|
} else {
|
2013-02-08 18:54:27 +01:00
|
|
|
$panel_header = pht('Create New Phriction Document');
|
|
|
|
$submit_button = pht('Create Document');
|
2011-07-11 21:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$uri = $document->getSlug();
|
|
|
|
$uri = PhrictionDocument::getSlugURI($uri);
|
|
|
|
$uri = PhabricatorEnv::getProductionURI($uri);
|
|
|
|
|
2011-07-15 23:24:50 +02:00
|
|
|
$cancel_uri = PhrictionDocument::getSlugURI($document->getSlug());
|
|
|
|
|
2012-01-12 20:57:22 +01:00
|
|
|
if ($draft &&
|
|
|
|
strlen($draft->getDraft()) &&
|
|
|
|
($draft->getDraft() != $content->getContent())) {
|
|
|
|
$content_text = $draft->getDraft();
|
|
|
|
|
2013-01-18 03:57:09 +01:00
|
|
|
$discard = phutil_tag(
|
2012-01-12 20:57:22 +01:00
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => $request->getRequestURI()->alter('nodraft', true),
|
|
|
|
),
|
2013-02-08 18:54:27 +01:00
|
|
|
pht('discard this draft'));
|
2012-01-12 20:57:22 +01:00
|
|
|
|
|
|
|
$draft_note = new AphrontErrorView();
|
|
|
|
$draft_note->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
|
|
|
|
$draft_note->setTitle('Recovered Draft');
|
2013-02-07 01:53:49 +01:00
|
|
|
$draft_note->appendChild(hsprintf(
|
|
|
|
'<p>Showing a saved draft of your edits, you can %s.</p>',
|
|
|
|
$discard));
|
2012-01-12 20:57:22 +01:00
|
|
|
} else {
|
|
|
|
$content_text = $content->getContent();
|
|
|
|
$draft_note = null;
|
|
|
|
}
|
|
|
|
|
2011-07-11 21:34:53 +02:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
2012-07-16 18:40:10 +02:00
|
|
|
->setWorkflow(true)
|
2011-07-11 21:34:53 +02:00
|
|
|
->setAction($request->getRequestURI()->getPath())
|
2011-08-31 21:00:34 +02:00
|
|
|
->addHiddenInput('slug', $document->getSlug())
|
2012-01-12 20:57:22 +01:00
|
|
|
->addHiddenInput('nodraft', $request->getBool('nodraft'))
|
2011-07-11 21:34:53 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2013-02-08 18:54:27 +01:00
|
|
|
->setLabel(pht('Title'))
|
2011-07-11 21:34:53 +02:00
|
|
|
->setValue($content->getTitle())
|
|
|
|
->setError($e_title)
|
|
|
|
->setName('title'))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormStaticControl())
|
2013-02-08 18:54:27 +01:00
|
|
|
->setLabel(pht('URI'))
|
2011-07-11 21:34:53 +02:00
|
|
|
->setValue($uri))
|
|
|
|
->appendChild(
|
2012-09-19 21:27:28 +02:00
|
|
|
id(new PhabricatorRemarkupControl())
|
2013-02-08 18:54:27 +01:00
|
|
|
->setLabel(pht('Content'))
|
2012-01-12 20:57:22 +01:00
|
|
|
->setValue($content_text)
|
2011-07-11 21:34:53 +02:00
|
|
|
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
2011-07-12 17:28:07 +02:00
|
|
|
->setName('content')
|
2012-11-27 23:06:31 +01:00
|
|
|
->setID('document-textarea')
|
|
|
|
->setUser($user))
|
2011-12-17 18:19:08 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2013-02-08 18:54:27 +01:00
|
|
|
->setLabel(pht('Edit Notes'))
|
2012-07-16 18:40:10 +02:00
|
|
|
->setValue($notes)
|
2011-12-17 18:19:08 +01:00
|
|
|
->setError(null)
|
|
|
|
->setName('description'))
|
2011-07-11 21:34:53 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2011-07-15 23:24:50 +02:00
|
|
|
->addCancelButton($cancel_uri)
|
2011-07-11 21:34:53 +02:00
|
|
|
->setValue($submit_button));
|
|
|
|
|
|
|
|
$panel = id(new AphrontPanelView())
|
2013-02-08 17:00:17 +01:00
|
|
|
->setNoBackground()
|
2011-07-11 21:34:53 +02:00
|
|
|
->setHeader($panel_header)
|
|
|
|
->appendChild($form);
|
|
|
|
|
2011-07-17 03:25:45 +02:00
|
|
|
$preview_panel =
|
|
|
|
'<div class="aphront-panel-preview aphront-panel-preview-wide">
|
|
|
|
<div class="phriction-document-preview-header">
|
2013-02-08 18:54:27 +01:00
|
|
|
'.pht('Document Preview').'
|
2011-07-17 03:25:45 +02:00
|
|
|
</div>
|
|
|
|
<div id="document-preview">
|
|
|
|
<div class="aphront-panel-preview-loading-text">
|
2013-02-08 18:54:27 +01:00
|
|
|
'.pht('Loading preview...').'
|
2011-07-17 03:25:45 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>';
|
|
|
|
|
|
|
|
Javelin::initBehavior(
|
|
|
|
'phriction-document-preview',
|
|
|
|
array(
|
|
|
|
'preview' => 'document-preview',
|
|
|
|
'textarea' => 'document-textarea',
|
2012-01-12 20:57:22 +01:00
|
|
|
'uri' => '/phriction/preview/?draftkey='.$draft_key,
|
2011-07-17 03:25:45 +02:00
|
|
|
));
|
|
|
|
|
2011-07-11 21:34:53 +02:00
|
|
|
return $this->buildStandardPageResponse(
|
|
|
|
array(
|
2012-01-12 20:57:22 +01:00
|
|
|
$draft_note,
|
2011-07-11 21:34:53 +02:00
|
|
|
$error_view,
|
|
|
|
$panel,
|
2011-07-17 03:25:45 +02:00
|
|
|
$preview_panel,
|
2011-07-11 21:34:53 +02:00
|
|
|
),
|
|
|
|
array(
|
2013-02-08 18:54:27 +01:00
|
|
|
'title' => pht('Edit Document'),
|
2011-07-11 21:34:53 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|