PhrictionEditController.php 9.33 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
    $current_version = null;
11
    if ($id) {
12
      $document = id(new PhrictionDocumentQuery())
13
14
        ->setViewer($viewer)
        ->withIDs(array($id))
15
        ->needContent(true)
16
17
18
19
20
21
        ->requireCapabilities(
          array(
            PhabricatorPolicyCapability::CAN_VIEW,
            PhabricatorPolicyCapability::CAN_EDIT,
          ))
        ->executeOne();
22
23
24
      if (!$document) {
        return new Aphront404Response();
      }
epriestley's avatar
epriestley committed
25

26
27
      $current_version = $document->getContent()->getVersion();

epriestley's avatar
epriestley committed
28
29
30
31
32
33
34
35
36
37
      $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 {
38
        $content = $document->getContent();
epriestley's avatar
epriestley committed
39
      }
40
41
42

    } else {
      $slug = $request->getStr('slug');
Bob Trahan's avatar
Bob Trahan committed
43
      $slug = PhabricatorSlug::normalize($slug);
44
45
46
47
      if (!$slug) {
        return new Aphront404Response();
      }

48
      $document = id(new PhrictionDocumentQuery())
49
        ->setViewer($viewer)
50
51
52
        ->withSlugs(array($slug))
        ->needContent(true)
        ->executeOne();
53
54

      if ($document) {
55
        $content = $document->getContent();
56
        $current_version = $content->getVersion();
57
      } else {
58
        $document = PhrictionDocument::initializeNewDocument($viewer, $slug);
59
        $content = $document->getContent();
60
61
62
      }
    }

63
64
65
66
67
68
69
70
71
72
73
    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',
74
        $viewer->getPHID(),
75
76
77
        $draft_key);
    }

78
79
80
81
82
83
84
85
86
87
88
89
    if ($draft &&
      strlen($draft->getDraft()) &&
      ($draft->getDraft() != $content->getContent())) {
      $content_text = $draft->getDraft();

      $discard = phutil_tag(
        'a',
        array(
          'href' => $request->getRequestURI()->alter('nodraft', true),
        ),
        pht('discard this draft'));

90
91
      $draft_note = new PHUIInfoView();
      $draft_note->setSeverity(PHUIInfoView::SEVERITY_NOTICE);
Joshua Spence's avatar
Joshua Spence committed
92
93
94
95
96
97
98
      $draft_note->setTitle(pht('Recovered Draft'));
      $draft_note->appendChild(
        hsprintf(
          '<p>%s</p>',
          pht(
            'Showing a saved draft of your edits, you can %s.',
            $discard)));
99
100
101
102
103
    } else {
      $content_text = $content->getContent();
      $draft_note = null;
    }

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

106
    $e_title = true;
107
108
    $e_content = true;
    $validation_exception = null;
109
    $notes = null;
110
111
    $title = $content->getTitle();
    $overwrite = false;
Chad Little's avatar
Chad Little committed
112
113
    $v_cc = PhabricatorSubscribersQuery::loadSubscribersForPHID(
      $document->getPHID());
114
115

    if ($request->isFormPost()) {
116

117
      $title = $request->getStr('title');
118
      $content_text = $request->getStr('content');
119
      $notes = $request->getStr('description');
120
      $current_version = $request->getInt('contentVersion');
121
122
      $v_view = $request->getStr('viewPolicy');
      $v_edit = $request->getStr('editPolicy');
Chad Little's avatar
Chad Little committed
123
      $v_cc   = $request->getArr('cc');
124
125
126
127
128
129
130
131

      $xactions = array();
      $xactions[] = id(new PhrictionTransaction())
        ->setTransactionType(PhrictionTransaction::TYPE_TITLE)
        ->setNewValue($title);
      $xactions[] = id(new PhrictionTransaction())
        ->setTransactionType(PhrictionTransaction::TYPE_CONTENT)
        ->setNewValue($content_text);
132
133
134
135
136
137
      $xactions[] = id(new PhrictionTransaction())
        ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
        ->setNewValue($v_view);
      $xactions[] = id(new PhrictionTransaction())
        ->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)
        ->setNewValue($v_edit);
Chad Little's avatar
Chad Little committed
138
139
140
      $xactions[] = id(new PhrictionTransaction())
        ->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS)
        ->setNewValue(array('=' => $v_cc));
141
142

      $editor = id(new PhrictionTransactionEditor())
143
        ->setActor($viewer)
144
145
146
147
148
149
150
151
        ->setContentSourceFromRequest($request)
        ->setContinueOnNoEffect(true)
        ->setDescription($notes)
        ->setProcessContentVersionError(!$request->getBool('overwrite'))
        ->setContentVersion($current_version);

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

153
154
155
156
        if ($draft) {
          $draft->delete();
        }

157
        $uri = PhrictionDocument::getSlugURI($document->getSlug());
158
        return id(new AphrontRedirectResponse())->setURI($uri);
