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

epriestley's avatar
epriestley committed
3
4
5
/**
 * @group phriction
 */
6
final class PhrictionEditController
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  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
25
26
27
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 {
        $content = id(new PhrictionContent())->load($document->getContentID());
      }
38
39
40

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

46
47
48
      $document = id(new PhrictionDocument())->loadOneWhere(
        'slug = %s',
        $slug);
49
50
51
52

      if ($document) {
        $content = id(new PhrictionContent())->load($document->getContentID());
      } else {
53
54
55
56
57
58
59
60
        if (PhrictionDocument::isProjectSlug($slug)) {
          $project = id(new PhabricatorProject())->loadOneWhere(
            'phrictionSlug = %s',
            PhrictionDocument::getProjectSlugIdentifier($slug));
          if (!$project) {
            return new Aphront404Response();
          }
        }
61
62
63
64
65
66
        $document = new PhrictionDocument();
        $document->setSlug($slug);

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

Bob Trahan's avatar
Bob Trahan committed
67
        $default_title = PhabricatorSlug::getDefaultTitle($slug);
68
69
70
71
        $content->setTitle($default_title);
      }
    }

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    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);
    }

87
88
    require_celerity_resource('phriction-document-css');

89
    $e_title = true;
90
    $notes = null;
91
92
93
94
    $errors = array();

    if ($request->isFormPost()) {
      $title = $request->getStr('title');
95
      $notes = $request->getStr('description');
96
97

      if (!strlen($title)) {
Chad Little's avatar
Chad Little committed
98
99
        $e_title = pht('Required');
        $errors[] = pht('Document title is required.');
100
101
102
103
      } else {
        $e_title = null;
      }

104
105
106
107
108
109
      if ($document->getID()) {
        if ($content->getTitle() == $title &&
            $content->getContent() == $request->getStr('content')) {

          $dialog = new AphrontDialogView();
          $dialog->setUser($user);
Chad Little's avatar
Chad Little committed
110
          $dialog->setTitle(pht('No Edits'));
epriestley's avatar
epriestley committed
111
112
          $dialog->appendChild(phutil_tag('p', array(), pht(
            'You did not make any changes to the document.')));
113
114
115
116
          $dialog->addCancelButton($request->getRequestURI());

          return id(new AphrontDialogResponse())->setDialog($dialog);
        }
117
118
119
120
121
122
123
      } 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);
Chad Little's avatar
Chad Little committed
124
        $dialog->setTitle(pht('Empty Page'));
epriestley's avatar
epriestley committed
125
126
        $dialog->appendChild(phutil_tag('p', array(), pht(
          'You can not create an empty document.')));
127
128
129
        $dialog->addCancelButton($request->getRequestURI());

        return id(new AphrontDialogResponse())->setDialog($dialog);
130
131
      }

132
      if (!count($errors)) {
133
        $editor = id(PhrictionDocumentEditor::newForSlug($document->getSlug()))
134
          ->setActor($user)
135
136
          ->setTitle($title)
          ->setContent($request->getStr('content'))
137
          ->setDescription($notes);
138

139
        $editor->save();
140

141
142
143
144
        if ($draft) {
          $draft->delete();
        }

145
        $uri = PhrictionDocument::getSlugURI($document->getSlug());
146
147
148
149
150
151
152
        return id(new AphrontRedirectResponse())->setURI($uri);
      }
    }

    $error_view = null;
    if ($errors) {
      $error_view = id(new AphrontErrorView())
Chad Little's avatar
Chad Little committed
153
        ->setTitle(pht('Form Errors'))
154
155
156
157
        ->setErrors($errors);
    }

    if ($document->getID()) {
Chad Little's avatar
Chad Little committed
158
159
      $panel_header = pht('Edit Phriction Document');
      $submit_button = pht('Save Changes');
160
    } else {
Chad Little's avatar
Chad Little committed
161
162
      $panel_header = pht('Create New Phriction Document');
      $submit_button = pht('Create Document');
163
164
165
166
167
168
    }

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

