forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhrictionMoveController.php
130 lines (116 loc) · 4.23 KB
/
PhrictionMoveController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
final class PhrictionMoveController extends PhrictionController {
public function handleRequest(AphrontRequest $request) {
$viewer = $this->getViewer();
$document = id(new PhrictionDocumentQuery())
->setViewer($viewer)
->withIDs(array($request->getURIData('id')))
->needContent(true)
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$document) {
return new Aphront404Response();
}
$slug = $document->getSlug();
$cancel_uri = PhrictionDocument::getSlugURI($slug);
$v_slug = $slug;
$e_slug = null;
$v_note = '';
$validation_exception = null;
if ($request->isFormPost()) {
$v_note = $request->getStr('description');
$v_slug = $request->getStr('slug');
$normal_slug = PhabricatorSlug::normalize($v_slug);
// If what the user typed isn't what we're actually using, warn them
// about it.
if (strlen($v_slug)) {
$no_slash_slug = rtrim($normal_slug, '/');
if ($normal_slug !== $v_slug && $no_slash_slug !== $v_slug) {
return $this->newDialog()
->setTitle(pht('Adjust Path'))
->appendParagraph(
pht(
'The path you entered (%s) is not a valid wiki document '.
'path. Paths may not contain spaces or special characters.',
phutil_tag('strong', array(), $v_slug)))
->appendParagraph(
pht(
'Would you like to use the path %s instead?',
phutil_tag('strong', array(), $normal_slug)))
->addHiddenInput('slug', $normal_slug)
->addHiddenInput('description', $v_note)
->addCancelButton($cancel_uri)
->addSubmitButton(pht('Accept Path'));
}
}
$editor = id(new PhrictionTransactionEditor())
->setActor($viewer)
->setContentSourceFromRequest($request)
->setContinueOnNoEffect(true)
->setContinueOnMissingFields(true)
->setDescription($v_note);
$xactions = array();
$xactions[] = id(new PhrictionTransaction())
->setTransactionType(
PhrictionDocumentMoveToTransaction::TRANSACTIONTYPE)
->setNewValue($document);
$target_document = id(new PhrictionDocumentQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withSlugs(array($normal_slug))
->needContent(true)
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$target_document) {
$target_document = PhrictionDocument::initializeNewDocument(
$viewer,
$v_slug);
}
try {
$editor->applyTransactions($target_document, $xactions);
$redir_uri = PhrictionDocument::getSlugURI(
$target_document->getSlug());
return id(new AphrontRedirectResponse())->setURI($redir_uri);
} catch (PhabricatorApplicationTransactionValidationException $ex) {
$validation_exception = $ex;
$e_slug = $ex->getShortMessage(
PhrictionDocumentMoveToTransaction::TRANSACTIONTYPE);
}
}
$form = id(new AphrontFormView())
->setUser($viewer)
->appendChild(
id(new AphrontFormStaticControl())
->setLabel(pht('Title'))
->setValue($document->getContent()->getTitle()))
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('Current Path'))
->setDisabled(true)
->setValue($slug))
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('New Path'))
->setValue($v_slug)
->setError($e_slug)
->setName('slug'))
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('Edit Notes'))
->setValue($v_note)
->setName('description'));
return $this->newDialog()
->setTitle(pht('Move Document'))
->setValidationException($validation_exception)
->appendForm($form)
->addSubmitButton(pht('Move Document'))
->addCancelButton($cancel_uri);
}
}