PhrictionEditController.php 7.67 KB
Newer Older
1
2
3
<?php

/*
4
 * Copyright 2012 Facebook, Inc.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * 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.
 */

epriestley's avatar
epriestley committed
19
20
21
/**
 * @group phriction
 */
22
final class PhrictionEditController
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  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();
      }
epriestley's avatar
epriestley committed
41
42
43
44
45
46
47
48
49
50
51
52
53

      $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());
      }
54
55
56

    } else {
      $slug = $request->getStr('slug');
Bob Trahan's avatar
Bob Trahan committed
57
      $slug = PhabricatorSlug::normalize($slug);
58
59
60
61
      if (!$slug) {
        return new Aphront404Response();
      }

62
63
64
      $document = id(new PhrictionDocument())->loadOneWhere(
        'slug = %s',
        $slug);
65
66
67
68

      if ($document) {
        $content = id(new PhrictionContent())->load($document->getContentID());
      } else {
69
70
71
72
73
74
75
76
        if (PhrictionDocument::isProjectSlug($slug)) {
          $project = id(new PhabricatorProject())->loadOneWhere(
            'phrictionSlug = %s',
            PhrictionDocument::getProjectSlugIdentifier($slug));
          if (!$project) {
            return new Aphront404Response();
          }
        }
77
78
79
80
81
82
        $document = new PhrictionDocument();
        $document->setSlug($slug);

        $content  = new PhrictionContent();
        $content->setSlug($slug);

Bob Trahan's avatar
Bob Trahan committed
83
        $default_title = PhabricatorSlug::getDefaultTitle($slug);
84
85
86
87
        $content->setTitle($default_title);
      }
    }

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    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);
    }

103
104
    require_celerity_resource('phriction-document-css');

105
106
107
108
109
110
111
112
113
114
115
116
117
118
    $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)) {
119
        $editor = id(PhrictionDocumentEditor::newForSlug($document->getSlug()))
120
121
122
123
          ->setUser($user)
          ->setTitle($title)
          ->setContent($request->getStr('content'))
          ->setDescription($request->getStr('description'));
124

125
        $editor->save();
126

127
128
129
130
        if ($draft) {
          $draft->delete();
        }

131
        $uri = PhrictionDocument::getSlugURI($document->getSlug());
132
133
134
135
136
137
138
139
140
141
142
143
144
        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';
145
      $submit_button = 'Save Changes';
146
147
148
149
150
151
152
      $delete_button = phutil_render_tag(
        'a',
        array(
          'href' => '/phriction/delete/'.$document->getID().'/',
          'class' => 'grey button',
        ),
        'Delete Document');
153
154
155
    } else {
      $panel_header = 'Create New Phriction Document';
      $submit_button = 'Create Document';
156
      $delete_button = null;
157
158
159
160
161
162
    }

    $uri = $document->getSlug();
    $uri = PhrictionDocument::getSlugURI($uri);
    $uri = PhabricatorEnv::getProductionURI($uri);

163
164
165
166
    $remarkup_reference = phutil_render_tag(
      'a',
      array(
        'href' => PhabricatorEnv::getDoclink('article/Remarkup_Reference.html'),
167
168
        'tabindex' => '-1',
        'target' => '_blank',
169
170
171
      ),
      'Formatting Reference');

172
173
    $cancel_uri = PhrictionDocument::getSlugURI($document->getSlug());

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    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;
    }

196
197
198
    $form = id(new AphrontFormView())
      ->setUser($user)
      ->setAction($request->getRequestURI()->getPath())
199
      ->addHiddenInput('slug', $document->getSlug())
200
      ->addHiddenInput('nodraft', $request->getBool('nodraft'))
201
202
203
204
205
206
207
208
209
210
211
212
213
      ->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')
214
          ->setValue($content_text)
215
          ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
216
          ->setName('content')
217
          ->setID('document-textarea')
218
          ->setEnableDragAndDropFileUploads(true)
219
          ->setCaption($remarkup_reference))
220
221
222
223
224
225
      ->appendChild(
        id(new AphrontFormTextControl())
          ->setLabel('Edit Notes')
          ->setValue($content->getDescription())
          ->setError(null)
          ->setName('description'))
226
227
      ->appendChild(
        id(new AphrontFormSubmitControl())
228
          ->addCancelButton($cancel_uri)
229
230
231
232
233
234
235
          ->setValue($submit_button));

    $panel = id(new AphrontPanelView())
      ->setWidth(AphrontPanelView::WIDTH_WIDE)
      ->setHeader($panel_header)
      ->appendChild($form);

236
237
238
239
    if ($delete_button) {
      $panel->addButton($delete_button);
    }

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    $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',
257
        'uri'       => '/phriction/preview/?draftkey='.$draft_key,
258
259
      ));

260
261
    return $this->buildStandardPageResponse(
      array(
262
        $draft_note,
263
264
        $error_view,
        $panel,
265
        $preview_panel,
266
267
268
269
270
271
272
      ),
      array(
        'title' => 'Edit Document',
      ));
  }

}