2011-07-11 21:34:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-01-12 20:57:22 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-07-11 21:34:53 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
if ($request->isFormPost()) {
|
|
|
|
$title = $request->getStr('title');
|
|
|
|
|
|
|
|
if (!strlen($title)) {
|
|
|
|
$e_title = 'Required';
|
|
|
|
$errors[] = 'Document title is required.';
|
|
|
|
} else {
|
|
|
|
$e_title = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!count($errors)) {
|
2011-08-31 21:00:34 +02:00
|
|
|
$editor = id(PhrictionDocumentEditor::newForSlug($document->getSlug()))
|
2011-08-26 21:50:28 +02:00
|
|
|
->setUser($user)
|
|
|
|
->setTitle($title)
|
|
|
|
->setContent($request->getStr('content'))
|
|
|
|
->setDescription($request->getStr('description'));
|
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())
|
|
|
|
->setTitle('Form Errors')
|
|
|
|
->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($document->getID()) {
|
|
|
|
$panel_header = 'Edit Phriction Document';
|
2011-07-17 03:25:45 +02:00
|
|
|
$submit_button = 'Save Changes';
|
2011-12-17 18:19:08 +01:00
|
|
|
$delete_button = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => '/phriction/delete/'.$document->getID().'/',
|
|
|
|
'class' => 'grey button',
|
|
|
|
),
|
|
|
|
'Delete Document');
|
2011-07-11 21:34:53 +02:00
|
|
|
} else {
|
|
|
|
$panel_header = 'Create New Phriction Document';
|
|
|
|
$submit_button = 'Create Document';
|
2011-12-17 18:19:08 +01:00
|
|
|
$delete_button = null;
|
2011-07-11 21:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$uri = $document->getSlug();
|
|
|
|
$uri = PhrictionDocument::getSlugURI($uri);
|
|
|
|
$uri = PhabricatorEnv::getProductionURI($uri);
|
|
|
|
|
2011-07-12 17:28:07 +02:00
|
|
|
$remarkup_reference = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => PhabricatorEnv::getDoclink('article/Remarkup_Reference.html'),
|
2011-12-07 15:46:37 +01:00
|
|
|
'tabindex' => '-1',
|
|
|
|
'target' => '_blank',
|
2011-07-12 17:28:07 +02:00
|
|
|
),
|
|
|
|
'Formatting Reference');
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2011-07-11 21:34:53 +02:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
|
|
|
->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())
|
|
|
|
->setLabel('Title')
|
|
|
|
->setValue($content->getTitle())
|
|
|
|
->setError($e_title)
|
|
|
|
->setName('title'))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormStaticControl())
|
|
|
|
->setLabel('URI')
|
|
|
|
->setValue($uri))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextAreaControl())
|
|
|
|
->setLabel('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')
|
2011-07-17 03:25:45 +02:00
|
|
|
->setID('document-textarea')
|
2011-07-15 23:17:55 +02:00
|
|
|
->setEnableDragAndDropFileUploads(true)
|
2011-07-12 17:28:07 +02:00
|
|
|
->setCaption($remarkup_reference))
|
2011-12-17 18:19:08 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setLabel('Edit Notes')
|
|
|
|
->setValue($content->getDescription())
|
|
|
|
->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())
|
|
|
|
->setWidth(AphrontPanelView::WIDTH_WIDE)
|
|
|
|
->setHeader($panel_header)
|
|
|
|
->appendChild($form);
|
|
|
|
|
2011-12-17 18:19:08 +01:00
|
|
|
if ($delete_button) {
|
|
|
|
$panel->addButton($delete_button);
|
|
|
|
}
|
|
|
|
|
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">
|
|
|
|
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',
|
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(
|
|
|
|
'title' => 'Edit Document',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|