169
170
    $cancel_uri = PhrictionDocument::getSlugURI($document->getSlug());

171
172
173
174
175
    if ($draft &&
        strlen($draft->getDraft()) &&
        ($draft->getDraft() != $content->getContent())) {
      $content_text = $draft->getDraft();

176
      $discard = phutil_tag(
177
178
179
180
        'a',
        array(
          'href' => $request->getRequestURI()->alter('nodraft', true),
        ),
Chad Little's avatar
Chad Little committed
181
        pht('discard this draft'));
182
183
184
185

      $draft_note = new AphrontErrorView();
      $draft_note->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
      $draft_note->setTitle('Recovered Draft');
vrana's avatar
vrana committed
186
187
188
      $draft_note->appendChild(hsprintf(
        '<p>Showing a saved draft of your edits, you can %s.</p>',
        $discard));
189
190
191
192
193
    } else {
      $content_text = $content->getContent();
      $draft_note = null;
    }

194
195
    $form = id(new AphrontFormView())
      ->setUser($user)
196
      ->setWorkflow(true)
197
      ->setAction($request->getRequestURI()->getPath())
198
      ->addHiddenInput('slug', $document->getSlug())
199
      ->addHiddenInput('nodraft', $request->getBool('nodraft'))
200
201
      ->appendChild(
        id(new AphrontFormTextControl())
Chad Little's avatar
Chad Little committed
202
          ->setLabel(pht('Title'))
203
204
205
206
207
          ->setValue($content->getTitle())
          ->setError($e_title)
          ->setName('title'))
      ->appendChild(
        id(new AphrontFormStaticControl())
Chad Little's avatar
Chad Little committed
208
          ->setLabel(pht('URI'))
209
210
          ->setValue($uri))
      ->appendChild(
211
        id(new PhabricatorRemarkupControl())
Chad Little's avatar
Chad Little committed
212
          ->setLabel(pht('Content'))
213
          ->setValue($content_text)
214
          ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
215
          ->setName('content')
216
217
          ->setID('document-textarea')
          ->setUser($user))
218
219
      ->appendChild(
        id(new AphrontFormTextControl())
Chad Little's avatar
Chad Little committed
220
          ->setLabel(pht('Edit Notes'))
221
          ->setValue($notes)
222
223
          ->setError(null)
          ->setName('description'))
224
225
      ->appendChild(
        id(new AphrontFormSubmitControl())
226
          ->addCancelButton($cancel_uri)
227
228
          ->setValue($submit_button));

Chad Little's avatar
Chad Little committed
229
230
231
232
    $form_box = id(new PHUIFormBoxView())
      ->setHeaderText(pht('Edit Document'))
      ->setFormError($error_view)
      ->setForm($form);
233

234
235
236
237
238
    $preview = id(new PHUIRemarkupPreviewPanel())
      ->setHeader(pht('Document Preview'))
      ->setPreviewURI('/phriction/preview/')
      ->setControlID('document-textarea')
      ->setSkin('document');
239

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
    $crumbs = $this->buildApplicationCrumbs();
    if ($document->getID()) {
      $crumbs->addCrumb(
        id(new PhabricatorCrumbView())
          ->setName($content->getTitle())
          ->setHref(PhrictionDocument::getSlugURI($document->getSlug())));
      $crumbs->addCrumb(
        id(new PhabricatorCrumbView())
          ->setName(pht('Edit')));
    } else {
      $crumbs->addCrumb(
        id(new PhabricatorCrumbView())
          ->setName(pht('Create')));
    }

Chad Little's avatar
Chad Little committed
255
    return $this->buildApplicationPage(
256
      array(
257
        $crumbs,
258
        $draft_note,
Chad Little's avatar
Chad Little committed
259
        $form_box,
260
        $preview,
261
262
      ),
      array(
Chad Little's avatar
Chad Little committed
263
264
        'title'   => pht('Edit Document'),
        'device'  => true,
265
266
267
268
      ));
  }

}