159
160
      } catch (PhabricatorApplicationTransactionValidationException $ex) {
        $validation_exception = $ex;
161
162
163
164
165
166
        $e_title = nonempty(
          $ex->getShortMessage(PhrictionTransaction::TYPE_TITLE),
          true);
        $e_content = nonempty(
          $ex->getShortMessage(PhrictionTransaction::TYPE_CONTENT),
          true);
167
168
169
170
171
172
173

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

174
175
        $document->setViewPolicy($v_view);
        $document->setEditPolicy($v_edit);
176
177
178
179
      }
    }

    if ($document->getID()) {
Chad Little's avatar
Chad Little committed
180
      $panel_header = pht('Edit Phriction Document');
181
182
183
184
185
186
      $page_title = pht('Edit Document');
      if ($overwrite) {
        $submit_button = pht('Overwrite Changes');
      } else {
        $submit_button = pht('Save Changes');
      }
187
    } else {
Chad Little's avatar
Chad Little committed
188
189
      $panel_header = pht('Create New Phriction Document');
      $submit_button = pht('Create Document');
190
      $page_title = pht('Create Document');
191
192
193
194
195
196
    }

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

197
198
    $cancel_uri = PhrictionDocument::getSlugURI($document->getSlug());

199
    $policies = id(new PhabricatorPolicyQuery())
200
      ->setViewer($viewer)
201
202
203
204
205
      ->setObject($document)
      ->execute();
    $view_capability = PhabricatorPolicyCapability::CAN_VIEW;
    $edit_capability = PhabricatorPolicyCapability::CAN_EDIT;

206
    $form = id(new AphrontFormView())
207
      ->setUser($viewer)
208
      ->addHiddenInput('slug', $document->getSlug())
209
      ->addHiddenInput('nodraft', $request->getBool('nodraft'))
210
      ->addHiddenInput('contentVersion', $current_version)
211
      ->addHiddenInput('overwrite', $overwrite)
212
213
      ->appendChild(
        id(new AphrontFormTextControl())
Chad Little's avatar
Chad Little committed
214
          ->setLabel(pht('Title'))
215
          ->setValue($title)
216
217
218
219
          ->setError($e_title)
          ->setName('title'))
      ->appendChild(
        id(new AphrontFormStaticControl())
Chad Little's avatar
Chad Little committed
220
          ->setLabel(pht('URI'))
221
222
          ->setValue($uri))
      ->appendChild(
223
        id(new PhabricatorRemarkupControl())
Chad Little's avatar
Chad Little committed
224
          ->setLabel(pht('Content'))
225
          ->setValue($content_text)
226
          ->setError($e_content)
227
          ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
228
          ->setName('content')
229
          ->setID('document-textarea')
230
          ->setUser($viewer))
Chad Little's avatar
Chad Little committed
231
232
233
234
235
236
237
      ->appendControl(
        id(new AphrontFormTokenizerControl())
          ->setLabel(pht('Subscribers'))
          ->setName('cc')
          ->setValue($v_cc)
          ->setUser($viewer)
          ->setDatasource(new PhabricatorMetaMTAMailableDatasource()))
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
      ->appendChild(
        id(new AphrontFormPolicyControl())
          ->setName('viewPolicy')
          ->setPolicyObject($document)
          ->setCapability($view_capability)
          ->setPolicies($policies)
          ->setCaption(
            $document->describeAutomaticCapability($view_capability)))
      ->appendChild(
        id(new AphrontFormPolicyControl())
          ->setName('editPolicy')
          ->setPolicyObject($document)
          ->setCapability($edit_capability)
          ->setPolicies($policies)
          ->setCaption(
            $document->describeAutomaticCapability($edit_capability)))
254
255
      ->appendChild(
        id(new AphrontFormTextControl())
Chad Little's avatar
Chad Little committed
256
          ->setLabel(pht('Edit Notes'))
257
          ->setValue($notes)
258
259
          ->setError(null)
          ->setName('description'))
260
261
      ->appendChild(
        id(new AphrontFormSubmitControl())
262
          ->addCancelButton($cancel_uri)
263
264
          ->setValue($submit_button));

265
    $form_box = id(new PHUIObjectBoxView())
266
267
      ->setHeaderText($panel_header)
      ->setValidationException($validation_exception)
Chad Little's avatar
Chad Little committed
268
      ->setForm($form);
269

270
271
272
    $preview = id(new PHUIRemarkupPreviewPanel())
      ->setHeader(pht('Document Preview'))
      ->setPreviewURI('/phriction/preview/')
273
274
      ->setControlID('document-textarea')
      ->addClass('phui-document-view');
275

276
277
    $crumbs = $this->buildApplicationCrumbs();
    if ($document->getID()) {
278
279
280
281
      $crumbs->addTextCrumb(
        $content->getTitle(),
        PhrictionDocument::getSlugURI($document->getSlug()));
      $crumbs->addTextCrumb(pht('Edit'));
282
    } else {
283
      $crumbs->addTextCrumb(pht('Create'));
284
285
    }

Chad Little's avatar
Chad Little committed
286
    return $this->buildApplicationPage(
287
      array(
288
        $crumbs,
289
        $draft_note,
Chad Little's avatar
Chad Little committed
290
        $form_box,
291
        $preview,
292
293
      ),
      array(
294
        'title'   => $page_title,
295
296
297
298
      ));
  }

}