PhrictionEditController.php 11.3 KB
Newer Older
1
2
<?php

3
final class PhrictionEditController
4
5
  extends PhrictionController {

6
7
8
  public function handleRequest(AphrontRequest $request) {
    $viewer = $request->getViewer();
    $id = $request->getURIData('id');
9

10
    $max_version = null;
11
    if ($id) {
12
      $is_new = false;
13
      $document = id(new PhrictionDocumentQuery())
14
15
        ->setViewer($viewer)
        ->withIDs(array($id))
16
        ->needContent(true)
17
18
19
20
21
22
        ->requireCapabilities(
          array(
            PhabricatorPolicyCapability::CAN_VIEW,
            PhabricatorPolicyCapability::CAN_EDIT,
          ))
        ->executeOne();
23
24
25
      if (!$document) {
        return new Aphront404Response();
      }
epriestley's avatar
epriestley committed
26

27
      $max_version = $document->getMaxVersion();
28

epriestley's avatar
epriestley committed
29
30
      $revert = $request->getInt('revert');
      if ($revert) {
31
32
33
34
35
        $content = id(new PhrictionContentQuery())
          ->setViewer($viewer)
          ->withDocumentPHIDs(array($document->getPHID()))
          ->withVersions(array($revert))
          ->executeOne();
epriestley's avatar
epriestley committed
36
37
38
39
        if (!$content) {
          return new Aphront404Response();
        }
      } else {
40
41
42
43
44
        $content = id(new PhrictionContentQuery())
          ->setViewer($viewer)
          ->withDocumentPHIDs(array($document->getPHID()))
          ->setLimit(1)
          ->executeOne();
epriestley's avatar
epriestley committed
45
      }
46
47
    } else {
      $slug = $request->getStr('slug');
Bob Trahan's avatar
Bob Trahan committed
48
      $slug = PhabricatorSlug::normalize($slug);
49
50
51
52
      if (!$slug) {
        return new Aphront404Response();
      }

53
      $document = id(new PhrictionDocumentQuery())
54
        ->setViewer($viewer)
55
56
57
        ->withSlugs(array($slug))
        ->needContent(true)
        ->executeOne();
58
59

      if ($document) {
60
61
62
63
64
65
66
        $content = id(new PhrictionContentQuery())
          ->setViewer($viewer)
          ->withDocumentPHIDs(array($document->getPHID()))
          ->setLimit(1)
          ->executeOne();

        $max_version = $document->getMaxVersion();
67
        $is_new = false;
68
      } else {
69
        $document = PhrictionDocument::initializeNewDocument($viewer, $slug);
70
        $content = $document->getContent();
71
        $is_new = true;
72
73
74
      }
    }

75
76
    require_celerity_resource('phriction-document-css');

77
    $e_title = true;
78
79
    $e_content = true;
    $validation_exception = null;
80
    $notes = null;
81
82
    $title = $content->getTitle();
    $overwrite = false;
Chad Little's avatar
Chad Little committed
83
84
    $v_cc = PhabricatorSubscribersQuery::loadSubscribersForPHID(
      $document->getPHID());
85

86
87
88
89
90
91
92
93
94
    if ($is_new) {
      $v_projects = array();
    } else {
      $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs(
        $document->getPHID(),
        PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
      $v_projects = array_reverse($v_projects);
    }

epriestley's avatar
epriestley committed
95
96
    $v_space = $document->getSpacePHID();

97
    $content_text = $content->getContent();
98
    $is_draft_mode = ($document->getContent()->getVersion() != $max_version);
99

100
101
102
103
    $default_view = $document->getViewPolicy();
    $default_edit = $document->getEditPolicy();
    $default_space = $document->getSpacePHID();

104
    if ($request->isFormPost()) {
105
106
107
108
109
      if ($is_new) {
        $save_as_draft = false;
      } else {
        $save_as_draft = ($is_draft_mode || $request->getExists('draft'));
      }
110

111
      $title = $request->getStr('title');
112
      $content_text = $request->getStr('content');
113
      $notes = $request->getStr('description');
114
      $max_version = $request->getInt('contentVersion');
115
116
      $v_view = $request->getStr('viewPolicy');
      $v_edit = $request->getStr('editPolicy');
117
118
      $v_cc = $request->getArr('cc');
      $v_projects = $request->getArr('projects');
epriestley's avatar
epriestley committed
119
      $v_space = $request->getStr('spacePHID');
120

121
122
123
124
125
126
      if ($save_as_draft) {
        $edit_type = PhrictionDocumentDraftTransaction::TRANSACTIONTYPE;
      } else {
        $edit_type = PhrictionDocumentContentTransaction::TRANSACTIONTYPE;
      }

127
      $xactions = array();
128

129
130
131
132
133
      if ($is_new) {
        $xactions[] = id(new PhrictionTransaction())
          ->setTransactionType(PhabricatorTransactions::TYPE_CREATE);
      }

134
      $xactions[] = id(new PhrictionTransaction())
135
        ->setTransactionType(PhrictionDocumentTitleTransaction::TRANSACTIONTYPE)
136
137
        ->setNewValue($title);
      $xactions[] = id(new PhrictionTransaction())
138
        ->setTransactionType($edit_type)
139
        ->setNewValue($content_text);
140
141
      $xactions[] = id(new PhrictionTransaction())
        ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
142
143
        ->setNewValue($v_view)
        ->setIsDefaultTransaction($is_new && ($v_view === $default_view));
144
145
      $xactions[] = id(new PhrictionTransaction())
        ->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)
146
147
        ->setNewValue($v_edit)
        ->setIsDefaultTransaction($is_new && ($v_edit === $default_edit));
epriestley's avatar
epriestley committed
148
149
      $xactions[] = id(new PhrictionTransaction())
        ->setTransactionType(PhabricatorTransactions::TYPE_SPACE)
150
151
        ->setNewValue($v_space)
        ->setIsDefaultTransaction($is_new && ($v_space === $default_space));
Chad Little's avatar
Chad Little committed
152
153
154
      $xactions[] = id(new PhrictionTransaction())
        ->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS)
        ->setNewValue(array('=' => $v_cc));
155

156
157
158
159
160
161
      $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
      $xactions[] = id(new PhrictionTransaction())
        ->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
        ->setMetadataValue('edge:type', $proj_edge_type)
        ->setNewValue(array('=' => array_fuse($v_projects)));

162
      $editor = id(new PhrictionTransactionEditor())
163
        ->setActor($viewer)
164
165
166
167
        ->setContentSourceFromRequest($request)
        ->setContinueOnNoEffect(true)
        ->setDescription($notes)
        ->setProcessContentVersionError(!$request->getBool('overwrite'))
168
        ->setContentVersion($max_version);
169
170
171

      try {
        $editor->applyTransactions($document, $xactions);
172

173
        $uri = PhrictionDocument::getSlugURI($document->getSlug());
174
175
176
177
178
179
180
181
        $uri = new PhutilURI($uri);

        // If the user clicked "Save as Draft", take them to the draft, not
        // to the current published page.
        if ($save_as_draft) {
          $uri = $uri->alter('v', $document->getMaxVersion());
        }

182
        return id(new AphrontRedirectResponse())->setURI($uri);
183
184
      } catch (PhabricatorApplicationTransactionValidationException $ex) {
        $validation_exception = $ex;
185
        $e_title = nonempty(
186
187
          $ex->getShortMessage(
            PhrictionDocumentTitleTransaction::TRANSACTIONTYPE),
188
189
          true);
        $e_content = nonempty(
190
191
          $ex->getShortMessage(
            PhrictionDocumentContentTransaction::TRANSACTIONTYPE),
192
          true);
193
194
195
196
197
198
199

        // if we're not supposed to process the content version error, then
        // overwrite that content...!
        if (!$editor->getProcessContentVersionError()) {
          $overwrite = true;
        }

200
201
        $document->setViewPolicy($v_view);
        $document->setEditPolicy($v_edit);
epriestley's avatar
epriestley committed
202
        $document->setSpacePHID($v_space);
203
204
205
206
      }
    }

    if ($document->getID()) {
207
      $page_title = pht('Edit Document: %s', $content->getTitle());
208
209
210
      if ($overwrite) {
        $submit_button = pht('Overwrite Changes');
      } else {
211
        $submit_button = pht('Save and Publish');
212
      }
213
    } else {
Chad Little's avatar
Chad Little committed
214
      $submit_button = pht('Create Document');
215
      $page_title = pht('Create Document');
216
217
218
219
220
221
    }

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

222
223
    $cancel_uri = PhrictionDocument::getSlugURI($document->getSlug());

224
    $policies = id(new PhabricatorPolicyQuery())
225
      ->setViewer($viewer)
226
227
228
229
      ->setObject($document)
      ->execute();
    $view_capability = PhabricatorPolicyCapability::CAN_VIEW;
    $edit_capability = PhabricatorPolicyCapability::CAN_EDIT;
230

231
    $form = id(new AphrontFormView())
232
      ->setUser($viewer)
233
      ->addHiddenInput('slug', $document->getSlug())
234
      ->addHiddenInput('contentVersion', $max_version)
235
      ->addHiddenInput('overwrite', $overwrite)
236
237
      ->appendChild(
        id(new AphrontFormTextControl())
Chad Little's avatar
Chad Little committed
238
          ->setLabel(pht('Title'))
239
          ->setValue($title)
240
241
242
243
          ->setError($e_title)
          ->setName('title'))
      ->appendChild(
        id(new AphrontFormStaticControl())
Chad Little's avatar
Chad Little committed
244
          ->setLabel(pht('URI'))
245
246
          ->setValue($uri))
      ->appendChild(
247
        id(new PhabricatorRemarkupControl())
Chad Little's avatar
Chad Little committed
248
          ->setLabel(pht('Content'))
249
          ->setValue($content_text)
250
          ->setError($e_content)
251
          ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
252
          ->setName('content')
253
          ->setID('document-textarea')
254
          ->setUser($viewer))
255
256
      ->appendControl(
        id(new AphrontFormTokenizerControl())
Luke081515's avatar
Luke081515 committed
257
          ->setLabel(pht('Tags'))
258
259
260
          ->setName('projects')
          ->setValue($v_projects)
          ->setDatasource(new PhabricatorProjectDatasource()))
Chad Little's avatar
Chad Little committed
261
262
263
264
265
266
267
      ->appendControl(
        id(new AphrontFormTokenizerControl())
          ->setLabel(pht('Subscribers'))
          ->setName('cc')
          ->setValue($v_cc)
          ->setUser($viewer)
          ->setDatasource(new PhabricatorMetaMTAMailableDatasource()))
268
269
      ->appendChild(
        id(new AphrontFormPolicyControl())
epriestley's avatar
epriestley committed
270
          ->setViewer($viewer)
271
          ->setName('viewPolicy')
epriestley's avatar
epriestley committed
272
          ->setSpacePHID($v_space)
273
274
          ->setPolicyObject($document)
          ->setCapability($view_capability)
275
          ->setPolicies($policies))
276
277
278
279
280
      ->appendChild(
        id(new AphrontFormPolicyControl())
          ->setName('editPolicy')
          ->setPolicyObject($document)
          ->setCapability($edit_capability)
281
          ->setPolicies($policies))
282
283
      ->appendChild(
        id(new AphrontFormTextControl())
Chad Little's avatar
Chad Little committed
284
          ->setLabel(pht('Edit Notes'))
285
          ->setValue($notes)
286
          ->setError(null)
287
288
289
290
          ->setName('description'));

    if ($is_draft_mode) {
      $form->appendControl(
291
        id(new AphrontFormSubmitControl())
292
293
294
          ->addCancelButton($cancel_uri)
          ->setValue(pht('Save Draft')));
    } else {
295
296
297
298
299
300
301
302
303
304
      $submit = id(new AphrontFormSubmitControl());

      if (!$is_new) {
        $draft_button = id(new PHUIButtonView())
          ->setTag('input')
          ->setName('draft')
          ->setText(pht('Save as Draft'))
          ->setColor(PHUIButtonView::GREEN);
        $submit->addButton($draft_button);
      }
305

306
307
308
309
310
      $submit
        ->addCancelButton($cancel_uri)
        ->setValue($submit_button);

      $form->appendControl($submit);
311
    }
312

313
    $form_box = id(new PHUIObjectBoxView())
314
      ->setHeaderText($page_title)
315
      ->setValidationException($validation_exception)
316
      ->setBackground(PHUIObjectBoxView::WHITE_CONFIG)
Chad Little's avatar
Chad Little committed
317
      ->setForm($form);
318

319
    $preview = id(new PHUIRemarkupPreviewPanel())
320
      ->setHeader($content->getTitle())
321
      ->setPreviewURI('/phriction/preview/'.$document->getSlug())
322
      ->setControlID('document-textarea')
323
      ->setPreviewType(PHUIRemarkupPreviewPanel::DOCUMENT);
324

325
326
    $crumbs = $this->buildApplicationCrumbs();
    if ($document->getID()) {
327
328
329
330
      $crumbs->addTextCrumb(
        $content->getTitle(),
        PhrictionDocument::getSlugURI($document->getSlug()));
      $crumbs->addTextCrumb(pht('Edit'));
331
    } else {
332
      $crumbs->addTextCrumb(pht('Create'));
333
    }
334
    $crumbs->setBorder(true);
335

336
    $view = id(new PHUITwoColumnView())
337
338
339
340
341
      ->setFooter(
        array(
          $form_box,
          $preview,
        ));
342
343
344
345
346

    return $this->newPage()
      ->setTitle($page_title)
      ->setCrumbs($crumbs)
      ->appendChild($view);
347
348
349
  }

}